Setup External SMTP Server to Send Nagios Notifcation Alerts (CentOS)

By default Nagios uses localhost to send email notification alerts to designated admins. You can of course setup DNS records to white list your Nagios IP/Hostname in order to prevent notifications being marked as spam. But it's a lot easier to setup up Nagios to use an external SMTP server.

SendEmail allows Nagios to do just that.

1. Download the SendEmail tar file from http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz

# wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz

 
2. Extract, and copy sendEmail to /usr/local/bin

# tar -xvzf sendEmail-v1.56.tar.gz
# cp sendEmail-v1.56/sendEmail /usr/local/bin

 

3. SendEmail requires the Net::SSLeay and IO::Socket::SSL perl modules be installed to function, so install these if they're not already:

# yum install perl-Net-SSLeay
# yum install perl-IO-Socket-SSL

 

4. Now modify the notify-host-by-email and notify-service-by-email commands in Nagios' commands.cfg file as follows:

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

 
Replace your_email@your_domain.com and smtp.server.ip_or_hostname:portnumber with the email address and SMTP server address (and SMTP port number if applicable) with your external SMTP server details.

If your server requires authentication add the following lines to each command:

 -xu your_email_username -xp your_password

 
If your server utilizes TLS add:

 -o tls=yes

5. Check out http://caspian.dotconf.net/menu/Software/SendEmail for more command line options.

6. Check your modified Nagios configuration is ok:

# /etc/init.d/nagios checkconfig

7. Restart your Nagios service:

# /etc/init.d/nagios restart

Your done. Nagios will now send alert notifcations via your external SMTP server.

Source: http://ricochen.wordpress.com/2012/04/18/use-external-smtp-server-to-send-nagios-alerts