There are various server and network monitoring tools available these days. Different infrastructure creates different requirements for a monitoring tool. I prefer Nagios over others because it is easy to setup and use. Along with Nagios comes open source aiding tools like Nconf, NRPE and pnp4Nagios.
In this blog we will:
- Understand Nagios, NConf, NRPE and pnp4nagios
- Install Nagios
- Configure host/services with NConf
- Monitor remote host using NRPE
- Draw cool graphs out of nagios data using pnp4nagios
What is Nagios?
Nagios is a widespread open-source monitoring tool used for supervising infrastructure, such as servers, network devices etc. It is highly customizable and has a huge community of users worldwide.
Some of the many features of Nagios include:
- Monitoring of network services (SMTP, POP3, HTTP, NNTP, PING, etc.)
- Monitoring of host resources (processor load, disk usage, etc.)
- Contact notifications when service or host problems occur and get resolved (via email, pager, or user-defined method)
- Ability to define event handlers to be run during service or host events for proactive problem resolution
- Automatic log file rotation
- Optional web interface for viewing current network status, notification and problem history, log file, etc.
What is NConf?
NagiosConfigurator is a web-based tool which allows users to manage the complete configuration of their Nagios setup. Is is specifically aimed at large environments, but can naturally also be used for small Nagios installations. One of the main benefits is that NConf allows users to define templates and presets, which can be applied when adding hosts. A host-preset contains all services to be added to a host, with all commands linked and all default values already set. It also contains the host-alive-check to be applied to the new host. All a user must do after adding a host is to tweak certain parameters, if necessary.
Using NConf User Interface we can add/delete a host/service/check in Nagios. Hence it is very useful tool for those who don't know (or don't want ) to modify Nagios configuraiton files.
What is NRPE?
Nagios Remote Plugin Excecutor is an add-on for Nagios. By default Nagios can monitor limited resources on remote host. Nagios can't check remote servers' memory, cpu or free disk space. This functionality is extended by using NRPE add-on with Nagios.
What is pnp4nagios?
pnp4nagios is an add-on for Nagios. It is useful in drawing graphs out of the Nagios performance data. pnp4nagios uses RRD(Round Robin Database) tool to store performance data obtained by Nagios plugins and then uses XML templetes to draw graphs.
pnp4nagios is a recursive acronym for PNP is NOt Perfparse for Nagios.
Round Robin Database is a database in which data is stored with Round Robin algorithm by overwriting old content. In this way the size of a database always remains constant.
Nagios installation
Prerequisite for installing Nagios on RHEL/CentOS:-
- Apache
- PHP
- GCC compiler
- GD development libraries
Install the prerequisites, if missing
yum install httpd php
yum install gcc glibc glibc-common
yum install gd gd-devel
If you are using CentOS 5.x, you can easily install Nagios from online repository using 'yum install'. Nagios can also be installed from source by compiling it. For beginners RPM installation is recommended.
Install following packages (use 'yum install' or 'rpm -ivh'):-
- nagios-nrpe-2.12-1.el5.rf
- nagios-3.2.0-1.el5.rf
- nagios-devel-3.2.0-1.el5.rf
- nagios-plugins-1.4.13-1.el5.rf
- nagios-plugins-nrpe.i386 0:2.12-16.el5
Above packages can also be downloaded from
here
If online repositories are unavailable and you don't want to install the above given RPMs, please refer to online documentation of Nagios which will guide you how to install Nagios by compiling TAR package.
After installation of Nagios, start the service
service nagios start
and browse
http://yourserver/nagios
This will show default Nagios dashboard with localhost.
If you wish to restrict access to Nagios dashboard then use Apache basic authentication. For this we shall create a htpasswd.users file which will store username(nagiosadmin) and password :-
htpasswd -c /etc/nagios/htpasswd.users nagiosadmin
Change ownership of this file
chown 600 /etc/nagios/htpasswd.users
In this case we installed Nagios using RPM so modifying web server configuration (httpd.conf) manually is not required. This will be taken care by RPM. But when Nagios is compiled from TAR package, httpd.conf needs to be modified by user himself.
NConf Installation
Prerequisite for installing NConf on RHEL/CentOS :-
- Apache webserver installed
- PHP 5 or higher, php-mysql intalled
- MySQL 5.0.2 or higher (with default InnoDB Database Engine) installed
- Perl 5.6 or higher, perl-DBI, perl-DBD-MySQL installed
- Nagios 3.x installed
- php.ini settings
- short_open_tag = On
- register_globals = Off
- magic_quotes_gpc = Off
Download NConf source tar package and unzip it into web server root (/var/www/html) then rename the directory according to convenience
cd /var/www/html
tar xvfz nconf-Version.tgz
mv nconf-Version nconf
Grant write privilege to Apache user on config, output, static_cfg and temp directories under nconf directory
cd /var/www/html/nconf
ls -l
chmod 775 config
chmod 775 output
chmod 775 static_cfg
chmod 775 temp
chown root:apache config
chown root:apache output
chown root:apache static_cfg
chown root:apache temp
Create a database in MySQL for NConf
mysql -uroot -psecret -hHostname
mysql> show engines;
mysql> create database nconfdb;
mysql> show databases;
Create a user 'nconfuser' with password 'secret' with all privileges on nconfdb
mysql> grant all privileges on nconfdb.* to `nconfuser`@`Hostname` identified by 'secret' with grant option;
OR
mysql> grant all privileges on nconfdb.* to `nconfuser`@`%` identified by 'secret' with grant option;
OR
mysql> grant all privileges on *.* to `nconfuser`@`%` identified by 'secret' with grant option;
Start Apache service (if not running) and begin NConf installation from browser
http://yourserver/nconf/INSTALL.php
It loads a pre-installation check page. Make sure all the requirements are OK.
Click Next and provide the database configuration details
Click Next and provide basic NConf information
NCONFDIR = The directory where NConf is located i.e. /var/www/html/nconf in our case (auto detected)
NAGIOS_BIN = The path to the Nagios binary i.e. /usr/bin/nagios in our case. The binary is needed in order to do syntax check of the generated config. NConf will not work properly if these checks cannot be executed. Default /var/www/html/nconf/bin/nagios will not work in our case.
Click Next and provide Authentication information
AUTH_ENABLED = TRUE
AUTH_TYPE = FILE
file_admin_password = secret
Click Next to save settings
Remove INSTALL, INSTALL.php, UPDATE, UPDATE.php before clicking on Finish
This will complete your NConf installation.
Now you can open NConf in browser
http://yourserver/nconf
NConf comes with few predefined hosts, switches, hostgroups etc. For example, a predefined host 'localhost' ships with new NConf. This will monitor the Nagios(Also NConf) server itself.
To get the predefined content from NConf, just click on "Generate Nagios Config" button on the left panel of NConf home page. This will export the MySQL database content to Nagios config format. Once the config is generated NConf will do a syntax check of new config. This will be done by running Nagios binary with -v option. NConf can run Nagios binary only if NAGIOS_BIN was set correctly during NConf installation.
Make sure your webserver user has access to your Nagios binary, or copy the binary to '/var/www/html/nconf/bin/' folder. NConf will only create the final output config file, if the syntax test was completed with 'Total Errors: 0'.
If NConf was able to successfully generate the the Nagios configuration, and if all the generated files passed the syntax check, you will now find a .tgz package file under: /var/www/html/nconf/output/NagiosConfig.tgz.xxxxyyyy
The NagiosConfig.tgz file contains the following:
Folder 'Default_collector':
extended_host_info.cfg
extended_service_info.cfg
hostgroups.cfg
hosts.cfg
servicegroups.cfg
services.cfg
Folder 'global':
checkcommands.cfg
contactgroups.cfg
contacts.cfg
host_templates.cfg
misccommands.cfg
service_templates.cfg
timeperiods.cfg
Deployment of generated config:
Simply unpack the '/var/www/html/nconf/output/NagiosConfig.tgz' archive, move the files to the '/etc/nagios/' directory and restart Nagios. You can easily do this with the below shell script which ships with NConf
/var/www/html/nconf/ADD-ONS/deploy_local.sh
If you are unable to locate this script, create one by yourself:
#!/bin/bash
OUTPUT_DIR="/var/www/html/nconf/output/"
NAGIOS_DIR="/etc/nagios/"
TEMP_DIR=${NAGIOS_DIR}"import/"
CONF_ARCHIVE="NagiosConfig.tgz"
if [ ! -e ${TEMP_DIR} ] ; then
mkdir -p ${TEMP_DIR}
fi
if [ ${OUTPUT_DIR}${CONF_ARCHIVE} -nt ${TEMP_DIR}${CONF_ARCHIVE} ] ; then
cp -p ${OUTPUT_DIR}${CONF_ARCHIVE} ${TEMP_DIR}${CONF_ARCHIVE}
tar -xf ${TEMP_DIR}${CONF_ARCHIVE} -C ${NAGIOS_DIR}
/etc/init.d/nagios reload
fi
exit
Now, Nagios must be informed to pick up new CFG files which are provided by above script of NConf. To achieve this we must modify /etc/nagios/nagios.cfg file such that comment all other CFG files and add NConf generated CFG files.
#cfg_file=/etc/nagios/objects/commands.cfg
#cfg_file=/etc/nagios/objects/contacts.cfg
#cfg_file=/etc/nagios/objects/timeperiods.cfg
#cfg_file=/etc/nagios/objects/templates.cfg
#cfg_file=/etc/nagios/objects/localhost.cfg
#cfg_file=/etc/nagios/objects/windows.cfg
#cfg_file=/etc/nagios/objects/switch.cfg
#cfg_file=/etc/nagios/objects/printer.cfg
#cfg_dir=/etc/nagios/servers
#cfg_dir=/etc/nagios/printers
#cfg_dir=/etc/nagios/switches
#cfg_dir=/etc/nagios/routers
## Modified by vyogesh to use NConf cfg files obtained by deploy_local.sh
cfg_dir=/etc/nagios/global
cfg_dir=/etc/nagios/Default_collector
After making any kind of changes to nagios.cfg we must do a syntax check then restart nagios service for the changes to take effect
/usr/bin/nagios -v /etc/nagios/nagios.cfg
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
service nagios restart
Now if you open http://yourserver/nagios you will see only localhost is being monitored. All other predefined (unnecessary in our case) hosts and services are removed.
Let us start adding remote host to Nagios monitoring using NConf