Friday, November 1, 2013

Nagios Set up on CentOS

Nagios Set up on CentOS

Nagios is an awesome Open Source monitoring tool, its provides you more comprehensive monitoring environment to always keep an eye on your all machines / networks whether you are in a your network environment.

1. Install Required Dependencies
[root@nagios-server]# yum install -y httpd php gcc glibc glibc-common gd gd-devel make net-snmp

2. Create Nagios User and Group
[root@nagios-server]# useradd nagios
[root@nagios-server]# groupadd nagcmd
Next, add both the nagios user and the apache user to the nagcmd group.
[root@nagios-server]# usermod -G nagcmd nagios
[root@nagios-server]# usermod -G nagcmd apache

3. Download Nagios Core 4.0.1 and Nagios Plugin 1.5
Create a folder to keep the downlaod file
[root@nagios-server]# mkdir /root/nagios
[root@nagios-server]# cd /root/nagios

[root@nagios-server nagios~]# wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-4.0.1.tar.gz
[root@nagios-server nagios~]# wget https://www.nagios-plugins.org/download/nagios-plugins-1.5.tar.gz

4. Extract Nagios Core and its Plugins
[root@nagios-server nagios~]# tar –xvf nagios-4.0.1.tar.gz
[root@nagios-server nagios~]# tar –xvf nagios-plugins-1.5.tar.gz

5. Configure Nagios Core
[root@nagios-server nagios~]# cd nagios-4.0.1
[root@nagios-server nagios-4.0.1 ]# ./configure --with-command-group=nagcmd
[root@nagios-server nagios-4.0.1 ]# make all
[root@nagios-server nagios-4.0.1 ]# make install
[root@nagios-server nagios-4.0.1 ]# make install-init
[root@nagios-server nagios-4.0.1 ]# make install-commandmode
[root@nagios-server nagios-4.0.1 ]# make install-config

6. Customizing Nagios Configuration
# vi /usr/local/nagios/etc/objects/contacts.cfg


7. Install and Configure Web Interface for Nagios
[root@nagios-server nagios-4.0.1 ]# make install-webconf
[root@nagios-server nagios-4.0.1]# htpasswd -s -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password:
Re-type new password:
Adding password for user nagiosadmin

[root@nagios-server ]# service httpd start

8. Compile and Install Nagios Plugin
[root@nagios-server nagios]# cd /root/nagios
[root@nagios-server nagios]# cd nagios-plugins-1.5
[root@nagios-server nagios]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
[root@nagios-server nagios]# make
[root@nagios-server nagios]# make install

9. Verify Nagios Configuration Files
[root@nagios-server nagios]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

10. Add Nagios and web services to System Startup
[root@nagios-server ]# chkconfig --add nagios
[root@nagios-server ]# chkconfig --level 35 nagios on
[root@nagios-server ]# chkconfig --add httpd
[root@nagios-server ]# chkconfig --level 35 httpd on
[root@nagios-server ]# service nagios start
or
service nagios start
service httpd start
chkconfig nagios on
chkconfig httpd on

11. Login to the Nagios Web Interface

http://localhost/nagios
if your server firwall are already allow https (80) access
System --> Administration --> Firewall
and allow WWW (HTTP) 80/tcp  if you want to remote from other machine to this server.

or
vi /etc/sysconfig/iptables
and add this line into the file
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
12. Host View
The last, If you meet the error:
(13)Permission denied: exec of '/usr/local/nagios/sbin/status.cgi' failed, or access to the page display "Internal Server Error" the same as the picture below


issue this command:
[root@nagios-server ]# chcon -R -t httpd_sys_content_t /usr/local/nagios

And try again, There is no more error.

Add the server for monitoring..!  ;)
To keep our server organize and easy to manage create a directory to store each object such as switch, routers, olt, servers, printer and other devices...

In this example I am going to create a file name server1.cfg with content
define host{
use                             linux-server
host_name                       server1
alias                           server1
address                         192.168.5.200
max_check_attempts              3
check_period                    24x7
notification_interval           30
notification_period             24x7
}

define service {
        use                             generic-service
        host_name                       server1
        service_description             SSH_Compile
        check_command                   check_ssh
        notifications_enabled           1
        }

define service{
        use                             generic-service
        host_name                       server1
        service_description             PING_Compiler
        check_command                   check_ping!100.0,20%!500.0,60%
        }
Restart service Nagios
service nagios restart

On Server1 is Ubuntu server: 12.10
apt-get install nagios-nrpe-server nagios-plugins
Edit file: vi /etc/nagios/nrpe.cfg
Allow Nagios server to check by adding IP of Nagios server.
allowed_hosts=127.0.0.1 192.168.5.210
Restart server NRPE
/etc/init.d/nagios-nrpe-server restart

Access to our Nagios Server:
Now I am going to add the alert server via:  Email, SMS and twit on a Twitter account
Email and SMS to who will be respond the service alert when it is warning or critical.
for Twitter should make your account as a privacy and allow only who involve to twitt if you approve only.

CONFIGURE NOTIFICATION

Add this repository if you didn't have any:
wget http://epel.mirror.net.in/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -Uvh epel-release-6-8.noarch.rpm

1. Email
Install service send mail
yum install sendmail

2. SMS
Register/Buy a SMS service provider
In this example I am going test with: https://www.clickatell.com with a developer account to get the API
3. Twitter
3.1 Create a Twitter account to twitt
3.2 Create any apps for your twitter account and grant the permission read and write
Note your: CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET
3.3 Install tweepy
yum install python-pip
pip-python install tweepy

Create a file name: tweepy-nagios.py with content
#!/usr/bin/env python

import tweepy

CONSUMER_KEY = 'your-consumeer-key-is-here'
CONSUMER_SECRET = 'your-consumeer-key-secret-is-here'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.secure = True
auth_url = auth.get_authorization_url()
print 'Please authorize: ' + auth_url
verifier = raw_input('PIN: ').strip()
auth.get_access_token(verifier)
print "ACCESS_KEY = '%s'" % auth.access_token.key
print "ACCESS_SECRET = '%s'" % auth.access_token.secret

Run this script by:
[root@nagios-server]# ./tweepy-nagios.py
Please authorize: https://api.twitter.com/oauth/authorize?oauth_token=My57lwAlSaqbvx97RAzt0y1pWdvbLSZoA9FTZkRM

(Copy and past the URL above into a browser get the PIN, and Enter the PIN to verified)


PIN: 1234567
ACCESS_KEY = 'here-is-your-access-key'
ACCESS_SECRET = 'here-is-your-access-key-secret'

Create an other file name: NagiosTwitt.py
vi NagiosTwitt.py with content:

#!/usr/bin/env python

import sys
import tweepy

CONSUMER_KEY = 'your-consumeer-key-is-here'
CONSUMER_SECRET = 'your-consumeer-key-secret-is-here'
ACCESS_KEY = 'here-is-your-access-key'
ACCESS_SECRET = 'here-is-your-access-key-secret'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.secure = True
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status(sys.argv[1])

Save and Run the script by: 
[root@nagios-server]#./NagiosTwitt.py "My First Twitt in 2014."

Check on you twitter account, you check got this msg if no error, move this script into the nagios plugin directory.

Edit in file command.cfg to add these 3 Commands (Email,SMS, and Twitter)
vi commands.cfg with content:
###########
## Email  ##
###########
# 'notify-host-by-email' command definition
define command{
        command_name    notify-host-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
        }

# 'notify-service-by-email' command definition
define command{
        command_name    notify-service-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
        }

#########
## SMS ##
#########
# host-notify-by-sms command definition
 define command{
 command_name host-notify-by-sms
 command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$ \nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | wget "http://api.clickatell.com/http/sendmsg?user=vannakkk&password=P@$$w0rd&api_id=12345678&to=85515545966&text='** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ @ $LONGDATETIME$ **'"
 }

# service-notify-by-sms command definition
define command{
 command_name service-notify-by-sms
 command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$ \nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | wget "http://api.clickatell.com/http/sendmsg?user=vannakkk&password=P@$$w0rd&api_id=12345678&to=85515545966&text='** $NOTIFICATIONTYPE$ Service Alert: $SERVICEDESC$ on $HOSTNAME$ State: $SERVICESTATE$ @ $LONGDATETIME$ **'"
 }

#############
##    Twitter   ##
#############
# host-notify-by-twitter command definition
define command{
    command_name    notify-host-by-tw
    command_line    /usr/lib64/nagios/plugins/NagiosTwitt.py "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **"
}
# service-notify-by-twitter command definition
define command{
    command_name    notify-service-by-tw
    command_line    /usr/lib64/nagios/plugins/NagiosTwitt.py "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **"
}

Create a contact
vi contacts.cfg with content:

define contact{
        contact_name             nagiosadmin
        use                             generic-contact
        alias                           Vannak Ken
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    w,u,c,r
        host_notification_options       d,r
        service_notification_commands   notify-service-by-email,service-notify-by-sms,notify-service-by-tw
        host_notification_commands      notify-host-by-email,host-notify-by-sms,notify-host-by-tw           
        email                           ken.vannakk@gmail.com
        }
Restart Service Nagios
service nagios restart

Let make a service or host down and wait to see the Email,SMS and Twitter twitt.

Go and Get a Coffee.... (coffee)

Any Problem, We go the Email, SMS and Twit on Twitter.




Enjoy,   ;)



No comments:

Post a Comment