How to Install and configure Nagios on Centos

Nagios Monitoring server (CentOS 6.x)
Install Required Dependencies
We need to install Aapche, PHP and some libraries before installing Nagios.

[root@greenhat]# yum install -y httpd php gcc glibc glibc-common gd gd-devel make net-snmp

Create Nagios User and Group

Create a new nagios user and nagcmd group account and set a password.
[root@greenhat]# useradd nagios
[root@greenhat]# groupadd nagcmd
Next, add both the nagios user and the apache user to the nagcmd group.
[root@greenhat]# usermod -G nagcmd nagios
[root@greenhat]# usermod -G nagcmd apache
Download Nagios Core 4.1.1 and Nagios Plugin 2.1
Create a directory for your Nagios installation and all its future downloads.
[root@greenhat]# mkdir ~/nagios
[root@greenhat]# cd ~/nagios
Now download latest Nagios Core 4.1.1 and Nagios plugins 2.1 packages with wget command.
[root@greenhat nagios~]# wget
/releases/nagios 4.1.1.tar.gz
[root@greenhat nagios~]# wget
Extract Nagios Core and its Plugins
We need to extract downloaded package with tar command as follows.
[root@greenhat nagios~]# tar xvf nagios-4.1.1.tar.gz
[root@greenhat nagios~]# tar xvf nagios-plugins-2.1.1.tar.gz

When you extract these tarballs with tar command, two new folders will appear in that directory. Now, first we will configure Nagios Core and to do so we need to go to Nagios directory and run configure file.
[root@greenhat nagios~]# cd nagios-4.1.1
[root@greenhat nagios-4.1.1 ]# ./configure --with-command-group=nagcmd
[root@greenhat nagios-4.1.1 ]# make all
[root@greenhat nagios-4.1.1 ]# make install
[root@greenhat nagios-4.1.1 ]# make install-init
[root@greenhat nagios-4.1.1 ]# make install-command mode
[root@greenhat nagios-4.1.1 ]# make install-config
 Customizing Nagios Configuration
Open the “contacts.cfg” file with your choice of editor and set the email address associated with the nagiosadmin contact definition to receiving email alerts.
 [root@greenhat]# vi /usr/local/nagios/etc/objects/contacts.cfg
 Install and Configure Web Interface for Nagios
We are done with all configurations in the backend, now we will configure Web Interface for Nagios with following command. The below command will Configure Web interface for Nagios and a web admin user will be created “nagiosadmin”.
[root@greenhat nagios-4.1.1 ]# make install-webconf
In this step, we will be creating a password for “nagiosadmin”. After executing this command, please provide a password twice and keep it remember because this password will be used when you login in the Nagios Web interface.
[root@greenhat nagios-4.1.1]# htpasswd -s -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
Restart Apache to make the new settings take effect.
[root@greenhat]# service httpd restart

 Compile and Install Nagios Plugin
We have downloaded nagios plugins in ~/nagios, Go there and configure and install it as directed below.
[root@greenhat nagios]# cd ~/nagios
[root@greenhat nagios]# cd nagios-plugins-2.1.1
[root@greenhat nagios]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
[root@greenhat nagios]# make
[root@greenhat nagios]# make install
Verify Nagios Configuration Files
Now we are all done with Nagios configuration and it time to verify it by following command.
[root@greenhat nagios]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Note: If it gives the following output then all configuration is right.
Total Warnings: 0
Total Errors:      0
Add Nagios Services to System Startup
[root@greenhat]# chkconfig nagios on
[root@greenhat]# service nagios start

Login to the Nagios Web Interface
Your nagios is ready to work, please open it in your browser with “http://Your-server-IP-address/nagios” or “http://FQDN/nagios” and Provide the username “nagiosadmin” and password.

Add Hosts
In the objects folder we add our hosts/clients. Firstly we create host filename in object folder for linux.
[root@greenhat]#vi /usr/local/nagios/etc/objects/host.cfg
In this host file we add our host name, address and its defined services which have to be monitor by nagios.
define host{
use                   linux-server
host_name       myhost
define service{
use                             generic-service
host_name                  myhost
service_description     Disk check
check_command         check_nrpe!check_sda3 }

Add Host in nagios

When we create host, then we add its config file in nagios.cfg.
[root@greenhat ]#cd /usr/local/nagios/etc/nagios.cfg
Add the hosts file in nagios. e.g.
Restart the nagios services and see the host in the nagios web interface

Remote Linux Host (Client)
Please use the below instructions to install Nagios Plugins and NRPE daemon on the Remote Linux Host.

Install Required Dependencies
We need to install required libraries like gcc, glibc, glibc-common and GD and its development libraries before installing.
[root@greenhat]# yum install -y gcc glibc glibc-common gd gd-devel make net-snmp openssl-devel
Create Nagios User
Create a new nagios user account and set a password.
[root@greenhat]# useradd nagios
[root@greenhat]# passwd nagios
Install the Nagios Plugins
Create a directory for installation and all its future downloads.

[root@greenhat]# mkdir /root/nagios
[root@greenhat]# cd /root/nagios
Now download latest Nagios Plugins 2.1 package with wget command.
[root@greenhat nagios~]# wget
Extract Nagios Plugins

Run the following tar command to extract the source code tarball.
[root@greenhat nagios~]# tar xvf nagios-plugins-2.1.1.tar.gz
Compile and Install Nagios Plugins

Next, compile and install using following commands
[root@greenhat nagios]# cd nagios-plugins-2.1.1
[root@greenhat nagios]# ./configure
[root@greenhat nagios]# make
[root@greenhat nagios]# make install
Set the permissions on the plugin directory.
[root@greenhat nagios]# chown nagios.nagios /usr/local/nagios
[root@greenhat nagios]# chown -R nagios.nagios /usr/local/nagios/libexec
[root@greenhat nagios]# yum install xinetd
Install NRPE Plugin

Download latest NRPE Plugin 2.15 packages with wget command.
[root@greenhat]# cd /root/nagios
[root@greenhat]# wget
[root@greenhat]# tar xzf nrpe-2.15.tar.gz
[root@greenhat]# cd nrpe-2.15
[root@greenhat]# ./configure
[root@greenhat]# make all
[root@greenhat]# make install-plugin
[root@greenhat]# make install-daemon
[root@greenhat]# make install-daemon-config
Install the NRPE daemon under xinetd as a service.
[root@greenhat]# make install-xinetd
Now open /etc/xinetd.d/nrpe file and add the localhost and IP address of the Nagios Monitoring Server.
only_from = localhost <nagios_ip_address>
Next, open /etc/services file add the following entry for the NRPE daemon at the bottom of the file.
nrpe            5666/tcp                 NRPE
Restart the xinetd service.
[root@greenhat]# service xinetd restart
Verify NRPE Daemon Locally
Run the following command to verify the NRPE daemon working correctly under xinetd.
[root@greenhat]# netstat -at | grep nrpe
tcp        0      0 *:nrpe                      *:*                         LISTEN
[root@greenhat]# /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
[root@greenhat]# iptables -A INPUT -p tcp -m tcp --dport 5666 -j ACCEPT
Run the following command to save the new iptables rule so it will survive at system reboots.
[root@greenhat]# service iptables save
Add port 5666 as trusted port in the firewall, if the firewall enabled.
Customize NRPE commands
The default NRPE configuration file that got installed has several command definitions that will be used to monitor this machine.
[root@greenhat]# vi /usr/local/nagios/etc/nrpe.cfg
By default all service remain same as it is but the disk service path/command should be changed. e.g
command[check_sda5]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda5
By default memory and bandwidth command doesn’t exist but we can use scritps to helps nagios to monitor the required statistics.

For Bandwidth 
For Memory

On Nagios Monitoring Server

Install NRPE Plugin
Go to the nagios download directory and download latest NRPE Plugin with wget command.
[root@greenhat]# cd /root/nagios
[root@greenhat]# wget
[root@greenhat]# tar -xzf nrpe-2.15.tar.gz
[root@greenhat]# cd nrpe-2.15
[root@greenhat]# ./configure
[root@greenhat]# make all
[root@greenhat]# make install-daemon
Verify NRPE Daemon Remotely

Make sure that the check_nrpe plugin can communicate with the NRPE daemon on the remote Linux host. Add the IP address in the command below with the IP address of your Remote Linux host.
[root@greenhat]# /usr/local/nagios/libexec/check_nrpe -H <remote_linux_ip_address>

Now NRPE command definition needs to be created in commands.cfg file.
[root@greenhat]# vi /usr/local/nagios/etc/objects/commands.cfg
define command{
        command_name check_nrpe
        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
[root@greenhat]# service nagios restart
That’s it. Now go to Nagios Monitoring Web interface at “http://Your-server-IP-address/nagios” or “http://FQDN/nagios” and provide the username “nagiosadmin” and password. Check that the Remote Linux Host was added and is being monitored.