+#!/bin/bash
+
+if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
+shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4
+set -eE -o pipefail
+trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR
+
+[[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
+
+
+usage() {
+ cat <<EOF
+Usage: ${0##*/} [-d]
+Install prometheus-node-exporter for debian based system with sysv or systemd
+
+-d Skip downloading and installing the latest binary
+-s Skip setup: config files, user, init scripts.
+-l Listen only on local interface.
+-h|--help Print help and exit.
+
+For downloading, requires curl wget and jq
+
+EOF
+ exit $1
+}
+
+##### begin command line parsing ########
+
+
+# defaults
+dl_bin=true
+do_setup=true
+listen_address=0.0.0.0
+
+while [[ $1 ]]; do
+ case $1 in
+ -d) dl_bin=false ;;
+ -s) do_setup=false ;;
+ -l) listen_address=127.0.0.1 ;;
+ -h|--help) usage 0 ;;
+ esac
+ shift
+done
+readonly dl_bin do_setup
+
+##### end command line parsing ########
+
+if $dl_bin; then
+ for p in curl wget jq; do
+ if ! type -t $p &>/dev/null; then
+ apt-get -y install $p
+ fi
+ done
+fi
+if $do_setup && ! type -t rsync &>/dev/null; then
+ # note: we could use diff or cmp instead.
+ apt-get -y install rsync
+fi
+
+sysd_reload=false
+installed_file=false
+
+i() { # install file
+ local tmp tmpdir dest="$1"
+ local base="${dest##*/}"
+ local dir="${dest%/*}"
+ if [[ $dir != "$base" ]]; then
+ # dest has a directory component
+ mkdir -p "$dir"
+ fi
+ tmpdir=$(mktemp -d)
+ cat >$tmpdir/"$base"
+ tmp=$(rsync -ic $tmpdir/"$base" "$dest")
+ if [[ $tmp ]]; then
+ printf "%s\n" "$tmp"
+ installed_file=true
+ if [[ $dest == /etc/systemd/system/* ]]; then
+ sysd_reload=true
+ fi
+ fi
+ rm -rf $tmpdir
+}
+
+
+if $dl_bin; then
+ if [[ -s /usr/local/src/node-exporter-url ]]; then
+ installed_url=$(cat /usr/local/src/node-exporter-url)
+ fi
+ url=$(curl -s https://api.github.com/repos/prometheus/node_exporter/releases/latest | jq -r '.assets[].browser_download_url | match(".*linux-amd64.tar.gz$").string')
+ if [[ ! $url ]]; then
+ echo $0: error failed to get url
+ exit 1
+ fi
+ if [[ $url != "$installed_url" ]]; then
+ tmpdir=$(mktemp -d)
+ cd $tmpdir
+ f=${url##*/}
+ wget -nv $url
+ tar -xf $f
+ dir=${f%.tar.gz}
+ install $dir/node_exporter /usr/local/bin/prometheus-node-exporter
+ printf "%s\n" "$url" >/usr/local/src/node-exporter-url
+ cd
+ rm -rf $tmpdir
+ fi
+fi
+
+if ! $do_setup; then
+ exit 0
+fi
+
+# taken from postinstall script
+if ! getent passwd prometheus &>/dev/null; then
+ adduser --quiet --system --home /var/lib/prometheus --no-create-home \
+ --group --gecos "Prometheus daemon" prometheus
+fi
+
+
+# textfile collector dir
+mkdir -p /var/lib/prometheus/node-exporter
+
+chown prometheus:prometheus /var/lib/prometheus
+
+
+i /etc/default/prometheus-node-exporter <<EOF
+# Set the command-line arguments to pass to the server.
+# Due to shell scaping, to pass backslashes for regexes, you need to double
+# them (\\d for \d). If running under systemd, you need to double them again
+# (\\\\d to mean \d), and escape newlines too.
+ARGS="--web.listen-address=${listen_address}:9100"
+# to see all possible args, run with --help
+EOF
+
+if [[ -d /etc/systemd/system ]]; then # we are using systemd
+ if [[ -e /lib/systemd/system/prometheus-node-exporter.service ]]; then
+ i /etc/systemd/system/prometheus.service.d/override.conf <<'EOF'
+[Unit]
+# needed to continually restart
+StartLimitIntervalSec=0
+
+[Service]
+Restart=always
+# time to sleep before restarting a service
+RestartSec=600
+
+# empty signifies to replace the existing value
+ExecStart=
+ExecStart=/usr/local/bin/prometheus $ARGS
+EOF
+ else # we dont have the distro package installed
+ i /etc/systemd/system/prometheus.service <<'EOF'
+[Unit]
+Description=Prometheus exporter for machine metrics
+Documentation=https://github.com/prometheus/node_exporter
+
+# addition to the distro package
+StartLimitIntervalSec=0
+
+
+[Service]
+Restart=on-failure
+User=prometheus
+EnvironmentFile=/etc/default/prometheus-node-exporter
+ExecStart=/usr/local/bin/prometheus-node-exporter $ARGS
+ExecReload=/bin/kill -HUP $MAINPID
+TimeoutStopSec=20s
+SendSIGKILL=no
+
+# additions to the distro package
+Restart=always
+RestartSec=600
+
+
+[Install]
+WantedBy=multi-user.target
+EOF
+ fi
+ if $sysd_reload; then
+ systemctl daemon-reload
+ fi
+ systemctl enable prometheus-node-exporter
+ if $installed_file; then
+ systemctl restart prometheus-node-exporter
+ fi
+else # not using systemd
+ i /etc/init.d/prometheus-node-exporter <<'EOF'
+#!/bin/sh
+# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
+if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
+ set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
+fi
+### BEGIN INIT INFO
+# Provides: prometheus-node-exporter
+# Required-Start: $remote_fs
+# Required-Stop: $remote_fs
+# Default-Start: 2 3 4 5
+# Default-Stop: 0 1 6
+# Short-Description: Prometheus exporter for machine metrics
+# Description: Prometheus exporter for machine metrics, written in Go
+# with pluggable metric collectors.
+### END INIT INFO
+
+# Author: Martina Ferrari <tina@debian.org>
+# Author: Guillem Jover <gjover@sipwise.com>
+
+DESC="Prometheus exporter for machine metrics"
+NAME=prometheus-node-exporter
+USER=prometheus
+GROUP=$USER
+DAEMON=/usr/bin/$NAME
+PIDFILE=/run/prometheus/$NAME.pid
+LOGFILE=/var/log/prometheus/$NAME.log
+
+START_ARGS="--no-close --background --make-pidfile"
+STOP_ARGS="--remove-pidfile"
+
+do_start_prepare()
+{
+ mkdir -p $(dirname $PIDFILE)
+}
+
+do_start_cmd_override()
+{
+ start-stop-daemon --start --quiet --oknodo \
+ --exec $DAEMON --pidfile $PIDFILE --user $USER --group $GROUP \
+ --chuid $USER:$GROUP $START_ARGS -- $ARGS >>$LOGFILE 2>&1
+}
+
+do_stop_cmd_override()
+{
+ start-stop-daemon --stop --quiet --oknodo --retry=TERM/30/KILL/5 \
+ --exec $DAEMON --pidfile $PIDFILE --user $USER $STOP_ARGS
+}
+
+alias do_reload=do_reload_sigusr1
+EOF
+ chmod +x /etc/init.d/prometheus-node-exporter
+
+ mkdir -p /var/log/prometheus
+ chown prometheus:prometheus /var/log/prometheus
+ update-rc.d prometheus-node-exporter defaults
+
+ running=false
+ if type -t pgrep &>/dev/null && pgrep -f prometheus-node-exporter &>/dev/null; then
+ running=true
+ fi
+ if $installed_file || ! $running; then
+ /etc/init.d/prometheus-node-exporter restart
+ fi
+
+ i /etc/logrotate.d/prometheus-node-exporter <<'EOF'
+/var/log/prometheus/prometheus-node-exporter.log {
+ weekly
+ rotate 10
+ copytruncate
+ compress
+ delaycompress
+ notifempty
+ missingok
+}
+EOF
+
+fi