Home > nagios, sendEmail > Use external smtp server to send nagios alerts

Use external smtp server to send nagios alerts

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
  1. Cham
    August 9, 2012 at 7:43 am

    Thank you, works very well

  2. adarsh
    January 8, 2013 at 6:26 am

    hi,
    m not having /var/log/sendEmail file . do i need to create dis.

    • January 8, 2013 at 7:12 am

      Please see my note #1, you only need to do this if you need to debug.

  3. adarsh
    January 8, 2013 at 8:13 am

    my %conf = (
    ## General
    “programName” => $0, ## The name of this program
    “version” => ‘1.56’, ## The version of this program
    “authorName” => ‘Brandon Zehm’, ## Author’s Name
    “authorEmail” => ‘caspian@dotconf.net’, ## Author’s Email Address
    “timezone” => ‘+0000’, ## We always use +0000 for the time zone
    “hostname” => ‘changeme’, ## Used in printmsg() for all output (is updated later in the script).
    “debug” => 0, ## Default debug level
    “error” => ”, ## Error messages will often be stored here

    ## Logging
    “stdout” => 1,
    “logging” => 0, ## If this is true the printmsg function prints to the log file
    “logfile” => ”, ## If this is specified (form the command line via -l) this file will be used for logging.

    ## Network
    “server” => ‘localhost’, ## Default SMTP server
    “port” => 25, ## Default port
    “bindaddr” => ”, ## Default local bind address
    “alarm” => ”, ## Default timeout for connects and reads, this gets set from $opt{‘timeout’}
    “tls_client” => 0, ## If TLS is supported by the client (us)
    “tls_server” => 0, ## If TLS is supported by the remote SMTP server

    ## Email
    “delimiter” => “—-MIME delimiter for sendEmail-” ## MIME Delimiter
    . rand(1000000), ## Add some randomness to the delimiter
    “Message-ID” => rand(1000000) . “-sendEmail”, ## Message-ID for email header

    );

    This is my /var/log/sendmail.pl file. could you pls tell in detail that where do i need to edit.
    I m new to this.

    Thanks in advance.

    • January 9, 2013 at 5:39 am

      “logging” => 1, ## If this is true the printmsg function prints to the log file
      “logfile” => “/var/log/sendEmail”, ## If this is specified (form the command line via -l) this file will be used for logging.

      Make sure /var/log/sendEmail exists and its mode is set to 666(touch /var/log/sendEmail && chmod 666 /var/log/sendEmail).

  4. prashanthi
    March 14, 2013 at 1:08 pm

    I have done the ablve the steps and able to run sendEmail. But even when i stopped nagios service also sendEmail is not giving any errors and it is able to send the mails.
    Please help me in understanding this.

    • March 16, 2013 at 12:22 pm

      I don’t understand what you meant by “But even when i stopped nagios service also sendEmail is not giving any errors and it is able to send the mails”. Did you mean that after you shut down nagios, you are getting mail from sendEmail? If so, by what sender? Were they real alerts generated by nagios before you shutdown nagios process? How did you shutdown nagios? Can you verify that it’s shutdown successfully? If sendMail is sending out email, what’s wrong with that? Did you enable logging?

  1. No trackbacks yet.

Leave a comment