[Nagios][1] is one of the most powerful network monitoring systems, which is widely used in the industry. It can actively monitor any network, and generate audio/email warnings and alerts when any problem is detected. The check types and alert timers are fully customizable.
Another incredible capability of Nagios is that it can monitor both hosts and services e.g., it can monitor IP addresses and TCP/UDP port numbers. To explain a little, let us assume that there is a web server that we want to monitor. Nagios can check whether the server is online by running ping on the IP/name of the server as well as it can be set up to provide warnings in case the round trip time (RTT) to the server increases. Further, Nagios can also check whether TCP port 80 (web server) is reachable e.g., the server is online but Apache/IIS is not responding.
There are also 3rd party monitoring tools that are based on Nagios, such as [Centreon][2], [FAN][3] , [op5 Monitor][4], which supplement standalone Nagios engine in terms of interface, automation, and technical support.
This tutorial explains **how to install and configure Nagios on Linux**.
### Install Nagios on Debian or Ubuntu ###
On a Debian-based system, the installation in itself is a very simple process thanks to apt-get.
root@mrtg:~# apt-get install nagios3
The mail server setting can be done during Nagios installation. It can also be configured later when needed.
Note: A valid SMTP configuration is needed for Nagios to be able send email notifications.
As it can be seen, Nagios supports multiple options for email delivery. The most common options would be Internet Site where the server sends email directly to the recipient. Another widely used option is using smarthost or relay server, in which the server sends the email to an intermediary mail server which in turn is responsible for delivering the mail to the recipient.
Next, the domain name of the server has to be included in the next step.
yum is used for installation. After [setting up the repoforge repository][8], run yum as follows.
[root@mrtg ~]# yum install nagios nagios-plugins
### Requirements for Monitoring ###
In this tutorial, we want to monitor the following.
1. All Linux server will be checked every 3 minutes.
1. All Cisco Routers will be checked every 3 minutes.
1. All email alerts should go to the address sentinel@example.tst.
1. Nagios will verify 3 times before sending out any alerts just to be sure that the problem is real.
1. If the RTT to any device exceeds 100 ms and/or packet loss exceeds 20%, email alert will be generated.
The rest of the tutorial will guide you to configure Nagios on Linux.
### Nagios Configuration on Ubuntu ###
It is important to know where Nagios configuration files are located. The following table shows the location of Nagios configuration files on Debian-based systems.
/etc/nagios-plugins Customizable scripts used for monitoring
/etc/nagios3 Configuration files to add hosts, services, define checks and timers
/usr/lib/nagios/plugins Executable files used for monitoring
The following steps are inter-related. Hosts, groups for hosts and adding services to host groups are defined.
### Adding Host Template ###
The templates defining what to do with a type host is defined. We use the files provided with the installation as sample.
> host_name our-server ; The hostname to be used by nagios
> alias our-server
> address 172.17.1.23 ; The IP address of the host
> }
>
> # Host 2
> define host{
> use cisco-device ; Name of host template to use
> host_name our-router ; The hostname to be used by nagios
> alias our-router
> address 172.17.1.1 ; The IP address of the host
> }
### Host Group Definition ###
For ease of management when there are multiple hosts, it is advisable that hosts of similar types are grouped together.
root@mrtg:/etc/nagios3/conf.d/# vim hostgroups_nagios2.cfg
> definehostgroup {
> hostgroup_name linux-server ; the name of the host group
> alias Linux Servers
> members our-server ; comma separated list of members
> }
>
> definehostgroup {
> hostgroup_name cisco-device ; the name of the host group
> alias Cisco Devices
> members our-server ; comma separated list of members
> }
### Service Definition ###
First, the command example-host-check is defined with warning value of 100 ms for latency and 20% for packet loss. Critical values are 5000 ms for latency and 100% packet loss. One IPv4 ping request is transmitted.
root@mrtg:~# vim /etc/nagios-plugins/config/ping.cfg
Finally, a dry run is initiated to check whether there are any configuration errors. If there are no errors, Nagios can be (re)started safely.
root@mrtg:~#nagios –v /etc/nagios3/nagios.cfg
root@mrtg:~# service nagios3 restart
### Nagios Configuration on CentOS/RHEL ###
The following shows the locations of Nagios configuration files on Redhat-based systems.
> /etc/nagios/objects Configuration files to add hosts, services, define checks and timers
> /usr/lib/nagios/plugins Executable files used for monitoring
### Adding Host Template ###
A template is created to define what needs to be done for a specific type of host. The files provided with the installation is modified.
[root@mrtg objects]# cd /etc/nagios/objects/
[root@mrtg objects]# vim templates.cfg
> define host{
> name linux-server
> use generic-host
> check_period 24x7
> check_interval 3
> retry_interval 1
> max_check_attempts 3
> check_command example-host-check
> notification_period 24x7
> notification_interval 0
> notification_options d,u,r
> contact_groups admins
> register 0
> }
>
> define host{
> name cisco-router
> use generic-host
> check_period 24x7
> check_interval 3
> retry_interval 1
> max_check_attempts 3
> check_command example-host-check
> notification_period 24x7
> notification_interval 0
> notification_options d,u,r
> contact_groups admins
> register 0
> }
### Adding Hosts and Host Groups ###
The configuration file provided with by default is used as a sample. The hosts and host groups are added in the same file.
[root@mrtg objects]# cp localhost.cfg example.cfg
[root@mrtg objects]# vim example.cfg
> #Adding Linux server
> define host{
> use linux-server
> host_name our-server
> alias our-server
> address 172.17.1.23
> }
>
> #Adding Cisco Router
> define host{
> use cisco-router
> host_name our-router
> alias our-router
> address 172.17.1.1
> }
>
> # HOST GROUP DEFINITION
> define hostgroup{
> hostgroup_name linux-servers
> alias Linux Servers
> members our-server
> }
>
> define hostgroup{
> hostgroup_name cisco-router
> alias cisco-router
> members our-router
> }
### Service Definition ###
A service called example-host-check is defined with warning values of 100 ms for latency and 20% for packet loss. The critical values are 5000 ms for latency and 100% for packet loss. Only one IPv4 ping request will be transmitted.
The email address where alerts will be sent is added into Nagios.
[root@objects objects]# vim contacts.cfg
> define contact{
> contact_name nagiosadmin
> use generic-contact
> alias Nagios Admin
> email nagios@localhost, sentinel@example.tst
> }
Finally, we are ready to start the Nagios service. A dry run is recommended to find out errors in configuration.
[root@mrtg ~]# nagios –v /etc/nagios/nagios.cfg
[root@mrtg ~]# service nagios restart
[root@mrtg ~]# chkconfig nagios on
### Access Nagios after Configuration ###
Now you are ready to use Nagios. Nagios can be accessed by opening the URL http://IP/nagios3 in case of Ubuntu/Debian or http://IP/nagios in case of CentOS/RHEL e.g. http://172.17.1.23/nagios3. The user "nagiosadmin" needs to be authenticated to access the page.