Book Image

Instant Nagios Starter

By : Michael Guthrie
Book Image

Instant Nagios Starter

By: Michael Guthrie

Overview of this book

<p>Whether your project is large or small, the world demands reliability from its information technology. Nagios provides an effective solution to monitor any type of technology, whether it is hardware, software, or business processes. No company wants to lose face because of poor reliability; it is better to know where the problems are before your customers do!.</p> <p>Instant Nagios Starter is a powerful tool, teaching you how to use Nagios as a monitoring solution and how it can be set up for intelligent monitoring as well as incident response.</p> <p>You will learn the fundamentals of how Nagios works and getting Nagios up and running in your environment. Get introduced to the key functionalities of Nagios and learn how to implement them for effective monitoring. With installation and setup broken down into easy-to-follow steps, you will be equipped with all the resources you’ll need to extend Nagios’ capabilities for any of your monitoring needs. Furthermore, learn to efficiently utilize the different tools that Nagios employs for monitoring, alerting as well as incident management.</p>
Table of Contents (7 chapters)

Quick start – Monitoring 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 section 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/

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                           [email protected]     
            }
  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!