How To Install And Configure Nagios On CentOS

Nagios is a popular networking monitoring software used by various hosting providers including Tech With Us servers,

Designed with scalability and flexibility in mind, Nagios gives you the peace of mind that comes from knowing your organization’s business processes won’t be affected by unknown outages.

Nagios is a powerful tool that provides you with instant awareness of your organization’s mission-critical IT infrastructure. Nagios allows you to detect and repair problems and mitigate future issues before they affect end-users and customers.

By using Nagios, you can:

  • Plan for infrastructure upgrades before outdated systems cause failures
  • Respond to issues at the first sign of a problem
  • Automatically fix problems when they are detected
  • Coordinate technical team responses
  • Ensure your organization’s SLAs are being met
  • Ensure IT infrastructure outages have a minimal effect on your organization’s bottom line
  • Monitor your entire infrastructure and business processes

Today i will be giving a quick tutorial about how to install Nagios for monitoring your servers easily from one Nagios installed VPS/Dedicated Server.

Basically Nagios contains 3 monitoring :

  • 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 right away via email!

So Lets Get Started,

Enabling RPMForge repository

For 32 bit machines use this 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 this 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                    tharunpkarun             ; Short name of user
        use                             generic-contact         ; Inherit default values from generic-contact template (defined above)
        alias                           Tharun P Karun            ; Full name of user

        email                           alerts@techwithus.com        ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
        }

Replace “tharunpkarun” with your desired username, replace “Tharun P Karun” with your Full Name & Replace “alerts@techwithus.com” with your email address / email address for recieveing alerts.

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: / 1924 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 uk1.techwithus.com -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://uk1.techwithus.com: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 uk1.techwithus.com. 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 uk1.techwithus.com -u '/' -s "Singing Bits And Bytes"
The Output of the command :

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 uk1.techwithus.com host to the /etc/nagios/objects/myhosts/uk1.techwithus.com.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/uk1.techwithus.com.cfg and insert the following snippet in the file:

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

We defined a host uk1.techwithus.com 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 tharunpkarun 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 from VPS from IntoVPS (UK Node)

Questions? Please comment 🙂




January 24, 2012
/
Previous Post Next Post

Tharun recommends you to read these fantastic articles