SHARE
TWEET

Untitled

a guest Jun 16th, 2019 136 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. #############################################################
  3. # Course: Unix System Administration and Programming        #
  4. # Assignment: assignment 2                                  #
  5. # Author: Vicky Perdana                                     #
  6. #############################################################
  7. # Unset PATH variable to ensure subsequent commands
  8. # do not rely on PATH environment variable
  9. PATH=
  10.  
  11. # DEFPATH is a location of directory path for Configuration file
  12. # TMPPATH is a location of directory path for temporary file
  13. DEFPATH="/home/v/vsuryape/courses/cosc1133/assignments/part2"
  14. TMPPATH="/tmp"
  15.  
  16. # Setting full path command variables
  17. # These commands are specific to PATH environment variable
  18. # Change/add below if your PATH is different
  19. DEF="/usr/bin"
  20. DEF1="/usr/ucb"
  21. DEF2="/usr/local/bin"
  22. GREP="$DEF/grep"
  23. AWK="$DEF2/awk"
  24. TEST="$DEF/test"
  25. SED="$DEF/sed"
  26. PS="$DEF/ps"
  27. ECHO="$DEF/echo"
  28. FIND="$DEF/find"
  29. DATE="$DEF/date"
  30. EXPR="$DEF/expr"
  31. WC="$DEF/wc"
  32. TOUCH="$DEF/touch"
  33. MAILX="$DEF/mailx"
  34. CAT="$DEF/cat"
  35. DIRNAME="$DEF/dirname"
  36. PWD="$DEF/pwd"
  37. RM="$DEF/rm"
  38.  
  39. # Script Configuration file
  40. CONF=$DEFPATH/script-conf
  41. CONFTEMP=$TMPPATH/script-conf.temp.$$
  42.  
  43. # Temp file for stderr and stdout
  44. ERRTEMP=$TMPPATH/${0}.1.$$
  45.  
  46. # Admin email Address
  47. ADMIN="vsuryape"
  48.  
  49. # Function checkProcess() performs the fundamental part
  50. # of this script. In overall, this script will check
  51. # whether all commands specified in Configuration file are
  52. # found and will try to start it if it's not available
  53. # However, if problem persists, useful messages will
  54. # be emailed to Admin
  55. checkProcess()
  56. {
  57.    COUNTER=1
  58.  
  59.    LINENUM=`$WC -l $CONF | $AWK '{print $1}'` # get num of lines
  60.    # Process the Config file  
  61.    while $TEST $COUNTER -le $LINENUM ; do
  62.      
  63.       LINE=`$SED -n "$COUNTER"p $CONF | $AWK '{sub(/^#\t..*/, ""); print}'`
  64.       COMMAND=`$SED -n "$COUNTER"p $CONF | $AWK -F'[\t]' '{print $1}'`
  65.       STARTCOMMAND=`$SED -n "$COUNTER"p $CONF | $AWK -F'[\t]' '{print $2}'`
  66.       FPCOMMAND="`$ECHO $LINE | $AWK '{print $2}'`"
  67.      
  68.       # Checkpoint 1: Check for blank line and empty string
  69.       # for field 1 and 2
  70.       CHECK1=0
  71.       if $TEST "$LINE" = "" -o "$COMMAND" = "" -o "$STARTCOMMAND" = ""
  72.       then
  73.          $ECHO "Could not process line number $COUNTER"
  74.          COUNTER=`$EXPR $COUNTER + 1`
  75.          CHECK1=1
  76.          continue
  77.       fi
  78.      
  79.       CONFCOMMAND=`$SED -n "$COUNTER"p $CONF | $AWK -F'[\t]' '{print $3}'`
  80.  
  81.       # Checkpoint 2: Check whether command is currently running
  82.       # and attempt to run it if it isn't, if problem persists
  83.       # set CHECK2 flag to false (1)
  84.       CHECK2=0
  85.       # Search for command in the list of processes
  86.       $PS -ef | $GREP -v grep | $GREP $COMMAND > /dev/null 2>&1
  87.       FOUND=$?
  88.       if $TEST $FOUND -ne 0 ; then
  89.          $TOUCH $ERRTEMP
  90.          if $TEST ! -r $FPCOMMAND -a ! -s $FPCOMMAND ; then
  91.             CHECK2=2
  92.          else
  93.             $STARTCOMMAND >$ERRTEMP 2>&1
  94.             STATUS=$?
  95.             if $TEST $STATUS -ne 0 ; then
  96.                CHECK2=1
  97.             fi
  98.          fi
  99.       fi
  100.  
  101.       # Checkpoint 3: Check for the existence of config file
  102.       # if ONLY daemon config file was supplied
  103.       # Store stderr message from find command into temp file
  104.       # if any
  105.       CHECK3=0
  106.       if $TEST "$CONFCOMMAND" != "" ; then
  107.          $FIND $CONFCOMMAND >>$ERRTEMP 2>&1
  108.          if $TEST ! -r $CONFCOMMAND ; then
  109.             CHECK3=1
  110.          fi
  111.       fi
  112.  
  113.       # Now, perform main operation, first create unique temp file
  114.       # This is performed only if CHECK3=1
  115.       if $TEST $CHECK2 -ne 0 ; then
  116.          $TOUCH $CONFTEMP
  117.          $ECHO "Configuration script file: $CONF" > $CONFTEMP
  118.          $ECHO "\nProcess '$COMMAND' not listed in ps -ef" >> $CONFTEMP
  119.          $ECHO "\nCould not start '$STARTCOMMAND'"  >> $CONFTEMP  
  120.          if $TEST $CHECK3 -eq 1 ; then
  121.             $ECHO "\nDaemon configuration file could not be found and is not readable"  >> $CONFTEMP
  122.          fi
  123.          if $TEST -s "$ERRTEMP" ; then
  124.             $ECHO "\nOutput from STDOUT and Error Messages from STDERR: "  >> $CONFTEMP
  125.             $CAT $ERRTEMP >> $CONFTEMP # append STDERR & STDOUT info into Email
  126.             $RM $ERRTEMP > /dev/null 2>&1
  127.          fi
  128.       # Send email to admin using mailx
  129.       $MAILX -s "`$DATE` 3065944 Daemon Monitor" $ADMIN < $CONFTEMP      
  130.       fi
  131.      
  132.       COUNTER=`$EXPR $COUNTER + 1`
  133.    done
  134. }
  135.  
  136. # Main Method
  137. # Check whether configuration file exist and not empty
  138. # for convenience, one can provide one argument which is the filename
  139. # of the config file
  140. if $TEST "$#" -gt 1 ; then
  141.    $ECHO "Only one argument is allowed to be passed\nAttempt to exit"
  142.    exit 1
  143. elif $TEST "$1" != "" ; then
  144.    if $TEST -s "$1" ; then
  145.       if $TEST "`$DIRNAME $1`" = "." ; then
  146.          CONF="`$PWD`/$1"
  147.       else
  148.          CONF="$1"
  149.       fi
  150.    else
  151.       $ECHO "Configuration file is not existed and/or empty\nAttempt to exit"
  152.       exit 1
  153.    fi
  154. elif $TEST ! -s $CONF ; then
  155.    $ECHO "Configuration file is not existed and/or empty\nAttempt to exit"
  156.    exit 1
  157. fi
  158. checkProcess
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top