Skip to content
This repository has been archived by the owner on Jan 23, 2020. It is now read-only.

[0.27] Introduce sensu-service bin stub, update service management scripts to use it #178

Merged
merged 9 commits into from
Aug 30, 2016
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Bunchr::Packages.new do |t|

# all linux platforms are currently using init.d
# this may change in the future.
t.files << '/etc/init.d/sensu-service'
t.files << '/etc/init.d/sensu-service-init'
t.files << '/etc/init.d/sensu-api'
t.files << '/etc/init.d/sensu-client'
t.files << '/etc/init.d/sensu-server'
Expand Down
75 changes: 75 additions & 0 deletions sensu_configs/bin/sensu-service
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env bash

action=$1
sensu_service=sensu-$2
forking=$3

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
PID_DIR=/var/run/sensu
LOG_DIR=/var/log/sensu
LOG_LEVEL=info
USER=sensu

if [ -f /etc/default/sensu ]; then
. /etc/default/sensu
fi

cd /opt/sensu

svc_exec="/opt/sensu/bin/$sensu_service"

logfile=$LOG_DIR/$sensu_service.log
pidfile=$PID_DIR/$sensu_service.pid
options="-c $CONFIG_FILE -d $CONFIG_DIR -e $EXTENSION_DIR -p $pidfile -l $logfile -L $LOG_LEVEL $OPTIONS"

if [ "x$forking" = "xfork" ]; then
options="-b $options"
fi

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
}

validate() {
validate_options="--validate_config -b -c $CONFIG_FILE -d $CONFIG_DIR -e $EXTENSION_DIR $OPTIONS"
$svc_exec $validate_options
}

start() {
set_sensu_paths
ensure_dir $PID_DIR $USER
ensure_dir $LOG_DIR $USER
exec $svc_exec $options
}

case "$action" in
start)
start
;;
validate)
validate
;;
*)
echo "Usage: $0 {start|validate} {client|server|api} [fork]"
exit 2
esac

exit $?

2 changes: 1 addition & 1 deletion sensu_configs/init.d/sensu-api
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
# Description: Sensu monitoring framework api
### END INIT INFO

/etc/init.d/sensu-service api $1
/etc/init.d/sensu-service-init api $1

exit $?
2 changes: 1 addition & 1 deletion sensu_configs/init.d/sensu-client
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
# Description: Sensu monitoring framework client
### END INIT INFO

/etc/init.d/sensu-service client $1
/etc/init.d/sensu-service-init client $1

exit $?
2 changes: 1 addition & 1 deletion sensu_configs/init.d/sensu-server
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
# Description: Sensu monitoring framework server
### END INIT INFO

/etc/init.d/sensu-service server $1
/etc/init.d/sensu-service-init server $1

exit $?
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
# Description: Sensu monitoring framework service script
### END INIT INFO

sensu_service=sensu-$1
sensu_service=$1
service_name=sensu-$1

EMBEDDED_RUBY=true
CONFIG_FILE=/etc/sensu/config.json
Expand Down Expand Up @@ -58,7 +59,7 @@ fi
## Each platform must implement at least the following functions:
##
## start_daemon $user $pidfile $executable "arguments"
## killproc -p $pid $sensu_service
## killproc -p $pid $service_name
## log_daemon_msg $@
## log_action_msg $@
## log_success_msg $@
Expand All @@ -69,10 +70,10 @@ fi
if [ "$system" = "redhat" ]; then
## source platform specific external scripts
. /etc/init.d/functions
[ -r /etc/sysconfig/$sensu_service ] && . /etc/sysconfig/$sensu_service
[ -r /etc/sysconfig/$service_name ] && . /etc/sysconfig/$service_name

## set or override platform specific variables
lockfile=${LOCKFILE-/var/lock/subsys/$sensu_service}
lockfile=${LOCKFILE-/var/lock/subsys/$service_name}

## set or override platform specific functions
start_daemon() {
Expand Down Expand Up @@ -102,10 +103,10 @@ fi
if [ "$system" = "debian" ]; then
## source platform specific external scripts
. /lib/lsb/init-functions
[ -r /etc/default/$sensu_service ] && . /etc/default/$sensu_service
[ -r /etc/default/$service_name ] && . /etc/default/$service_name

## set or override platform specific variables
lockfile=${LOCKFILE-/var/lock/$sensu_service}
lockfile=${LOCKFILE-/var/lock/$service_name}

## set or override platform specific functions
start_daemon() {
Expand All @@ -122,10 +123,10 @@ fi
if [ "$system" = "suse" ]; then
## source platform specific external scripts
. /lib/lsb/init-functions
[ -r /etc/default/$sensu_service ] && . /etc/default/$sensu_service
[ -r /etc/default/$service_name ] && . /etc/default/$service_name

## set or override platform specific variables
lockfile=${LOCKFILE-/var/lock/subsys/$sensu_service}
lockfile=${LOCKFILE-/var/lock/subsys/$service_name}

## set or override platform specific functions
start_daemon() {
Expand All @@ -147,20 +148,12 @@ fi

cd /opt/sensu

exec=/opt/sensu/bin/$sensu_service
exec="/opt/sensu/bin/sensu-service"

pidfile=$PID_DIR/$sensu_service.pid
logfile=$LOG_DIR/$sensu_service.log
pidfile=$PID_DIR/$service_name.pid
logfile=$LOG_DIR/$service_name.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
}
options="start $service_name fork"

set_sensu_paths() {
if [ "x$EMBEDDED_RUBY" = "xtrue" ]; then
Expand Down Expand Up @@ -197,29 +190,19 @@ wait_for_stop() {
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"
log_daemon_msg "Starting $service_name"

[ -x $exec ] || exit 5

status &> /dev/null

if [ $? -eq 0 ]; then
log_action_msg "$sensu_service is already running."
log_action_msg "$service_name 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=$?
Expand All @@ -241,10 +224,7 @@ start() {
}

stop() {
if [ "x$CLIENT_DEREGISTER_ON_STOP" = "xtrue" ]; then
deregister_client
fi
log_daemon_msg "Stopping $sensu_service"
log_daemon_msg "Stopping $service_name"

# try pgrep
pid=$(pid_pgrep)
Expand All @@ -269,7 +249,7 @@ stop() {
if [ $retval -eq 0 ]; then
echo_ok
else
log_failure_msg "Timed out waiting for $sensu_service to stop"
log_failure_msg "Timed out waiting for $service_name to stop"
echo_fail
fi
else
Expand All @@ -278,7 +258,7 @@ stop() {

return $retval
else
log_action_msg "$sensu_service is stopped"
log_action_msg "$service_name is stopped"
return 3
fi
}
Expand All @@ -290,7 +270,7 @@ status() {
pid=$(pid_pgrep)

if [ -n "$pid" ]; then
log_action_msg "$sensu_service (pid $pid) is running"
log_action_msg "$service_name (pid $pid) is running"
return 0
fi

Expand All @@ -301,28 +281,27 @@ status() {
kill -0 $pid

if [ $? -eq 0 ]; then
log_action_msg "$sensu_service (pid $pid) is running"
log_action_msg "$service_name (pid $pid) is running"
return 0
else
log_action_msg "$sensu_service dead but pid file exists"
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 "$sensu_service dead but subsys locked"
log_action_msg "$service_name dead but subsys locked"
return 2
fi

log_action_msg "$sensu_service is stopped"
log_action_msg "$service_name 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"
validate_pidfile=$PID_DIR/$service_name-validate.pid
start_daemon $USER $validate_pidfile $exec "validate $service_name fork"
}

restart() {
Expand Down
2 changes: 1 addition & 1 deletion sensu_configs/systemd/sensu-api.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Description=sensu api
[Service]
User=sensu
Group=sensu
ExecStart=/opt/sensu/bin/sensu-api -c /etc/sensu/config.json -d /etc/sensu/conf.d -e /etc/sensu/extensions -l /var/log/sensu/sensu-api.log
ExecStart=/opt/sensu/bin/sensu-service start api
KillMode=process
Restart=on-failure
RestartSec=1min
Expand Down
4 changes: 2 additions & 2 deletions sensu_configs/systemd/sensu-client.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ Description=sensu client
[Service]
User=sensu
Group=sensu
ExecStart=/opt/sensu/bin/sensu-client -c /etc/sensu/config.json -d /etc/sensu/conf.d -e /etc/sensu/extensions -l /var/log/sensu/sensu-client.log
ExecStart=/opt/sensu/bin/sensu-service start client
KillMode=process
Restart=on-failure
RestartSec=1min

[Install]
WantedBy=multi-user.target
WantedBy=multi-user.target
2 changes: 1 addition & 1 deletion sensu_configs/systemd/sensu-server.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Description=sensu server
[Service]
User=sensu
Group=sensu
ExecStart=/opt/sensu/bin/sensu-server -c /etc/sensu/config.json -d /etc/sensu/conf.d -e /etc/sensu/extensions -l /var/log/sensu/sensu-server.log
ExecStart=/opt/sensu/bin/sensu-service start server
KillMode=process
Restart=on-failure
RestartSec=1min
Expand Down
23 changes: 2 additions & 21 deletions sensu_configs/upstart/sensu-api.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ stop on [!12345]

respawn

env PROG=sensu-api
env PROG=sensu-service

chdir /opt/sensu/bin

Expand All @@ -16,30 +16,11 @@ pre-start script
end script

script
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
USER=sensu

if [ -f /etc/default/sensu ]; then
. /etc/default/sensu
fi

logfile=$LOG_DIR/$PROG.log

options="-c $CONFIG_FILE -d $CONFIG_DIR -e $EXTENSION_DIR -l $logfile -L $LOG_LEVEL $OPTIONS"

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

exec setuidgid $USER ./$PROG $options
exec setuidgid $USER ./$PROG start api
end script
Loading