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
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 conﬁguration ﬁles, 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 conﬁguration ﬁles in this directory, Nagios knows what to monitor, when to monitor, who to notify, and how to respond to events. Interaction with these ﬁles 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 conﬁguration ﬁles is available in the Nagios core manual on sourceforge.net, and it is by far the best reference for a complete understanding of Nagios conﬁguration ﬁles. It can be found at the following link:
(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:
Start by creating two new directories to store our configuration files:
Then open nagios.cfg with your preferred text editor to add the new directories:
# You can specify individual object config files as shown below:
# Definitions for monitoring the local (Linux) host
Add the following lines to allow all files in our hosts and services directories to be automatically added to the monitoring configuration:
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:
Create a new file at /usr/local/nagios/etc/objects/hosts called test.cfg, and open it in a text editor:
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!
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:
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:
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.
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:
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.
alias Test User
Let's also add this contact to the admins contact group, which already exists in the same file:
alias Nagios Administrators
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
Step 5 – Verifying configuration and restarting Nagios
All monitoring and event handling is done based on rules deﬁned in the object conﬁguration ﬁles, so the monitoring process requires a valid conﬁguration in order to run. Always verify any conﬁguration changes before attempting to restart Nagios, using the following steps. Attempting to restart Nagios with conﬁguration 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 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
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:
Access the web interface to see the new host and service in Nagios!
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 :
- Monitoring CUPS- part2 [Article]
- Troubleshooting Nagios 3.0 [Article]
- Notifications and Events in Nagios 3.0- part2 [Article]
About the Author :
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.