Listing 2: mon_dsk
#!/bin/sh
#
# mon_dsk
#
# Script to check the disk utilization of filesystems listed in the
# accompanying data file, "mon_dsk.dat"
#
#######################################
INSTALL_DIR="/tools" # Installation directory
INFILE="mon_dsk.dat" # Input data file
DFCMD="/bin/df -k" # OS-specific "df" command
PATH=$INSTALL_DIR:$PATH # Search path, beginning with
# $INSTALL_DIR
STATFILE="$INSTALL_DIR/mon_stat" # Status file
#
# This function removes an entry from the $STATFILE.
#
clear_statfile()
{
# If called without an argument, quietly exit
if [ $# = 0 ]; then
exit
# Assume that the argument is a filesystem and remove that line
# from the $STATFILE
else
echo 'g?^\<.*\> '$1'$?d\nwq' | ex $STATFILE > /dev/null 2>&1
fi
}
# If the input data file exists, continue; otherwise 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 WARN SEV CRIT FAT; do
# As long as the listed FS exists, continue; otherwise display
# a warning message
if [ -d "$FS" ]; then
cd "$FS"
# Calculate the disk usage of the FS
CAP=`$DFCMD . | tail -1 | awk '{print $5}' | cut -f1 -d'%'`
echo "FS '$FS' is at $CAP%"
# Start comparing the current usage to the thresholds
if [ "$CAP" -ge "$FAT" ]; then
if grep "FATAL $FS\$" $STATFILE > /dev/null 2>&1; then
break
else
mon_error FATAL "$FS is at $CAP%"
clear_statfile $FS
echo "FATAL $FS" >> $STATFILE
fi
elif [ "$CAP" -ge "$CRIT" ]; then
if grep "CRIT $FS\$" $STATFILE > /dev/null 2>&1; then
break
else
mon_error CRITICAL "$FS is at $CAP%"
clear_statfile $FS
echo "CRIT $FS" >> $STATFILE
fi
elif [ "$CAP" -ge "$SEV" ]; then
if grep "SEVERE $FS\$" $STATFILE > /dev/null 2>&1; then
break
else
mon_error SEVERE "$FS is at $CAP%"
clear_statfile $FS
echo "SEVERE $FS" >> $STATFILE
fi
elif [ "$CAP" -ge "$WARN" ]; then
if grep "WARN $FS\$" $STATFILE > /dev/null 2>&1; then
break
else
mon_error WARN "$FS is at $CAP%"
clear_statfile $FS
echo "WARN $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' for tool '$0'"
fi
# End of File
|