Configuring Nagios to monitor services using NRPE

In my first blog on nagios configuration Configuring Nagios to monitor localhost ,we had seen how to configure nagios to monitor services on localhost. Now,we will see further how to configure nagios to monitor services on remote host.


Active and Passive Checks

Nagios provides few addons to monitor services on remote hosts. Some of the popular add-ons are 1) NCSA and 2)NRPE
Also it provides two types of checks to monitor services on a remote host.  Active Check and Passive Check: Let's see what are active and passive checks with respect to Nagios.

Active Checks:  Active checks are the most common method for monitoring hosts and services.It is initiated by the monitoring server or in other words the Nagios server. When Nagios needs to check the status of a host or service it will execute a plugin and pass the information about what needs to be checked. The plugin will then check the operational state of the host or service and report the results back to the Nagios daemon. Nagios will process the results of the host or service check and take appropriate action as necessary

Passive Checks: Passive checks are initiated on Nagios client hosts and uses external applications/processes like NCSA etc. The major difference between active and passive checks is that active checks are initiated and performed by Nagios, while passive checks are performed by external applications.

Nagios Remote Plugin Executor(NRPE): 

The NRPE addon allows to execute Nagios Plugin on remote Unix based hosts.This helps in monitoring "Local" Rescources(like CPU load , memory usage etc) on remote hosts as these resources are not exposed to external machines.

The NRPE addon consists of two pieces:
   --The check_nrpe plugin, which resides on the local monitoring machine
   --The NRPE daemon, which runs on the remote Linux/Unix machine

NRPE works as follows:
   --Nagios will execute the check_nrpe plugin and tell it what service needs to be
     checked
   --The check_nrpe plugin contacts the NRPE daemon on the remote host over an
     (optionally) SSL-protected connection
   --The NRPE daemon runs the appropriate Nagios plugin to check the service
     or resource
   --The results from the service check are passed from the NRPE daemon back to
     the check_nrpe plugin, which then returns the check results to the Nagios
     process.
  
Configuring NRPE to monitor remote hosts :

In order to configure NRPE to work properly , we need do install NRPE on both Nagios monitoring server and the client hosts which we are going to monitor. There are many other steps besides installing and configuring NRPE. It is better to first start with configuring NRPE on remote linux host and then proceed to configure NRPE on Nagios monitoring server.

A) Remote Linux Host SetUp :
  
 Become root on the system or use sudo for executing the commands.

  1. Add user nagios
             
           useradd nagios
           passwd nagios

   2. Download nagios-plugin and install it. Change ownership to nagios user for
       nagios installation directory
          
          wget http://sourceforge.net/projects/nagiosplug/files/nagiosplug/1.4.15 
          /nagios-plugins-1.4.15.tar.gz/download
          mkdir downloads
          cp nagios-plugins-1.4.15.tar.gz  downloads/
          cd downloads
          tar zxvf nagios-plugins-1.4.15.tar.gz
          cd nagios-plugins-1.4.15
          ./configure   
           make
           make install

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

        
   3. Install NRPE

      NRPE runs as a xinetd service . So need to install xinetd first if it is not
      already installed on your system

                  yum install xinetd
    
      Download and Install NRPE

                wget http://sourceforge.net/projects/nagios/files/nrpe-2.x/nrpe- 
                2.13/nrpe-2.13.tar.gz/download
                
                cp nrpe-2.13.tar.gz downloads/
                cd downloads 
                tar zxvf nrpe-2.13.tar.gz
                cd nrpe-2.13
                ./configure
                make all
                make install-plugin
                make install-daemon
                make install-daemon-config
                make install-xinetd

    4.  Edit /etc/xinetd.d/nrpe to add the ip-address of the Nagios monitoring
         server in the only_from directive . For me it is 192.168.122.1

                  service nrpe
               {
                      flags                 = REUSE
                      socket_type      = stream   
                      port                  = 5666   
                      wait                  = no
                      user                  = nagios
                      group               = nagios
                      server               = /usr/local/nagios/bin/nrpe         

                      server_args      = -c /usr/local/nagios/etc/nrpe.cfg --inetd
                      log_on_failure  += USERID
                      disable               = no
                      only_from       = 127.0.0.1 192.168.122.1
                 }        

     Now edit /etc/services to add nrpe
      
                       nrpe            5666/tcp                        # NRPE

    
    5. Restart xinetd service and test whether nrpe is running

                   /sbin/service xinetd restart             
 

                   netstat -atn | grep 5666
                   tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN   
                                     or
                   netstat -at | grep nrpe
                   tcp        0      0 *:nrpe                      *:*                         LISTEN   

          Now check the status of nrpe using the below command. It will display
          nrpe version as output

                  /usr/local/nagios/libexec/check_nrpe -H localhost

                  NRPE v2.13
  
     6.Add new rules in iptables to allow incoming connections for NRPE or just
        stop iptables initially to test the setup . Later on we can add rules to allow
        incoming connection for NRPE

                                        service iptables stop
 
With this we are now done with the basic configuration of NRPE to make it work properly. Have a look at NRPE configuration file to get some idea about the directives and default commands used by nrpe to check status of services. We can edit the config file to run NRPE as a standalone daemon or to add new commands to monitor new services.

Some of the commands listed in /usr/local/nagios/etc/nrpe.cfg   are as follows along with their execution results.

/usr/local/nagios/libexec/check_nrpe -H localhost -c check_load
OK - load average: 0.15, 0.03, 0.01|load1=0.150;15.000;30.000;0; load5=0.030;10.000;25.000;0; load15=0.010;5.000;20.000;0;

So , here we are done configuring and testing NRPE on the remote host . We will now move to the next step . Configuring NRPE on Nagios monitoring server so that it uses NRPE to monitor services on remote host.


B) Configuring NRPE on Nagios Monitoring Server.

On the monitoring server , we need to do the following:

--    Install Nagios , Nagios-plugins and the NRPE check_nrpe plugin
--   Create a Nagios command definition for using the check_nrpe plugin
--   Create Nagios host and service definitions for monitoring the remote host
  
1.  It is assumed that Nagios and Nagios plugins are already installed on the monitoring server and it is working fine . If not done yet , please follow my earlier blog on configuring Nagios to monitor localhost.

2.  Install NRPE plugin 

              tar zxvf nrpe-2.13.tar.gz
              cd nrpe-2.13
             ./configure
             make all
             make install-plugin


3.  Test communication with NRPE on the remote Linux host where we configured NRPE on Step 1)

        /usr/local/nagios/libexec/check_nrpe -H 192.168.122.85
         NRPE v2.13

  Here replace the ip address with the ip address of your remote host where we
  configured NRPE in the first step . Getting the version of NRPE as a output of
  the command tells that we are good to go ahead.

 3. Create a command definition in one of the Nagios object configuration files
 (/usr/local/nagios/etc/objects/commands.cfg) in order to use the check_nrpe plugin. For that , open the above config file and add the following command definition

        define command{
              command_name        check_nrpe
              command_line     $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
             }


 4. We are using this setup to monitor linux hosts . For that we need to create a
     configuration file for linux specific hosts to monitor services and make Nagios
     aware of this configuration file by putting the config file in nagios config file.  
    To achieve this, open nagios config file (vi /usr/local/nagios/etc/nagios.cfg) to
    add the configuration file to monitor remote linux hosts

                      # Definitions for monitoring the remote (Linux) host
                      cfg_file=/usr/local/nagios/etc/objects/linuxhosts.cfg

5.  We will then start creating linuxhosts.cfg by adding the host which we
     are going to monitor and also add the services which we are going to monitor
     on that host. For us ,the host that we are going to monitor is node2.

 
    5A. Adding hosts to monitor

               define host{
                                     use              linux-server       ; Name of host template to use
                          host_name             node2
                                   alias              node2
                               address             192.168.122.85
                         }


           define hostgroup{
                                hostgroup_name     linuxservers ;  The name of the hostgroup
                                                   alias     LinuxServers             
                                              members    node2   
                                          }

   5B. Adding Services to monitor


     define service{
                                use                            generic-service
                                host_name                node2
                                service_description  CPU Load
                                check_command       check_nrpe!check_load
                       }

      define service{
                                 use                           generic-service 
                                 host_name                node2
                                 service_description  Total Processes
                                check_command        check_nrpe!check_total_procs
                      }

       define service{
                                   use                           generic-service
                                   host_name                node2
                                   service_description  Current Users
                                   check_command       check_nrpe!check_users
                        }

 


6. Restart Nagios

At this point we have installed the check_nrpe plugin and added host and service definitions to monitor . Now its time to make those changes live...

Verify  Nagios configuration file.

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
If there are errors, fix them. If everything is fine, restart Nagios.
              
                            service nagios restart

That's it! We should see the host and service definitions that we created in the Nagios web interface. In a few minutes Nagios should have the current status information for the remote Linux machine.


Comments

Mammu said…
Very useful...
Rakesh Kumar said…
Thank you very much! It works..was very useful

Popular posts from this blog

PSSH : Parallel SSH to execute commands on a number of hosts

How to add check_http as a service in Nagios Monitoring using NRPE