Nagios Plugins 2.2.1 with SSH Remote Exec for Clients

This post is in the category: Guides

Posts here are mostly step-by-step guides on how to replicate something I have set up in the past. Read over my About page to see how I show commands/output and read the disclaimer.

Configure the Server

I assume you already configured the server using this guide: Nagios 4.4.1 Install from Source on Ubuntu 18.04

Log in to your Nagios server and su into the nagios user. You might have to su from root since that account usually doesn’t have a password.

[root@server]$ su - nagios

Out of curiosity, see if you have a .ssh folder.

[nagios@server]$ ls -alh ~/.ssh
ls: cannot access '/home/nagios/.ssh': No such file or directory

More than likely you don’t. So that also means that there is no SSH key pair. Let’s create one.

[nagios@server]$ ssh-keygen

Basically mash the enter key until you see the fingerprint art. We don’t want a passphrase on this key because it will be used by automated scripts.

Now output the public key and save locally for later.

[nagios@server]$ cat .ssh/

Configure the Client(s)

Install Option #1 – Install From Source

[root@client]$ useradd -m -s /bin/bash nagios
[root@client]$ cd ~
[root@client]$ wget
[root@client]$ tar -xzvf nagios-plugins-2.2.1.tar.gz

By the way, this tar command was written without referencing the docs.

Install some required packages.

[root@client]$ apt-get install libgd-dev php build-essential unzip libnet-snmp-perl libperl5.26 libpq5 libsensors4 libsnmp-base libsnmp30 libtalloc2 libtdb1 libwbclient0 samba-common samba-common-bin smbclient snmp whois libmysqlclient-dev libssl-dev

Now install them

[root@client]$ cd ~/nagios-plugins-2.2.1
[root@client]$ ./configure --with-nagios-user=nagios --with-nagios-group=nagios
[root@client]$ make
[root@client]$ make install

Option #2 – Apt Install

[root@client]$ useradd -m -s /bin/bash nagios
[root@client]$ apt-get install nagios-plugins

I reference all plugin paths from the source install, so create a link if you do the deb package.

[root@client]$ ln -s /usr/lib/nagios/plugins /usr/local/nagios/libexec

Additional Plugin Install

[root@client]$ cd /usr/local/nagios/libexec
[root@client]$ wget
[root@client]$ chmod +x

Authorize Connections

[nagios@client]$ mkdir ~/.ssh
[nagios@client]$ vim ~/.ssh/authorized_keys

Paste in the SSH public key created earlier on the server.

Fix some file permissions.

[nagios@client]$ chmod 0600 ~/.ssh/authorized_keys

Test Commands

Run this lengthy command on the server:

[nagios@server]$ /usr/local/nagios/libexec/check_by_ssh -H -C "/usr/local/nagios/libexec/check_load -w 2,1,1 -c 8,4,4"
OK - load average: 0.14, 0.10, 0.09|load1=0.140;2.000;8.000;0; load5=0.100;1.000;4.000;0; load15=0.090;1.000;4.000;0;

Note that the load average returned is from the client, not the server.

Example Configuration

Below is an example config to start with. You can have a generic command that will call most any remote command. In the service definition, pass the remote command in the first parameter and the warning/critical thresholds in the following parameters. The fourth parameter, $ARG4$ can be used to pass any additional parameters if needed.

define command {
        command_name    check_by_ssh
        command_line    /usr/local/nagios/libexec/check_by_ssh -t 30 -H '$HOSTADDRESS$' -C "/usr/local/nagios/libexec/$ARG1$ -w '$ARG2$' -c '$ARG3$' $ARG4$"

define hostgroup {
    hostgroup_name     hg-linux
    alias              Linux Servers

define service {
    use                     generic-service
    hostgroup_name      hg-linux
    service_description CPU Load
    check_command       check_by_ssh!check_load!4,2,2!8,4,4
    check_interval      1
    retry_interval      1

define host {
    use                     host-generic
    host_name               server-01
    alias                   server-01
    hostgroups              hg-linux
This entry was posted in Guides and tagged , on by .

About Andrew Wells

I have been developing on the LAMP stack since about 2006. I run Ubuntu XFCE on my desktop and have a history of managing Ubuntu and CentOS servers. I code web applications mostly in PHP but have experience with other languages as well. When I'm not working, I can be found working in my home lab or out snowboarding, hiking, camping, or biking depending on the season.

Leave a Reply

Your email address will not be published. Required fields are marked *