Linux menu

Wednesday, September 17, 2014

How to Install and Configure Nagios 4.0.7 In Linux

Nagios is a system and network monitoring application that offers monitoring and alerting services for servers, switches, applications, and services. It alerts the users when things go wrong and alerts them a second time when the problem has been resolved.
Nagios runs periodic checks on user-specified resources and services, it can be used to monitor both the local system and remote hosts. Resources that can be monitored include CPU, memory and disk space loads, log files, temperature or hardware errors. It can monitor various parameters and problems for services like HTTP, SMTP, DNS, and with the help of plugins it can be highly extended. Nagios provides all the information v ia mail alerts and in a user-friendly interface.
This guide will help you install the latest Nagios 4.0.x from the source files on CentOS 7, although the same procedure can be used on any system based on Red Hat.

Installing Nagios

Before installing Nagios you should have a working Apache/PHP installation on your system, in case you haven’t you can use the following command to install the required packages:
# yum install -y httpd php
You will also need the following libraries to compile and run Nagios 4.0 from source:
# yum install -y gcc glibc glibc-common make gd gd-devel net-snmp
Next you will have to add the nagios user to the system so you avoid running it as root:
# useradd nagios
And a new nagcmd group that will allow external commands to be run via the web interface:
# groupadd nagcmd
# usermod -G nagcmd nagios
# usermod -G nagcmd apache
Next step is to download the installation packages. Create a new directory to store the downloaded content:
# mkdir ~/nagios
# cd ~/nagios
The latest versions of the Nagios Core and Nagios Plugins packages can be found on the following websites:
You can use wget to download the files:
# wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-4.0.7.tar.gz
# wget http://www.nagios-plugins.org/download/nagios-plugins-2.0.3.tar.gz
And then extract the files with tar:
# tar zxvf nagios-4.0.7.tar.gz
# tar zxvf nagios-plugins-2.0.3.tar.gz
Now we need to compile and install the files so we go to the Nagios directory and run the configuration script
# cd nagios-4.0.7
# ./configure –with-command-group=nagcmd
You should get the following output:
nagios configure
Next we need to compile and install the Nagios files:
# make all
# make install
And we should get this output:
nagios make install
Finally we need to install the init scripts, the required files to run Nagios from the command line and the sample configuration files with the following commands:
# make install-init
# make install-commandmode
# make install-config
With your favorite text editor you need to open /usr/local/nagios/etc/objects/contacts.cfg and modify the email field from nagios@localhost to the email address where you wish to receive notifications.
# vim /usr/local/nagios/etc/objects/contacts.cfg
And the configure file should look like this:
nagios contacts cfg
Now that we have Nagios installed and ready to run we need to install the web component by running the following command:
# make install-webconf
And we need to setup the password for the user nagiosadmin. This username will be used to access the web interface so it is important to remember the password that you will input here. Set the password running the following command and enter the password twice:
# htpasswd -s -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password:
Re-type new password:
Adding password for user nagiosadmin
Restart apache for the settings to take effect:
# systemctl start httpd.service
It’s time to install the Nagios Plugins by running the following commands:
# cd ~/nagios/nagios-plugins-2.0.3
# ./configure –with-nagios-user=nagios –with-nagios-group=nagios
# make
# make install
Next we have to make Nagios start at boot time, so first verify that the configuration file has no errors running the following command:
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
And you should get the output:
nagios verify
If everything is fine add the service to run on boot with this commands:
# chkconfig –add nagios
# chkconfig –level 35 nagios on
And start the service with the following command:
# systemctl start nagios.service

The web interface

Now that everything is installed and running you can access the web interface with a browser either by using the address http://localhost/nagios if you are doing so from the local computer or by using the address http://SERVER-IP/nagios if you are accessing it from a remote computer.
When prompted for username and password you will introduce the username “nagiosadmin” and the password that you entered in step 6. In case you forget this password you can modify it by rerunning the htpasswd command in step 6.
The web interface when you log-in for the first time will look like this:
Nagios on CentOS 7
You can now use the left-hand side menu to access the various areas of Nagios and perform different tasks.

(Optional) Install NRPE on remote hosts

If you need to monitor remote hosts, you will also need to install NRPE on this hosts so you can run the Nagios plugins remotely on this hosts and gather information. Take note that this is necessary only for more complex functions of Nagios like resource monitoring or anything that needs a plugin to be executed on the remote host. It is not needed to check services that are already visible to server or information that can be gathered via SNMP.
First you need to go to http://sourceforge.net/projects/nagios/files/nrpe-2.x/ to download the latest version of NRPE,, you can do this with wget and then extract it’s content:
# wget http://downloads.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gz
# tar zxvf nrpe-2.15.tar.gz
# cd nrpe-2.15
Next copy the content of src to a directory from where you want to run nrpe like this:
# mkdir /usr/share/nrpe
# cp src/* /usr/share/nrpe/
And then copy the config file to /etc folder like this:
# cp sample-config/nrpe.cfg /etc
Now you can start nrpe with the following command:
# /usr/share/nrpe/nrpe -c /etc/nrpe.cfg -d
Now that NRPE is running you can use Nagios to monitor this remote host.

No comments: