Email replies acknowledge host/service problems

  1. Make sure you can reply to emails from your nagios host. This may require sendmail configuration to get it to process email for a particular interface IP.
  2. Add an alias in sendmail for the user ID that sends your alerts (mine is nagios@nagioshost)

I added this to /etc/aliases:
nagios: |/etc/smrsh/process_email_ack

I placed my script in /usr/local/bin, but added a symlink to /etc/smrsh. I think this is a sendmail security requirement. The script must be executable by the ‘mail’ user ID, and ‘mail’ should be in the ‘nagios’ group or the script won’t be able to write to the command file.

Run the newaliases command to process this change.

  1. Create a script to process emails. Mine is shown in the next reply, but it is far from perfect!

#!/bin/bash

rm -f /tmp/incomingmail.tmp

add filter to discard emails not containing the “***** Nagios *****”

#determine if this is a host or service acknowledgement and process accordingly

#loop through STDIN, and echo out to a temp file
while true; do
read LINE || break
echo $LINE >> /tmp/incomingmail.tmp
done

#Look for the line containing Host: and parse out the hostname
HOST=cat /tmp/incomingmail.tmp |grep "Host: "|awk 'BEGIN { FS = ": " } ; { print $2 }'

#likewise for the Service: line
SERVICE=cat /tmp/incomingmail.tmp|grep "Service: "|awk 'BEGIN { FS = ": " } ; { print $2 }'

#Would like to be able to take the contents of the email reply and parse that into comments
#for now, we use a generic comment
COMMENTS=“Acknowledged via email reply”

#This doesn’t work yet because there are multiple lines that match the “To:” RE
#AUTHOR=cat /tmp/incomingmail.tmp|grep "To: "|awk 'BEGIN { FS = ": " } ; { print $2 }'

AUTHOR=“email recipient”

now=date +%s

commandfile="/usr/local/nagios/var/rw/nagios.cmd"

/usr/bin/printf “%lu] ACKNOWLEDGE_SVC_PROBLEM;$HOST;$SERVICE;1;1;0;$AUTHOR;$COMMENTS\n” $now > $commandfile

Currently this won’t work right for host problems, I need to add logic to distinguish between host and service alerts, and to discard any junk (such as undeliverable message replies). Ultimately I want to be able to parse the reply for comments and include those in the acknowledgement.