Created
January 13, 2020 21:35
-
-
Save glaracuente/6d9261d2dcd874d27a4321f4cd5084a2 to your computer and use it in GitHub Desktop.
sensu-server init script with kill-9 . Lines 265-272 are the only additions to the original
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# chkconfig: 345 90 01 | |
# description: Sensu monitoring framework service | |
### BEGIN INIT INFO | |
# Provides: server | |
# 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 | |
service_name=sensu-server | |
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 | |
# explicitly add /sbin to PATH so that start-stop-server is always available | |
PATH=$PATH:/sbin | |
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 $service_name | |
## 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/$service_name ] && . /etc/sysconfig/$service_name | |
## set or override platform specific variables | |
lockfile=${LOCKFILE-/var/lock/subsys/$service_name} | |
## 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/$service_name ] && . /etc/default/$service_name | |
## set or override platform specific variables | |
lockfile=${LOCKFILE-/var/lock/$service_name} | |
## 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/$service_name ] && . /etc/default/$service_name | |
## set or override platform specific variables | |
lockfile=${LOCKFILE-/var/lock/subsys/$service_name} | |
## 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" | |
daemon_exec="/opt/sensu/bin/sensu-server" | |
pidfile=$PID_DIR/$service_name.pid | |
logfile=$LOG_DIR/$service_name.log | |
options="start server fork" | |
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.4.0:$GEM_PATH | |
else | |
export PATH=$PATH:$PLUGINS_DIR:$HANDLERS_DIR | |
fi | |
} | |
pid_pgrep() { | |
pgrep -f -P 1 -u $USER " $daemon_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 | |
} | |
start() { | |
log_daemon_msg "Starting $service_name" | |
set_sensu_paths | |
[ -x $exec ] || exit 5 | |
status &> /dev/null | |
if [ $? -eq 0 ]; then | |
log_action_msg "$service_name is already running." | |
echo_ok | |
exit 0 | |
fi | |
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() { | |
log_daemon_msg "Stopping $service_name" | |
if [ -f "$pidfile" ] ; then | |
# try the pid file first | |
read pid < "$pidfile" | |
fi | |
# test if pid is running | |
kill -0 $pid > /dev/null 2>&1 | |
retval=$? | |
if [ $retval -ne 0 ]; then | |
# fall back to pgrep | |
pid=$(pid_pgrep) | |
fi | |
if [ -n "$pid" ]; then | |
# test if pid is running | |
kill -0 $pid > /dev/null 2>&1 | |
retval=$? | |
# if the pid is not running service is already stopped | |
if [ $retval -ne 0 ]; then | |
echo_ok | |
return 0 | |
fi | |
kill $pid | |
retval=$? | |
if [ $retval -eq 0 ]; then | |
wait_for_stop | |
retval=$? | |
if [ $retval -ne 0 ]; then | |
kill -9 $pid > /dev/null 2>&1 | |
fi | |
wait_for_stop | |
retval=$? | |
if [ $retval -eq 0 ]; then | |
echo_ok | |
else | |
log_failure_msg "Timed out waiting for $service_name to stop" | |
echo_fail | |
fi | |
else | |
echo_fail | |
fi | |
return $retval | |
else | |
log_action_msg "$service_name is stopped" | |
return 0 | |
fi | |
} | |
status() { | |
local pid | |
# try pgrep | |
pid=$(pid_pgrep) | |
if [ -n "$pid" ]; then | |
log_action_msg "$service_name (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 "$service_name (pid $pid) is running" | |
return 0 | |
else | |
log_action_msg "$service_name dead but pid file exists" | |
return 1 | |
fi | |
fi | |
# check for the lock file | |
if [ -f "$lockfile" ]; then | |
log_action_msg "$service_name dead but subsys locked" | |
return 2 | |
fi | |
log_action_msg "$service_name is stopped" | |
return 3 | |
} | |
validate_config() { | |
set_sensu_paths | |
validate_pidfile=$PID_DIR/$service_name-validate.pid | |
start_daemon $USER $validate_pidfile $exec "validate server fork" | |
} | |
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 | |
} | |
ensure_dir() { | |
if [ ! -d $1 ]; then | |
mkdir -p $1 | |
chown -R $2 $1 | |
chmod 755 $1 | |
fi | |
} | |
ensure_dir $PID_DIR $USER | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
status) | |
status | |
;; | |
restart) | |
restart | |
;; | |
validate_config) | |
validate_config | |
;; | |
*) | |
echo "Usage: $0 {start|stop|status|restart|validate_config}" | |
exit 2 | |
esac | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment