#!/bin/sh

. /usr/local/bcnu/etc/bcnuenv
. $BCNUHOME/agent/agent_head

OK=1
DEFPATTERNS="$BCNUHOME/etc/logpatterns"

eval exparam=\"$BCNUPARAM\"

for param in $exparam
do
	bcnu_param
set -x
	FILE=$p1
	PATTERNS=${p2:=$DEFPATTERNS}

	if [ ! -r "$FILE" ] 
	then
		echo "$BCNUAGENT $FILE not found"
		continue
	fi

	$BCNUMESS $FILE >$BCNUTMP/traps.$$
	
	BASEFILE=`basename $FILE`
	ERRFILE="$BCNUTMP/$BASEFILE.error"
	WARNFILE="$BCNUTMP/$BASEFILE.warning"

	touch $BCNUHOME/flags/$BASEFILE.pos	# to make sure it doesn't get deleted

	sed -ne "\+^\[$FILE - error+,\+^\[+p" $PATTERNS | sed -ne "/^[^\[#]/p" >$ERRFILE
	sed -ne "\+^\[$FILE - warning+,\+^\[+p" $PATTERNS | sed -ne "/^[^\[#]/p" >$WARNFILE

	if [ -s "$WARNFILE" ]
	then
	 if $GREP -i -f $WARNFILE $BCNUTMP/traps.$$ >$BCNUTMP/traps.out 2>/dev/null
	   then
		cat $BCNUTMP/traps.out |
		{
		while read indate intime inhost inoid indesc
		do
			BCNUMSG="$BCNU -h $inhost -m 'warning - snmp trap received' -f $BCNUTMP/traps.out -e $BCNU_WARN $BCNUHOST"
			bcnu_warn_send
		done
		}
	 fi
	fi
	if [ -s "$ERRFILE" ]
	then
	 if $GREP -i -f $ERRFILE $BCNUTMP/traps.$$ >$BCNUTMP/traps.out 2>/dev/null
	   then
		cat $BCNUTMP/traps.out |
		{
		while read indate intime inhost inoid indesc
		do
			BCNUMSG="$BCNU -h $inhost -m 'error - snmp trap received' -f $BCNUTMP/traps.out -e $BCNU_ERR $BCNUHOST"
			bcnu_err_send
		done
		}
	 fi
	fi

done

#rm $BCNUTMP/traps.$$
