grepwood

/etc/init.d/sensu-service

Apr 24th, 2015
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.51 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # chkconfig: 345 90 90
  4. # description: Sensu monitoring framework service
  5.  
  6. ### BEGIN INIT INFO
  7. # Provides:       sensu-service
  8. # Required-Start: $remote_fs $network
  9. # Required-Stop:  $remote_fs $network
  10. # Default-Start:  2 3 4 5
  11. # Default-Stop:   0 1 6
  12. # Description:    Sensu monitoring framework service script
  13. ### END INIT INFO
  14.  
  15. service=sensu-$1
  16.  
  17. EMBEDDED_RUBY=false
  18. CONFIG_FILE=/etc/sensu/config.json
  19. CONFIG_DIR=/etc/sensu/conf.d
  20. EXTENSION_DIR=/etc/sensu/extensions
  21. PLUGINS_DIR=/etc/sensu/plugins
  22. HANDLERS_DIR=/etc/sensu/handlers
  23. LOG_DIR=/var/log/sensu
  24. LOG_LEVEL=info
  25. PID_DIR=/var/run/sensu
  26. USER=sensu
  27. SERVICE_MAX_WAIT=10
  28.  
  29. if [ -f /etc/default/sensu ]; then
  30.     . /etc/default/sensu
  31. fi
  32.  
  33. system=unknown
  34. if [ -f /etc/redhat-release ]; then
  35.     system=redhat
  36. elif [ -f /etc/system-release ]; then
  37.     system=redhat
  38. elif [ -f /etc/debian_version ]; then
  39.     system=debian
  40. elif [ -f /etc/SuSE-release ]; then
  41.     system=suse
  42. elif [ -f /etc/gentoo-release ]; then
  43.     system=gentoo
  44. elif [ -f /etc/arch-release ]; then
  45.     system=arch
  46. elif [ -f /etc/slackware-version ]; then
  47.     system=slackware
  48. elif [ -f /etc/lfs-release ]; then
  49.     system=lfs
  50. fi
  51.  
  52. ##
  53. ## Set platform specific bits here.
  54. ## The original platform for this script was redhat so the scripts are partial
  55. ## to redhat style. Eventually we may want to be more LSB compliant
  56. ## such as what Debian platforms already implement instead.
  57. ##
  58. ## Each platform must implement at least the following functions:
  59. ##
  60. ##     start_daemon $user $pidfile $executable "arguments"
  61. ##     killproc -p $pid $service
  62. ##     log_daemon_msg $@
  63. ##     log_action_msg $@
  64. ##     log_success_msg $@
  65. ##     log_failure_msg $@
  66. ##     echo_ok
  67. ##     echo_fail
  68. ##
  69. if [ "$system" = "redhat" ]; then
  70.     ## source platform specific external scripts
  71.     . /etc/init.d/functions
  72.     [ -r /etc/sysconfig/$service ] && . /etc/sysconfig/$service
  73.  
  74.     ## set or override platform specific variables
  75.     lockfile=${LOCKFILE-/var/lock/subsys/$service}
  76.  
  77.     ## set or override platform specific functions
  78.     start_daemon() {
  79.         daemon --user $1 --pidfile $2 "$3 $4"
  80.     }
  81.     log_daemon_msg() {
  82.         echo -n $"$1"
  83.     }
  84.     echo_ok() {
  85.         echo_success; echo
  86.     }
  87.     echo_fail() {
  88.         echo_failure; echo
  89.     }
  90.     log_success_msg() {
  91.         success $"$@"
  92.     }
  93.     log_failure_msg() {
  94.         failure $"$@"
  95.         echo $"$@"
  96.     }
  97.     log_action_msg() {
  98.         echo $@
  99.     }
  100. fi
  101.  
  102. if [ "$system" = "debian" ]; then
  103.     ## source platform specific external scripts
  104.     . /lib/lsb/init-functions
  105.     [ -r /etc/default/$service ] && . /etc/default/$service
  106.  
  107.     ## set or override platform specific variables
  108.     lockfile=${LOCKFILE-/var/lock/$service}
  109.  
  110.     ## set or override platform specific functions
  111.     start_daemon() {
  112.         start-stop-daemon --start --chuid $1 --pidfile $2 --exec $3 -- $4
  113.     }
  114.     echo_ok() {
  115.         log_end_msg 0
  116.     }
  117.     echo_fail() {
  118.         log_end_msg 1
  119.     }
  120. fi
  121.  
  122. if [ "$system" = "suse" ]; then
  123.     ## source platform specific external scripts
  124.     . /lib/lsb/init-functions
  125.     [ -r /etc/default/$service ] && . /etc/default/$service
  126.  
  127.     ## set or override platform specific variables
  128.     lockfile=${LOCKFILE-/var/lock/subsys/$service}
  129.  
  130.     ## set or override platform specific functions
  131.     start_daemon() {
  132.         startproc -s -u $1 -p $2 $3 $4
  133.     }
  134.     echo_ok() {
  135.         log_success_msg
  136.     }
  137.     echo_fail() {
  138.         log_failure_msg
  139.     }
  140.     log_daemon_msg() {
  141.         echo -n $"$1"
  142.     }
  143.     log_action_msg() {
  144.         echo $@
  145.     }
  146. fi
  147. # TODO: support other platforms in the future: gentoo, arch, slackware, etc
  148.  
  149. cd /opt/sensu
  150.  
  151. exec=/opt/sensu/bin/$service
  152.  
  153. pidfile=$PID_DIR/$service.pid
  154. logfile=$LOG_DIR/$service.log
  155.  
  156. options="-b -c $CONFIG_FILE -d $CONFIG_DIR -e $EXTENSION_DIR -p $pidfile -l $logfile -L $LOG_LEVEL $OPTIONS"
  157.  
  158. ensure_dir() {
  159.     if [ ! -d $1 ]; then
  160.         mkdir -p $1
  161.         chown -R $2 $1
  162.         chmod 755 $1
  163.     fi
  164. }
  165.  
  166. set_sensu_paths() {
  167.     if [ "x$EMBEDDED_RUBY" = "xtrue" ]; then
  168.         export PATH=/opt/sensu/embedded/bin:$PATH:$PLUGINS_DIR:$HANDLERS_DIR
  169.         export GEM_PATH=/opt/sensu/embedded/lib/ruby/gems/2.0.0:$GEM_PATH
  170.     else
  171.         export PATH=$PATH:$PLUGINS_DIR:$HANDLERS_DIR
  172.     fi
  173. }
  174.  
  175. start() {
  176.     log_daemon_msg "Starting $service"
  177.  
  178.     [ -x $exec ] || exit 5
  179.  
  180.     status &> /dev/null
  181.  
  182.     if [ $? -eq 0 ]; then
  183.         log_action_msg "$service is already running."
  184.         echo_ok
  185.         exit 0
  186.     fi
  187.  
  188.     set_sensu_paths
  189.     ensure_dir $PID_DIR $USER
  190.     ensure_dir $LOG_DIR $USER
  191.     echo "we're not really starting, just dumping the command we'd run"
  192.     echo "everything else is vanilla /etc/init.d/sensu-service"
  193.     echo "start_daemon $USER $pidfile $exec \"$options\""
  194.     exit
  195.     start_daemon $USER $pidfile $exec "$options"
  196.  
  197.     retval=$?
  198.     sleep 1
  199.  
  200.     # make sure it's still running. some errors occur only after startup
  201.     status &> /dev/null
  202.     if [ $? -ne 0 ]; then
  203.         echo_fail
  204.         exit 1
  205.     fi
  206.  
  207.     if [ $retval -eq 0 ]; then
  208.         touch $lockfile
  209.     fi
  210.     echo_ok
  211.  
  212.     return $retval
  213. }
  214.  
  215. stop() {
  216.     log_daemon_msg "Stopping $service"
  217.  
  218.     if [ -f $pidfile ]; then
  219.         killproc -p $pidfile $service
  220.         retval=$?
  221.         if [ $retval -eq 0 ]; then
  222.             rm -f $pidfile
  223.             rm -f $lockfile
  224.             echo_ok
  225.         else
  226.             echo_fail
  227.         fi
  228.         sleep 1
  229.         return $retval
  230.     else
  231.         pid=$(pgrep -P1 -fl $exec | grep -v grep | grep -v bash | cut -f1 -d" ")
  232.         if [ ! "$pid" == "" ]; then
  233.             kill $pid
  234.             retval=$?
  235.             if [ $retval -eq 0 ]; then
  236.                 rm -f $pidfile
  237.                 rm -f $lockfile
  238.                 echo_ok
  239.             else
  240.                 echo_fail
  241.             fi
  242.             sleep 1
  243.             return $retval
  244.         else
  245.             echo_fail
  246.         fi
  247.     fi
  248. }
  249.  
  250. status() {
  251.     local pid
  252.  
  253.     # First try "ps"
  254.     pid=$(pgrep -P1 -fl $exec | grep -v grep | grep -v bash | cut -f1 -d" ")
  255.     if [ -n "$pid" ]; then
  256.         log_action_msg "$service (pid $pid) is running"
  257.         return 0
  258.     fi
  259.  
  260.     # Next try "/var/run/*.pid" files
  261.     if [ -f "$pidfile" ] ; then
  262.         read pid < "$pidfile"
  263.         if [ -n "$pid" ]; then
  264.             log_action_msg "$service dead but pid file exists"
  265.             return 1
  266.         fi
  267.     fi
  268.  
  269.     # See if $lockfile
  270.     if [ -f "$lockfile" ]; then
  271.         log_action_msg "$service dead but subsys locked"
  272.         return 2
  273.     fi
  274.  
  275.     log_action_msg "$service is stopped"
  276.     return 3
  277. }
  278.  
  279. # sometimes sensu components need a moment to shutdown, so
  280. # let's implement a waiting approach to restarts with a timeout
  281. restart() {
  282.     count=0; success=0
  283.  
  284.     stop
  285.  
  286.     while [ $count -lt $SERVICE_MAX_WAIT ]; do
  287.         count=$((count + 1))
  288.         status &> /dev/null
  289.         if [ $? -eq 0 ]; then
  290.             # still running
  291.             sleep 1
  292.         else
  293.             success=1
  294.             break
  295.         fi
  296.     done
  297.  
  298.     if [ $success = 1 ]; then
  299.         start
  300.     else
  301.         log_failure_msg "Timed out waiting for $service to stop"
  302.         return 1
  303.     fi
  304. }
  305.  
  306. case "$2" in
  307.     start)
  308.         start
  309.         ;;
  310.     stop)
  311.         stop
  312.         ;;
  313.     status)
  314.         status
  315.         ;;
  316.     restart)
  317.         restart
  318.         ;;
  319.     *)
  320.         echo "Usage: $0 {client|server|api|dashboard} {start|stop|status|restart}"
  321.         exit 2
  322. esac
  323.  
  324. exit $?
Advertisement
Add Comment
Please, Sign In to add comment