Wednesday, 23 September 2015

Nagios Notification Alert Mail



[root@linux ~]#  wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz

[root@linux ~]# tar -zxvf  sendEmail-v1.56.tar.gz
 
[root@linux ~]# cp -a sendEmail-v1.XX/sendEmail /usr/local/bin

[root@linux ~]# chmod +x /usr/local/bin/sendEmail
  
[root@linux ~]# /usr/local/bin/

[root@linux ~]# cd /usr/local/nagios/libexec



[root@linux ~]# vi alert_mail_host

#!/bin/bash
# ex9.sh
subject=$1" Host Alert: "$2" is "$3
echo $subject
body="<html><body><table width="100%" border=0 cellpadding=0 cellspacing=0><tr ><th colspan="2" >Nagios Infomatic(M)Pvt Ltd</th></tr><br/>"
case $3 in
"OK") body=$body"<tr bgcolor=#00B050><font color=#ffffff>";;
"DOWN") body=$body"<tr bgcolor=#EC1B23><font color=#ffffff>";;
"UP") body=$body"<tr bgcolor=#00B050><font color=#ffffff>";;
"WARNING") body=$body"<tr bgcolor=#FFFF00><font color=#000000>";;
"CRITICAL") body=$body"<tr bgcolor=#EC1B23><font color=#ffffff>";;
*) body=$body"<tr bgcolor=#FFFF00><font color=#000000>";;
esac
body=$body"<b><td style=width:150px>Notification Type: </td><td>$1</td></b></font></tr>"
body=$body"<tr bgcolor=#eeeeee><td>Host: </td><td>$2</td></tr>"
body=$body"<tr bgcolor=#fefefe><td>Address: </td><td>$4</td></tr>"
body=$body"<tr bgcolor=#eeeeee><td>Date/Time: </td><td>$6</td></tr>"
body=$body"<tr bgcolor=#fefefe><td>State: </td><td>$3</td></tr>"
body=$body"<tr bgcolor=#eeeeee><td>Info: </td><td><font color=#0000CC>$5</font></td></tr>"
if [ $1 = 'ACKNOWLEDGEMENT' ]
then
body=$body"<tr bgcolor=#eeeeee><td>Comment: </td><td><font color=#0000CC>$8</font></td></tr>"
fi
body=$body"</table></body></html>"
/usr/local/bin/sendEmail -f nagios@nagios.com -t "$7" -s 172.16.2.111 -u "$subject" -m "$body" -xu nagios@nagios.com -xp Password1 -o message-content-type=html tls=no


[root@linux ~]# vi alert_mail_service

#!/bin/bash
# ex9.sh
subject=$1" Service Alert: "$2"-"$8" is "$3
echo $subject
body="<html><body><table width="100%" border=0 cellpadding=5 cellspacing=0><tr><th colspan="2" >nagios Infomatic(M)Pvt Ltd</th></tr><br/>"
case $3 in
"OK") body=$body"<tr bgcolor=#00B050><font color=#ffffff>";;
"DOWN") body=$body"<tr bgcolor=#EC1B23><font color=#ffffff>";;
"UP") body=$body"<tr bgcolor=#00B050><font color=#ffffff>";;
"WARNING") body=$body"<tr bgcolor=#FFFF00><font color=#000000>";;
"CRITICAL") body=$body"<tr bgcolor=#EC1B23><font color=#ffffff>";;
*) body=$body"<tr bgcolor=#FFFF00><font color=#000000>";;
esac
body=$body"<td style=width:150px>Notification Type: </td><b><td>$1</td></b></font></tr>"
body=$body"<tr bgcolor=#fefefe><td>Service: </td><td>$8</td></tr>"
body=$body"<tr bgcolor=#eeeeee><td>Host: </td><td>$2</td></tr>"
body=$body"<tr bgcolor=#fefefe><td>Address: </td><td>$4</td></tr>"
body=$body"<tr bgcolor=#eeeeee><td>Date/Time: </td><td>$6</td></tr>"
body=$body"<tr bgcolor=#fefefe><td>State: </td><td>$3</td></tr>"
body=$body"<tr bgcolor=#eeeeee><td>Info: </td><td><font color=#0000CC>$5</font></td></tr>"
if [ $1 = 'ACKNOWLEDGEMENT' ]
then
body=$body"<tr bgcolor=#eeeeee><td>Comment: </td><td><font color=#0000CC>$9</font></td></tr>"
fi
body=$body"</table></body></html>"
/usr/local/bin/sendEmail -f nagios@nagios.com -t "$7" -s 192.168.2.111 -u "$subject" -m "$body" -xu nagios@nagios.com -xp Password1 -o message-content-type=html tls=no



[root@linux ~]# cd /usr/local/nagiosql

[root@linux ~]# vi contacts.cfg

Enter Email ID

[root@linux ~]# vi commands.cfg


define command {
command_name notify-host-by-email
command_line /usr/local/nagios/libexec/alert_mail_host "$NOTIFICATIONTYPE$" "$HOSTNAME$" "$HOSTSTATE$" "$HOSTADDRESS$" "$HOSTOUTPUT$" "$LONGDATETIME$" "$CONTACTEMAIL$" "$NOTIFICATIONCOMMENT$"
register 1
}

define command {
command_name notify-service-by-email
command_line /usr/local/nagios/libexec/alert_mail_service "$NOTIFICATIONTYPE$" "$HOSTNAME$" "$HOSTSTATE$" "$HOSTADDRESS$" "$HOSTOUTPUT$" "$LONGDATETIME$" "$CONTACTEMAIL$" "$NOTIFICATIONCOMMENT$"
register 1





In nagiosql

Alerting - contact data - Enter Email Id.

commands - definitions – notify-host-by-email

copy below path

/usr/local/nagios/libexec/alert_mail_host "$NOTIFICATIONTYPE$" "$HOSTNAME$" "$HOSTSTATE$" "$HOSTADDRESS$" "$HOSTOUTPUT$" "$LONGDATETIME$" "$CONTACTEMAIL$" "$NOTIFICATIONCOMMENT$"

commands - definitions – notify-service-by-email

copy below path

/usr/local/nagios/libexec/alert_mail_service "$NOTIFICATIONTYPE$" "$HOSTNAME$" "$HOSTSTATE$" "$HOSTADDRESS$" "$HOSTOUTPUT$" "$LONGDATETIME$" "$CONTACTEMAIL$" "$NOTIFICATIONCOMMENT$"


[root@linux ~]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

[root@linux ~]# service nagios restart

[root@linux ~]# tail -f /usr/local/nagios/var/nagios.log