#!/bin/sh

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


for param in $BCNUPARAM
do
	eval $param
done
#set -x

# most of these thresholds are too high but can be overridded in BCNUPARAM
PROCSWAP=${PROCSWAP:=1}		# processes swapped
WAITSWAP=${WAITSWAP:=100}	# runnable but swapped
SWAPFREE=${SWAPFREE:=0}		# free swap space (on some platforms)
PAGESWAP=${PAGESWAP:=1000}	# pages swapped
PROCIDLE=${PROCIDLE:=0}		# processor idle

$VMSTAT 2 2  >$BCNUTMP/vmstat.out
#bcnu_check_err

tail -1 $BCNUTMP/vmstat.out | $AWK "$VMSTATAWK" |
{
	read waitswap swapfree procswap pageswap puser psystem pidle

	if [ $procswap -gt $PROCSWAP ]
	   then
		BCNUMSG="$BCNU -m 'error - more than $PROCSWAP processes swapped ($procswap)' -f $BCNUTMP/vmstat.out -e $BCNU_ERR -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_err_send
		OK=
	fi
	if [ $waitswap -gt $WAITSWAP ]
	   then
		BCNUMSG="$BCNU -m 'warning - more than $WAITSWAP processes waiting ($waitswap)' -f $BCNUTMP/vmstat.out -e $BCNU_WARN -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_warn_send
		OK=
	fi
	if [ $pageswap -gt $PAGESWAP ]
	   then
		BCNUMSG="$BCNU -m 'warning - more than $PAGESWAP pages swapped ($pageswap)' -f $BCNUTMP/vmstat.out -e $BCNU_WARN -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_warn_send
		OK=
	fi
	if [ $swapfree -lt $SWAPFREE ]
	   then
		BCNUMSG="$BCNU -m 'warning - less than $SWAPFREE free swap ($swapfree)' -f $BCNUTMP/vmstat.out -e $BCNU_WARN -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_warn_send
		OK=
	fi
	if [ $pidle -lt $PROCIDLE ]
	   then
		BCNUMSG="$BCNU -m 'warning - less than $PROCIDLE % processor idle ($pidle)' -f $BCNUTMP/vmstat.out -e $BCNU_WARN -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_warn_send
		OK=
	fi
if [ $OK ]
	then
		BCNUMSG="$BCNU -m 'ok - vmstat stable' -f $BCNUTMP/vmstat.out -e $BCNU_OK -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_send
fi
}
