Skip to content

Instantly share code, notes, and snippets.

@eloo
Last active December 22, 2023 09:00
Show Gist options
  • Star 45 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save eloo/a06d7c70ff2a841b7bb98cd322b851b9 to your computer and use it in GitHub Desktop.
Save eloo/a06d7c70ff2a841b7bb98cd322b851b9 to your computer and use it in GitHub Desktop.
Init.d script for prometheus node exporter
# Set the command-line arguments to pass to the server.
ARGS='-web.listen-address=:9100 -collector.diskstats.ignored-devices="^(ram|loop|fd)\\d+$"'
# Prometheus-node-exporter supports the following options:
# -collector.diskstats.ignored-devices="^(ram|loop|fd|(h|s|v|xv)d[a-z])\\d+$": Regexp of devices to ignore for diskstats.
# -collector.filesystem.ignored-mount-points="^/(sys|proc|dev)($|/)": Regexp of mount points to ignore for filesystem collector.
# -collector.ipvs.procfs="/proc": procfs mountpoint.
# -collector.megacli.command="megacli": Command to run megacli.
# -collector.ntp.server="": NTP server to use for ntp collector.
# -collector.textfile.directory="": Directory to read text files with metrics from.
# -collectors.enabled="diskstats,filesystem,loadavg,meminfo,stat,textfile,time,netdev,netstat": Comma-separated list of collectors to use.
# -collectors.print=false: If true, print available collectors and exit.
# -debug.memprofile-file="": Write memory profile to this file upon receipt of SIGUSR1.
# -log.level=info: Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal, panic].
# -web.listen-address=":9100": Address on which to expose metrics and web interface.
# -web.telemetry-path="/metrics": Path under which to expose metrics.
#!/bin/sh
### BEGIN INIT INFO
# Provides: Node exporter
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Node exporter for prometheus written in Go
### END INIT INFO
DAEMON=/opt/prometheus/node_exporter
NAME=node_exporter
USER=prometheus
PIDFILE=/var/run/prometheus/$NAME.pid
LOGFILE=/var/log/prometheus/$NAME.log
ARGS=""
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
do_start_prepare()
{
mkdir -p `dirname $PIDFILE` || true
mkdir -p `dirname $LOGFILE` || true
chown -R $USER: `dirname $LOGFILE`
chown -R $USER: `dirname $PIDFILE`
}
do_start_cmd()
{
do_start_prepare
echo -n "Starting daemon: "$NAME
start-stop-daemon --chuid $USER -C --background --start --quiet --pidfile $PIDFILE --make-pidfile --exec $DAEMON -- $ARGS >> $LOGFILE 2>&1
echo "."
}
do_stop_cmd()
{
echo -n "Stopping daemon: "$NAME
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
rm $PIDFILE
echo "."
}
uninstall() {
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
local SURE
read SURE
if [ "$SURE" = "yes" ]; then
stop
rm -f "$PIDFILE"
echo "Notice: log file was not removed: '$LOGFILE'" >&2
update-rc.d -f <NAME> remove
rm -fv "$0"
fi
}
status() {
printf "%-50s" "Checking $NAME..."
if [ -f $PIDFILE ]; then
PID=$(cat $PIDFILE)
if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
printf "%s\n" "The process appears to be dead but pidfile still exists"
else
echo "Running, the PID is $PID"
fi
else
printf "%s\n" "Service not running"
fi
}
case "$1" in
start)
do_start_cmd
;;
stop)
do_stop_cmd
;;
status)
status
;;
uninstall)
uninstall
;;
restart)
stop
start
;;
*)
echo "Usage: $1 {start|stop|status|restart|uninstall}"
exit 1
esac
exit 0
#!/bin/sh
### BEGIN INIT INFO
# Provides: Prometheus
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Prometheus - Monitoring system & time series database
### END INIT INFO
WORKING_DIR=/opt/prometheus
DAEMON=$WORKING_DIR/prometheus
NAME=prometheus
USER=prometheus
PIDFILE=/var/run/prometheus/$NAME.pid
LOGFILE=/var/log/prometheus/$NAME.log
ARGS=""
do_start_prepare()
{
mkdir -p `dirname $PIDFILE` || true
mkdir -p `dirname $LOGFILE` || true
chown -R $USER: `dirname $LOGFILE`
chown -R $USER: `dirname $PIDFILE`
}
do_start_cmd()
{
do_start_prepare
echo -n "Starting daemon: "$NAME
start-stop-daemon --chdir $WORKING_DIR --chuid $USER -C --background --start --quiet --pidfile $PIDFILE --make-pidfile --exec $DAEMON -- $ARGS >> $LOGFILE 2>&1
echo "."
}
do_stop_cmd()
{
echo -n "Stopping daemon: "$NAME
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
rm $PIDFILE
echo "."
}
uninstall() {
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
local SURE
read SURE
if [ "$SURE" = "yes" ]; then
stop
rm -f "$PIDFILE"
echo "Notice: log file was not removed: '$LOGFILE'" >&2
update-rc.d -f <NAME> remove
rm -fv "$0"
fi
}
status() {
printf "%-50s" "Checking $NAME..."
if [ -f $PIDFILE ]; then
PID=$(cat $PIDFILE)
if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
printf "%s\n" "The process appears to be dead but pidfile still exists"
else
echo "Running, the PID is $PID"
fi
else
printf "%s\n" "Service not running"
fi
}
case "$1" in
start)
do_start_cmd
;;
stop)
do_stop_cmd
;;
status)
status
;;
uninstall)
uninstall
;;
restart)
stop
start
;;
*)
echo "Usage: $1 {start|stop|status|restart|uninstall}"
exit 1
esac
exit 0
@tmikulin
Copy link

tmikulin commented Jul 22, 2018

Hi,

with: "sudo node_exporter restart" I get

stop: missing job name
Try stop --help' for more information. start: missing job name Try start --help' for more information.

I just changed the case section:
restart)
stop
start

restart)
do_stop_cmd
do_start_cmd

and it works.

@OverWorldD
Copy link

Noob question, sorry. Do I just copy these to /etc/init.d/?
Do I need to change the file extensions or something? Do I need to change the chmod?

@eloo
Copy link
Author

eloo commented Oct 31, 2019

Noob question, sorry. Do I just copy these to /etc/init.d/?
Do I need to change the file extensions or something? Do I need to change the chmod?

normally you would simply move it to /etc/init.d/ and drop the extensions
to enable it chmod +x the file

so it would result in something like this (for the node_exporter script)

-rwxr-xr-x   1 root root 2.1K Dec  7  2016 node_exporter*

@OverWorldD
Copy link

OverWorldD commented Oct 31, 2019

Thank you!

I did this, but I'm getting the following for prometheus(.init.d):

root@marsboard:/etc/init.d# chmod +x prometheus
root@marsboard:/etc/init.d# service prometheus start
chown: invalid spec: 'prometheus:'
chown: invalid spec: 'prometheus:'
Starting daemon: prometheus.
root@marsboard:/etc/init.d#

If I try to restart:
root@marsboard:/etc/init.d# service prometheus restart
/etc/init.d/prometheus: 86: /etc/init.d/prometheus: stop: not found
/etc/init.d/prometheus: 87: /etc/init.d/prometheus: start: not found

my prometheus has the following settings:
-rwxr-xr-x 1 root root 1994 Oct 31 18:59 prometheus

chown: invalid spec: but I'm running as root and the file is root:root...

The service is not starting, neither node_export or prometheus

Both of them work if I run them from /opt/prometheus and /opt/node_exporter respectively with ./prometheus & or /.node_exporter & :
10998 pts/0 00:00:15 prometheus
11052 pts/0 00:00:11 node_exporter
11085 pts/0 00:00:00 ps

What am I missing here?

@OverWorldD
Copy link

OverWorldD commented Oct 31, 2019

Aah, user for the script is prometheus and I'm running as root. changed the script user to root and i didnt get the chown errors...
I still get the start and stop errors though... and the service is not running...

ok, changed the start and stop in restart case to do_start_cmd and do_stop_cmd as @tmikulin suggested so I got rid of the start and stop errors. now prometheus seems to start, but not node_exporter...

@eloo
Copy link
Author

eloo commented Nov 1, 2019

the scripts are only for starting the binary using init.d (autostart)
you still need to download the binary manually and so on.

so you need the prometheus binary in /opt/prometheus as prometheus and the config besides it

@zen
Copy link

zen commented Sep 21, 2020

Current node_exporter does not use single dash ('-') style parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment