#!/bin/sh

#a script to parse nagios acknowledgements from epagers. 

CMD='/var/nagios/rw/nagios.cmd'
TMP='/tmp'
TFILE=`/bin/tempfile -d ${TMP}`

while read i
do
	echo $i >> ${TFILE}
done

if egrep -i '^Return-Path: <>' ${TFILE} || egrep -i 'from:.*MAILER.*DAEMON' ${TFILE}
then
	echo "Is a bounce"
	exit 0
fi

MESSAGE=`egrep -i '^ack:' ${TFILE} | cut -d: -f2`
SERVICE=`egrep 'Service: ' ${TFILE} | cut -d: -f2| sed -e 's/^ //'`
HOST=`egrep 'Host: ' ${TFILE} | cut -d: -f2| sed -e 's/^ //'`
FROM=`egrep '^From: ' ${TFILE} | cut -d: -f2| sed -e 's/^ //'`
USER=`egrep 'User:' ${TFILE} | cut -d: -f2| sed -e 's/^ //'`
DATE=`date '+%s'`


if [ -z "$HOST" ]
then
	echo "does not appear to be an ack message"
	exit 0
fi

if [ -z "$MESSAGE" ]
then
	MESSAGE='Acknowledged via epager, no details given'
fi

#2;1;0-- 2= enable/disable sticky ack;  1= send notification ; 0=persistant comment
#[1089382177] ACKNOWLEDGE_SVC_PROBLEM;server1;SSH;2;1;0;Dave Josephsen;heres my text

echo "[${DATE}] ACKNOWLEDGE_SVC_PROBLEM;${HOST};${SERVICE};2;1;0;${USER};${MESSAGE}" >> $CMD
echo "[${DATE}] ADD_SVC_COMMENT;${HOST};${SERVICE};1;${USER};ACKNOWLEDGEMENT: ${MESSAGE}" >> $CMD


rm ${TFILE}

