How To Install And Configure Nagios On CentOS

Nagios is a popular networking monitoring software. Nagios lets you monitor your IT infrastructure, be it servers, routers, switches or other devices. In this post, I will walk you through the steps of installing and configuring a basic setup of Nagios on CentOS.

At the end of the tutorial you will be have a working Nagios setup to monitor

  • local disk space usage
  • local system load
  • an external website

Whenever there is a problem or recovery with any of the above, Nagios will alert you via email.

Unless you have a strong reason to compile Nagios yourself, you should use the binary packages available for your Linux distribution.

RPMForge provides packages that are not included in CentOS repositories. We will use the Nagios packages from RPMForge.

Enabling RPMForge repository

For 32 bit machines use these commands:

wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.1-1.el5.rf.i386.rpm
rpm -ivh rpmforge-release-0.5.1-1.el5.rf.i386.rpm

For 64 bit machines use these commands:

wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.1-1.el5.rf.x86_64.rpm
rpm -ivh rpmforge-release-0.5.1-1.el5.rf.x86_64.rpm

Installing And Configuring Nagios

Install Nagios and Nagios plugins.

yum install nagios nagios-plugins nagios-plugins-all 

Configure startup services.
Use ntsysv and mark nagios to be started when the computer starts. Alternatively you can use the command chkconfig to configure startup services.

ntsysv

Start the nagios daemon.

/etc/init.d/nagios start

Let's start configuring Nagios. All the configuration object files are stored in the directory /etc/nagios/objects/. Each configuration object file ends with .cfg extension. The first thing you have to do is edit your contact information.

Edit the file /etc/nagios/objects/contacts.cfg to set your email address.

define contact{
        contact_name                    nagiosadmin             ; Short name of user
        use                             generic-contact         ; Inherit default values from generic-contact template (defined above)
        alias                           Nagios Admin            ; Full name of user
 
        email                           nagios@localhost        ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
        }

Replace nagios@localhost with your actual email address.

By default, a localhost host object is defined. You can find the details in the file /etc/nagios/objects/localhost.cfg. If you observe, Nagios is configured to monitor PING, Root Partition, Current Users, Total Processes, Current Load, Swap Usage, SSH and HTTP on the local machine. At the beginning of the article, I said we are going to monitor three services. But hey, you got more for free! Each check_command relates to a plugin. For example, check_disk relates to the plugin /usr/lib/nagios/plugins/check_disk. On 64 bit machines, the path to Nagios plugins is /usr/lib64/nagios/plugins.

Run the command manually to see how it works.

/usr/lib/nagios/plugins/check_disk -w 50% -c 60%

In the above command, -w switch indicates the warning level and -c switch indicates the critical level. Increase or decrease the warning and critical argument values and observe the status. In the configuration file the arguments are declared using the exclamatory mark separator.

Edit the values in /etc/nagios/objects/localhost.cfg file

check_local_disk!50%!60%!/

To get an alert, alter the values and restart nagios daemon. Wait for about 5 minutes.

Here's the body of a sample notification email I received:

***** Nagios *****
 
Notification Type: PROBLEM
 
Service: Root Partition
Host: localhost
Address: 127.0.0.1
State: CRITICAL
 
Date/Time: Mon Nov 8 00:53:32 IST 2010
 
Additional Info:
 
DISK CRITICAL - free space: / 1294 MB (47% inode=92%):

If you did not receive the email, check your mail server logs. Also, you can view the Nagios log at /var/log/nagios/nagios.log.

tail /var/log/nagios/nagios.log

Configuring Nagios To Monitor An External Website

Next, let's configure Nagios to monitor a website. We will put our configuration object files in the directory /etc/nagios/objects/myhosts.

mkdir /etc/nagios/objects/myhosts

Let's run the check_http plugin in the shell.

/usr/lib/nagios/plugins/check_http -H techchorus.net -u '/' -s "Singing Bits And Bytess"

The output of the command:

HTTP CRITICAL: HTTP/1.1 200 OK - string 'Singing Bits And Bytess' not found on 'http://techchorus.net:80/' - 40046 bytes in 3.114 second response time |time=3.113945s;;;0.000000 size=40046B;;;0

Nagios checked the host techchorus.net. It sent an HTTP request to the host techchorus.net. The -u switch indicaes the URL part after the hostname techchorus.net. Using the -s switch we specify the string to be checked in the HTTP response. Nagios returned the status HTTP CRITICAL because the string 'Singing Bits And Bytess' was not found in the response. We intentionally, made a typographical mistake. We specified 'Bytess' instead of 'Bytes'.

Let's run the check_http plugin again. This time with the correct string.

/usr/lib/nagios/plugins/check_http -H techchorus.net -u '/' -s "Singing Bits And Bytes"
HTTP OK: HTTP/1.1 200 OK - 40046 bytes in 2.151 second response time |time=2.150789s;;;0.000000 size=40046B;;;0

Let's add the techchorus.net host to the /etc/nagios/objects/myhosts/techchorus.net.cfg configuration object file and the check_http monitoring service.

Before adding adding the host object configuration file, add this line to your Nagios main configuration file:

cfg_dir=/etc/nagios/objects/myhosts to /etc/nagios/nagios.cfg

When you start the Nagios daemon, it reads all the configuration object files in the specified cfg_dir parameter.

Create the file /etc/nagios/objects/myhosts/techchorus.net.cfg and insert the following snippet in the file:

define host{
        use                     linux-server          
        host_name               techchorus.net
        alias                   techchorus.net
        address                 70.85.180.226
        }
define service{
        use                             local-service         ; Name of service template to use
        host_name                       techchorus.net
        service_description             HTTP 1
        check_command                   check_http_techchorus
        notifications_enabled           1
        }
define command{
        command_name                    check_http_techchorus
        command_line                    $USER1$/check_http -H techchorus.net -u '/' -s "Singing Bits And Bytess"
        }

We defined a host techchorus.net and instructed Nagios to monitor the web server on the defined host using check_http plugin.

Restart Nagios

/etc/init.d/nagios restart

If Nagios did not start, use the command

nagios -v /etc/nagios/nagios.cfg

to debug.

Configuring The Nagios Web Interface

Nagios also comes with a web interface. To use it with Apache web server, create a .htpassword file for HTTP basic authentication.

htpasswd -bc /etc/nagios/htpasswd.users nagiosadmin yourpasswordhere

Restart apache

/etc/init.d/httpd restart

Point your browser to <your-host-or-ip>/nagios. If you install Nagios on the host example.com, visit http://example.com/nagios.

This tutorial has been tested with CentOS 5.5 32 bit version and Nagios 3.2.

Resources

How was your experience with Nagios?

Comments

Greetings friends I'm trying to monitor a disk on an external server (/ dev/sda1). As I do? I have tried with the command check_ssh and gives me error. Please can you help?
thanks

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.