Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #############################################################
- # Course: Unix System Administration and Programming #
- # Assignment: assignment 2 #
- # Author: Vicky Perdana #
- #############################################################
- # Unset PATH variable to ensure subsequent commands
- # do not rely on PATH environment variable
- PATH=
- # DEFPATH is a location of directory path for Configuration file
- # TMPPATH is a location of directory path for temporary file
- DEFPATH="/home/v/vsuryape/courses/cosc1133/assignments/part2"
- TMPPATH="/tmp"
- # Setting full path command variables
- # These commands are specific to PATH environment variable
- # Change/add below if your PATH is different
- DEF="/usr/bin"
- DEF1="/usr/ucb"
- DEF2="/usr/local/bin"
- GREP="$DEF/grep"
- AWK="$DEF2/awk"
- TEST="$DEF/test"
- SED="$DEF/sed"
- PS="$DEF/ps"
- ECHO="$DEF/echo"
- FIND="$DEF/find"
- DATE="$DEF/date"
- EXPR="$DEF/expr"
- WC="$DEF/wc"
- TOUCH="$DEF/touch"
- MAILX="$DEF/mailx"
- CAT="$DEF/cat"
- DIRNAME="$DEF/dirname"
- PWD="$DEF/pwd"
- RM="$DEF/rm"
- # Script Configuration file
- CONF=$DEFPATH/script-conf
- CONFTEMP=$TMPPATH/script-conf.temp.$$
- # Temp file for stderr and stdout
- ERRTEMP=$TMPPATH/${0}.1.$$
- # Admin email Address
- ADMIN="vsuryape"
- # Function checkProcess() performs the fundamental part
- # of this script. In overall, this script will check
- # whether all commands specified in Configuration file are
- # found and will try to start it if it's not available
- # However, if problem persists, useful messages will
- # be emailed to Admin
- checkProcess()
- {
- COUNTER=1
- LINENUM=`$WC -l $CONF | $AWK '{print $1}'` # get num of lines
- # Process the Config file
- while $TEST $COUNTER -le $LINENUM ; do
- LINE=`$SED -n "$COUNTER"p $CONF | $AWK '{sub(/^#\t..*/, ""); print}'`
- COMMAND=`$SED -n "$COUNTER"p $CONF | $AWK -F'[\t]' '{print $1}'`
- STARTCOMMAND=`$SED -n "$COUNTER"p $CONF | $AWK -F'[\t]' '{print $2}'`
- FPCOMMAND="`$ECHO $LINE | $AWK '{print $2}'`"
- # Checkpoint 1: Check for blank line and empty string
- # for field 1 and 2
- CHECK1=0
- if $TEST "$LINE" = "" -o "$COMMAND" = "" -o "$STARTCOMMAND" = ""
- then
- $ECHO "Could not process line number $COUNTER"
- COUNTER=`$EXPR $COUNTER + 1`
- CHECK1=1
- continue
- fi
- CONFCOMMAND=`$SED -n "$COUNTER"p $CONF | $AWK -F'[\t]' '{print $3}'`
- # Checkpoint 2: Check whether command is currently running
- # and attempt to run it if it isn't, if problem persists
- # set CHECK2 flag to false (1)
- CHECK2=0
- # Search for command in the list of processes
- $PS -ef | $GREP -v grep | $GREP $COMMAND > /dev/null 2>&1
- FOUND=$?
- if $TEST $FOUND -ne 0 ; then
- $TOUCH $ERRTEMP
- if $TEST ! -r $FPCOMMAND -a ! -s $FPCOMMAND ; then
- CHECK2=2
- else
- $STARTCOMMAND >$ERRTEMP 2>&1
- STATUS=$?
- if $TEST $STATUS -ne 0 ; then
- CHECK2=1
- fi
- fi
- fi
- # Checkpoint 3: Check for the existence of config file
- # if ONLY daemon config file was supplied
- # Store stderr message from find command into temp file
- # if any
- CHECK3=0
- if $TEST "$CONFCOMMAND" != "" ; then
- $FIND $CONFCOMMAND >>$ERRTEMP 2>&1
- if $TEST ! -r $CONFCOMMAND ; then
- CHECK3=1
- fi
- fi
- # Now, perform main operation, first create unique temp file
- # This is performed only if CHECK3=1
- if $TEST $CHECK2 -ne 0 ; then
- $TOUCH $CONFTEMP
- $ECHO "Configuration script file: $CONF" > $CONFTEMP
- $ECHO "\nProcess '$COMMAND' not listed in ps -ef" >> $CONFTEMP
- $ECHO "\nCould not start '$STARTCOMMAND'" >> $CONFTEMP
- if $TEST $CHECK3 -eq 1 ; then
- $ECHO "\nDaemon configuration file could not be found and is not readable" >> $CONFTEMP
- fi
- if $TEST -s "$ERRTEMP" ; then
- $ECHO "\nOutput from STDOUT and Error Messages from STDERR: " >> $CONFTEMP
- $CAT $ERRTEMP >> $CONFTEMP # append STDERR & STDOUT info into Email
- $RM $ERRTEMP > /dev/null 2>&1
- fi
- # Send email to admin using mailx
- $MAILX -s "`$DATE` 3065944 Daemon Monitor" $ADMIN < $CONFTEMP
- fi
- COUNTER=`$EXPR $COUNTER + 1`
- done
- }
- # Main Method
- # Check whether configuration file exist and not empty
- # for convenience, one can provide one argument which is the filename
- # of the config file
- if $TEST "$#" -gt 1 ; then
- $ECHO "Only one argument is allowed to be passed\nAttempt to exit"
- exit 1
- elif $TEST "$1" != "" ; then
- if $TEST -s "$1" ; then
- if $TEST "`$DIRNAME $1`" = "." ; then
- CONF="`$PWD`/$1"
- else
- CONF="$1"
- fi
- else
- $ECHO "Configuration file is not existed and/or empty\nAttempt to exit"
- exit 1
- fi
- elif $TEST ! -s $CONF ; then
- $ECHO "Configuration file is not existed and/or empty\nAttempt to exit"
- exit 1
- fi
- checkProcess
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement