Thursday, July 15, 2010

Nagios - Use an external smtp server to send notifications



One of the most important feature of Nagios is the notification system; by default Nagios will use the local mail daemon (mail) to send all the notifications.
Maybe this kind of system was working fine in past, but today, with this huge amount of spam circulating all over the world, this kind of emails from a not verified source can be filtered by the same company antispam system.
In order to avoid this issue I will explain how to configure Nagios using an external smtp server also with smtp authentication.


Since the Nagios notification event is still a triggered command, we have to download an alternative command line email client.


I suggest the powerful "sendemail" command, available at the following link:

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

Once the download is finished unpack sendemail:

[root@localhost] tar -xvf sendEmail-v1.56.tar.gz

Then copy the binary in the bin folder

[root@localhost] cp /root/sendEmail-v1.56/sendEmail /usr/local/bin/

In order to troubleshoot any eventual problem, please use the following workaround to create and give the necessary authorization to write in the sendemail log file:

[root@localhost] touch /var/log/sendEmail
[root@localhost] chmod 666 /var/log/sendEmail


(remember, by default, Nagios does not start with full administrative rights, so command sent by Nagios too)
Now we need to set some variables that we need to recall later depending on your particular environment and smtp configuration:

So edit the resource.cfg file located in:
/usr/local/nagios/etc/resource.cfg
Add the following lines with your particular smtp configuration:

$USER5$=email@address.tld

$USER7$=smtp.server.tld

#If you don't use smtp authentication skip this variables and use the version B) of commands.cfg

$USER9$=authsmtpusername
$USER10$=authsmtpassword


Save and exit (<- This commands change depending from your editor, sadly I've been in too many disputes aboute which linux editor is better, so don't expect any advice from me) After this, we are gonna change the commands.cfg file located in:
/usr/local/nagios/etc/objects/commands.cfg

First perform a backup of the config file:
cd /usr/local/nagios/etc/objects/
cp commands.cfg commands.cfg.old


Edito the commands.cfg file and you will find two commands:
'notify-host-by-email' and 'notify-service-by-email'

A) SMTP AUTHENTICATION COMMANDS
Now, if you use SMTP authentication replace the two commands with the following:

# '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" | /usr/local/bin/sendEmail -s $USER7$ -xu $USER9$ -xp $USER10$ -t $CONTACTEMAIL$ -f $USER5$ -l /var/log/sendEmail -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -m "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" }

# '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$" | /usr/local/bin/sendEmail -s $USER7$ -xu $USER9$ -xp $USER10$ -t $CONTACTEMAIL$ -f $USER5$ -l /var/log/sendEmail -u "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -m "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" }


B) NO SMTP-AUTHENTICATION
This is pretty much the same but without passing the authentication triggers to sendemail:

# '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" | /usr/local/bin/sendEmail -s $USER7$ -t $CONTACTEMAIL$ -f $USER5$ -l /var/log/sendEmail -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -m "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" }

# '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$" | /usr/local/bin/sendEmail -s $USER7$ -t $CONTACTEMAIL$ -f $USER5$ -l /var/log/sendEmail -u "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -m "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" }

Now the configuration is finished. Anyway, before applying the new configuration, run the following command to check for any error or mistake:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

If there are no errors, restart the nagios service:

service nagios restart

To troubleshoot the sendemail command run the following to check in the log file:

tail -f /var/log/sendEmail


Hope this post will be useful to you. If you have any troube or you want to suggest me a better system do not hesitate to write a line.

Regards
Daniel

16 comments:

  1. Great post - I wanted nagios posting to a google apps address, so I needed to add -o tls=yes and it worked a treat!

    ReplyDelete
  2. Hi Daniele,

    I have configured everything as mentioned, now i'm getting the following error, could please help me out.

    ERROR => Received: 530 5.7.0 Must issue a STARTTLS command first.

    ReplyDelete
  3. To issue STARTTLS add the following option to sendEmail command:

    -o tls=yes

    ReplyDelete
  4. Thank you so much! I just couldn't get smtp email working in Nagios, and this worked perfectly! :-)

    ReplyDelete
  5. Thank you and I would recommend your post to my friends ..

    ReplyDelete
  6. Hi all

    > tail -f /var/log/sendEmail

    Oct 21 12:33:57 localhost sendEmail[17463]: NOTICE => Authentication not supported by the remote SMTP server!
    Oct 21 12:33:57 localhost sendEmail[17463]: ERROR => Received: 530 5.7.0 Must issue a STARTTLS command first. a10sm3904486paw.17


    i getting error;
    if this parameter is requiter then where put this(File name,command) -o tls=yes

    ReplyDelete
  7. Its not working for me.

    Error: Unexpected start of object definition in file '/usr/local/nagios/etc/objects/commands.cfg' on line 33. Make sure you close preceding objects before starting a new one.
    Error processing object config files!

    Can u PLEASE help me?

    ReplyDelete
    Replies
    1. i solved it.

      ty anyway. great post!

      Delete
  8. Hi, I am trying to configure it with gmail. But I am not getting anything in logfile. When I run this from command line

    #ll |sendEmail -f testuser@gmail.com -u TEST -m Test -s smtp.googlemail.com:465 -xu testuser@gmail.com -xp Testing$#@! -t myuser@rediffmail.com -o tls=yes

    I am getting this error.
    Apr 06 02:43:00 localhost sendEmail[29533]: ERROR => Timeout while reading from smtp.googlemail.com:465 There was no response after 60 seconds.

    When I use #telnet smtp.googlemail.com 465, it works. Please guide me what to do

    ReplyDelete
  9. hi, i'm trying to configure it with my work mail. the command work when i issue it from the command line, it doesn't when i set it up in Centreon/nagios. i've done all the above config, but still doesen't work.

    thanks

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Hi Erjol,

      I had some problems with the Centreon/Nagios too. But I sorted it out.

      Im not sure if its too late. But if u still have the issue, could you post ur error message ?

      Delete
  10. Hi Daniele Cuocci

    Thanks for a great blog :)

    sendEmail stopped working after a reboot, how do I get it up and running again? I can't see sendEmail service in server --status-all :( Hope you can help

    ReplyDelete
  11. Nice post with great details. I really appreciate your work Thanks for sharing.

    desktop notification software

    ReplyDelete
  12. Hi, Please help... I've setup an instance of Fully Automated Nagios (FAN) and followed your guide. I can send an email from the CLI using sendEmail but Nagios isn't able to send any notifications. I have setup the configurations using the Centrion web interface. It's as if Nagios is not executing what it needs to when an alarm is triggered because I'm getting no sendEmail log activity. I need to troubleshoot further but don't know where to start. I'm hoping someone else encountered the issue with FAN... Can anyone point me in the right direction?

    ReplyDelete