Friday, 21 September 2007

Installing and Configuring Nagios

1) Getting Nagios

You need root access on the server, where you would like to install Nagios. Login as root.

Create a directory for downloads.

#mkdir -p /usr/src/backs/nagios
#cd /usr/src/backs/nagios

Download source of nagios and nagios-plugins at http://www.nagios.org/download/.

#wget http://keihanna।dl।sourceforge.net/sourceforge/naagios
/nagios-1.2.tar.gz
#wget http://easynews.dl.sourceforge.net/sourceforge/nagiosplug
/nagios-plugins-1.3.1.tar.gz

2) Compilation and installation of Nagios

Login as root and create a user, say "nagios"
#su
#adduser nagios
#passwd nagios
Changing password for user nagios.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

In some servers normal users may not have permission to use 'mail' inorder to send alerts.
usermod -G mail nagios
Make a directory for working with the source code.
mkdir -p /usr/src/works/nagios
cd /usr/src/works/nagios/
tar -zxvf /usr/src/backs/nagios/nagios-1.2.tar.gz
cd nagios-1.2/
./configure
The default is fine for normal operation. for details see the file 'INSALL'.
./configure --prefix=prefix --with-cgiurl=cgiurl --with-htmurl=htmurl
--with-nagios-user=someuser --with-nagios-grp=somegroup
./configure
make all
make install
make install-init # Needed to add the service 'nagios'.
make install-commandmode
make install-config
Install the plugins
cd ../
tar -zxvf /usr/src/backs/nagios/nagios-plugins-1.3.1.tar.gz
cd nagios-plugins-1.3.1/
./configure
NOTE:- For mysql and postgresql plugins to work, their libraries have
to be installed. so to avoid installing those we will use 'check_tcp'
Instead later with seperate port for mysql and postgresql.
make
make check
make install

Libraries will be installed in '/usr/local/nagios/libexec' position by the default installation. If there is no contents do the following.

 mkdir /usr/local/nagios/libexec
mv /usr/lib/nagios/plugins/* /usr/local/nagios/libexec/
ln -s /lib/libcrypto.so.0.9.7a /lib/libcrypto.so.4

3) Post install Configuration of Nagios

Edit apache's httpd.conf file. (the location of the httpd.conf file may differ)
vim /etc/httpd/conf/httpd.conf
Add the following line at the end of the file.
Include /etc/httpd/conf/nagios.conf
Create the file '/etc/httpd/conf/nagios.conf'.
vi /etc/httpd/conf/nagios.conf
Add the following contents to this file.
-------------------------------------------------
ScriptAlias /nagios/cgi-bin/ /usr/local/nagios/sbin/

AllowOverride AuthConfig
Options ExecCGI
Allow from all
Order allow,deny


Alias /nagios/ /usr/local/nagios/share/

Options None
AllowOverride AuthConfig
Order allow,deny
Allow from all

--------------------------------------------------

Make sure the updations done with http is fine and restart apache.
#service httpd restart
Create '.htaccess' file in '/usr/local/nagios/share/' and '/usr/local/nagios/sbin/'.
#touch /usr/local/nagios/share/.htaccess
#touch /usr/local/nagios/sbin/.htaccess
Add the following contents to both these files.
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
require valid-user

Create the username, password for logging into the Nagios interface.

htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
ls -l /usr/local/nagios/etc/htpasswd.users
Make it available for the user who runs httpd.
chmod o+r /usr/local/nagios/etc/htpasswd.users
Restart the httpd service.
service httpd restart

5)Advanced Nagios Configurations.

Rename the sample files to the real names.
cd /usr/local/nagios/etc/
for i in *sample ; do mv $i `echo "$i" |sed s/-sample//` ; done
Edit the main conf file, nagios.cfg. and change only these lines.
check_external_commands=1
Make these Cgi specific changes in cgi.cfg.
use_authentication=1
authorized_for_system_information=nagiosadmin
authorized_for_configuration_information=nagiosadmin
authorized_for_system_commands=nagiosadmin
authorized_for_all_services=nagiosadmin
authorized_for_all_hosts=nagiosadmin
authorized_for_all_service_commands=nagiosadmin
authorized_for_all_host_commands=nagiosadmin

Add servers that need to be monitored

Make the Host specific changes to the hosts.cfg.
 Generic host definition template
define host{
name generic-host
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
process_perf_data 1
retain_status_information 1
retain_nonstatus_information 1

register 0
}


define host{
use generic-host ; Name of host
template to use

host_name
alias
address
check_command check-host-alive
max_check_attempts 10
notification_interval 120
notification_period 24x7
notification_options d,u,r
}
Repeat for all the hosts we want to check.

Group them in hostgroups.cfg

define hostgroup{
hostgroup_name
alias
contact_groups
members <,,..>
}
Repeat for all the hosts groups.

Specify the services that need to be checked

Edit the details of all the services, we want checked by Nagios in services.cfg.
 Generic service definition template
define service{
name generic-service
active_checks_enabled 1
passive_checks_enabled 1
parallelize_check 1
obsess_over_service 1
check_freshness 0
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
process_perf_data 1
retain_status_information 1
retain_nonstatus_information 1
register 0
}


define service{
use generic-service
host_name
service_description
is_volatile 0
check_period 24x7
max_check_attempts 3
normal_check_interval 3
retry_check_interval 1
contact_groups
notification_interval 120
notification_period 24x7
notification_options w,u,c,r
check_command
}
Repeat for each service on each host.

Specify Alert Contacts

In order to specify details of the contacts for alerts, we need to edit contacts.cfg.
 'nagios' contact definition
define contact{
contact_name
alias
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-by-email,notify-by-epager
host_notification_commands
host-notify-by-email,host-notify-by-epager
email
pager
}
Repeat for all contacts.

Group the contacts

Edit contactgroups.cfg, in order to specify groups of contacts.
define contactgroup{
contactgroup_name
alias
members
}
Repeat for all groups to include all contacts

Escelating with nagios

If you need to send selective alerts, for example when critical issues occur, simply configure the escalations.cfg file.
define serviceescalation{
host_name
service_description <>
first_notification 2
last_notification 6
contact_groups <>
notification_interval 0
}
Repeat for all escalations Once again, check all the files, nagios.cfg cgi.cfg hosts.cfg hostgroups.cfg, services.cfg, contacts.cfg, contactgroups.cfg, escalations.cfg and checkcommands.cfg Restart httpd and then Nagios.
service httpd restart
Add Nagios to system services.
chkconfig --add nagios
service nagios restart
Check for any errors reported, and solve them one by one if any.
Enable nagios in runlevels, so that it starts on boot.
chkconfig nagios on
Create cron.daily entry if we want to restart the service daily. Here is the script.
vi etc/cron.daily/nagios-restart.cron
#!/bin/sh
/sbin/service nagios restart >/dev/null 2>&1

Make it executable
chmod +x etc/cron.daily/nagios-restart.cron
And finally, Check if nagios is working fine.!!! ;-)
http://NAGIOS-SERVER-NAME/nagios/index.html

No comments: