Monitor Remote Host using Nagios and NRPE Service

Monitor Remote Host
Monitor Remote Host

Monitor Remote Host with Nagios

In this article, we are going to learn to Add/Monitor Remote host in nagios with help of NRPE service.

After understanding the installation and configuration of Nagios server and NRPE in our previous article. We are now working on to configure NRPE Service on Remote host to monitor.

If Nagios is new for you then please check below article first and learn to install Nagios,

Read: Steps to install Nagios core on Linux

NRPE Service

NRPE (Nagios Remote Plugin Executor) Client plugin allows you to monitor any remote Linux/Unix services or network devices. This NRPE add-on allows Nagios to monitor any local resources like CPU load, Swap, Memory usage, Online users, etc. on remote Linux machines. After all, these local resources are not mostly exposed to external machines, an NRPE agent must be installed and configured on the remote machines.

This article will help you to install NRPE service on your Linux system and add/Monitor Remote host in Nagios server.

Step 1: Install & Configure NRPE Service

We are installing the NRPE service using “apt-get command”. Before start installing, first update the apt-get repository and then install the NRPE Service.

$ apt-get update

$ apt-get install nagios-nrpe-server

Now, create the new user and group which has access to NRPE and Nagios Plugins on server.

$ useradd  nagios

$ groupadd nagios

Step 2: Install Nagios Plugins

After installation of NRPE service, we need to install Nagios plugins which is used to monitor remote server.

Download the Nagios Plugins from below mentioned link on your remote server and extract it with tar command.

$ cd /opt/

$ wget http://nagios-plugins.org/download/nagios-plugins-2.0.3.tar.gz

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

$ cd nagios-plugins-2.0.3/

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

$ make && make install

Step 3: Change Permission 

Change the permission of plugin directory and provide executable permission to the files.

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

$ chmod -R +x /usr/local/nagios/libexec/

Nagios plugins usually installed under the “/usr/local/nagios/libexec/” so check this directory is available on server which means installation is done successfully.

Step 4: Configure NRPE Service

Open nrpe.cfg file which is resides under “/etc/nagios/” and add below commands which helps to monitor our remote server.

vim /etc/nagios/nrpe.cfg

## Add Nagios server ip in allowed_hosts list ##

allowed_hosts=127.0.0.1, 55.45.264.212 [relpaced this ip with your nagios server ip]

## Add below commands to check or monitor status of server ##

command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10

command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20

command[check_ping]=/usr/local/nagios/libexec/check_ping -H 8.8.8.8 -w 100.0,20% -c 500.0,60%

command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20 -c 10

command[check_cpu]=/usr/local/nagios/libexec/check_cpu -w 80 -c 90

command[check_mem]=/usr/local/nagios/libexec/check_mem -w 10 -c 5 -f

command[check_root]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/xvda

command[check_data]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/mapper/

command[check_mnt]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/xvdb

command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z

command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200

command[check_apache_procs]=/usr/local/nagios/libexec/check_proc_cpu -p apache2 -w 40 -c 50

Save and close the file.

We have used some custom Nagios plugins to monitor the services like apache, nginx process, cpu, mem status etc.

You can download the Custom Nagios Plugin from below link,

Dowload Custom Nagios Plugin !!

Step 5: Restart the NRPE Service

To reflect the changes in “nrpe.cfg” file, we need to restart the NRPE Service.

$ /etc/init.d/nagios-nrpe-server restart

$ netstat -tulnp | grep nrpe

Output :

tcp 0 0 0.0.0.0:5666     0.0.0.0:*       LISTEN      27496/nrpe

tcp  0  0 :::5666          :::*            LISTEN      27496/nrpe

If you get output similar to above, means it working correctly.

Step 6: Add Firewall Rule in IPtables

Open Echo requests port 5666 which is accessible from the remote server.

If Iptable is enabled on the system  then add below rule in IPtables file.

vim /etc/sysconfig/iptables

## Use your nagios server ip at bold place.##

-A INPUT -s nagios-server-ip -m state --state NEW -m tcp -p tcp --dport 5666 -j ACCEPT

If you are using AWS EC2 Server then open below port in security group,

Custom  ICMP  Rule  Echo Request  N/A  nagios-server-ip/32

Custom  TCP  Rule  TCP  5666   nagios-server-ip/32

Step 7: Restart NRPE Service

Restart NRPE service and use below commands to test added commands in nrpe.cfg is working properly.

$ /etc/init.d/nagios-nrpe-server restart

Run below Commands on a Remote Host

$ /usr/local/nagios/libexec/check_cpu -w 60 -c 90
Output will come like this:

OK - user: 6.85, nice: 0.50, sys: 1.23, iowait: 0.50, irq: 0.50, softirq: 0.62 idle: 93.28 | user=6.85; nice=0.50; sys=1.23; softirq=0.62; iowait=0.50; irq=0.50; idle=93.28;

Using the above command, you can check all the other commands manually running on the remote host.

Changes on Nagios Monitoring Server

On Nagios Server, We are going to install NRPE Plugin. 

Download the NRPE Plugin using “wget” command,

$ cd /opt/

$ wget http://liquidtelecom.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gz


$ tar -xvf nrpe-2.15.tar.gz

$ cd /opt/nrpe-2.15

$ ./configure --with-ssl=/usr/bin/openssl --with-ssl-lib=/usr/lib/x86_64-linux-gnu

$ make all

$ make install-daemon

Now go to the /opt/nrpe-2.15/src and you will find the check_nrpe plugin so just move this plugin to the “/usr/local/nagios/libexec/“ directory.

Make sure that the check_nrpe plugin can communicate with the NRPE daemon on the remote Linux host.

To check NRPE Plugin Connectivity run below command,

$ /usr/local/nagios/libexec/check_nrpe -H  -c check_mem
Output :

CRITICAL - 3.3% (503084 kB) free!|TOTAL=15375700KB;;;; USED=14872616KB;13838130;14606915;; FREE=503084KB;;;; CACHES=7094916KB;;;;

If the above command produced output like this means everything is working properly.

Add Remote Host In Nagios

Go to the “/usr/local/nagios/etc” and create the directory with name of Remote machine then create file “host.cfg” & “remote-host.cfg”

$ cd /usr/local/nagios/etc/

$ mkdir clients

$ touch hosts.cfg

$ touch remote-host.cfg

Now open the “nagios.cfg” file and add the name of the directory where we have created the host configuration files.

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

##This will read all the config files from this directory.

cfg_dir=/usr/local/nagios/etc/clients

Now go back to the client directory and open the file hosts.cfg and add the default host template name and define remote hosts as shown below.

define host{

       use            linux-server ## important to define

       host_name      nagios_host

       alias          Client-url optional

       address        Remote-host-ip ##important to define

      }

Now open file “remote-host.cfg” from the same directory and add below services.

define service{

       use                       generic-service

       host_name                 nagios_host

       service_description       Current Users

       check_command              check_nrpe!check_users

       }

define service{

       use                       generic-service

       host_name                 nagios_host

       service_description       Current Load

       check_command              check_nrpe!check_load

       }

define service{

       use                        generic-service

       host_name                  nagios_host

       service_description        PING

       check_command            check_nrpe!check_ping

       }

define service{

       use                        generic-service

       host_name                  nagios_host

       service_description        Swap Usage

       check_command              check_nrpe!check_swap

       }

define service{

       use                        generic-service

       host_name                  nagios_host

       service_description        Current CPU Usage

       check_command               check_nrpe!check_cpu

       }

define service{

        use                       generic-service

        host_name                 nagios_host

        service_description       Current Memory Usage

        check_command             check_nrpe!check_mem

        }

define service{

       use                        generic-service

       host_name                  nagios_host

       service_description        Root Partition

       check_command               check_nrpe!check_root

       }

define service{

       use                        generic-service

       host_name                  nagios_host

       service_description        Zombie Processes

       check_command              check_nrpe!check_zombie_procs

       }

define service{

       use                        generic-service

       host_name                  nagios_host

       service_description        Total Processes

        check_command              check_nrpe!check_total_procs

       }

Note: From the above two files we have used two important things one is linux-server in hosts.cfg file and second is generic-service in remote-host.cfg file.

linux-server and generic-service defined under the objects/templates.cfg file if you use different names then you will get the error.

Now NRPE command definition needs to be created in commands.cfg file

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

define command{

       command_name check_nrpe

       command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -t 30

}

Finally, our configuration is done and now verify Nagios Configuration files for any syntax errors using below command,

$ /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Restart the Nagios Service,

$ /etc/init.d/nagios restart

That’s it. Now go to Nagios Monitoring Web interface at “http://Your-server-IP-address/” and click on hosts you will see our remote host is successfully added on the server.

Nagios Remote host

If you’re facing any difficulties while adding the remote host to Nagios. Please do comment queries or problems via the comment section, till then stay tuned to Linuxgrow.com for more such valuable articles.

Prashant

Welcome to Linuxgrow. I'm Prashant, a tech-blogger from Mumbai, India. I started Linuxgrow as a passion and to share my knowledge about technologies. Here at Linuxgrow, I write about Linux technologies, Aws Cloud, Wordpress blogging and scripting knowledge. You can read more about me at About us page. Thank You :)

You may also like...

Leave a Reply

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

Facebook