Listing 3: mon_proc
#!/bin/sh
#
#mon_proc
#
# Script to check the disk utilization of filesystems listed in the
# accompanying data file, "mon_dsk.dat"
#
#######################################
INSTALL_DIR="/tools" # Installation directory
INFILE="mon_proc.dat" # Input data file
PSCMD="/bin/ps -ef" # OS-specific "ps" command
PATH=$INSTALL_DIR:$PATH # Search path, beginning with
# $INSTALL_DIR
STATFILE="$INSTALL_DIR/mon_stat" # Status file
HOST=`uname -n` # Host name, for reporting
#
# 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?^NOPROC '$1'$?d\nwq' | ex $STATFILE > /dev/null 2>&1
echo 'g?^RESTART '$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 PROC path and the severity levels
grep -v "^#" "$INFILE" | while read PROC RESTART; do
# If the process is runnning, clean the statfile
if $PSCMD | grep "$PROC" > /dev/null 2>&1; then
clear_statfile $PROC
echo "process '$PROC' running; cleaning statfile"
# Else, if we've already recording its status, do nothing
elif grep "NOPROC $PROC\$" $STATFILE > /dev/null 2>&1; then
echo "status of process '$PROC' already recorded"
:
# If we've already attempted to restart it, generate an alarm
elif grep "RESTART $PROC\$" $STATFILE > /dev/null 2>&1; then
mon_error CRITICAL "Process '$PROC' is not running on
'$HOST'"
echo "process '$PROC' not restarting; alerting admin"
echo "NOPROC $PROC" >> $STATFILE
# Otherwise, it's the first try: restart it
else
mon_error WARN "Attempting to restart process '$PROC'"
echo "process '$PROC' not running; attempting restart"
echo "RESTART $PROC" >> $STATFILE
$RESTART > /dev/null 2>&1
fi
done
else
echo "Error: No input file '$INFILE' for tool '$0'"
fi
# End of File
|