Adding Hosts to Nagios


nagios LogoIn my first nagios tutorial I talked you through the instillation process required for nagios, if you have not yet installed nagios please follow my tutorial, Installing Nagios on Ubuntu.

In this tutorial we are going to go through adding hosts to nagios, sounds simple rite, well there’s a bit more to it then you think, but its not too difficult and its great to see nagios in action after all this time setting it up.

So where to start, well that’s look at the structure of the nagios config files, this diagram outlines how all the config files link together.




Unless you changed it your config files should be in the /usr/local/nagios/etc:

Notes on Config Files

  1. Lines that start with # are ignored buy nagios so are used as human readable comments
  2. Variables are Case-Sensitive
  3. Variables must start at the beginning of a line, no white space before them.

Main Configuration FileNagios Config File Outline
This file is read first buy the Nagios Daemon, basically its job is to point to the rest of the configuration files, if we make a new config file we need to add its location to this file before nagios can read it.

Resource File
This is where user created macros are stored, however the main purpose of these files is to hide sensitive information, such as password, from the CGI’s

Object Definition Files
This is where we define hosts, services, host groups, contacts, contact groups, commands, etc.. Basically this is were you define anything you want nagios to monitor and choose how you want it to monitor them.

CGI Config File
This is where you outline the CGI’s configuration, this also has a reference to the main config file so the cig’s know how you configured nagios.

Ok, so now that’s actually add some settings, first make a new object definition file buy typing:

sudo nano /usr/local/nagios/etc/objects/newhost.cfg 

and adding the following lines to it:

# Define a host for the local machine

define host{
        use                     linux-server            ; Name of host template to use
                                                        ; This host definition will inherit all variables that are defined
                                                        ; in (or inherited by) the linux-server host template definition.
        host_name               google.com
        alias                   google.com
        address                 www.google.com
        }

###############################################################################
###############################################################################
#
# SERVICE DEFINITIONS
#
###############################################################################
###############################################################################

# Define a service to "ping" the local machine

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       google.com
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%
        }

# Define a service to check HTTP on the local machine.
# Disable notifications for this service by default, as not all users may have HTTP enabled.

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       google.com
        service_description             HTTP
        check_command                   check_http
        notifications_enabled           0
        }

now edit your main config file to tell it the location of the new config file:

sudo nano /usr/local/nagios/etc/nagios.cfg

add the folowwing line to the file

cfg_file=/usr/local/nagios/etc/objects/newhost.cfg

Now restart your nagios daemon:

sudo /etc/init.d/nagios restart 

and hay presto your nagios server is now monitoring google. Ok so how did all that work, well first lets go through the newhosts.cfg file:

define host
Here we define a new host:
use linux-server: We use Linux-Server because its the easiest template to use, more on this later

host_name google.com: This is the host name, this is how you reference your hosts settings, it doesn’t have to be its address just something meaningful related to the host

alias google.com: This is the long name for the host, this is only used for display purposes

address www.google.com: This is important, its the actual address for the host, either a DNS name or IP address will work here.


define service
This is were we define the services on each host, for google we are only going to check if its alive(ping) and check to see if its web server is running (http). We need to two services for this, however the settings are the same for each:

use This is the service template, use generic-serivce is the standard template.

host_name This is where we reference what host we want the service connected to, use the name of the host defined earlier.

service_description This is the name the user will see on the nagios web page

check_command This is the command we are going to run to test if the service is running, more on commands later.

So what about the nagios.cfg, why did we add that line to it, well as we created a new cfg file we need to tell nagios about it, you can either have one massive file for each host or make a new file for each one, however if you make a new file for each one you have to add it to the nagios.cfg.

Rite, hopefully that has explained the Hosts file to you, now would be a good idea to add your own hosts, start with something simple like another website, try “yahoo.com”, if you encounter problems check my config file for google and yahoo here. Remember to restart nagios after changing any config files with:

sudo /etc/init.d/nagios restart 

if you get any errors when restarting run the following command to check for problems with your config files

sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Please leave any comments for feeback.

, , , , , , , , ,

Comments are closed.