Listing 1: The Filesystem Activity Monitor
###############################################################################
### T H E F I L E S Y S T E M A C T I V I T Y M O N I T O R ###
### by ###
### Bill Genosa ###
### ###
### This program is to be run hourly by cron. It will monitor changes in ###
### in filesystem size, record the changes in a logfile, and display a ###
### message on the console to alert the System Administrator. If the /tmp ###
### filesystem becomes 90% full, the program will find files in /tmp that ###
### are greater than 3 days old and delete them. Mail will be sent with ###
### a list of the files deleted. ###
###############################################################################
### Our program makes use of functions to perform repetitive tasks.
### Functions must be declared before you can use them in the program.
###-------------------- Declare fsyssize function -----------------------------
fsyssize() { ### Begin fsyssize
function.
### Output of this function will later
### be redirected to a file.
df -t | awk '{ ### Begin awk.
### The df command provides two lines of output for each filesystem it checks.
### The first field or $1 will equal the name of the filesystem for the first
### line of output. The third field or $3 will equal the number of free
### blocks available for locally mounted filesystems.
if ($1 != "total:" && $3 != "):") { ### Begin if statement.
split($1,fsys," "); FSYS=fsys[1] ### Store local filesystem name.
split($3,avail," "); AVAIL=avail[1] ### Store local filesystem size.
} ### End if statement.
### For the remotely mounted filesystem, the third field or $3 will be
### equal to "):". The fourth field or $4 will equal the number of free
### blocks available.
if ($1 != "total:" && $3 == "):")
{ ### Begin
if statement.
split($1,fsys," "); FSYS=fsys[1] ### Store remote filesystem name.
split($4,avail," "); AVAIL=avail[1] ### Store remote filesystem size.
} ### End if statement.
### The first field or $1 will always be equal to "total:" for the second
### line of output df provides. The second field or $2 will be equal to
### the total number of blocks in the filesystem.
if ($1 == "total:") { ### Begin if statement.
split($2,total," "); TOTAL=total[1] ### Store total number of blocks.
PERCENT=AVAIL * 100 / TOTAL ### Calculate the percentage of
### of available blocks left.
MEGLEFT=(AVAIL * .001) / 2 ### Calculate megs available.
### Output the filesystem name, available blocks, total blocks in filesystem,
### the percentage free, and total megabytes available.
printf ("%s %d %d %d %d\n", FSYS, AVAIL, TOTAL, PERCENT, MEGLEFT)
} ### End if statement.
}' ### End awk.
} ### End fsyssize function.
###---------------------- Declare compare function ----------------------------
compare() { ### Begin compare function.
### When compare is called, it expects
### the name of a filesystem for an
### augment.
### Set up some environment variables
PREVIOUS=`grep "$1 " $PREVCHK | awk '{print $4}'` ### Previous % available.
CURRENT=`grep "$1 " $CURRCHK | awk '{print $4}'` ### Current % available.
DIFF=`expr $PREVIOUS - $CURRENT` ### Calculate difference.
MEGAVAIL=`grep "$1 " $CURRCHK | awk '{print $5}'` ### Total MB available.
case $1 in ### Begin case statement.
/|/usr|/usr2|/informix) ### Log any changes in size
if [ $DIFF -ne 0 ] ### for these filesystems.
then
echo "$1 has decreased by $DIFF % at `date`." > /dev/console
echo "$1 has decreased by $DIFF % at `date` leaving $MEGAVAIL MB." >> $LOGFILE
fi
;;
/tmp) ### If tmp decreases to less than
if [ $MEGAVAIL -le 9 ] ### 10%, list all files changed more
then ### than three days ago, mail the
### list to root, and delete them.
echo "$1 has decreased by $DIFF % at `date`." > /dev/console
echo "$1 has decreased by $DIFF % at `date` leaving $MEGAVAIL MB." >> $LOGFILE
find /tmp -ctime +3 -print | mail root
find /tmp -ctime +3 -exec rm -f {} \; > /dev/null
fi
;;
/usr/spool/lp) ### Remotely mounted filesystem.
;; ### To be checked on that system.
*) ### Must be newly mounted filesystem.
echo $1 is not being checked in $0. > /dev/console
;;
esac ### End case statement.
} ### End compare function.
###----------------------- The program starts here ----------------------------
### Set up some environment variables
LOGFILE=/usr/bill/prog/logfile ### File where changes in file system size
### are recorded for observation.
PREVCHK=/usr/bill/temp/prevchk ### Temporary storage file containing the
### filesystem sizes of the previous check.
CURRCHK=/usr/bill/temp/currchk ### Temporary storage file containing the
### filesystem sizes of the current check.
if [ ! -f $PREVCHK ] ### If $PREVCHK doesn't exist, then we must
then ### create it and then exit the program.
fsyssize > $PREVCHK ### The next time the program runs it will
exit ### find $PREVCHK and will create $CURRCHK
else ### instead.
fsyssize > $CURRCHK
fi
while read LINE ### Now we can compare the
do ### filesystem sizes in
FILESYSTEM=`echo $LINE | awk '{print $1}'` ### $PREVCHK and $CURRCHK.
compare $FILESYSTEM
done < $CURRCHK ### After the compare, move
### $CURRCHK to $PREVCHK for the
mv $CURRCHK $PREVCHK ### next time the program runs.
###-------------------------------- End --------------------------------------ok
|