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/
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
:
Start by creating two new directories to store our configuration files:
cd /usr/local/nagios/etc/objects mkdir hosts mkdir services
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
Add the following lines to allow all files in our
hosts
andservices
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
Save the file and close it, it's time to add our first new 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:
Create a new file at
/usr/local/nagios/etc/objects/hosts
calledtest.cfg
, and open it in a text editor:define host{ host_name test alias test address 127.0.0.1 use linux-server }
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.
Save the file and close it. You just added your first new host!
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:
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 }
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.Save the file and close it.
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:
Open
/usr/local/nagios/etc/objects/contacts.cfg
in your preferred text editor.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.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] }
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 }
Save the file and close it.
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
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.
Nagios configurations can be verified on any installation, by running the Nagios binary file with the
-v
flag, followed by the mainnagios.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
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
Things look okay. No serious problems were detected during the pre-flight check.
Once configuration verification succeeds, Nagios can be restarted with the following command:
/etc/init.d/nagios restart
Access the web interface to see the new host and service in Nagios!