Nagios is a popular system monitor that offers monitoring and alerting servers, applications, services, network devices… It’s very modular and multiplatform, because the core of nagios is the processor of the output of the plugins that can be developed in different language Perl, Java, C, Python… Also Nagios allow get information with the snmp protocol like cpu load, process that are running, disk space, network interfaces, etc.
In this tutorial I’ll configure a small scenario composed by one Nagios server (192.168.1.68), Mail server Postfix+Dovecot (192.168.1.200), DNS server (192.168.1.50) and a router (192.1681.1), and I’ll configure nagios to monitor the main services in our scenario like SMTP, POP/IMAP, DNS, MySQL…
1.- Install EPEL repository:
# rpm -ivh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-5.noarch.rpm # rpm --import https://fedoraproject.org/static/217521F6.txt
2.- Installing nagios and plugins:
# yum install nagios nagios-plugins nagios-plugins-all ndoutils-mysql mysql-server httpd php net-snmp net-snmp-utils postfix
3.- Change nagiosadmin password for web gui:
# cd /etc/nagios/ # htpasswd passwd nagiosadmin
4.- Disable selinux:
# setenforce 0 # vi /etc/sysconfig/selinux SELINUX=disabled
5.- Add iptables rule for web access (/etc/sysconfig/iptables):
-A INPUT -m tcp -p tcp --dport 80 -j ACCEPT # service iptables restart
6.- Run services and add default init level to start:
# chkconfig --levels 235 httpd on # chkconfig --levels 235 mysqld on # chkconfig --levels 235 postfix on # chkconfig --levels 235 snmpd on # chkconfig --levels 235 ndo2db on # chkconfig --levels 235 nagios on
7.- Prepare nagios to work with a mysql database:
# service mysqld start # mysqladmin -u root password root # mysql -u root -p mysql> create database nagios; mysql> grant all on nagios.* to 'nagios'@'localhost' identified by 'nagios'; mysql> quit # cd /usr/share/doc/ndoutils-mysql-1.4/db # chmod +x installdb && ./installdb -u nagios -p nagios -h localhost -d nagios # vi /etc/nagios/ndo2db.cfg: db_servertype=mysql db_host=localhost db_port=3306 db_name=nagios db_prefix=nagios_ db_user=nagios db_pass=nagios # vi /etc/nagios/nagios.cfg: broker_module=/usr/lib/nagios/brokers/ndomod.so config_file=/etc/nagios/ndomod.cfg # service ndo2db start # service nagios start
8.- Configuring postfix for mail alerts (/etc/postfix/main.cf):
# vi /etc/postfix/main.cf myhostname = nagios-server.mydomain.com inet_interfaces = localhost relay_domains = mydomain.com, $mydestination relay_host = mail.mydomain.com smtpd_recipient_restrictions = reject_unauth_destination, permit_mynetworks,reject # service postfix start
Nagios configuration files:
- /etc/nagios/cgi.cfg : Setup of web access features.
- /etc/nagios/private/resource.cfg : This file config is used by Nagios to define internal variables.
- /etc/nagios.nagios.cfg : Main file configuration to Nagios
- /etc/nagios/objects/*.cfg : This directory are by default files with definitions of services, contacts, hosts, groups, timeperiods, commands… used by default config of Nagios.
- /etc/nagios/objects/templates.cfg : default templates defined by default.
– Defining contacts:
# vi /etc/nagios/objects/contacts.cfg
define contact{ contact_name nagiosadmin ; Short name of user use generic-contact ; Inherit default values from generic-contact template alias Nagios Admin ; Full name of user email [email protected] ; ***CHANGE TO YOUR MAIL ADDRESS*** }
define contactgroup{ contactgroup_name admins alias Nagios Administrators members nagiosadmin }
With this configuration we define a contact named nagiosadmin with mail notification [email protected], and it is included in the admins group. The nagiosadmin contact use a template generic-contact, this settings are in the file templates.cfg:
define contact{ name generic-contact ; The name of this contact template service_notification_period 24x7 ; service notifications can be sent anytime host_notification_period 24x7 ; host notifications can be sent anytime service_notification_options w,u,c,r,f,s ; send notifications for all service states, flapping events, and scheduled downtime events host_notification_options d,u,r,f,s ; send notifications for all host states, flapping events, and scheduled downtime events service_notification_commands notify-service-by-email ; send service notifications via email host_notification_commands notify-host-by-email ; send host notifications via email register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE! }
This contact templates use mail notification, time timeperiod for host and service 24×7.
Add in /etc/nagios/nagios.cfg:
cfg_file=/etc/nagios/objects/contacts.cfg
– Define timeperiods :
# vi /etc/nagios/objects/timeperiods.cfg
define timeperiod{ timeperiod_name 24x7 alias 24 Hours A Day, 7 Days A Week sunday 00:00-24:00 monday 00:00-24:00 tuesday 00:00-24:00 wednesday 00:00-24:00 thursday 00:00-24:00 friday 00:00-24:00 saturday 00:00-24:00 }
with this definition we configure a 24×7 time, so we can exclude time ranges like holidays and festivity days, in this timeperiod for example:
define timeperiod{ name us-holidays timeperiod_name us-holidays alias U.S. Holidays january 1 00:00-00:00 ; New Years monday -1 may 00:00-00:00 ; Memorial Day (last Monday in May) july 4 00:00-00:00 ; Independence Day monday 1 september 00:00-00:00 ; Labor Day (first Monday in September) thursday 4 november 00:00-00:00 ; Thanksgiving (4th Thursday in November) december 25 00:00-00:00 ; Christmas }
define timeperiod{ timeperiod_name 24x7-with-holidays use 24x7 ;Include all the days exclude us-holidays ;Exclude holidays }
Add in /etc/nagios/nagios.cfg:
cfg_file=/etc/nagios/objects/timeperiods.cfg
– Adding hosts examples (/etc/nagios/objects/mail.mydomain.cfg):
define host{ use linux-server ; Name of host template to use ; This host definition will inherit all variables that are defined ; in (or inherited by) the linux-server host template definition. hostgroups linux-servers ; Hostgroups that belongs host_name mail.mydomain.com alias mail.mydomain.com icon_image redhat.gif ;icon image that show in nagios gui statusmap_image redhat.gd2 ;icon image that show nagios in map section address 192.168.1.200 parents dns-server ;parent nodes }
The image logos can found in the directory: /usr/share/nagios/html/images/logos/
define hostgroup{ hostgroup_name mail-spool ; The name of the hostgroup alias Mail spool ; Long name of the group members mail.mydomain.com ; Comma separated list of hosts that belong to this group }
This entries are for define a new host, included in the hostgroup linux-servers and uses the template linux-server:
define host{ name linux-server ; The name of this host template use generic-host ; This template inherits other values from the generic-host template check_period 24x7 ; By default, Linux hosts are checked round the clock check_interval 5 ; Actively check the host every 5 minutes retry_interval 1 ; Schedule host check retries at 1 minute intervals max_check_attempts 10 ; Check each Linux host 10 times (max) check_command check-host-alive ; Default command to check Linux hosts notification_period workhours ; Linux admins hate to be woken up, so we only notify during the day ; Note that the notification_period variable is being overridden from ; the value that is inherited from the generic-host template! notification_interval 120 ; Resend notifications every 2 hours notification_options d,u,r ; Only send notifications for specific host states contact_groups admins ; Notifications get sent to the admins by default register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE! }
Add in /etc/nagios/nagios.cfg:
cfg_file=/etc/nagios/objects/mail.mydomain.cfg
– Services definitions examples:
# vi /etc/nagios/objects/dns-server.cfg
define service{ use generic-service host_name dns-server service_description check root check_command check_dig!192.168.1.50!.!ns notifications_enabled 1 }
# vi /etc/nagios/objects/localhost.cfg:
define service{ use local-service ; Name of service template to use host_name nagios-server service_description MySQL check_command check_mysql!localhost!nagios!nagios!3306 notifications_enabled 1 }
# vi /etc/nagios/objects/mail.mydomain.cfg
define service{ use generic-service ; Name of service template to use host_name mail.mydomain.com service_description SMTP check_command check_smtp notifications_enabled 1 }
– Command definition examples:
# DNS check definition define command { command_name check_dns command_line $USER1$/check_dns -H $ARG1$ -s $HOSTADDRESS$ }
#Dig check query for root servers define command { command_name check_dig command_line $USER1$/check_dig -H $ARG1$ -l $ARG2$ -T $ARG3$ }
#check for mysql connection define command { command_name check_mysql command_line $USER1$/check_mysql -H $ARG1$ --username $ARG2$ --password $ARG3$ --port $ARG4$ }
Nagios must be restarted for each change.
# service nagios restart
9.- Accessing the web interface (http://192.168.1.68/nagios):
- Tactical Overview:
- Hosts monitored:
- Host Groups:
- Services:
- Network Map:
- Nagios mail alerts: