How to Add Linux Remote Host to Icinga2 Monitoring Server Using nrpe Plugin

What is NRPE?
The NRPE (Nagios Remote Plugin Executor) 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.
Note: The NRPE addon requires that Nagios Plugins must be installed on the remote Linux machine. Without these, the NRPE daemon will not work and will not monitor anything.

I-Installation of NRPE Plugin
To use the NRPE, you will need to do some additional tasks on both the Nagios Monitoring Host and Remote Linux Host that the NRPE installed on. We will be covering both the installation parts separately.

I-1.On Remote Linux Host

       I-1-A-Centos Remote Host

       Install Required Dependencies
       #yum install -y gcc glibc glibc-common gd gd-devel make net-snmp openssl-devel
       Create Nagios User
       #useradd nagios -M -s /bin/false

       Install Nagios Plugin
       #cd /usr/src/
       #wget https://nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz
       #tar xzvf nagios-plugins-2.1.1.tar.gz
       #cd nagios-plugins-2.1.1
       #./configure
       #make
       #make install
       #chown nagios.nagios /usr/local/nagios
       #chown -R nagios.nagios /usr/local/nagios/libexec

       Install Xinetd
       #yum install xinetd

       Install NRPE Plugin
       Download latest NRPE Plugin 2.15 packages with wget command.
       #cd /usr/src
       #wget http://liquidtelecom.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gz
       #tar xzvf nrpe-2.15.tar.gz
       #cd nrpe-2.15
       #./configure
       #make all
       #make install-plugin
       #make install-daemon
       #make install-daemon-config
       Install the NRPE daemon under xinetd as a service.
       #make install-xinetd

       Now open /etc/xinetd.d/nrpe file and add the localhost and IP address of the icinga2                         Monitoring Server.
       #vim /etc/xinetd.d/nrpe
       only_from = 127.0.0.1 localhost <nagios_ip_address>
       Next, open /etc/services file add the following entry for the NRPE daemon at the bottom of the          file.
       #vim /etc/services  
nrpe            5666/tcp                 NRPE
       Restart the xinetd service.
       #service xinetd restart
       Verify NRPE Daemon Locally
       #netstat -at | grep nrpe
tcp     0   0 *:nrpe                  *:*                 LISTEN
If you get output similar to above, means it working correctly. If not, make sure to check the following things.
 -Check you’ve added nrpe entry correctly in /etc/services file
 -The only_from contains an entry for “nagios_ip_address” in the /etc/xinetd.d/nrpe file.
 -The xinetd is installed and started.
 -Check for the errors in the system log files for about xinetd or nrpe and fix those problems.
Next, verify the NRPE daemon is functioning properly. Run the “check_nrpe” command that was installed earlier for testing purposes.
#/usr/local/nagios/libexec/check_nrpe -H localhost
you will get a following string on the screen, it shows you what version of NRPE is installed:
NRPE v2.15

Configure Firewall Rules
Make sure that the Firewall on the local machine will allow the NRPE daemon to be accessed from remote servers. To do this, run the following iptables command.
#iptables -A INPUT -p tcp -m tcp --dport 5666 -j ACCEPT
#service iptables save
Set up logging
Make sure that xinetd writes to its own log file, so it doesn't clutter /var/log/messages.
#vi /etc/xinetd.conf
locate the line defining 
"log_type", and replace it by log_type = FILE /var/log/xinetd.log
Customize NRPE commands
Add commands to NRPE by editing /usr/local/nagios/etc/nrpe.cfg. Here is an example:
#vi /usr/local/nagios/etc/nrpe.cfg
locate the sections listing NRPE commands and add (use whatever mount points you want do monitor): this is my customize nrpe commands(change path of nagios plugins to your right path)
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_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200 
command[check_disk]=/usr/local/nagios/libexec//check_disk -w 10% -c 5% -W 10% -K 5% -A
command[check_zombie_procs]=/usr/local/nagios/libexec/plugins/check_procs -w 5 -c 10 -s Z
command[check_http]=/usr/local/nagios/libexec/check_http -I localhost
command[check_imap]=/usr/local/nagios/libexec/check_imap -H localhost
command[check_imaps]=/usr/local/nagios/libexec/check_simap -H localhost
command[check_pop]=/usr/local/nagios/libexec/check_pop -H localhost
command[check_pops]=/usr/local/nagios/libexec/check_spop -H localhost
command[check_smtp]=/usr/local/nagios/libexec/check_smtp -H localhost
command[check_smtps]=/usr/local/nagios/libexec/check_ssmtp -H localhost
command[check_smtp_587]=/usr/local/nagios/libexec/check_smtp -H localhost -p 587
Now test the new commands in localhost:
#/usr/local/nagios/libexec/check_nrpe -H localhost -c check_users
   I-1-B-Debian Remote Host
       Installing NRPE Service
       You need to install NRPE service on every remote host that you want to monitor using NRPE.            NRPE service daemon on each remote host will then communicate with a Nagios monitoring              server.
        #apt-get install nagios-nrpe-server 
        Preparing Configuration File
        #vim /etc/nagios/nrpe.cfg 
## NRPE service port can be customized ##
server_port=5666
dont_blame_nrpe=1
## the nagios monitoring server is permitted ##
## NOTE: There is no space after the comma ##
allowed_hosts=127.0.0.1,X.X.X.X-IP_v4_of_Nagios_server

## The following examples use hard-coded command arguments.
## These parameters can be modified as needed.

## NOTE: For CentOS 64 bit, use /usr/lib64 instead of /usr/lib ##
#customize command below:

command[check_users]=/usr/lib/nagios/plugins/check_users -w 5 -c 10
command[check_load]=/usr/lib/nagios/plugins/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/lib/nagios/plugins/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/lib/nagios/plugins/check_procs -w 150 -c 200
#service nagios-nrpe-server restart 
#netstat -tpln | grep 5666 
tcp   0   0   0.0.0.0:5666    0.0.0.0:*    LISTEN    19885/nrpe

I-2.Configure Icinga2 Monitoring Server for NRPE(on debian8 host)

#apt-get install nagios-nrpe-plugin nagios-plugins
after install it will get many check command plugin on /usr/local/nagios/libexec/check_* (Ex:check_nrpe) if don't have check_nrpe we need to apt-get install nagios-plugins

#vim /etc/icinga2/constants.conf 
Add path LibexecDir plugins the end of file
const LibexecDir = "/usr/local/nagios/libexec"
Now we need to define command plugins
#vim /usr/share/icinga2/include/command-plugins.conf
add the end of file
/*defind command check with nrpe */ 
template CheckCommand "nrpe-common" { 
    import "plugin-check-command"      
    command = [ LibexecDir + "/check_nrpe" ] arguments = {
          "-H" = "$nrpe_address$"
          "-t" = "$nrpe_timeout$"
          "-p" = "$nrpe_port$"
          "-c" = "$nrpe_command$"
       // "-a" = {
       // value = "$nrpe_args$"
       // repeat_key = false
       // order = 1
       // }
    }        
   vars.nrpe_address = "$address$"
   vars.nrpe_port = 5666
   vars.nrpe_timeout = 60
} 
object CheckCommand "nrpe-users" {#nrpe-users will use for service (check_        command)
       import "nrpe-common"
       vars.nrpe_command = "check_users" #check_users is customize command        that defined on other remote host   
}
object CheckCommand "nrpe-disk" {
       import "nrpe-common"
       vars.nrpe_command = "check_disk"
}
object CheckCommand "nrpe-load" {
       import "nrpe-common"
       vars.nrpe_command = "check_load"
}
object CheckCommand "check-total-procs" {
       import "nrpe-common"
       vars.nrpe_command = "check_total_procs"
}
//for nas xm11068
object CheckCommand "nrpe-disk-root-home" {
       import "nrpe-common"
       vars.nrpe_command = "check_disk_root_home_nas"
}
object CheckCommand "nrpe-check-http" {
       import "nrpe-common"
       vars.nrpe_command = "check_http"
}
//mail-server
object CheckCommand "nrpe-check-smtp-25" {
       import "nrpe-common"
       vars.nrpe_command = "check_smtp"
}
object CheckCommand "nrpe-check-smtps-ssl-465" {
       import "nrpe-common"
       vars.nrpe_command = "check_smtps"
}
object CheckCommand "nrpe-check-smtp-ssl-587" {
       import "nrpe-common"
       vars.nrpe_command = "check_smtp_587"
}
object CheckCommand "nrpe-check-imap" {
       import "nrpe-common"
       vars.nrpe_command = "check_imap"
}
object CheckCommand "nrpe-check-imaps-993" {
       import "nrpe-common"
       vars.nrpe_command = "check_imaps"
}
//object CheckCommand "nrpe-check-pop-110" {
//     import "nrpe-common"
//     vars.nrpe_command = "check_pop"
//}
object CheckCommand "nrpe-check-spop-995" {
       import "nrpe-common"
       vars.nrpe_command = "check_pops"
}
Then we add to the service of any host by:
object Service "nrpe:cur-users" {#nrpe:cur-users will display on interface
    import "generic-service"
    host_name = "xm11068"
    check_command = "nrpe-users" #nrpe-users we just defined on command-plu    gin above (object CheckCommand "nrpe-users")
    vars.sla = "24x7"
}
/*object Service "nrpe:check-disk" {
    import "generic-service"
    host_name = "xm11068"
    check_command = "nrpe-disk"
    vars.sla = "24x7"
}*/
object Service "nrpe:check-disk-home" {
    import "generic-service"
    host_name = "xm11068"
    check_command = "nrpe-disk-root-home"
    vars.sla = "24x7"
}
object Service "nrpe:check-total-procs" {
    import "generic-service"
    host_name = "xm11068"
    check_command = "check-total-procs"
    vars.sla = "24x7"
}
object Service "nrpe:check-load" {
    import "generic-service"
    host_name = "xm11068"
    check_command = "nrpe-load"
    vars.sla = "24x7"
}
#/etc/init.d/icinga2 restart
#/etc/init.d/nagios-nrpe-server restart

At your Icinga2 server (not the one you're installing the NRPE client on!), run the following command, replacing the ip address with the name or address of the actual client:
#/usr/local/nagios/libexec/check_nrpe -H 10.3.1.17 -c check_load
Note:check_load is command that define on remote host ip 10.3.1.17