Monday, May 27, 2013

Nagios - Monitoring tool on CentOS 6.4 Script (1-4)




Part 1 Nagios Startup
Part 2 How to monitor Devices (1)
       Linux & Windows servers
Part 3 How to monitor Devices (2)
       Cisco Router & Web server
Part 4 Nagios Visualization 
       Nagiosgraph, NDOUtils with MySQL
Part 5 Nagios Notification & Security

Chapter 1 Network Monitoring Structure


Chapter 2  Testing Environment
1.NMS – Nagios (CentOS 6.4)
l IP Address: 192.168.80.6
l Hostname: client.chul.com
l Need to install and start Nagios service with Web server Apache
2. Managed Devices
1) Linux Server – 192.168.80.5 (CentOS 6.4)
l Need to install nagios-plugins & nrpe package and start nrpe with Xinetd
l Hostname: server.chul.com
2)Windows Server 2008 -  192.168.80.48
l Need to install NSClient++ file and start NSClient++ service
3) Cisco Router -  DHCP
l Configure snmp service and activate SNMP service
4) Apache web server on Linux server
 Need to start web server on Linux sever -  192.168.80.5
Chapter 3 Nagios Installation
1. Package installation
Download rpmforge-release and epel-release from rpmfind.net
# rpm –ivh rpmforge-*; rpm –ivh epel*
# rpm –qa | grep nagios
      nagios-common-3.4.4-1.el6.i686
      nagios-3.4.4-1.el6.i686
      nagios-nrpe-2.14-1.el6.rf.i686
      nagios-plugins-all-1.4.16-5.el6.i686
      nagios-plugins-1.4.16-5.el6.i686
      nagios-devel-3.4.4-1.el6.i686
      nagios-nsca-2.7.2-4.el6.rf.i686
      nagios-plugins-nrpe-2.14-1.el6.rf.i686
      nagios-nsca-client-2.7.2-4.el6.rf.i686
2. Nagios Configuration
a. Modify username
# vi /etc/nagios/cgi.cfg                                
 authorized_for_all_services=nagios
 authorized_for_all_hosts=nagios

b. Nagios password file
# vi /etc/httpd/conf.d/nagios.conf
   Access Control
# htpasswd -c /etc/nagios/htpasswd.users  nagios
3. Nagios files in /etc/nagios
nagios.cfg : the primary Nagios configuration file where lot of global parameters
cgi.cfg  :  configuration information for nagios web interface.
resource.cfg : some sensitive information (username, password etc.) to a plugin to
monitor a specific service
contacts.cfg:  all the contacts who needs to be notified
commands.cfg : all the commands to check services
timeperiods.cfg : define the timeperiods with time templetes
templates.cfg : multiple host or service definition similar to use all common templates
localhost.cfg : defines the monitoring for the local host as  sample configuration file
printer.cfg : sample config file for printer
switch.cfg : sample config file for switch
windows.cfg : sample config file for a windows machine

4. Access Nagios server
# service httpd restart ; nagios –v /etc/nagios/nagios.cfg
# service nagios restart ; tail –f /var/log/nagios/nagios.log


Chapter 4  Managed Device - Linux Server
1.Linux Server Configuration
a. Structure of connection between NMS and Linux server
1) Nagios Server (check_nrpe) —> Remote host (NRPE deamon) —> check_disk(returns disk space usage)
2) Remote host (NRPE deamon) -> Nagios Server (check_nrpe) 
b. NRPE installation and startup
# yum install nagios-plugins-nrpe nagios-nrpe
# vi /etc/xinetd.d/nrpe
l disable = no
l only_from = 127.0.0.1 192.168.80.6
# service xinetd restart
# vi /etc/services
  nrpe                  5666/tcp     # NRPE
# netstat –at | grep nrpe
# /usr/lib/nagios/plugins/check_nrpe -H localhost
# vi /etc/nagios/nrpe.cfg                             // command checking

2. Nagios Server configuration
a. Installation and service checking
# yum install nagios-nrpe –y
# /usr/lib/nagios/plugins/check_nrpe -H 192.168.80.5
b. Creating server file
# cp localhost.cfg server.cfg
# vi /etc/nagios/objects/server.cfg                 //for host and service definition
§  define host{
use linux-server
host_name  server
    alias               Remote Host
    address         192.168.80.5
    contact_groups  admins
c. Adding server to nagios.cfg
# vi /etc/nagios/nagios.cfg
cfg_file=/etc/nagios/objects/server.cfg
# nagios –v /etc/nagiios/nagios.cfg
# service nagios restart
# service httpd restart
d. Service Access

Chapter 5  Managed Device –Windows Server

1. Structure of Windows host with Nagios server
a. Request: Nagios Server (check_nt) --> Remote host (NSClient++) —>                 USEDDISKSPACE
b. Reply: USEDDISKSPACE (returns disk space usage) ->Remote host (NSClient++)  ->Nagios Server (check_nt)
2. 4 steps to install nagios on remote Windows host
a. Install NSClient++ on the remote windows server
    http://sourceforge.net/projects/nscplus
b. Modify the NSClient++ Service
Control Panel -> Administrative Tools -> Services-> NSClientpp
c. Modify the nsclient.ini
§  Edit the C:\Program Files\NSClient++\nsclient.ini
§  allowed_hosts=192.168.80.6
§  port=12489
§  password=Password
d. Start the NSClient++ Service
Control Panel -> Administrative tools -> Services -> Select “NSClientpp (Nagios) 0.3.1.14 2008-03-12 w32″
3. 6 configuration steps on nagios monitoring server
a. Verify check_nt command and windows-server template
# vi /etc/nagios/objects/commands.cfg
define command{
command_name    check_nt
command_line       $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -v $ARG1$ $ARG2$
}
# vi /etc/nagios/objects/templates.cfg                   // windows server template
b. Uncomment windows.cfg in /etc/nagios/nagios.cfg
cfg_file=/etc/nagios/objects/windows.cfg
c. Modify /etc/nagios/objects/windows.cfg
define host{
use                 windows-server              ; Inherit default values from a template
host_name           remote-windows-host      ; The name we're giving to this host
alias              Remote Windows Host     ; A longer name
address             192.168.80.48                  ; IP address of the remote windows host
}
d. Define windows services that should be monitored.
Modify /etc/nagios/objects/windows.cfg
Change host_name
e. Enable Password Protection (/etc/nagios/commands.cfg)
define command{
 command_name   check_nt
 command_line      $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -s Password -v $ARG1$ $ARG2$
}
f. Verify Configuration and Restart Nagios.
# nagios -v  /etc/nagios/nagios.cfg
# /usr/lib/nagios/plugins/check_nrpe –H 192.168.80.48
# service nagios restart

Chapter 6  Managed Device – Cisco Router
1.Cisco Router Configuration
Router(config)# access-list 5 permit 192.168.80.6
Router(config)# snmp-server community jeong ro 5
Router(config)# snmp-server host 192.168.80.6 jeong
Router(config)# snmp-server enable traps snmp linkdown linkup coldstart warmstart
In the Nagios server
# snmpget -v 1 -c jeong 192.168.80.11 SNMPv2-MIB::sysDescr.0
2. Nagios server configuration
a. Download and install the Nagios plugin from nagios.frank4dd.com/plugins/
# vi /usr/lib/nagios/plugins/contrib/check_snmp_patchlevel.pl
20 use lib "/usr/lib/nagios/plugins/" ;
#./check_snmp_patchlevel.pl -H 192.168.80.11 -g ios –C jeong
b. Configure the plugin and service in Nagios
# vi /etc/nagios/objects/commands.cfg
define command{
  command_name    check_snmp_cisco_ios
  command_line    $USER1$/check_snmp_patchlevel.pl -H $HOSTADDRESS$ -g ios
-C $ARG1$
}
c. Configuring the Nagios Version Monitoring Service
# vi /etc/nagios/objects/router.cfg
# vi /etc/nagios/nagios.cfg
cfg_file=/etc/nagios/objects/router.cfg
# nagios –v /etc/nagios/nagios.cfg
# service nagios restart
# service httpd restart
d. Test Router data on Nagios

Chapter 7  Managed Device – Webserver
1.Load module in Apache webserver
# grep status /etc/httpd/conf/httpd.conf
LoadModule status_module modules/mod_status.so
ExtendedStatus On
 <Location /server-status>
 SetHandler server-status
 Order deny,allow
 Deny from all
 Allow from 192.168.80.6 127.0.0.1
 </Location>
# service httpd restart
2. Check nagios plugin for webserver in Nagios server
# /usr/lib/nagios/plugins/contrib/check_apachestatus.pl -H 192.168.80.5
3. Configuring the new Nagios Service
# vi /etc/nagios/objects/commands.cfg
# check_apachestatus.pl -H  [-p ] [-t ] [-w  -c ] [-V]
define command{
  command_name    check_http_stat
  command_line    $USER1$/contrib/check_apachestatus.pl -H $HOSTADDRESS$ $ARG1$
}
# vi /etc/nagios/objects/webserver.cfg
# vi /etc/nagios/nagios.cfg
 cfg_file=/etc/nagios/objects/webserver.cfg
# nagiios –v /etc/nagios/nagios.cfg
# service nagios resart

Chapter 8 How to install Nagiosgraph
1.Install Pre packages
# yum install rrdtool perl-GD perl-CGI rrdtool-perl perl-Time-HiRes –y
2. Download and Install nagiosgraph from
http://sourceforge.net/projects/nagiosgraph/files/nagiosgraph
# cd nagiosgraph-1.4.4
# ./install.pl --check-prereq
# ./install.pl --install
3. Adding nagiosgraph to nagios.cfg
# vi /etc/nagios/nagios.cfg
4. Adding command to commands.cfg
# vi /etc/nagios/objects/commands.cfg
# command to process nagios performance data for nagiosgraph
define command {
          command_name process-service-perfdata-for-nagiosgraph
          command_line /usr/local/nagiosgraph/bin/insert.pl
}
5. Copy nagiosgraph-apache
# cp /usr/local/nagiosgraph/etc/nagiosgraph-apache.conf /etc/httpd/conf.d/
6. Serive restart
# service httpd restart
# nagios –v /etc/nagios/nagios.cfg
# service nagios restart
7. Access nagiosgraph
http://localhost/nagiosgraph/cgi-bin/showconfig.cgi
8. Define service to the host file
# vi /etc/nagios/objects/server.cfg
define service {
          name nagiosgraph
          action_url /nagiosgraph/cgi-bin/show.cgi?host=$HOSTNAME$&serv ice=$SERVICEDESC$
          register 0
}
define service {
          use generic-service,nagiosgraph
          host_name  server
9. Service restart
# nagios –v /etc/nagios/nagios.cfg
# service nagios restart
10. Check nagiosgraph on Nagios
Click services

Chapter 9  NDOUtils with MySQL
1. NDOUtils
Have all of Nagios core’s configuration and data written into MySQL database
2. Packages installation
# yum install ndoutils ndoutils-mysql –y
3. Create database for ndoutils on MySQL
mysql> CREATE DATABASE nagios;
mysql> CREATE USER 'ndo'@'localhost' IDENTIFIED BY ‘ndo’;
mysql> GRANT ALL ON nagios.* TO 'ndo'@'localhost';
mysql> flush privileges;
# cd /usr/share/doc/ndoutils-mysql-1.4/db
# perl installdb -u ndo -p ndo -h localhost -d nagios       //load nagios schema
4. Edit nagios.cfg file
# vi /etc/nagios/nagios.cfg
221 event_broker_options=-1
222 broker_module=/usr/lib/nagios/brokers/ndomod.so config_file=/etc/nagios/ndomod.cfg
5. Edit ndo2db.cfg
# vi /etc/nagios/ndo2db.cfg
      db_host=localhost
      db_port=3306 .
      db_name=nagios
      db_user=ndo
 db_pass=ndo
6. Start ndo service
# service ndo2db start
# ps –ef | grep ndo2db
# nagios –v /etc/nagios/nagios.cfg
# service nagios restart
7. Nagios data on MySQL
# mysql –u ndo –p  –D nagios
a. Retrieve nagios_services table
mysql> select count(1) from nagios_services;
b. Retrieve the content and date/time of the latest ten notifications:
mysql> select start_time, long_output from nagios_notifications ORDER BY start_time DESC LIMIT 10;
c. Retrieve the content and date/time of the latest ten host or service comments:
mysql> select entry_time, comment_data from nagios_commenthistory ORDER BY entry_time DESC LIMIT 10;
d. Count the number of hosts currently in the OK state:
mysql> select COUNT(1) from nagios_hoststatus WHERE current_state=0;