#!/bin/sh

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

#set -x 

OUTFILE=$BCNUTMP/${BCNUAGENT}.out
MSGFILE=$BCNUTMP/${BCNUAGENT}.msg
PSFILE=$BCNUTMP/${BCNUAGENT}.ps.out

rm -f $OUTFILE $MSGFILE $PSFILE 2>/dev/null

OK=1
COMPACT=""
DEFMSGTRANS="compact"
ERR=""
WARN=""
WSPACE=`printf " \t"`

$PS >$PSFILE
bcnu_check_err "critical error running ps command"

for param in $BCNUPARAM
do
bcnu_param
#set -x

process=$p1
MINPROCS=${p2:=1}
MAXPROCS=${p3:=199}
WARNPROCS=${p4:=99}
MSGTRANS=${p5:=$DEFMSGTRANS}
if [ "$MSGTRANS" = compact ]
	then
	COMPACT=1
fi

if grep "$process" $PSFILE >$BCNUTMP/procs.out 2>&1
	then

	pcount=`wc -l < $BCNUTMP/procs.out| sed "s/ //g"`

        if [ $pcount -lt $MINPROCS ]
           then
		MSG="error - $pcount $process processes (should be at least $MINPROCS)"
		if [ $COMPACT ]
		   then
			echo $MSG >>$MSGFILE
			bcnu_check_err "append msg-file"
			ERR=1
		else
                	BCNUMSG="$BCNU -m '$MSG' -f $PSFILE -e $BCNU_ERR -t $BCNUHOSTTYPE $BCNUHOST"
                	bcnu_err_send
		fi
                OK=
        elif [ $pcount -ge $MAXPROCS ]
           then
                MSG="error - $pcount $process processes (should be less than $MAXPROCS)"
		if [ $COMPACT ]
		   then
			echo $MSG >>$MSGFILE
			bcnu_check_err "append msg-file"
			ERR=1
		else
                	BCNUMSG="$BCNU -m '$MSG' -f $PSFILE -e $BCNU_ERR -t $BCNUHOSTTYPE $BCNUHOST"
                	bcnu_err_send
		fi
                OK=
        elif [ $pcount -ge $WARNPROCS -a $pcount -lt $MAXPROCS ]
           then
                MSG="warning - $pcount $process processes (should be less than $WARNPROCS)"
		if [ $COMPACT ]
		   then
			echo $MSG >>$MSGFILE
			bcnu_check_err "append msg-file"
			ERR=1
		else
                	BCNUMSG="$BCNU -m '$MSG' -f $PSFILE -e $BCNU_WARN -t $BCNUHOSTTYPE $BCNUHOST"
                	bcnu_warn_send
		fi
                OK=
	else
		if [ $COMPACT ]
		   then
                	MSG="ok - $pcount $process processes runnning"
			echo $MSG >>$MSGFILE
			bcnu_check_err "append msg-file"
		fi
	fi
else
	MSG="error - process $process not running"
	if [ $COMPACT ]
	   then
		echo $MSG >>$MSGFILE
		bcnu_check_err "append msg-file"
		ERR=1
	else
               	BCNUMSG="$BCNU -m '$MSG' -f $PSFILE -e $BCNU_ERR -t $BCNUHOSTTYPE $BCNUHOST"
               	bcnu_err_send
	fi
	OK=
fi
done

printf "Monitored Processes:\n\n" > $OUTFILE
bcnu_check_err "create output file"

sort_by_severity $MSGFILE '-k 1' >> $OUTFILE
bcnu_check_err "sort messages"

printf "\nFull Process Listing:\n\n" >> $OUTFILE
bcnu_check_err "append output file"

cat $PSFILE >> $OUTFILE
bcnu_check_err "append output file"

if [ $OK ]
	then
	if [ $COMPACT ]
	   then
		BCNUMSG="$BCNU -m 'ok - all processes running' -e $BCNU_OK -t $BCNUHOSTTYPE -f $OUTFILE $BCNUHOST"
		bcnu_send
	else
		BCNUMSG="$BCNU -m 'ok - all processes running' -e $BCNU_OK -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_send
	fi
else
	if [ $COMPACT ]
	   then
		if [ $ERR ]
		   then
			BCNUMSG="$BCNU -m 'error - processes unstable' -e $BCNU_ERR -t $BCNUHOSTTYPE -f $OUTFILE $BCNUHOST"
			bcnu_err_send
		else
			if [ $WARN ]
				then
				BCNUMSG="$BCNU -m 'warning - processes unstable' -e $BCNU_WARN -t $BCNUHOSTTYPE -f $OUTFILE $BCNUHOST"
				bcnu_warn_send
			fi
		fi
	fi
fi
