Skip to content

Instantly share code, notes, and snippets.

@rubendob
Created September 6, 2018 09:42
Show Gist options
  • Save rubendob/6157f2177d16e4b228782cd88fdeb2ff to your computer and use it in GitHub Desktop.
Save rubendob/6157f2177d16e4b228782cd88fdeb2ff to your computer and use it in GitHub Desktop.
Prometheus node exporter in Debian Wheezy
#!/bin/sh
### BEGIN INIT INFO
# Provides: node_exporter
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Should-Start: $all
# Should-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Exporter for machine metrics.
# Description: Prometheus exporter for machine metrics,
# written in Go with pluggable metric collectors.
### END INIT INFO
set -e
. /lib/lsb/init-functions
NAME=node_exporter
DESC="Exporter for machine metrics"
DAEMON=/usr/local/bin/node_exporter
USER=root
CONFIG=
PID="/var/run/prometheus/$NAME.pid"
LOG="/var/log/prometheus/$NAME.log"
ALERTMANAGER_OPTS=
DAEMON_OPTS="$ALERTMANAGER_OPTS"
DAEMON_OPTS="$DAEMON_OPTS --collector.loadavg --collector.meminfo --collector.filesystem --collector.diskstats.ignored-devices=\"^(ram|loop|fd)\\d+$\""
# Check if DAEMON binary exist
[ -f $DAEMON ] || exit 0
[ -f "/etc/default/$NAME" ] && . /etc/default/$NAME
service_not_configured () {
if [ "$1" != "stop" ]; then
printf "\tPlease configure $NAME and then edit /etc/default/$NAME\n"
printf "\tand set the \"START\" variable to \"yes\" in order to allow\n"
printf "\t$NAME to start.\n"
fi
exit 0
}
service_checks () {
# Check if START variable is set to "yes", if not we exit.
if [ "$START" != "yes" ]; then
service_not_configured $1
fi
# Prepare directories
mkdir -p "/var/run/prometheus" "/var/log/prometheus"
chown -R $USER "/var/run/prometheus" "/var/log/prometheus"
# Check if PID exists
if [ -f "$PID" ]; then
PID_NUMBER=`cat $PID`
if [ -z "`ps axf | grep ${PID_NUMBER} | grep -v grep`" ]; then
echo "Service was aborted abnormally; clean the PID file and continue..."
rm -f "$PID"
else
echo "Service already started; skip..."
exit 1
fi
fi
}
start () {
service_checks $1
$DAEMON $DAEMON_OPTS > $LOG 2>&1 &
RETVAL=$?
echo $! > $PID
if [ $RETVAL ]; then
log_end_msg 0
else
log_end_msg 1
fi
}
stop () {
if start-stop-daemon --retry TERM/5/KILL/5 --oknodo --stop --quiet --pidfile $PID 2>&1 1>$LOG
then
log_end_msg 0
rm $PID
else
log_end_msg 1
fi
}
case "$1" in
start)
log_daemon_msg "Starting $DESC -" "$NAME"
start
;;
stop)
log_daemon_msg "Stopping $DESC -" "$NAME"
stop
;;
reload)
log_daemon_msg "Reloading $DESC configuration -" "$NAME"
#-- sorry but node_exporter doesn't handle -HUP signal...
#if start-stop-daemon --stop --signal HUP --quiet --oknodo --pidfile $PID --startas /bin/bash -- -c "exec $DAEMON $DAEMON_OPTS > $LOG 2>&1"
#then
# log_end_msg 0
#else
# log_end_msg 1
#fi
stop
start
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC -" "$NAME"
stop
start
;;
syntax)
$DAEMON --help
;;
status)
status_of_proc -p $PID $DAEMON $NAME
;;
*)
log_action_msg "Usage: /etc/init.d/$NAME {start|stop|reload|restart|force-reload|syntax|status}"
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment