Check Email Delivery in Nagios

If you are using Nagios, Check MK or OP5 you will most likely want to monitor the status of your mailserver. You can do this by checking the server it self or checking other specific options for Exchange or Domino. This is great but it will not warn you if mails are not getting through to you server or reaching out to the destinations. Do do this you will have to check the full email loop in two directions.



Send a mail to an external host, check the external host so that the mail has arrived

Send a mail from an external host and check your server so that the mail has been revived.

Even though I have been working with Nagios for many years I found this a little complicated and it was just recently I got this working. The documentations is great but I could just not find any good tutorial how to set this up. So maybe this will help you. You may have to use different settings but this worked for me.

Start by downloading the check here.

Put the check command in the correct plug in folder on your Nagios server.

The main command is check_email_delevery. However this check is executing check_smtp_send and check_imap_receive as well. So your mail must be able to handle imap. First a mail is sent, then revived and after this the plugin searches for the mail and deletes it.

Normally I would set up the commands in Nagios with arguments like below.

define command{
    command_name    check_email_delivery
    command_line    $USER1$/check_email_delivery -p '$USER1$/check_smtp_send -H $HOSTADDRESS$ --mailfrom $ARG3$ --mailto $ARG4$ -U $ARG5$ -P $ARG6$ --subject "Nagios %TOKEN1%" -w $ARG1$ -c $ARG2$' -p 
'$USER1$/check_imap_receive -H $HOSTADDRESS$ -U $ARG5$ -P $ARG6$ -s SUBJECT -s "Nagios %TOKEN1%" -w $ARG1$ -c $ARG2$' -w $ARG1$,$ARG1$ -c $ARG2$,$ARG2$

define service{
        use                             generic-service
        service_description             EMAIL DELIVERY
        check_command                   check_email_delivery!5!120!!!!password

However there are so many specific settings so I just created two separate commands. One for in to out and one for out to in.

First I test the command directly

Send mail from external host to my server

./check_email_delivery -p './check_smtp_send -H --mailfrom --mailto -U -P mypassword --header "Subject: Nagios %TOKEN1%" -w 100 -c 200' -p './check_imap_receive
 -H -U -P mypassword --download --download-max 50 -s SUBJECT -s "Nagios %TOKEN1%" -w 50 -c 220' -w 50,50 -c 220,220
EMAIL DELIVERY OK - 8 seconds, 0 delay | delay=0s;50;220;0 elapsed=8s

Send mail from myserver to external host

./check_email_delivery -p './check_smtp_send -H --mailfrom --mailto -U -P mypassword --header "Subject: Nagios %TOKEN1%" -w 100 -c 200' -p './check_imap_receive
 -H -U -P mypassword -s SUBJECT -s "Nagios %TOKEN1%" -w 50 -c 220' -w 50,50 -c 220,220
EMAIL DELIVERY OK - 8 seconds, 0 delay | delay=0s;50;220;0 elapsed=8s

It should look something like above. Note that there is usually one host for imap and one for smtp. I tried to use several free email providers like Yahoo and Gmail but in the end the only one that worked was Gmx.

I use OP5 so in the command section in Monitor I added a two new check commands. I just created one for each direction.

$USER1$/check_email_delivery -p '$USER1$/check_smtp_send -H --mailfrom --mailto -U -P mypassword --header "Subject: Nagios %TOKEN1%" -w 50 -c 120' -p
 '$USER1$/check_imap_receive -H -U -P mypassword -s SUBJECT -s "Nagios %TOKEN1%" -w 100 -c 1000' -w 50,50 -c 120,120

The result looks like below. You will now be alerted if mails can not be sent or revived to your server.



There are detailed instructions on the official plugin page. I had to try several settings before I was done. For example I used the download max option on my server to get the mails. Also configure the check to do checks every 30 minutes or similar. Doing checks to often may affect your mailserver or the Nagios server.