Archive

Archive for the ‘nagios’ Category

Use external smtp server to send nagios alerts

April 18, 2012 7 comments

By default Nagios uses localhost to send out email, this would be a problem if the nagios server’s IP is not designated to send emails. A quick solution is to use utilities that support sending emails through external smtp server, here’s a nice one:

http://caspian.dotconf.net/menu/Software/SendEmail/

Steps (Assuming Nagios version is 3):
1) Download the zip file from http://caspian.dotconf.net/menu/Software/SendEmail/#download, unzip and copy sendEmail.pl to /usr/local/bin

2) Modify commands.cfg so the notify-host-by-email and notify-service-by-email are similar to the followings:
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/local/bin/sendEmail -f nagios_notification@your_domain.com -s smtp.server.ip_or_hostname -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -t $CONTACTEMAIL$
}

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$" | /usr/local/bin/sendEmail -f nagios_notification@your_domain.com -s smtp.server.ip_or_hostname -u "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -t $CONTACTEMAIL$
}

change nagios_notification@your_domain.com and smtp.server.ip_or_hostname to suit your need of course. Consult http://caspian.dotconf.net/menu/Software/SendEmail/#download for more command line options if the smtp server requires authentication, for example.

3) Make sure the new configuration is syntax-error free:
/etc/init.d/nagios checkconfig

4) Restart nagios
/etc/init.d/nagios restart

Notes:
If for some reason nagios doesn’t send out email, please do the followings:
1) enable debugging in /usr/local/bin/sendEmail.pl by setting “logFile” to, say, “/var/log/sendEmail”, change its mode to 666
2) check if nagios contacts contain valid email address. I found out sendEmail doesn’t work with records like
systemadmin: my_real@email.address in /etc/aliases.

Categories: nagios, sendEmail