2015-06-18 18:32:37 +00:00
|
|
|
#! /usr/bin/env bash
|
|
|
|
|
2015-09-16 15:31:48 +00:00
|
|
|
# chkconfig: 2345 99 01
|
|
|
|
# description: Telegraf daemon
|
|
|
|
|
2015-06-18 18:32:37 +00:00
|
|
|
### BEGIN INIT INFO
|
|
|
|
# Provides: telegraf
|
|
|
|
# Required-Start: $all
|
|
|
|
# Required-Stop: $remote_fs $syslog
|
|
|
|
# Default-Start: 2 3 4 5
|
|
|
|
# Default-Stop: 0 1 6
|
|
|
|
# Short-Description: Start telegraf at boot time
|
|
|
|
### END INIT INFO
|
|
|
|
|
|
|
|
# this init script supports three different variations:
|
|
|
|
# 1. New lsb that define start-stop-daemon
|
|
|
|
# 2. Old lsb that don't have start-stop-daemon but define, log, pidofproc and killproc
|
|
|
|
# 3. Centos installations without lsb-core installed
|
|
|
|
#
|
|
|
|
# In the third case we have to define our own functions which are very dumb
|
|
|
|
# and expect the args to be positioned correctly.
|
|
|
|
|
|
|
|
# Command-line options that can be set in /etc/default/telegraf. These will override
|
|
|
|
# any config file values.
|
|
|
|
TELEGRAF_OPTS=
|
|
|
|
|
|
|
|
USER=telegraf
|
|
|
|
GROUP=telegraf
|
|
|
|
|
|
|
|
if [ -r /lib/lsb/init-functions ]; then
|
|
|
|
source /lib/lsb/init-functions
|
|
|
|
fi
|
|
|
|
|
|
|
|
DEFAULT=/etc/default/telegraf
|
|
|
|
|
|
|
|
if [ -r $DEFAULT ]; then
|
2018-04-06 20:17:24 +00:00
|
|
|
set -o allexport
|
2015-06-18 18:32:37 +00:00
|
|
|
source $DEFAULT
|
2018-04-06 20:17:24 +00:00
|
|
|
set +o allexport
|
2015-06-18 18:32:37 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$STDOUT" ]; then
|
|
|
|
STDOUT=/dev/null
|
|
|
|
fi
|
|
|
|
if [ ! -f "$STDOUT" ]; then
|
|
|
|
mkdir -p `dirname $STDOUT`
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$STDERR" ]; then
|
2015-07-06 16:07:06 +00:00
|
|
|
STDERR=/var/log/telegraf/telegraf.log
|
2015-06-18 18:32:37 +00:00
|
|
|
fi
|
|
|
|
if [ ! -f "$STDERR" ]; then
|
|
|
|
mkdir -p `dirname $STDERR`
|
|
|
|
fi
|
|
|
|
|
|
|
|
OPEN_FILE_LIMIT=65536
|
|
|
|
|
|
|
|
function pidofproc() {
|
|
|
|
if [ $# -ne 3 ]; then
|
|
|
|
echo "Expected three arguments, e.g. $0 -p pidfile daemon-name"
|
|
|
|
fi
|
|
|
|
|
2015-10-15 16:52:08 +00:00
|
|
|
if [ ! -f "$2" ]; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2015-06-18 18:32:37 +00:00
|
|
|
local pidfile=`cat $2`
|
|
|
|
|
|
|
|
if [ "x$pidfile" == "x" ]; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2015-10-15 16:52:08 +00:00
|
|
|
if ps --pid "$pidfile" | grep -q $(basename $3); then
|
2015-06-18 18:32:37 +00:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
function killproc() {
|
|
|
|
if [ $# -ne 3 ]; then
|
|
|
|
echo "Expected three arguments, e.g. $0 -p pidfile signal"
|
|
|
|
fi
|
|
|
|
|
|
|
|
pid=`cat $2`
|
|
|
|
|
|
|
|
kill -s $3 $pid
|
|
|
|
}
|
|
|
|
|
|
|
|
function log_failure_msg() {
|
|
|
|
echo "$@" "[ FAILED ]"
|
|
|
|
}
|
|
|
|
|
|
|
|
function log_success_msg() {
|
|
|
|
echo "$@" "[ OK ]"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Process name ( For display )
|
|
|
|
name=telegraf
|
|
|
|
|
|
|
|
# Daemon name, where is the actual executable
|
2016-01-08 15:47:09 +00:00
|
|
|
daemon=/usr/bin/telegraf
|
2015-06-18 18:32:37 +00:00
|
|
|
|
|
|
|
# pid file for the daemon
|
2015-07-06 16:07:06 +00:00
|
|
|
pidfile=/var/run/telegraf/telegraf.pid
|
2015-06-18 18:32:37 +00:00
|
|
|
piddir=`dirname $pidfile`
|
|
|
|
|
|
|
|
if [ ! -d "$piddir" ]; then
|
|
|
|
mkdir -p $piddir
|
2016-01-08 15:47:09 +00:00
|
|
|
chown $USER:$GROUP $piddir
|
2015-06-18 18:32:37 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Configuration file
|
2016-01-08 15:47:09 +00:00
|
|
|
config=/etc/telegraf/telegraf.conf
|
|
|
|
confdir=/etc/telegraf/telegraf.d
|
2015-06-18 18:32:37 +00:00
|
|
|
|
|
|
|
# If the daemon is not there, then exit.
|
|
|
|
[ -x $daemon ] || exit 5
|
|
|
|
|
|
|
|
case $1 in
|
|
|
|
start)
|
|
|
|
# Checked the PID file exists and check the actual status of process
|
2019-04-17 22:47:03 +00:00
|
|
|
if [ -e "$pidfile" ]; then
|
|
|
|
if pidofproc -p $pidfile $daemon > /dev/null; then
|
2020-01-14 20:30:03 +00:00
|
|
|
log_failure_msg "$name process is running"
|
|
|
|
else
|
|
|
|
log_failure_msg "$name pidfile has no corresponding process; ensure $name is stopped and remove $pidfile"
|
|
|
|
fi
|
|
|
|
exit 0
|
2015-06-18 18:32:37 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Bump the file limits, before launching the daemon. These will carry over to
|
|
|
|
# launched processes.
|
|
|
|
ulimit -n $OPEN_FILE_LIMIT
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
log_failure_msg "set open file limit to $OPEN_FILE_LIMIT"
|
|
|
|
fi
|
|
|
|
|
|
|
|
log_success_msg "Starting the process" "$name"
|
2017-05-05 21:29:40 +00:00
|
|
|
if command -v startproc >/dev/null; then
|
|
|
|
startproc -u "$USER" -g "$GROUP" -p "$pidfile" -q -- "$daemon" -pidfile "$pidfile" -config "$config" -config-directory "$confdir" $TELEGRAF_OPTS
|
|
|
|
elif which start-stop-daemon > /dev/null 2>&1; then
|
2016-01-08 15:47:09 +00:00
|
|
|
start-stop-daemon --chuid $USER:$GROUP --start --quiet --pidfile $pidfile --exec $daemon -- -pidfile $pidfile -config $config -config-directory $confdir $TELEGRAF_OPTS >>$STDOUT 2>>$STDERR &
|
2015-06-18 18:32:37 +00:00
|
|
|
else
|
2016-05-16 18:43:13 +00:00
|
|
|
su -s /bin/sh -c "nohup $daemon -pidfile $pidfile -config $config -config-directory $confdir $TELEGRAF_OPTS >>$STDOUT 2>>$STDERR &" $USER
|
2015-06-18 18:32:37 +00:00
|
|
|
fi
|
|
|
|
log_success_msg "$name process was started"
|
|
|
|
;;
|
|
|
|
|
|
|
|
stop)
|
|
|
|
# Stop the daemon.
|
|
|
|
if [ -e $pidfile ]; then
|
2019-04-17 22:47:03 +00:00
|
|
|
if pidofproc -p $pidfile $daemon > /dev/null; then
|
2018-12-27 02:54:50 +00:00
|
|
|
# periodically signal until process exists
|
|
|
|
while true; do
|
|
|
|
if ! pidofproc -p $pidfile $daemon > /dev/null; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
killproc -p $pidfile SIGTERM 2>&1 >/dev/null
|
|
|
|
sleep 2
|
|
|
|
done
|
|
|
|
|
|
|
|
log_success_msg "$name process was stopped"
|
|
|
|
rm -f $pidfile
|
2015-06-18 18:32:37 +00:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
log_failure_msg "$name process is not running"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
2016-03-05 03:11:53 +00:00
|
|
|
reload)
|
|
|
|
# Reload the daemon.
|
|
|
|
if [ -e $pidfile ]; then
|
2019-04-17 22:47:03 +00:00
|
|
|
if pidofproc -p $pidfile $daemon > /dev/null; then
|
2016-03-05 03:11:53 +00:00
|
|
|
if killproc -p $pidfile SIGHUP; then
|
|
|
|
log_success_msg "$name process was reloaded"
|
|
|
|
else
|
|
|
|
log_failure_msg "$name failed to reload service"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
log_failure_msg "$name process is not running"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
2015-06-18 18:32:37 +00:00
|
|
|
restart)
|
|
|
|
# Restart the daemon.
|
|
|
|
$0 stop && sleep 2 && $0 start
|
|
|
|
;;
|
|
|
|
|
|
|
|
status)
|
|
|
|
# Check the status of the process.
|
|
|
|
if [ -e $pidfile ]; then
|
|
|
|
if pidofproc -p $pidfile $daemon > /dev/null; then
|
|
|
|
log_success_msg "$name Process is running"
|
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
log_failure_msg "$name Process is not running"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
log_failure_msg "$name Process is not running"
|
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
|
|
|
version)
|
|
|
|
$daemon version
|
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
|
|
|
# For invalid arguments, print the usage message.
|
|
|
|
echo "Usage: $0 {start|stop|restart|status|version}"
|
|
|
|
exit 2
|
|
|
|
;;
|
|
|
|
esac
|