#!/bin/bash 
# script that checks for new errpt
#
excludeFile=/usr/local/ct/nagios/etc/check_errpt.ini
errptlast=/tmp/errptlast
errpttmp=/tmp/errpttmp
admin=root
#
# check if first run
errpt | egrep -v '^IDENTIFIER' > $errpttmp
if [ ! -f $errptlast ] ; then
	# first run generate a timestamp 
	date +%m%d%H%M%y > $errptlast
fi
excludelist=""
if [ -f $excludeFile ]
then
   rawExcludeList=`sed -e 's/#.*//' -e 's/[ ^I]*$//' -e '/^$/ d' $excludeFile`
   for curID in $rawExcludeList
   do
     excludelist="$curID,$excludelist" 
   done
   excludelist=`echo $excludelist | sed 's/.$//g'`
fi
if [ "$excludelist"x != x ]
then
  excludelist=" -k $excludelist "
fi
lastts=`cat $errptlast`
count=`errpt -s "$lastts" $excludelist -T PERM, UNKN, PERF | egrep -v '^IDENTIFIER' | wc -l`
exit1=$?
count=`echo $count`
countcrit=`errpt -s "$lastts" $excludelist -T PERM, UNKN, PERF | egrep -v '^IDENTIFIER' | sed -n 's!.* \(P\) .*!\1!p' | wc -l`
exit2=$?
countcrit=`echo $countcrit`
if [ "$count" -ge 1 ]  ; then
  # determine level
  errpt -A -D -s "$lastts" $excludelist -T PERM, UNKN, PERF | mail -s "$count new error reports of which $countcrit critical  on `hostname` since $lastts" $admin 	
		date +%m%d%H%M%y > $errptlast
		if [ "$countcrit" -ge 1 ] ; then
			echo "CRITICAL $count new error reports of which $countcrit critical generated since $lasttsi | errors=$count"
			exit 2
		else
			echo "WARN $count new error reports generated since $lastts | errors=$count"
			exit 1
		fi
fi
if [ "$count" == 0 ] && [ "$countcrit" == 0 ] && [ "$exit1" == 0 ] && [ "$exit2" == 0 ] ; then
	echo "OK 0 new Error Reports since $lastts | errors=$count"
	exit 0
fi
echo "CRITICAL Errpt Check failed"
exit 3
