In our earlier tutorials, we have discussed how to install Nagios server & add Linux client for monitoring using NRPE plugin & also to add Windows clients for monitoring using NSClient++. But those are not the only ways to connect clients to Nagios server for monitoring, we can also allow Nagios server to monitor Linux machines by ssh using the check_by_ssh plugin.

In this tutorial, we will learn to configure nagios monitoring using ssh. SSH is usually installed on almost all the Linux distributions, so no extra packages need to be installed. We only need check_by_ssh plugin, this plugin allows Nagios to execute monitoring plugins and scripts on the remote machine in a secure manner, without having to supply authentication credentials.

So let's start the configuration for setting up Nagios monitoring using SSH,

(Recommended Read: Complete guide to install Zabbix on CentOS7 )

(Also Read: Complete monitoring solution : Install OMD (Open Monitoring Distribution) )

 

Step 1- Installing & configuring Nagios plugins on Client

Firstly, install the following packages to resolve all dependencies required for installing the plugins ,

$ yum install -y gcc glibc glibc-common gd gd-devel make net-snmp openssl-devel

now add a user for Nagios by name “nagios”,

$ useradd nagios

$ passwd nagios

Next download the Nagios plugins to the system using the following command,

$ wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz

We will now extract the plugins from the downloaded tar archive,

$ tar -xvf nagios-plugins-2.2.1.tar.gz

$ cd nagios-plugins-2.2.1

And now we will compile & install the plugins

$ ./configure –with-nagios-user=nagios

$ make

$ make install

After the plugins are installed, we will change the ownership of the plugins folders

$ chown nagios.nagios /usr/local/nagios

$ chown -R nagios.nagios /usr/local/nagios/libexec

 

Step 2- Configure password less login between Nagios server & client

Now, we need to setup password less login between Nagios server & client using the ssh public/private key setup. To create the Public/Private keys, login to nagios server & change the user to nagios,

$ su nagios

Than create the keys using the following command,

$ ssh-keygen

Press Enter to select default file name & passphrase. Once the keys have been generated, we can locate them in the '/home/nagios/.ssh' folder. Goto that folder,

$ cd /home/nagios/.ssh

& copy the public key (it's named 'id_rsa.pub') to client machine using the following command,

$ ssh-copy-id -i ~/.ssh/id_rsa.pub nagios@client_IP

Next login to the client machine & goto folder '/home/nagios/.ssh' & make sure that the permissions for the folder 'authorized_keys' are 700, so that only 'nagios' user can read, write & execute to that folder,

$ chmod 700 /home/nagios/.ssh/authorized_keys

 

Testing the connection

In order for check_by_ssh to work, we should should be able to login to client machine from nagios server without any authentication. We have already made the necessary changes on both Nagios server & client machine, all we have to do is to test the connection between the two.

To test the connection, ssh into the client machine from Nagios server using the following command,

$ ssh nagios@client_IP

& we should be directly able to connect to the client machine, without any username & password.

Once we have successfully tested the tconnection between nagios server & client, we will next check the check_by_ssh plugin connectivity from server to client. So logout from client & log back into the nagios server & execute the following command,

$ /usr/local/nagios/libexec/check_by_ssh -H client_ip -C uptime

& we should get following output,

12:47:01 up 3:04, 2 users, load average: 0.04, 0.07, 0.08

This shows that check_by_ssh plugin is also working the way its supposed to .

 

Configuring check_by_ssh on server

This will be the last step to setup Nagios monitoring using SSH, we now have to define the commands definition to use check_by_ssh for connecting to clients. To do this , we will have to edit 'commands.cfg' , its located in '/usr/local/nagios/etc' folder. Open the file,

$ vim /usr/local/nagios/etc/commands.cfg

& add the following commands to the file,

define command{

command_name check_remote_disk

command_line $USER1$/check_by_ssh -p $ARG1$ \

-H $HOSTADDRESS$ -C '/usr/lib/nagios/plugins/check_disk \

-w $ARG2$ -c $ARG3$ -p $ARG4$'

}

define command{

command_name check_remote_users

command_line $USER1$/check_by_ssh -p $ARG1$ \

-H $HOSTADDRESS$ -C '/usr/lib/nagios/plugins/check_users \

-w $ARG2$ -c $ARG3$'

}

define command{

command_name check_remote_load

command_line $USER1$/check_by_ssh -p $ARG1$ \

-H $HOSTADDRESS$ -C '/usr/lib/nagios/plugins/check_load \

-w $ARG2$ -c $ARG3$'

}

define command{

command_name check_remote_procs

command_line $USER1$/check_by_ssh -p $ARG1$

-H $HOSTADDRESS$ -C '/usr/lib/nagios/plugins/check_procs \

-w $ARG2$ -c $ARG3$ -s $ARG4$'

}

define command{

command_name check_remote_swap

command_line $USER1$/check_by_ssh -p $ARG1$ \

-H $HOSTADDRESS$ -C '/usr/lib/nagios/plugins/check_swap \

-w $ARG2$ -c $ARG3$'

}

Depending on how you configured the nagios & plugins, you might need to change the same in commands mentioned above. Now save the file & exit & restart the nagios server to implement the changes,

$ service nagios restart

Now visit the nagios monitoring page & you should be able to see the client information. With this we end this tutorial on how to setup Nagios monitoring using SSH, please feel free to send in your queries and suggestions using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

Linux TechLab is thankful for your continued support.