Guest User

Untitled

a guest
Jan 12th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.36 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Script freely provided by Bill Landry (bill@inetmsg.com); however, use
  4. # at your own peril! (:-O).  Comments, suggestions, and recommendations
  5. # for improving this script are always welcome.  Feel free to report any
  6. # issues, as well.
  7.  
  8. # This script will monitor and report the status of ClamD.  It can also
  9. # be configured to attempt to restart the ClamD daemon if it is found to
  10. # be non-responsive.  All variables below should be set correctly if set
  11. # to restart a failed, crashed, or non-responsive daemon.  Before trying
  12. # to restart the ClamD daemon, the script will first delete any orphaned
  13. # pid, lock, or socket files that may have been left due to a crash.
  14.  
  15. ######################################################################################
  16. # START OF USER CONFIGURATION SECTION - SET PROGRAM PATHS AND OTHER VARIABLE OPTIONS #
  17. ######################################################################################
  18.  
  19. # Edit quoted variables below to meet your own particular
  20. # needs/requirements, but do not remove the "quote" marks.
  21.  
  22. # Set and export program paths.
  23. PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
  24. export PATH
  25.  
  26. # Set path to clamd.pid file (see clamd.conf for path location).
  27. clamd_pid="/var/run/clamav/clamd.pid"
  28.  
  29. # If running clamd in "LocalSocket" mode (*NOT* in TCP/IP mode), and
  30. # either "SOcket Cat" (socat) or the "IO::Socket::UNIX" perl module
  31. # are installed on the system, and you want to report whether clamd
  32. # is running or not, uncomment the "clamd_socket" variable below (you
  33. # will be warned if neither socat nor IO::Socket::UNIX are found, but
  34. # the script will still run).  You will also need to set the correct
  35. # path to your clamd socket file (if unsure of the path, check the
  36. # "LocalSocket" setting in your clamd.conf file for socket location).
  37. clamd_socket="/var/run/clamav/clamd.ctl"
  38.  
  39. # If you would like to attemtp to restart ClamD if detected not running,
  40. # uncomment the next 2 lines.  Confirm the path to the "clamd_lock" file
  41. # (usually can be found in the clamd init script) and also enter the clamd
  42. # start command for your particular distro for the "start_clamd" variable
  43. # (the sample start command shown below should work for most linux distros).
  44. # NOTE: these 2 variables are dependant on the "clamd_socket" variable
  45. # shown above - if not enabled, then the following 2 variables will be
  46. # ignored, whether enabled or not.
  47. #clamd_lock="/var/lock/subsys/clamd"
  48. #start_clamd="service clamd start"
  49. start_clamd="/etc/init.d/clamav-daemon start"
  50.  
  51. # To only report issues, set the following variable to "yes".
  52. only_report_issues="yes"
  53.  
  54. # Log update information to '$log_file_path/$log_file_name'.
  55. enable_logging="no"
  56. log_file_path="/var/log"
  57. log_file_name="clamd-status.log"
  58.  
  59. # Set the following variable to "yes" once you have completed the
  60. # "USER CONFIGURATION SECTION" of this script.
  61. user_configuration_complete="yes"
  62.  
  63. #######################################################################################
  64. # END OF USER CONFIGURATION SECTION - YOU SHOULD NOT NEED TO EDIT ANYTHING BELOW HERE #
  65. #######################################################################################
  66.  
  67. # Use functions to make code more readable.
  68. comment () {
  69.   test "$only_report_issues" = "no" && echo "$1"
  70. }
  71.  
  72. log () {
  73.   test "$enable_logging" = "yes" && echo "`date "+%b %e %T"` $1" >> $log_file_path/$log_file_name
  74. }
  75.  
  76. # Check to see if the script's "USER CONFIGURATION SECTION" has been completed.
  77. if [ "$user_configuration_complete" != "yes" ]
  78.   then
  79.      echo ""
  80.      echo "               *** SCRIPT CONFIGURATION HAS NOT BEEN COMPLETED ***"
  81.      echo "   Please review and configure the 'USER CONFIGURATION SECTION' of the script."
  82.      echo "    Once the user configuration section has been completed, rerun the script."
  83.      echo ""
  84.      log "ALERT - SCRIPT HALTED, user configuration not completed"
  85.   exit 1
  86. fi
  87.  
  88. if [ -t 0 ] ; then
  89.   only_report_issues="no"
  90.   log "INFO - Script was run manually"
  91. fi
  92.  
  93. # If ClamD status check is enabled ("clamd_socket" variable is uncommented
  94. # and the socket path is correctly specified in "User Edit" section above),
  95. # then test to see if clamd is running or not.
  96. if [ -n "$clamd_socket" ] ; then
  97.   if [ "`perl -e 'use IO::Socket::UNIX; print $IO::Socket::UNIX::VERSION,"\n"' 2> /dev/null`" ]
  98.      then
  99.         io_socket1=1
  100.         if [ "`perl -MIO::Socket::UNIX -we '$s = IO::Socket::UNIX->new(shift); $s->print("PING"); \
  101.           print $s->getline; $s->close' "$clamd_socket" 2> /dev/null`" = "PONG" ] ; then
  102.            io_socket2=1
  103.            comment "===================="
  104.            comment "= ClamD is running ="
  105.            comment "===================="
  106.            log "INFO - ClamD is running"
  107.         fi
  108.      else
  109.         socat="`which socat 2> /dev/null`"
  110.         if [ -n "$socat" -a -x "$socat" ] ; then
  111.            socket_cat1=1
  112.            if [ "`(echo "PING"; sleep 1;) | socat - "$clamd_socket" 2> /dev/null`" = "PONG" ] ; then
  113.               socket_cat2=1
  114.               comment "===================="
  115.               comment "= ClamD is running ="
  116.               comment "===================="
  117.               log "INFO - ClamD is running"
  118.            fi
  119.         fi
  120.   fi
  121.   if [ -z "$io_socket1" -a -z "$socket_cat1" ]
  122.      then
  123.         echo ""
  124.         echo "                         --- WARNING ---"
  125.         echo "   It appears that neither 'SOcket CAT' (socat) nor the perl module"
  126.         echo "   'IO::Socket::UNIX' are installed on the system.  In order to run"
  127.         echo "   the ClamD socket test to determine whether ClamD is running or"
  128.         echo "   or not, either 'socat' or 'IO::Socket::UNIX' must be installed."
  129.         log "WARNING - neither socat nor IO::Socket::UNIX perl module found, cannot test whether ClamD is running"
  130.      else
  131.         if [ -z "$io_socket2" -a -z "$socket_cat2" ] ; then
  132.            echo ""
  133.            echo "     *************************"
  134.            echo "     *     !!! ALERT !!!     *"
  135.            echo "     * CLAMD IS NOT RUNNING! *"
  136.            echo "     *************************"
  137.            echo ""
  138.            log "ALERT - ClamD is not running"
  139.            if [ -n "$start_clamd" ] ; then
  140.               echo "    Attempting to start ClamD..."
  141.               echo ""
  142.               if [ -n "$io_socket1" ]
  143.                  then
  144.                     rm -f $clamd_pid $clamd_lock $clamd_socket 2> /dev/null
  145.                     $start_clamd > /dev/null && sleep 5
  146.                     if [ "`perl -MIO::Socket::UNIX -we '$s = IO::Socket::UNIX->new(shift); \
  147.                       $s->print("PING"); print $s->getline; $s->close' "$clamd_socket" \
  148.                       2> /dev/null`" = "PONG" ]
  149.                        then
  150.                           echo "=================================="
  151.                           echo "= ClamD was successfully started ="
  152.                           echo "=================================="
  153.                           log "NOTICE - ClamD was successfuly started"
  154.                        else
  155.                           echo "     *************************"
  156.                           echo "     *     !!! PANIC !!!     *"
  157.                           echo "     * CLAMD FAILED TO START *"
  158.                           echo "     *************************"
  159.                           echo ""
  160.                           echo "Check to confirm that the clamd start process defined for"
  161.                           echo "the 'start_clamd' variable in the 'USER EDIT SECTION' is"
  162.                           echo "set correctly for your particular distro.  If it is, then"
  163.                           echo "check your logs to determine why clamd failed to start."
  164.                           echo ""
  165.                           log "CRITICAL - ClamD failed to start"
  166.                        exit 1
  167.                     fi
  168.                  else
  169.                     if [ -n "$socket_cat1" ] ; then
  170.                        rm -f $clamd_pid $clamd_lock $clamd_socket 2> /dev/null
  171.                        $start_clamd > /dev/null && sleep 5
  172.                        if [ "`(echo "PING"; sleep 1;) | socat - "$clamd_socket" 2> /dev/null`" = "PONG" ]
  173.                           then
  174.                              echo "=================================="
  175.                              echo "= ClamD was successfully started ="
  176.                              echo "=================================="
  177.                              log "NOTICE - ClamD was successfuly started"
  178.                           else
  179.                              echo "     *************************"
  180.                              echo "     *     !!! PANIC !!!     *"
  181.                              echo "     * CLAMD FAILED TO START *"
  182.                              echo "     *************************"
  183.                              echo ""
  184.                              echo "Check to confirm that the clamd start process defined for"
  185.                              echo "the 'start_clamd' variable in the 'USER EDIT SECTION' is"
  186.                              echo "set correctly for your particular distro.  If it is, then"
  187.                              echo "check your logs to determine why clamd failed to start."
  188.                              echo ""
  189.                              log "CRITICAL - ClamD failed to start"
  190.                           exit 1
  191.                        fi
  192.                     fi
  193.               fi
  194.            fi
  195.         fi
  196.   fi
  197. fi
Add Comment
Please, Sign In to add comment