5 min read

(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 [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!

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:


LEAVE A REPLY

Please enter your comment!
Please enter your name here