Skip to content

Instantly share code, notes, and snippets.

@dubcl
Last active April 3, 2023 22:27
Show Gist options
  • Save dubcl/2343c60adae019f3d07139bce06ee060 to your computer and use it in GitHub Desktop.
Save dubcl/2343c60adae019f3d07139bce06ee060 to your computer and use it in GitHub Desktop.
node_exporter systemd config

Download https://github.com/prometheus/node_exporter/releases

cd /opt
wget https://url-to-release
tar zxf foo-release.tar.gz
mv foo-release prometheus
useradd --no-create-home --shell /bin/false prometheus
chown -Rf prometheus.prometheus prometheus/

Create/Edit /etc/systemd/system/node_exporter.service

[Unit]
Description=Node Exporter
StartLimitIntervalSec=500
StartLimitBurst=5

[Service]
User=prometheus
Group=prometheus
EnvironmentFile=/etc/default/node_exporter
ExecStart=/opt/prometheus/node_exporter $OPTIONS
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

Create/Edit /etc/default/node_exporter

OPTIONS='--collector.diskstats.device-exclude="^(ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$" --collector.systemd'

Reload daemons

systemctl daemon-reload

Start node_exporter

systemctl start node_exporter

Verify execution

systemctl status node_exporter

If OK, enable for autostart

systemctl enable node_exporter

Maybe work with other exporters.

for init.d

#!/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

OPTIONS=""
[ -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 --background --start --quiet --pidfile $PIDFILE --make-pidfile --exec $DAEMON -- $OPTIONS >> $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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment