Quick start – Monitoring hosts and services

Exclusive offer: get 50% off this eBook here
Instant Nagios Starter [Instant]

Instant Nagios Starter [Instant] — Save 50%

An easy guide to getting a Nagios server up and running for monitoring, altering, and reporting with this book and ebook

$14.99    $7.50
by Michael Guthrie | June 2013 | Linux Servers Networking & Telephony Open Source

In this article by Mike Guthrie, author of Instant Nagios Starter, we will see how to monitor hosts and services. The logic of Nagios as a monitoring engine is determined by a set of object configuration files, which are located under the etc directory of the Nagios tree. For a source installation, this directory is located at /usr/local/nagios/etc. From the configuration files in this directory, Nagios knows what to monitor, when to monitor, who to notify, and how to respond to events. Interaction with these files is required in order to set up monitoring with Nagios, so understanding how to work with them is essential. This article will be an introduction to the basics of adding new hosts, services, contacts, and templates. A complete reference for all the Nagios configuration files is available in the Nagios core manual on sourceforge.net, and it is by far the best reference for a complete understanding of Nagios configuration files. It can be found at the following link:

http://nagios.sourceforge.net/docs/3_0/

(For more resources related to this topic, see here.)

Step 1 – Modifying nagios.cfg

Nagios knows which configuration files to parse by maintaining a master reference in the nagios.cfg file. For any new configuration file to be recognized by Nagios, either the file or the directory it is in has to be defined in nagios.cfg. For source installations, this file is located at /usr/local/nagios/etc/nagios.cfg. Perform the following steps to modify nagios.cfg:

  1. Start by creating two new directories to store our configuration files:

    cd /usr/local/nagios/etc/objects
    mkdir hosts
    mkdir services

  2. Then open nagios.cfg with your preferred text editor to add the new directories:

    # You can specify individual object config files as shown below:
    cfg_file=/usr/local/nagios/etc/objects/commands.cfg
    cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
    cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
    cfg_file=/usr/local/nagios/etc/objects/templates.cfg
    # Definitions for monitoring the local (Linux) host
    cfg_file=/usr/local/nagios/etc/objects/localhost.cfg

  3. Add the following lines to allow all files in our hosts and services directories to be automatically added to the monitoring configuration:

    cfg_dir=/usr/local/nagios/etc/objects/hosts
    cfg_dir=/usr/local/nagios/etc/objects/services

  4. Save the file and close it, it's time to add our first new host!

Step 2 – Adding a host

A host in Nagios is any machine or device with an IP address or a host name. The following example will demonstrate the creation of a basic host configuration file that we can add to the monitoring configuration:

  1. Create a new file at /usr/local/nagios/etc/objects/hosts called test.cfg, and open it in a text editor:

    define host{
    host_name test
    alias test
    address 127.0.0.1
    use linux-server
    }

  2. There are many more configuration directives that can be specified for a host, but as a best practice, it's best to specify as many of these values in a template as possible.

  3. Save the file and close it. You just added your first new host!

Step 3 – Adding a service

Services in Nagios are processes, applications, metrics, and anything else that can be monitored under the scope of the associated host. The following example creates a basic service definition for the test used, and will be used to start monitoring with a simple HTTP check:

  1. Service configurations are created in much the same way that hosts are. Create a new file named test.cfg, at /usr/local/nagios/etc/objects/services, and open it in a text editor:

    define service{
    host_name test
    service_description HTTP
    check_command check_http
    use generic-service
    }

  2. Services can be applied to a single host, a list of hosts, or even an entire host group, and check_command specified for each of them can be customized to take custom arguments as well. However, for the moment, let's start things simple and keep moving forward.

  3. Save the file and close it.

Step 4 – Creating and assigning contacts

Alerting is an essential part of monitoring infrastructure with Nagios, but it is recommended to minimize or even disable the use of alerts while setting up your monitoring environment. Users who receive too many false alerts will be trained to ignore them. Setting up effective alerting starts with creating appropriate contacts and contact groups for the hosts and services that are being monitored. Contacts also form the basis for host and service permissions in Nagios. A regular-level user in Nagios will only be able to view and submit commands for hosts and services that they are contacts for, unless he/she is granted some level of global access in the cgi.cfg file. The following are the steps to create and assign contacts:

  1. Open /usr/local/nagios/etc/objects/contacts.cfg in your preferred text editor.

  2. By default, the nagiosadmin contact is already created for you. This account should typically be reserved for the top-level Nagios administrator. For other users, new contacts should be created.

  3. Add a new contact definition with your preferred username and e-mail address.

    define contact{
    contact_name test
    use generic-contact
    alias Test User
    email test@example.com
    }

  4. Let's also add this contact to the admins contact group, which already exists in the same file:

    define contactgroup{
    contactgroup_name admins
    alias Nagios Administrators
    members nagiosadmin,test
    }

  5. Save the file and close it.

  6. In order to allow the access of the web interface to the new contacts, they need to be added to the htpasswd.users file using the following command:

    htpasswd -c /usr/local/nagios/etc/htpasswd.users test

Step 5 – Verifying configuration and restarting Nagios

All monitoring and event handling is done based on rules defined in the object configuration files, so the monitoring process requires a valid configuration in order to run. Always verify any configuration changes before attempting to restart Nagios, using the following steps. Attempting to restart Nagios with configuration errors will halt all the monitoring on the system.

  1. Nagios configurations can be verified on any installation, by running the Nagios binary file with the -v flag, followed by the main nagios.cfg file. On a source installation, this command can be run as follows:

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

  2. If all goes well, you'll see the following message verifying that there are no configuration errors and that Nagios is ready to be restarted:

    Total Warnings: 0
    Total Errors: 0

  3. Things look okay. No serious problems were detected during the pre-flight check.

  4. Once configuration verification succeeds, Nagios can be restarted with the following command:

    /etc/init.d/nagios restart

  5. Access the web interface to see the new host and service in Nagios!

Summary

In this article, we saw how to monitor hosts and services using a step-by-step approach. These four steps give an easy way to achieve our goal.

Resources for Article :


Further resources on this subject:


Instant Nagios Starter [Instant] An easy guide to getting a Nagios server up and running for monitoring, altering, and reporting with this book and ebook
Published: May 2013
eBook Price: $14.99
See more
Select your format and quantity:

About the Author :


Michael Guthrie

Michael Guthrie is the lead developer at Nagios enterprises and has developed new features and add-ons for Nagios Core, Nagios XI, and Nagios Fusion. Michael started his IT career at Nagios Enterprises as a student developer writing documentation and new components for Nagios Core and Nagios XI, as well as giving technical support. At work, Michael particularly enjoys working on UI development, performance tuning, and data visualizations. When he's not at work he enjoys spending time with his family, being outside, and working on his house.

Michael started his jobs at Nagios Enterprises by writing a substantial amount of documentation for Nagios XI, which is now the Nagios XI Administrator's Manual.

Books From Packt


 Xen Virtualization
Xen Virtualization

 PostgreSQL 9.0 High Performance
PostgreSQL 9.0 High Performance

Building Telephony Systems with OpenSER
Building Telephony Systems with OpenSER

Scalix: Linux Administrator's Guide
Scalix: Linux Administrator's Guide

 Zenoss Core Network and System Monitoring
Zenoss Core Network and System Monitoring

 Zenoss Core 3.x Network and System Monitoring
Zenoss Core 3.x Network and System Monitoring

 Nagios Core Administration Cookbook
Nagios Core Administration Cookbook

 Learning Nagios 3.0
Learning Nagios 3.0


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software