Nagios | Gmail | SMTP Notification
First create a gmail account to be used by your Nagios server for email notification using its smtp service.
Use this Nagios plugin. Please read the installation instruction.
The send_gmail.py contains the variable that will be configured to match your system. The file installation_notes.txt contains the instructions on how to use this nagios plugin.
The following steps are the actions that were taken to enable the service. In this example I will be using CentOS. Please substitute the correct values for your operating system.
Modify send_gmail nagios plugin
Extract nagios send_gmail plugin to plugin directory then modify the file.
[root@omserver5 ~]# cd /usr/lib/nagios/plugins/
[root@omserver5 plugins]# vi send_gmail.py
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1) #0 for quiet or 1 for verbosity
server.ehlo('nagios-monitoring@gmail.com')
server.starttls()
server.ehlo('nagios-monitoring@gmail.com') # say hello again
server.login('nagios-monitoring@gmail.com', 'nagios-monitoring-password')
server.sendmail('nagios-monitoring@gmail.com', Addresses, "Subject: " + subject + '\nTo:' + address + '\n\n' + body)
Please modify the parameters that suite your needs.
in command.cfg first follow what is written in the instruction but if things dont work then this is what I did. I commented the original lines and add the lines from di instruction with a slight modification.
# OLD
# '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$
# }
# OLD
# '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$" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
# }
#notify host via gmail using python script. \\nnn will insert return, use for body of mssg
define command{
command_name notify-host-by-email
command_line /usr/bin/python /usr/lib/nagios/plugins/send_gmail.py -a CONTACTEMAIL$ -b "***** S.O.G.C. Service Monitoring *****\\nnn\\nnnNotification Type: $NOTIFICATIONTYPE$\\nnnHost: $HOSTNAME$\\nnnState: $HOSTSTATE$\\nnnAddress: $HOSTADDRESS$\\nnnInfo: $HOSTOUTPUT$\\nnnDate/Time: $LONGDATETIME$" -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **"
}
#notify service via gmail using python script. \\nnn will insert return, use for body of mssg
define command{
command_name notify-service-by-email
command_line /usr/bin/python /usr/lib/nagios/plugins/send_gmail.py -a CONTACTEMAIL$ -b "***** S.O.G.C. Service Monitoring *****\\nnn\\nnnNotification Type: $NOTIFICATIONTYPE$\\nnnService: $SERVICEDESC$\\nnnHost: $HOSTALIAS$\\nnnAddress: $HOSTADDRESS$\\nnnState: $SERVICESTATE$\\nnnDate/Time: $LONGDATETIME$\\nnnAdditional Info: $SERVICEOUTPUT$" -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **"
}
Then test the plugin. If there are errors regarding python then supply the correct path in send_gmail.py and all will be fine.
That's it

