#!/bin/bash # chkconfig: 345 90 90 # description: Sensu monitoring framework service ### BEGIN INIT INFO # Provides: sensu-service # Required-Start: $remote_fs $network # Required-Stop: $remote_fs $network # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Description: Sensu monitoring framework service script ### END INIT INFO sensu_service=sensu-$1 EMBEDDED_RUBY=true CONFIG_FILE=/etc/sensu/config.json CONFIG_DIR=/etc/sensu/conf.d EXTENSION_DIR=/etc/sensu/extensions PLUGINS_DIR=/etc/sensu/plugins HANDLERS_DIR=/etc/sensu/handlers LOG_DIR=/var/log/sensu LOG_LEVEL=info PID_DIR=/var/run/sensu USER=sensu SERVICE_MAX_WAIT=10 if [ -f /etc/default/sensu ]; then . /etc/default/sensu fi system=unknown if [ -f /etc/redhat-release ]; then system=redhat elif [ -f /etc/system-release ]; then system=redhat elif [ -f /etc/debian_version ]; then system=debian elif [ -f /etc/SuSE-release ]; then system=suse elif [ -f /etc/gentoo-release ]; then system=gentoo elif [ -f /etc/arch-release ]; then system=arch elif [ -f /etc/slackware-version ]; then system=slackware elif [ -f /etc/lfs-release ]; then system=lfs fi ## ## Set platform specific bits here. ## The original platform for this script was redhat so the scripts are partial ## to redhat style. Eventually we may want to be more LSB compliant ## such as what Debian platforms already implement instead. ## ## Each platform must implement at least the following functions: ## ## start_daemon $user $pidfile $executable "arguments" ## killproc -p $pid $sensu_service ## log_daemon_msg $@ ## log_action_msg $@ ## log_success_msg $@ ## log_failure_msg $@ ## echo_ok ## echo_fail ## if [ "$system" = "redhat" ]; then ## source platform specific external scripts . /etc/init.d/functions [ -r /etc/sysconfig/$sensu_service ] && . /etc/sysconfig/$sensu_service ## set or override platform specific variables lockfile=${LOCKFILE-/var/lock/subsys/$sensu_service} ## set or override platform specific functions start_daemon() { daemon --user $1 --pidfile $2 "$3 $4" } log_daemon_msg() { echo -n $"$1" } echo_ok() { echo_success; echo } echo_fail() { echo_failure; echo } log_success_msg() { success $"$@" } log_failure_msg() { failure $"$@" echo $"$@" } log_action_msg() { echo $@ } fi if [ "$system" = "debian" ]; then ## source platform specific external scripts . /lib/lsb/init-functions [ -r /etc/default/$sensu_service ] && . /etc/default/$sensu_service ## set or override platform specific variables lockfile=${LOCKFILE-/var/lock/$sensu_service} ## set or override platform specific functions start_daemon() { start-stop-daemon --start --chuid $1 --pidfile $2 --exec $3 -- $4 } echo_ok() { log_end_msg 0 } echo_fail() { log_end_msg 1 } fi if [ "$system" = "suse" ]; then ## source platform specific external scripts . /lib/lsb/init-functions [ -r /etc/default/$sensu_service ] && . /etc/default/$sensu_service ## set or override platform specific variables lockfile=${LOCKFILE-/var/lock/subsys/$sensu_service} ## set or override platform specific functions start_daemon() { startproc -s -u $1 -p $2 $3 $4 } echo_ok() { log_success_msg } echo_fail() { log_failure_msg } log_daemon_msg() { echo -n $"$1" } log_action_msg() { echo $@ } fi cd /opt/sensu exec=/opt/sensu/bin/$sensu_service pidfile=$PID_DIR/$sensu_service.pid logfile=$LOG_DIR/$sensu_service.log options="-b -c $CONFIG_FILE -d $CONFIG_DIR -e $EXTENSION_DIR -p $pidfile -l $logfile -L $LOG_LEVEL $OPTIONS" ensure_dir() { if [ ! -d $1 ]; then mkdir -p $1 chown -R $2 $1 chmod 755 $1 fi } set_sensu_paths() { if [ "x$EMBEDDED_RUBY" = "xtrue" ]; then export PATH=/opt/sensu/embedded/bin:$PATH:$PLUGINS_DIR:$HANDLERS_DIR export GEM_PATH=/opt/sensu/embedded/lib/ruby/gems/2.3.0:$GEM_PATH else export PATH=$PATH:$PLUGINS_DIR:$HANDLERS_DIR fi } pid_pgrep() { pgrep -f -P 1 -u $USER " $exec " } wait_for_stop() { stopped=1; attempts=0 while [ $attempts -lt $SERVICE_MAX_WAIT ]; do attempts=$((attempts + 1)) status &> /dev/null if [ $? -eq 0 ]; then # still running sleep 1 else rm -f $pidfile rm -f $lockfile stopped=0 break fi done return $stopped } deregister_client() { log_action_msg "Deregistering sensu-client" echo "{ \"name\": \"api_call\", \"output\": \"delete\", \"process\": \"init\", \"user\": \"root\", \"status\": 1, \"handler\": \"${CLIENT_DEREGISTER_HANDLER}\"}" > \ /dev/tcp/localhost/3030 } start() { log_daemon_msg "Starting $sensu_service" [ -x $exec ] || exit 5 status &> /dev/null if [ $? -eq 0 ]; then log_action_msg "$sensu_service is already running." echo_ok exit 0 fi set_sensu_paths ensure_dir $PID_DIR $USER ensure_dir $LOG_DIR $USER start_daemon $USER $pidfile $exec "$options" retval=$? sleep 1 # make sure it's still running, some errors occur only after startup. status &> /dev/null if [ $? -ne 0 ]; then echo_fail exit 1 fi if [ $retval -eq 0 ]; then touch $lockfile fi echo_ok return $retval } stop() { if [ "x$CLIENT_DEREGISTER_ON_STOP" = "xtrue" ]; then deregister_client fi log_daemon_msg "Stopping $sensu_service" # try pgrep pid=$(pid_pgrep) if [ ! -n "$pid" ]; then # try the pid file if [ -f "$pidfile" ] ; then read pid < "$pidfile" fi fi if [ -n "$pid" ]; then kill $pid retval=$? if [ $retval -eq 0 ]; then wait_for_stop retval=$? if [ $retval -eq 0 ]; then echo_ok else log_failure_msg "Timed out waiting for $sensu_service to stop" echo_fail fi else echo_fail fi return $retval else log_action_msg "$sensu_service is stopped" return 3 fi } status() { local pid # try pgrep pid=$(pid_pgrep) if [ -n "$pid" ]; then log_action_msg "$sensu_service (pid $pid) is running" return 0 fi # try the pid file if [ -f "$pidfile" ] ; then read pid < "$pidfile" kill -0 $pid if [ $? -eq 0 ]; then log_action_msg "$sensu_service (pid $pid) is running" return 0 else log_action_msg "$sensu_service dead but pid file exists" return 1 fi fi # check for the lock file if [ -f "$lockfile" ]; then log_action_msg "$sensu_service dead but subsys locked" return 2 fi log_action_msg "$sensu_service is stopped" return 3 } validate_config() { validate_pidfile=$PID_DIR/$sensu_service-validate.pid validate_options="--validate_config -b -c $CONFIG_FILE -d $CONFIG_DIR -e $EXTENSION_DIR -p $validate_pidfile $OPTIONS" start_daemon $USER $validate_pidfile $exec "$validate_options" } restart() { validate_config retval=$? if [ $retval -eq 0 ]; then stop retval=$? if [ $retval -eq 0 ] || [ $retval -eq 3 ]; then start else return $retval fi else return $retval fi } case "$2" in start) start ;; stop) stop ;; status) status ;; restart) restart ;; validate_config) validate_config ;; *) echo "Usage: $0 {client|server|api} {start|stop|status|restart|validate_config}" exit 2 esac exit $?