Nagios Installation and Configuration to Monitor IP Networks

Last updated by rkarim1981@gmail.com June 23, 2009

Nagios is the industry-standard for enterprise-class Open Source monitoring of hosts, services, applications, and networks. I have written a step-by-step howto as follows.

STEP-01: Install Ubuntu Server Edition 8.04/8.10/9.04

Install Ubuntu Server Edition 8.04/8.10/9.04 On a particular server/pc/vmware which will be your nagios network monitoring server.

Installation Requirements...
To install nagios packages you have to have a Internet Connection, and While you install Ubuntu Server; Select the following packages.
   >> OpenSSH Server
   >> LAMP

Also determine that; you have One ethernet interface (you may add additional ethernet interfaces for particular section of your network segment) and from this server/pc your monitoring hosts (which you will monitor) are reachable (by icmp and traceroute).


Suppose,
   interface eth0 is connected to Internet
   eth0 have IP: 123.49.42.180

STEP: 02- Put IP Address on LAN Interface and DNS Address

# vi /etc/network/interfaces
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
   address 123.49.42.180
   netmask 255.255.255.224
   network 123.49.42.160
   broadcast 123.49.42.191
   gateway 123.49.42.161

Save + Exit

# /etc/init.d/networking restart

Put dns server address...
# vi /etc/resolv.conf
nameserver 4.2.2.1
nameserver 4.2.2.2
Save + Exit

Now you should get the Internet


STEP: 03- Install some basic packages

# apt-get update
# apt-get -q -y install rcconf vim-nox iftop

STEP: 04- Install Nagios3 packages

# apt-get update
# apt-get install nagios3
# apt-get install smbclient nagios-plugins nagios-plugins-basic nagios-plugins-standard nagios-plugins-extra
# updatedb

STEP: 05- Set Nagios3 Admin Password

Adding password for user nagiosadmin
# cd /etc/nagios3
# htpasswd -c htpasswd.users nagiosadmin

Output
New password:
Re-type new password:

Now open your browser, Browse URL: http://123.49.42.180/nagios3/ you will be asked for username and password, give the username nagiosadmin and password that you have just set. You will see the nagaios page; but still there are more work we have to do in next steps.

STEP: 06- Backup Nagios3 Directory

Bakcup the nagios3 directory (you may need it in future)
# mkdir -p /opt/nag3dir_bak
# cp -r /etc/nagios3 /opt/nag3dir_bak/

STEP: 07- Edit NAGIOS Configuration Files

Here I suppose you have 7 monitoring hosts and they are divided into three groups. (You may change it as your requirements). >host_1 to host_6 are in 3 groups and host google in Group-1 and will run only HTTP Check.

So now edit (copy/past) the file hostgroups_nagios2.cfg
# echo "" > /etc/nagios3/conf.d/hostgroups_nagios2.cfg
# vim /etc/nagios3/conf.d/hostgroups_nagios2.cfg

Add the following lines (make change of Group Name and Hostname as your own)...
#--------- /etc/nagios3/conf.d/hostgroups_nagios2.cfg -------

########################################### Group_1
define hostgroup {
    hostgroup_name Group_1
    alias Group_1
    members host_1, host_2, google
}

########################################### Group_2
define hostgroup {
    hostgroup_name Group_2
    alias Group_2
    members host_3, host_4
}

########################################### Group_3
define hostgroup {
    hostgroup_name Group_3
    alias Group_3
    members host_5, host_6
}
Save + Exit

Edit hosts.cfg file ...
# echo "" > /etc/nagios3/conf.d/hosts.cfg
# vim /etc/nagios3/conf.d/hosts.cfg

Note about the parameter of parents:
The keyword parents defines the previous hop ip of the host for which we define parents. For Example if we, put the parents=host_1 in the host defination section of host_2; it means host_2 is the child of host_1 and in Nagios Status Map, if host_1 goes down then host_2 also will show "down". Again host_2 also can be the parents of any other host. In this way there will be a visual network diagram (tree) drawn in Nagios Status Map. This key word is important to identify the network outage.

So, Carefully define the parents according to your network diagram. If you don't define the parents; by default parents will be the nagios server itself.

Now add the following lines (make change of IP address of hosts as your own)...
#--------- /etc/nagios3/conf.d/hosts.cfg -------

########################################### Group_1
define host {
    use generic-host;
    host_name google;
    address 64.233.189.99;
}

define host {
    use generic-host;
    host_name host_1;
    address 203.22.28.222;
}

define host {
    use generic-host;
    host_name host_2;
    parents host_1;
    address 203.22.28.223;
}

########################################### Group_2
define host {
    use generic-host;
    host_name host_3;
    address 203.22.28.224;
}

define host {
    use generic-host;
    host_name host_4;
    address 203.22.28.225;
}

########################################### Group_3
define host {
    use generic-host;
    host_name host_5;
    address 203.22.28.226;
}

define host {
    use generic-host;
    host_name host_6;
    address 203.22.28.227;
}
Save + Exit

Edit services_nagios2.cfg file.
This file defines that, which services you want monitor for any specific host; the service can be only host-alive (icmp-ping) or smtp, http, pop3, mysql, disk space etc.

# echo "" > /etc/nagios3/conf.d/services_nagios2.cfg
# vim /etc/nagios3/conf.d/services_nagios2.cfg

Add the follwing lines ...
#--------- /etc/nagios3/conf.d/services_nagios2.cfg -------

define service {
    hostgroup_name Group_1
    service_description PING
    check_command check_ping!100.0,20%!900.0,60%
    use generic-service
    notification_interval 0 ; set > 0 if you want to be renotified
}

define service {
    hostgroup_name Group_2
    service_description PING
    check_command check_ping!100.0,20%!500.0,60%
    use generic-service
    notification_interval 0 ; set > 0 if you want to be renotified
}

define service {
    hostgroup_name Group_3
    service_description PING
    check_command check_ping!100.0,20%!500.0,60%
    use generic-service
    notification_interval 0 ; set > 0 if you want to be renotified
}

define service {
    host_name google
    service_description HTTP
    check_command check_http
    use generic-service
    notification_interval 0 ; set > 0 if you want to be renotified
}
Save + Exit

Now remove extra config files and verify the nagios configuration files and start/restart the nagios service; here there should not show any error. If any error exits you have to troubleshoot it and rechek the configuration files above. Good luck...

# cd /etc/nagios3/conf.d
# rm extinfo_nagios2.cfg host-gateway_nagios3.cfg
# cd /etc/nagios3/
# nagios3 -v nagios.cfg
# /etc/init.d/nagios3 restart

So, your nagios is up and running. Now you have to add some general users who will access the nagios with several level of access those are defined in /etc/nagios3/cgi.cfg . Here I only added a user named as "guest" , you may add more users according your need and level of access permission.

Edit the cgi.cfg file ...
# vim /etc/nagios3/cgi.cfg

Add guest as shown in the file (/etc/nagios3/cgi.cfg) as putting a comma (,) after the user nagiosadmin. Here you may add more users just putting a comma (,) before each user name.
#--------- /etc/nagios3/cgi.cfg -------

authorized_for_configuration_information=nagiosadmin,guest
authorized_for_all_services=nagiosadmin,guest
authorized_for_all_hosts=nagiosadmin,guest
Save + Exit

Create the user guest set the password.
# cd /etc/nagios3
# htpasswd htpasswd.users guest

Output
New password:
Re-type new password:
Adding password for user guest

Now Browse the url: http://123.49.42.180/nagios3 Login with user guest and see the nagios pages and hosts service status. I hope you may explore more after implementing this howto by yourself.

You may create a symbolic link for nagios3 access page as only nagios...
# ln -s /usr/share/nagios3/htdocs /var/www/nagios

Check new url: Login with guest
http://123.49.42.180/nagios

Changing the guest Password
# htpasswd htpasswd.users guest

Changing the nagiosadmin Password
# htpasswd htpasswd.users nagiosadmin


...I believe that "There are two ways of spreading light, be candle or be a mirror to reflect it"