Hamsa K
Editor
13 min read | 5 years ago

How to Add Linux Host to Nagios Server

How to add linux host to Nagios monitoring server

In our previous article we discussed about installation and configuration of nagios server. Here we will discuss about adding remote host to nagios server using nrpe.

NRPE plugin: Nagios Remote Plugin Executor(nrpe) plugin allows you to monitor applications and services running on remote Linux / windows hosts. This NRPE Add-on helps Nagios to monitor local resources like memory,disk,ping,swap,CPU etc of remote host.

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.

Installation of nrpe plugin on remote host

Remote host ip: 192.168.0.10

Nagios server ip: 192.168.0.192

First we need to install few dependency packages like below

[root@lampblogs ~]# yum install -y gcc glibc glibc-common gd gd-devel make net-snmp openssl-devel

create nagios user and set password

[root@lampblogs ~]# useradd nagios
[root@lampblogs ~]# passwd nagios

Install nagios plugins

Create one directory for installation and download plugins into that directory.

[root@lampblogs ~]# mkdir nagios
[root@lampblogs ~]# cd nagios
[root@lampblogs nagios]# wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
[root@lampblogs nagios]# tar -xvzf nagios-plugins-2.2.1.tar.gz

once you have downloaded and extracted nagios plugins, Now you need to compile them as shown below.

Compile and install plugins

Run below command to compile

[root@lampblogs nagios]# cd nagios-plugins-2.2.1/
[root@lampblogs nagios-plugins-2.2.1]# ./configure 
[root@lampblogs nagios-plugins-2.2.1]# make
[root@lampblogs nagios-plugins-2.2.1]# make install

set permissions on plugin directory

[root@lampblogs nagios-plugins-2.2.1]# chown nagios.nagios /usr/local/nagios
[root@lampblogs nagios-plugins-2.2.1]# chown -R nagios.nagios /usr/local/nagios/libexec

Install NRPE

download and install nrpe plugins 

[root@lampblogs nagios-plugins-2.2.1]# cd /root/nagios
[root@lampblogs nagios]# wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-3.2.1/nrpe-3.2.1.tar.gz
[root@lampblogs nagios]# tar -xvzf nrpe-3.2.1.tar.gz

compile and install nrpe as shown below

[root@lampblogs nagios]# cd nrpe-3.2.1
[root@lampblogs nrpe-3.2.1]# ./configure
[root@lampblogs nrpe-3.2.1]# make all
##### Install binaries #####
[root@lampblogs nrpe-3.2.1]# make install 
####### Install Configuration Files  ########
[root@lampblogs nrpe-3.2.1]# make install-config

update services file

The /etc/services file is used by applications to translate human readable service names into port numbers when connecting to a machine across a network.

[root@lampblogs ~]# echo >> /etc/services
[root@lampblogs ~]# echo '# Nagios services' >> /etc/services
[root@lampblogs ~]# echo 'nrpe    5666/tcp' >> /etc/services

Install service /daemon files

[root@lampblogs nrpe-3.2.1]# make install-init

update nrpe config file

Add nagios server ip allowed hosts in nrpe.cfg

[root@lampblogs ~]# vi /usr/local/nagios/etc/nrpe.cfg

change allowed_hosts as below (add nagios server ip) and save it.

allowed_hosts=127.0.0.1,192.168.0.192

Now start nrpe service and also enable in startup as below

[root@lampblogs ~]# systemctl start nrpe
[root@lampblogs ~]# systemctl enable nrpe

verify nrpe daemon with netstat command

[root@lampblogs ~]# netstat -antp | grep nrpe
tcp        0      0 0.0.0.0:5666            0.0.0.0:*               LISTEN      11285/nrpe          
tcp6       0      0 :::5666                 :::*                    LISTEN      11285/nrpe 

Next, verify the NRPE daemon is functioning properly. Run the “check_nrpe” command that was installed earlier for testing purposes.

[root@lampblogs ~]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1

sample output

NRPE v3.2.1

configure firewall 

Make sure that firewall on the local machine will allow the NRPE daemon to be accessed from remote servers.To do this run following command

[root@lampblogs ~]# firewall-cmd --permanent --zone=public --add-port=5666/tcp

customize nrpe commands

The default NRPE configuration file that got installed has several command definitions that will be used to monitor this machine.sample file is located at below path.

[root@lampblogs ~]# vi /usr/local/nagios/etc/nrpe.cfg

The following are the default command definitions that are located at the bottom of the configuration file. You can check them by using the following commands.

[root@lampblogs ~]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_load
CRITICAL - load average: 0.50, 1.16, 1.19|load1=0.500;0.150;0.300;0; load5=1.160;0.100;0.250;0; load15=1.190;0.050;0.200;0; 
[root@lampblogs ~]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_users
USERS OK - 2 users currently logged in |users=2;5;10;0
[root@lampblogs ~]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_sda1
DISK OK - free space: /boot 815 MB (80.47% inode=100%);| /boot=198MB;811;912;0;1014
[root@lampblogs ~]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_total_procs
PROCS WARNING: 179 processes | procs=179;150;200;0;
[root@lampblogs ~]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_zombie_procs
PROCS OK: 0 processes with STATE = Z | procs=0;5;10;0;

If you want to check like ssh,swap usage and http service etc then add below to nrpe.cfg file and check these also like above.

command[check_ssh]=/usr/local/nagios/libexec/check_ssh -p $ARG1$ -H $HOSTADDRESS$
command[check_swap]=/usr/local/nagios/libexec/check_swap  -w 20% -c 10%
command[check_http]=/usr/local/nagios/libexec/check_http -I 192.168.0.192

You can edit and add new command definitions by editing the NRPE config file. Finally, we have successfully installed and configured NRPE agent on Remote host.Now we will configure Nrpe and few services on Nagios server.

Install nrpe plugin in nagios server like below

[root@centos ~]# mkdir nagios
[root@centos ~]# cd nagios
[root@centos nagios]# wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-3.2.1/nrpe-3.2.1.tar.gz
[root@centos nagios]# tar xzf nrpe-3.2.1.tar.gz
[root@centos nagios]# cd nrpe-3.2.1
[root@centos nrpe-3.2.1]# ./configure
[root@centos nrpe-3.2.1]# make all
[root@centos nrpe-3.2.1]# make install 
[root@centos nrpe-3.2.1]# make install-config
[root@centos nrpe-3.2.1]# make install-init
############ Add service like below ################
[root@centos ~]# echo >> /etc/services
[root@centos ~]# echo '# Nagios services' >> /etc/services
[root@centos ~]# echo 'nrpe    5666/tcp' >> /etc/services

now start and enable nrpe service like below

[root@centos ~]# systemctl start nrpe
[root@centos ~]# systemctl enable nrpe

Verify nrpe daemon remotely

Make sure that the check_nrpe plugin can communicate with the NRPE daemon on the remote host.you need to add ip address of remote host

[root@centos ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.0.10
NRPE v3.2.1

Allow nrpe port in firewall if firewall is running on your server and also make sure firewall not blocking between remote host and nagios server.

Add remote Linux host to Nagios server

To add remote host you need to create two new files hosts.cfg and services.cfg

[root@centos ~]# cd /usr/local/nagios/etc/
[root@centos etc]# touch hosts.cfg
[root@centos etc]# touch services.cfg

Now add these two files to main Nagios configuration file.

[root@centos ~]# vi /usr/local/nagios/etc/nagios.cfg

add below lines to config file

# You can specify individual object config files 
cfg_file=/usr/local/nagios/etc/hosts.cfg
cfg_file=/usr/local/nagios/etc/services.cfg

Now open hosts.cfg file and add the default host template name and define remote hosts as shown below. Make sure to replace host_name, alias and address with your remote host server details.

[root@centos ~]# vi /usr/local/nagios/etc/hosts.cfg
## Default Linux Host Template ##
define host{
name                            linux-box               ; Name of this template
use                             generic-host            ; Inherit default values
check_period                    24x7        
check_interval                  5       
retry_interval                  1       
max_check_attempts              10      
check_command                   check-host-alive
notification_period             24x7    
notification_interval           30      
notification_options            d,r     
contact_groups                  admins  
register                        0                       ; DONT REGISTER THIS - ITS A TEMPLATE
}
## Default
define host{
use                             linux-box               ; Inherit default values from a template
host_name                       lampblogs.com           ; The name we're giving to this server
alias                           CentOS 7                ; A longer name for the server
address                         192.168.0.10            ; IP address of Remote Linux host
}

save the file. Next open services.cfg file and add the following services to be monitored.

[root@centos ~]# vi /usr/local/nagios/etc/services.cfg
define service{
        use                       generic-service       
        host_name                 lampblogs.com
        service_description       PING
        check_command             check_ping!100.0,20%!500.0,60%
        }
define service{
        use                       generic-service       
        host_name                 lampblogs.com
        service_description       SSH
        check_command             check_ssh
        notifications_enabled         0
        }
define service{
        use                     generic-service
        host_name               lampblogs.com
        service_description     CPU Load
        check_command           check_nrpe!check_load
        }
define service{
        use                     generic-service
        host_name               lampblogs.com
        service_description     Total Processes
        check_command           check_nrpe!check_total_procs
        }
define service{
        use                     generic-service
        host_name               lampblogs.com
        service_description     Current Users
        check_command           check_nrpe!check_users
       }
define service{
        use                     generic-service
        host_name               lampblogs.com
        service_description     HTTP
        check_command           check_nrpe!check_http
        }
define service{
        use                      generic-service         
        host_name                lampblogs.com
        service_description      Swap Usage
        check_command            check_nrpe!check_swap
        }

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

[root@centos ~]# vi /usr/local/nagios/etc/objects/commands.cfg

Add below content at the end of the file.

###############################################################################
# NRPE CHECK COMMAND
#
# Command to use NRPE to check remote host systems
###############################################################################
define command{
        command_name check_nrpe
        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }

Now, verify Nagios Configuration files for any errors.

[root@centos ~]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

sample output

Total Warnings: 0
Total Errors:   0
Things look okay - No serious problems were detected during the pre-flight check

Finally restart nagios service with below command

[root@centos ~]# systemctl restart nagios
[root@centos ~]# systemctl status nagios

That's it. Now goto nagios web interface in your browser with http://ip_address/nagios with username nagiosadmin and password.check whether remote linux is added and is being monitored.

click on services and check all are running perfect.



Warning! This site uses cookies
By continuing to browse the site, you are agreeing to our use of cookies. Read our terms and privacy policy