Created
September 24, 2013 17:05
-
-
Save oldpatricka/6687876 to your computer and use it in GitHub Desktop.
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 99 01 | |
# description: tcollector agent daemon | |
# | |
# processname: tcollector.py | |
# Get TSD_HOST from a file that can be set in ctx | |
TSD_HOST="nimbus-opentsdb.no-ip.info" | |
# TSD_HOST=dns.name.of.tsd | |
TCOLLECTOR_PATH=${TCOLLECTOR_PATH-'/usr/local/tcollector'} | |
test -n "$TSD_HOST" || { | |
echo >&2 "TSD_HOST is not set in $0" | |
exit 1 | |
} | |
# Try to get hostname from instance data | |
HOSTNAME=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4) | |
while ! echo $HOSTNAME | grep '[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' ; do | |
sleep 1 | |
HOSTNAME=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4) | |
done | |
PIDFILE=${PIDFILE-'/var/run/tcollector.pid'} | |
PROG=$TCOLLECTOR_PATH/tcollector.py | |
LOG=${LOG-'/var/log/tcollector.log'} | |
COMMAND=$1 | |
shift | |
ARGS="-p 4242 -c $TCOLLECTOR_PATH/collectors -H $TSD_HOST -t host=$HOSTNAME -P $PIDFILE" | |
ARGS="$ARGS $@" | |
# Sanity checks. | |
test -d "$TCOLLECTOR_PATH" || { | |
echo >&2 "No such directory: $TCOLLECTOR_PATH" | |
echo >&2 "You might need to set the TCOLLECTOR_PATH variable in $0" | |
exit 2 | |
} | |
test -f "$PROG" || { | |
echo >&2 "No such file: $PROG" | |
echo >&2 "You might need to set the TCOLLECTOR_PATH variable in $0" | |
exit 3 | |
} | |
for i in "$PIDFILE" "$LOG"; do | |
# If the file doesn't exist, check that we have write access to its parent | |
# directory to be able to create it. | |
test -e "$i" || i=`dirname "$i"` | |
test -w "$i" || { | |
echo >&2 "$0: error: Cannot write to $i" | |
exit 4 | |
} | |
done | |
which_python () { | |
for python in /usr/bin/python2.7 /usr/bin/python2.6 /usr/bin/python2.5 /usr/bin/python; do | |
test -x "$python" && echo "$python" && return | |
done | |
echo >&2 'Could not find a Python interpreter' | |
exit 1 | |
} | |
PYTHON=$(which_python) | |
start () { | |
echo "Starting $PROG" | |
$PYTHON $PROG $ARGS >> $LOG 2>&1 & | |
} | |
# stop [signum] | |
stop () { | |
echo "Stopping $PROG" | |
pkill $1 -f "/usr/bin/python.* $PROG -c" | |
} | |
status () { | |
if pgrep -f "/usr/bin/python.* $PROG -c" >/dev/null; then | |
echo "$PROG" running | |
return 0 | |
fi | |
return 1 | |
} | |
forcerestart () { | |
stop | |
try=1 | |
sleep 1 | |
while status; do | |
try=$((try + 1)) | |
if [[ $try -gt 3 ]]; then | |
stop -9 | |
else | |
stop | |
fi | |
echo "Waiting for $PROG to die.." | |
sleep 5 | |
done | |
start | |
} | |
case $COMMAND in | |
start) status || start | |
;; | |
force-restart) | |
forcerestart | |
;; | |
restart) | |
# tcollector already respawns collectors if they | |
# have changed on-disk, and kills old ones/starts | |
# new ones. The only thing tcollector doesn't do | |
# is restart itself if itself has changed. For a more | |
# graceful restart, just make sure we're running and | |
# restart only if tcollector is newer on disk than | |
# since it started. This doesn't check for dependencies | |
# like asyncproc.py, but that's ok. | |
if status; then | |
newer=$(find $PROG -newer $PIDFILE | wc -l) | |
if [[ $newer -gt 0 ]]; then | |
forcerestart | |
fi | |
else | |
start | |
fi | |
;; | |
stop) stop | |
;; | |
status) status | |
exit $? | |
;; | |
*) echo >&2 "usage: $0 <start [args]|stop|restart|status|force-restart>" | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment