How would you rate this article?

Rating: 2 user(s) have rated this article Average rating: 5.0
Posted by: retro
Date: 23/11/2008
Category: Linux and Open Source
Views: this article has been read 1757 times

Recent Articles Get the RSS feed

(26/06/2010)

Nochex merchant accounts provide you with everything you need to accept payments on your web site. With no monthly fees and support for a number of ecommerce solutions, including nopCommerce, it has never been easier to start selling online!

(25/05/2010)

Check out our latest project for community interest company S.C.A.

(25/05/2010)

We are pleased to announce support for version 4.0 of the .NET Framework on all of our hosting plans.

(11/02/2010)

We have just completed development of a new web site for UK based Aerial Spares.

(11/02/2010)

Today sees the release of the official nopCommerce user guide. It explains every part of the application in detail and includes a getting started guide so you can get up and running quickly.

read more read more

In part 1 we discussed how to build an openSUSE server from scratch and install the necessary packages to get Nagios up and running. In this article we discuss how to actually monitor a host using the most common type of check, issuing a PING command to determine the activity of a host.

This article is designed just to get you started. For a complete guide go to http://nagios.sourceforge.net/docs/3_0/toc.html.
 
Problem
 
You have a number of business critical applications hosted on 2 servers in your organisation. You want to be informed as soon as a server loses connection/goes offline.
 
Solution
 
Log in to your server's console and switch to root (su).
 
The first thing we need to do is to set up our hosts. The easiest way of doing this is to configure a directory that nagios will automatically use to process your cfg files.
 
Type the following to browse to the nagios directory and open the nagios configuration file:
 
LinuxServer1:/ # cd /etc/nagios
LinuxServer1:/etc/nagios # ls
cgi.cfg  command.cfg  htpasswd.users  nagios.cfg  objects  resource.cfg
LinuxServer1:/etc/nagios # vi nagios.cfg
 
Navigate to the following section in the file and uncomment out the line #cfg_dir=/etc/nagios/servers
 
# You can also tell Nagios to process all config files (with a .cfg
# extension) in a particular directory by using the cfg_dir
# directive as shown below:
 
#cfg_dir=/etc/nagios/servers
#cfg_dir=/etc/nagios/printers
#cfg_dir=/etc/nagios/switches
#cfg_dir=/etc/nagios/routers 
 
Save the file and close by issuing the following command
 
:wq
 
We now need to create the servers directory.
 
Issue the following command
 
LinuxServer1:/etc/nagios # mkdir /etc/nagios/servers
 
Browse to this directory and create a new file myservers.cfg
 
LinuxServer1:/etc/nagios # cd /etc/nagios/servers
LinuxServer1:/etc/nagios/servers # vi myservers.cfg
 
and enter the content below (replace with your own server IP addresses)
 
# CONFIG FOR MONITORING BUSINESS SERVERS
# LAST MODIFIED 23-11-2008
# COMMENTS - CONFIG FILE FOR MONITORING 2 BUSINESS SERVERS
 
############################################################
# HOST DEFINITION
############################################################
 
# DEFINE HOSTS
 
define host {
                use     windows-server  ; Name of host template to use. This host will inherit all variables that are defined in (or inherited by) the windows-server host template definition
                host_name       businessserver1
                alias           Web Server
                address         89.238.131.136
                }
 
define host {
                use windows-server
                host_name       businessserver2
                alias           Exchange  Server
                address         192.168.0.1
                }
 
##########################################################
# HOST GROUP DEFINITION
##########################################################
 
# DEFINE A HOSTGROUP FOR WINDOWS SERVERS
 
define hostgroup {
                hostgroup_name  windows-servers ; name of the hostgroup
                alias           Windows Servers ; Long name of the group
                members         businessserver1,businessserver2 ; comma separated list of hosts that belong to this group
                }
 
#########################################################
# SERVICE DEFINITIONS
#########################################################
 
# DEFINE A SERVICE TO PING THE MACHINE
 
define service {
        use     local-service ; name of service template to use
        host_name       businessserver1
        service_description     PING
        check_command           check_ping!100.0,20%!500.0,60%
        }
define service {
        use     local-service
        host_name       businessserver2
        service_description     PING
        check_command           check_ping!100.0,20%!500.0,60%
        }
 
 
To save and close the file, issue the command 
 
:wq
 
Brief explanation.
 
The file /etc/nagios/objects/templates.cfg contains a number of predefined host and service templates. This means you only need to add your settings once and can then inherit those templates when defining a host. In the above example we are inheriting the "windows-server" template which is already defined.
 
The "windows-server" template does however specify that it is a member of the "windows-servers" hostgroup so we have created this and added our hosts to it.
 
Finally we created a couple of service definitions that issue the relevant commands. Both of these inherit from the default "local-service" template.
 
We now need to reload our config file by issuing the following command:
 
LinuxServer1:/ # /etc/init.d/nagios reload
Reload service nagios Passed configuration check - reloading...       done
 
If we log into nagios now we should see the following in our hostgroup overview:
 
Hostgroup overview
 
Whilst we can now see the from the web interface if a server goes down, the problem we are trying to address is how we are notified of such an event.
 
To enable email notifications we need to add a new contact to the default contacts group.
 
Issue the following command to change config.cfg:
 
LinuxServer1:/ # vi /etc/nagios/objects/contacts.cfg
 
We need to add a new contact with the email address we wish to send our notifications to (change for your own email address):
 
define contact{
        contact_name                    retroviz
        use                             generic-contact
        alias                           RetroViz Email
        email                           support@retroviz.local
        }
 
and then add them to the default contact group (at the bottom of the cfg file). Modify the group definition as below:
 
# We only have one contact in this simple configuration file, so there is
# no need to create more than one contact group.
 
define contactgroup{
        contactgroup_name       admins
        alias                   Nagios Administrators
        members                 nagiosadmin,retroviz
        }
 
and save and close by issuing the command
 
:wq
 
We now need to reload our config file by issuing the following command:
 
LinuxServer1:/ # /etc/init.d/nagios reload
Reload service nagios Passed configuration check - reloading...       done
 
To test our email notifications we can open Nagios browse into one of the servers (Host Details > Server Name) and choose the "Send Custom Notification" option from the menu on the right:
 
Host Commands
 
Enter a comment and click "Commit".
 
 
Sure enough, an email is sent to our support mail box:
 
Nagios email 
 
A little note about sending emails.
 
Unfortunately the nagios documentation does not discuss how you actually configure your linux server to send the mail. It works in this case because the gateway on the LAN happens to be a SBS running Exchange. There are a number of SMTP packages available that allow for sending of email from your server so if you do not happen to have your own SMTP server, you may want to check out the documentation on the opensuse web site.

Send to Friend  Send to friend

Comments

Comment this article
Name:
E-mail:
Comment:
Add Cancel