#!/bin/sh

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

#set -x 

OK=1
ERR=""
WARN=""
OUTFILE=$BCNUTMP/${BCNUAGENT}.out
MSGFILE=$BCNUTMP/${BCNUAGENT}.msg
TMPFILE=$BCNUTMP/${BCNUAGENT}.tmp
DEFMSGTRANS="compact"
WSPACE=`printf " \t"`

bcnu_du()
{
#set -x
	{
		$DF $FS
		if $DU $FS 2>/dev/null
			then :
		else
			echo "(Warning) Errors suppressed"
		fi
	} >$BCNUTMP/du-$FSOUT.now

}
bcnu_du_cmp()
{
	bcnu_du
#set -x
	{
	if [ -f $BCNUTMP/du-$FSOUT.last ]
	    then
		printf "\n%s\n" "Differences in ($FS) since last check"
		diff -C0 $BCNUTMP/du-$FSOUT.now $BCNUTMP/du-$FSOUT.last
	    else
		echo "Sorry nothing to check for ($FS)"
	fi
	} >$BCNUTMP/du-$FSOUT.out
	if [ "$MSGTRANS" = compact ] ; then
		cat $BCNUTMP/du-$FSOUT.out >$BCNUTMP/df-du.out
	else
		cat $BCNUTMP/df.out $BCNUTMP/du-$FSOUT.out >$BCNUTMP/df-du.out
	fi
}

fsexist()
{
	$MOUNT | egrep -e "^$FS[$WSPACE]" -e "[$WSPACE]?$FS[$WSPACE]" >/dev/null
	return $?
}


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

$DF >$BCNUTMP/df.out

for param in $BCNUPARAM
do
  bcnu_param
  FS=$p1
  FSWARN=$p2
  FSSIZE=$p3
  MSGTRANS=${p4:=$DEFMSGTRANS}
  for FS in `eval eval ls -d $FS`; do

#        if fsexist
#	   then	
#		:
#	else
#		continue
#        fi

	if [ "$FS" = "/" ]
	   then
			FSOUT="root"
	   else
			FSOUT=`echo $FS | sed "s/\//-/g"`
	fi

#echo "FS ($FS) FSWARN ($FSWARN) FSSIZE ($FSSIZE) FSOUT ($FSOUT)"

# if the du file doesn't exist for the fs then create it

	if [ ! -f $BCNUTMP/du-$FSOUT.last ]
	then
		bcnu_du
		mv $BCNUTMP/du-$FSOUT.now $BCNUTMP/du-$FSOUT.last
	fi

	FSLINE=`egrep -e "^$FS[$WSPACE]" -e "[$WSPACE]?$FS$" $BCNUTMP/df.out | sed "s/%//"`
	set -- $FSLINE
	FSPCT=$5
#set -x
	if [ -z "$FSLINE" ]
	   then
		if [ "$MSGTRANS" = compact ] ; then
		  printf "%s\n" "error - filesystem $FS is not mounted" >>$MSGFILE
		  ERR=1
		else
		  BCNUMSG="$BCNU -m 'error - filesystem $FS is not mounted' -f $BCNUTMP/df.out -e $BCNU_ERR -t $BCNUHOSTTYPE $BCNUHOST"
		  bcnu_err_send
		fi	
		OK=
	elif [ "$FSPCT" -gt "$FSSIZE" ]
	   then
		bcnu_du_cmp
		if [ "$MSGTRANS" = compact ] ; then
		  printf "%s\n" "error - filesystem $FS >$FSSIZE% full ($FSPCT%)" >>$MSGFILE
		  cat $BCNUTMP/df-du.out >>$TMPFILE
		  ERR=1
		else
		  BCNUMSG="$BCNU -m 'error - filesystem $FS >$FSSIZE% full ($FSPCT%)' -f $BCNUTMP/df-du.out -e $BCNU_ERR -t $BCNUHOSTTYPE $BCNUHOST"
		  bcnu_err_send
		fi	
		OK=
	elif [ "$FSPCT" -gt "$FSWARN" ]
	   then
		if [ "$MSGTRANS" = compact ] ; then
		  printf "%s\n" "warning - filesystem $FS >$FSWARN% full ($FSPCT%)" >>$MSGFILE
		  WARN=1
		else
		  bcnu_du_cmp
		  BCNUMSG="$BCNU -m 'warning - filesystem $FS >$FSWARN% full ($FSPCT%)' -f $BCNUTMP/df-du.out -e $BCNU_WARN -t $BCNUHOSTTYPE $BCNUHOST"
		  bcnu_warn_send
		fi	
		OK=
	else
		if [ "$MSGTRANS" = compact ] ; then
		  printf "%s\n" "ok - filesystem $FS <$FSWARN% full ($FSPCT%)" >>$MSGFILE
		fi	
	fi
  done
done
if [ $OK ]
	then
		printf "%s\n\n" "Monitored Filesystems:" >$OUTFILE
	       	sort_by_severity $MSGFILE >> $OUTFILE
	
		printf "\n%s\n\n" "Filesystem Usage:" >>$OUTFILE
		cat $BCNUTMP/df.out >>$OUTFILE
		BCNUMSG="$BCNU -m 'ok - filesystems stable' -f $OUTFILE -e $BCNU_OK -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_send
elif [ "$MSGTRANS" = compact ]
	then

	printf "%s\n\n" "Monitored Filesystems:" >$OUTFILE
       	sort_by_severity $MSGFILE >> $OUTFILE

	printf "\n%s\n\n" "Filesystem Usage:" >>$OUTFILE
	cat $BCNUTMP/df.out >>$OUTFILE
	cat $TMPFILE >>$OUTFILE

	if [ $ERR ]
	then
		BCNUMSG="$BCNU -m 'error - filesystems unstable' -e $BCNU_ERR -t $BCNUHOSTTYPE -f $OUTFILE $BCNUHOST"
		bcnu_err_send
	else
		if [ $WARN ]
		then
			BCNUMSG="$BCNU -m 'warning - filesystems unstable' -e $BCNU_WARN -t $BCNUHOSTTYPE -f $OUTFILE $BCNUHOST"
			bcnu_warn_send
			sleep 1
		fi
	fi
fi
