How-to: Nagios email notifications

How-to: Nagios email notifications

This post is based on an article I read at https://www.unixmen.com/send-alerts-nagios-core-using-gmail-yahoo/ and assumes a working Nagios installation with at least a basic configuration as described here.

However, I found that my configuration required some amendments to get it working. I found them by trial and error so decided to record my steps here for future reference.

Nagios utilises an external application to send emails. By default it assumes the use of an application called “mail” but, having failed to get that working I’ve used a tool called sendEmail, as follows:

Start by installing sendEmail in an SSH session on your Nagios host:

wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
tar -zxvf sendEmail-v1.56.tar.gz
sudo cp -a sendEmail-v1.56/sendEmail /usr/local/bin
sudo chmod +x /usr/local/bin/sendEmail

Test to see if there’s output by issuing:

sendEmail

Output that looks like this shows a successful installation:

Configure logging:

sudo touch /var/log/sendEmail
sudo chmod 666 /var/log/sendEmail

The next step is to send a test email. You’ll need a few bits of info:

  • An address to send the test email to
  • An account from which to send the test email – you’ll need the smtp server address, email address, username and password

To send the test, enter the following, replacing the fields marked “<<<>>>” with your information from above:

sendEmail -f <<<from_address>>> -t <<<to_address>>> -u subject -m testing -s <<<smtp_server_address>>> -o tls=no -l /var/log/sendEmail -xu <<<username>>> -xp <<<password>>>

Check the logs and check your email account to see if it was received:

sudo nano /var/log/sendEmail

You should see a line clearly stating a successful transaction. If you didn’t, as I didn’t first time, then it’s worth adjusting the following:

  • the order of the parameters – I found that I couldn’t get it to work until I placed the parameters in the order above (ie -f, -t, -u, -m, -s, -o, -l, -xu, -xp). I did manage to get it working with them in other orders but only by omitting the password and forcing sendEmail to request it interactively – which is fine for a test but no good for the automated calling of sendEmail by Nagios.
  • the characters included in the password – I found that any passwords including & or ! would cause issues when used in the automated command but worked fine when called interactively by omitted the -xp parameter

Next, once your sendEmail installation is working, configure Nagios:

sudo nano /usr/local/nagios/etc/resource.cfg

To open the resource.cfg file in the nano editor. Add the following lines at the bottom:

# added by me to enable email notifications using sendEmail
$USER5$=<<<from_address>>>
$USER7$=<<<smtp_server_address>>>
$USER9$=<<<username>>>
$USER10$=<<<password>>>

Close and save (with Ctrl+X, Y, <ENTER>)

Edit the commands.cfg file by opening with nano:

sudo nano /usr/local/nagios/etc/objects/commands.cfg

and amend the default command executed by Nagios when an event involving a host occurs. The easiest way to do this is to remove the existing line (I’d suggest commenting it out by placing a # at the start – this way you can easily revert to the working configuration by removing the # later) detailing the command to execute.

A command is defined by including a block contained within the curly braces of a “define command” entry:

define command {
 <<<command details go here>>>
}

by default the Nagios command to be executed when sending an email notification for a host event, contains a command_name entry and a command_line entry. Comment out the lengthy command_line entry by starting the line with a # so that

define command {
 command_name notify-host-by-email
 command_line ......existing/default details
}

becomes

define command {
 command_name notify-host-by-email
# command_line ......existing/default details
}

Then add the new command_line by copying the following into a new line below the commented line:

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 $USER5$ -t $CONTACTEMAIL$ -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" -s $USER7$ -o tls=no -l /var/log/sendEmail -xu $USER9$ -xp $USER10$

so that you have:

define command {
 command_name notify-host-by-email
# command_line ......existing/default details
 command_line <<<new entry copy-and-pasted from above>>>
}

Finally, ensure your contacts.cfg contains the address you want to receive alerts at:

sudo nano /usr/local/nagios/etc/objects/contacts.cfg

replacing nagios@localhost with your email address – ie <<<to_address>>> from the test above.

Restart Nagios to force it to run with the new configuration and head to your dashboard to test:

http://ipaddress/nagios

You can then force a notification email to be sent by visiting the “Hosts” page within the “Current Status” menu, opening a host information page by clicking on the name of a host and choosing, from the right-hand panel, “Send custom host notification”.

On the page that opens, fill in a random entry in the comment box and click commit. If all is well you should receive an email. If you don’t then start troubleshooting by accessing the notifications and event logs pages within the reports menu to see whether the system attempted to send a notification, and any error output if it did.

Once a host is successfully sending notifications, you’ll want to update the command related to services in a similar way to that above.

Edit the commands.cfg file and update the “notify-service-by-email” section so that its command_line entry reads as follows:

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 $USER5$ -t $CONTACTEMAIL$ -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$" -s $USER7$ -o tls=no -l /var/log/sendEmail -xu $USER9$ -xp $USER10$

Don’t forget to restart once you’ve saved the config file changes.

One Reply to “How-to: Nagios email notifications”

Leave a Reply

Your email address will not be published. Required fields are marked *