#!/bin/sh
# dfmon.sh - monitor/audit daemon
# 20040830 - created by
#
# 20050425 - added -x option to ssh command in order to speed up ssh call
# 20050811 - added SSH fail notice

# spaces at start/end required
TEST=" tserver "
PROD=" pserver "
ALL="$PROD $TEST"
DLIMIT=90
MAILA="user@domain.com"
MAILB="user@domain pager@domain.com"

# check drive space
echo
echo -n "Checking ->"
echo >space.list
for HOST in `echo $ALL`
do
   echo -n " $HOST"
   if [ "$HOST" = "solaris" ]
   then
      ssh -x $HOST df -k >dfmon.$$ 2>/dev/null
      [ $? -ne 0 ] && FAIL="$FAIL $HOST"
   else
      ssh -x $HOST df >dfmon.$$ 2>/dev/null
      [ $? -ne 0 ] && FAIL="$FAIL $HOST"
   fi

   COUNT=`cat dfmon.$$ | wc -l | xargs echo`
   COUNT=`expr $COUNT - 1`
   cat dfmon.$$ | tail -$COUNT >temp.$$ 2>/dev/null
   cat temp.$$ | grep -v cdrom >dfmon.$$
   while read LINE
   do
      PERCENT=`echo $LINE | awk '{ print $5 }'`
      # redo PERCENT var if host is AIX
      [ "$HOST" = "aixsrvr" ] && PERCENT=`echo $LINE | awk '{ print $4 }'`
      PERCENT=`echo $PERCENT | sed 's/%//g'`
      if [ $PERCENT -gt $DLIMIT ]
      then
         PART=`echo $LINE | awk '{ print $6 }'`
	 # redo PART var if host is AIX
         [ "$HOST" = "aixsrvr" ] && PART=`echo $LINE | awk '{ print $7 }'`
	 HCAPS=`echo $HOST | tr [a-z] [A-Z]`
	 echo "${HCAPS}'s Partition $PART is ${PERCENT}% Full" >> space.list
         [ "$EMAIL" != "$MAILB" ] && EMAIL=$MAILA
	 # page if a prod server and > 98 percent full
	 echo $PROD | grep $HOST >/dev/null 2>&1
         [ $? -eq 0 -a $PERCENT -gt 98 ] && EMAIL=$MAILB
      fi
   done <dfmon.$$
   rm dfmon.$$
   rm temp.$$
done

if [ -n "$FAIL" ]
then
	echo >> space.list
	echo "WARNING - Drive Space Check Failed on the Following Servers:" >>space.list
	echo "$FAIL" >>space.list
	echo >> space.list
	echo "Please make sure dfmon account exists and the SSH exchanges happen auto magically." >> space.list
	echo "If server is permanently down, then remove it from the list of servers to monitor." >> space.list
fi

if  [ -s space.list ]
then
   echo
   cat space.list
   {
      echo "subject: Drive Partition Exceeds ${DLIMIT}% Full Limit"
      echo "Partition Information:"
      cat space.list
      echo
      echo "Servers Checked:"
      echo "P:$PROD"
      echo "T:$TEST"
   } | /usr/sbin/sendmail -ONoRecipientAction=add-to -fdfmon -oi "$EMAIL"
fi
rm space.list
echo