#!/bin/sh
#
# (C) R. J. Livermore  1995-2000
# (C) Livermore Software Laboratories, Inc. 1995-2000
#
# NOTICE TO USERS OF SOURCE CODE EXAMPLES
#
# FAS PROVIDES THE SOURCE CODE EXAMPLES, "AS IS" WITHOUT WARRANTY OF ANY
# KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE IMPLIED
# WARRANTIES OR MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE
# ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOURCE CODE EXAMPLES
# IS WITH YOU.  SHOULD ANY PART OF THE SOURCE CODE PROVE DEFECTIVE, YOU
# (AND NOT FAS OR AGENT OF FAS) ASSUME THE ENTIRE COST OF ALL NECESSARY
# SERVICING, REPAIR OR CORRECTION.
#
# file:  /usr/local/etc/disk_mon
#
# This script checks the disk utilization of filesystems listed in the 
# /etc/firewall/diskmon.conf file.  When a file system crosses a threshold boundary
# deined in diskmon.conf an appropriate message is delivered to the specified
# recipients.
#
# cron will need to be configured to automate the script.  To do so,
# insert a line at the bottom of /usr/spool/cron/crontabs/root specifying
# the download script and the time and date of download.  The first numer
# in a cron statement is minutes, the second is hours in military time, the 
# fifth is the day of the week, and everything after the day of the week
# is executed.  See example.
# example:
#		43 2 * * 1 /usr/local/etc/diskmon > /dev/null 2>&1
#
INSTALL_DIR="/usr/local/etc"		# Installation directory
INFILE="/etc/firewall/diskmon.conf"	# Input data file
# Set up df command for specific system type
if [ "`uname`" = "AIX" ]; then
   DFCMD="/bin/df -v"                   # df command
elif [ "`uname`" = "HP-UX" ]; then
   DFCMD="/usr/bin/df -v"               # df command 
elif [ "`uname`" = "SunOS" ]; then
   DFCMD="/bin/df -k"                   # df command
elif [ "`uname`" = "Linux" ]; then
   DFCMD="/bin/df -k"                   # df command
else
  echo "Unsupported operating system"
  exit
fi
PATH=$INSTALL_DIR:$PATH			# Search path beginning with
					# $INSTALL_DIR
STATFILE="/home/hermes/diskmon.stats"	# Status file
#===============================================================================
# This function removes an entry from the $STATFILE
#
#===============================================================================
clear_statfile()
{
   # If called without an argument, quietly remove that line
   if [ $# = 0 ]; then
      exit
   # Assume the argument is a filesystem and remove that line
   # from STATFILE
   else
      echo 'g?^\<.*\> '$1'$?d\nwq' | ex $STATFILE > /dev/null 2>&1
   fi
}
#===============================================================================
#
# If the input data file exists, continue; other wise exit with a
# message.
#
#===============================================================================
if [ -f "$INFILE" ]; then
   # Ignore lines in the input file beginning with "#".  From each
   # other line, read the FS path and the severity levels
   grep -v "^#" "$INFILE" | while read FS NOTICE WARN CRIT ALERT; do
      # As long as the listed FS exists, continue; otherwise display 
      # a warning message
      if [ -d "$FS" ]; then
         cd "$FS"
         # Calculate the disk usage for FS
         CAP=`$DFCMD . | tail -l | grep -v Filesystem | awk '{print $5}' | cut -f1 -d'%'`
	 if [ $# -gt 0  -a  "$1" -eq "D" ]; then 
            echo "FS '$FS' is at $CAP%"
         fi 
         # Start comparing the current usage to the thresholds
         if [ "$CAP" -ge "$ALERT" ]; then
	    if grep "ALERT $FS\$" $STATFILE > /dev/null 2>&1; then 
               continue
            else
               message_router ALERT "`hostname` $FS is at $CAP%"
               clear_statfile $FS
               echo "ALERT $FS" >> $STATFILE
            fi
         elif [ "$CAP" -ge "$CRIT" ]; then
            if grep "CRIT $FS\$" $STATFILE > /dev/null 2>&1; then
               continue
            else    
               message_router CRIT "$FS is at $CAP%"
               clear_statfile $FS
               echo "CRIT $FS" >> $STATFILE
            fi   
         elif [ "$CAP" -ge "$WARN" ]; then
            if grep "WARN $FS\$" $STATFILE > /dev/null 2>&1; then
               continue
            else    
               message_router WARN "$FS is at $CAP%"
               clear_statfile $FS
               echo "WARN $FS" >> $STATFILE
            fi  
         elif [ "$CAP" -ge "$NOTICE" ]; then
            if grep "NOTICE $FS\$" $STATFILE > /dev/null 2>&1; then
               continue
            else    
               message_router NOTICE "diskmon: $FS is at $CAP percent capacity."
               clear_statfile $FS
               echo "NOTICE $FS" >> $STATFILE
            fi
         else
            clear_statfile $FS
         fi
      else
         echo "Error: Attempt to monitor non-existant filesystem $FS"
      fi
   done
else
   echo "Error: NO input file '$INFILE' '$0'"
fi
#end of file            
