Wednesday, November 23, 2016

Install NAGIOS With send Email and SMS Alert

Install NAGIOS With send Email and SMS Alert

OS: Ubuntu Server 14.04
IP Address: 123.123.123.55

vim /etc/network/interfaces
With content:
auto eth0
iface eth0 inet static
   address 123.123.123.55
   netmask 255.255.255.0
   gateway 123.123.123.2
   dns-nameservers 123.123.123.2

Log in as root and install

1. Install Required Packages:
apt-get install wget apache2 apache2-utils php5 libapache2-mod-php5 build-essential libgd2-xpm-dev -y

Resstart Service Apache2
service apache2 start

2. Setup User Account for Nagios
useradd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd www-data

3. Installing Nagios Core Service
cd /opt/
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-4.0.8.tar.gz
tar xvzf nagios-4.0.8.tar.gz
cd nagios-4.0.8
./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-config
make install-commandmode


Now create nagios apache2 configuration file.
vim /etc/apache2/sites-available/nagios.conf

ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"

   Options ExecCGI
   AllowOverride None
   Order allow,deny
   Allow from all
   AuthName "Restricted Area"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
   Require valid-user


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


   Options None
   AllowOverride None
   Order allow,deny
   Allow from all
   AuthName "Restricted Area"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
   Require valid-user


Enable Site:
a2ensite nagios


4. Configuring Apache Authentication
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

Enable Module cgi
a2enmod cgi

Resstart Service Apache2
service apache2 restart

5. Installing Nagios Plugins

cd /opt
wget http://nagios-plugins.org/download/nagios-plugins-2.0.3.tar.gz
tar xvzf nagios-plugins-2.0.3.tar.gz
cd nagios-plugins-2.0.3
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install

Verify Configuration and Start Nagios
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

service nagios start


Configure Nagios to auto start on system boot.

ln -s /etc/init.d/nagios /etc/rcS.d/S99nagios

Access Nagios Web Interface

http://123.123.123.55/nagios/

nagiosadmin

6. Configure Nagios
cp /usr/local/nagios/etc/nagios.cfg /usr/local/nagios/etc/nagios.cfg.orig

vim /usr/local/nagios/etc/nagios.cfg
Add this line

cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/contactgroup.cfg
cfg_file=/usr/local/nagios/etc/objects/hostgroup.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg

cfg_dir=/usr/local/nagios/etc/servers
cfg_dir=/usr/local/nagios/etc/switches
cfg_dir=/usr/local/nagios/etc/routers

cd /usr/local/nagios/etc
mkdir servers switches routers

6. Configure send email and sms alert

6.1. vim /usr/local/nagios/etc/objects/templates.cfg
define host{
        name                    host-check-template
        check_command           check-host-alive
        check_period            24x7
        max_check_attempts      5
        notification_interval   60
        notification_period     24x7
        notification_options    d,r
        contact_groups          hosts-template
        register                0
        }

define service{
        name                    service-check-template
        is_volatile             0
        check_period            24x7
        max_check_attempts      5
        normal_check_interval   5
        retry_check_interval    5
        notification_interval   60
        notification_period     24x7
        notification_options    w,u,c,r
        contact_groups          hosts-template
        register                0
        }

6.2. vim /usr/local/nagios/etc/objects/contactgroup.cfg
define contactgroup{
        contactgroup_name       hosts-template
        alias                   Hosts Template
        }

6.3. vim /usr/local/nagios/etc/objects/hostgroup.cfg
define hostgroup{
        hostgroup_name  Hosts-Template
        alias           Host Template
        }

6.4. vim /usr/local/nagios/etc/routers/router-gateway.cfg
define host{
        use             host-check-template
        host_name       router-gateway
        alias           Router Gateway
        address         123.123.123.101
        hostgroups      Hosts-Template
        }

define service{
        use                             service-check-template
        host_name                       router-gateway
        service_description             PING Router Gateway
        check_command                   check_ping!100.0,20%!500.0,60%
        }




6.5. vim /usr/local/nagios/etc/objects/commands.cfg

# 'notify-host-by-sms' command definition
define command{
        command_name    notify-host-by-sms
        command_line    /usr/local/nagios/libexec/send_sms.sh $CONTACTPAGER$ "Host: $HOSTNAME$ $HOSTSTATE$ Address: $HOSTADDRESS$ Date $LONGDATETIME$"
     
        }
# 'notify-service-by-sms' command definition
define command{
        command_name    notify-service-by-sms
        command_line    /usr/local/nagios/libexec/send_sms.sh $CONTACTPAGER$ "Service: $SERVICEDESC$ $HOSTALIAS$ $HOSTADDRESS$ $SERVICESTATE$ $LONGDATETIME$"
     
        }

# '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" | /usr/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" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
        }

6.6 vim /usr/local/nagios/libexec/send_sms.sh
Create a free 10 SMS or buy it from: http://www.msg2send.com
and use the following script with its API

#!/bin/bash
DEST=$1
MSG=$2
log=/var/log/sms.log
EMAIL_TO=kenvannak@yahoo.com
#Generate unique ID for each SMS
tagid=`echo $(($(date +%s%N)/1000000))`

function sendmail
{
        message="$1"
        echo "CHECK SERVER NAGIOS!!. $message" | mail -s "PROVIDER SMS FAIL" $EMAIL_TO
}
echo "" >> $log
echo "###################################################" >> $log
echo $(date +"%T-%m-%d-%Y") >> $log
echo "Messag to send: $MSG" >> $log
NUMBER_CARAT=`echo $MSG | wc -c`
echo  "Number caracteres: $NUMBER_CARAT" >> $log

if [ $NUMBER_CARAT -gt 153 ];
then
        NEW_MSG=${MSG:0:138}
        MSG="SMS SHORTED! $NEW_MSG"
        echo "New Mesagge: $MSG" >> $log
        NUMBER_CARAT=`echo $MSG | wc -c`
        echo  "Number caracteresi new mesagge: $NUMBER_CARAT" >> $log
fi
echo "" >> $log
RESULT=`curl --data "id=sms-user&pw=SMSPASSWORD&dnr=%2B$DEST&senderid=%2B85515545966&msg= $MSG&tagid=$tagid"  http://api.msg2send.com/api`
echo "Resutl for curl command: $RESULT" >> $log
echo "This SMS has the TAGID: $tagid" >> $log
#Before to check the status code we sleep some seconds
sleep 17
STATUS=`curl --data "id=sms-user&pw=SMSPASSWORD&dnr=%2B$DEST&tagid=$tagid" http://api.msg2send.com/apim`
echo "Resutl for SMS Delivery: $STATUS" >> $log



Make the Script Executable
chmod +x /usr/local/nagios/libexec/send_sms.sh

touch /var/log/sms.log
chown nagios:nagios /var/log/sms.log


6.7. vim /usr/local/nagios/etc/objects/contacts.cfg
define contact {
        contact_name                    vannakk-sms
        alias                           vannakk SMS
        contactgroups                   hosts-template
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    w,c,r
        host_notification_options       d,r
        service_notification_commands   notify-service-by-sms
        host_notification_commands      notify-host-by-sms
        pager                           85515545966
        }

define contact {
        contact_name                    vannakk-email
        alias                           vannakk EMAIL
        contactgroups                   hosts-template
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    w,c,r
        host_notification_options       d,r
        service_notification_commands   notify-service-by-email
        host_notification_commands      notify-host-by-email
        email                           ken.vannakk@gmail.com
        }

Install Mail Command
apt-get install postfix heirloom-mailx


VERIFY CONFIG
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

RESTART NAGIOS
/etc/init.d/nagios restart

CHECK NAGIOS LOG
tailf /usr/local/nagios/var/nagios.log

CHECK MAIL LOG
tailf /var/log/mail.log

CHECK SMS LOG
tailf /var/log/sms.log

After Add host:

Test Send Mail Alert


Test SMS Alert

No comments:

Post a Comment