#!/bin/sh

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

param=$BCNUPARAM
bcnu_param
DAYS=${p1:=1}
FIVE=${p2}00
FIFTEEN=${p3}00

#set -x

$UPTIME  >$BCNUTMP/uptime.out
bcnu_check_err "critical error running uptime command"

sed "s/[.,]//g" $BCNUTMP/uptime.out | awk '{ printf("%d %d %d %d\n",$3,$10,$11,$12) }' |
{
	read days one five fifteen

	# check for no days in uptime
	if grep -v "day" $BCNUTMP/uptime.out >/dev/null
	   then
		days=0
	fi

	if [ $fifteen -ge $FIFTEEN ]
	   then
		$PS >>$BCNUTMP/uptime.out
		BCNUMSG="$BCNU -m 'error - load average 15 minutes ($fifteen hundredths)' -f $BCNUTMP/uptime.out -e $BCNU_ERR -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_err_send
		OK=
	elif [ $five -ge $FIVE ]
	   then
		$PS >>$BCNUTMP/uptime.out
		BCNUMSG="$BCNU -m 'warning - load average 5 minutes ($five hundredths)' -f $BCNUTMP/uptime.out -e $BCNU_WARN -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_warn_send
		OK=
	elif [ $days -lt $DAYS ]
	   then
		BCNUMSG="$BCNU -m 'warning - system only up $days days' -f $BCNUTMP/uptime.out -e $BCNU_WARN -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_warn_send
		OK=
	fi

if [ $OK ]
	then
		BCNUMSG="$BCNU -m 'ok - uptime stable' -f $BCNUTMP/uptime.out -e $BCNU_OK -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_send
fi
}
