3 if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
4 shopt -s inherit_errexit
2>/dev
/null ||
: # ignore fail in bash < 4.4
6 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR
8 [[ $EUID == 0 ]] ||
exec sudo
-E "${BASH_SOURCE[0]}" "$@"
14 Install prometheus-node-exporter for debian based system with sysv or systemd
16 -d Skip downloading and installing the latest binary
17 -s Skip setup: config files, user, init scripts.
18 -l Listen only on local interface.
19 -h|--help Print help and exit.
21 For downloading, requires curl wget and jq
27 ##### begin command line parsing ########
33 listen_address
=0.0.0.0
39 -l) listen_address
=127.0.0.1 ;;
44 readonly dl_bin do_setup
46 ##### end command line parsing ########
49 for p
in curl wget jq
; do
50 if ! type -t $p &>/dev
/null
; then
55 if $do_setup && ! type -t rsync
&>/dev
/null
; then
56 # note: we could use diff or cmp instead.
57 apt-get
-y install rsync
64 local tmp tmpdir dest
="$1"
65 local base
="${dest##*/}"
66 local dir
="${dest%/*}"
67 if [[ $dir != "$base" ]]; then
68 # dest has a directory component
73 tmp
=$
(rsync
-ic $tmpdir/"$base" "$dest")
77 if [[ $dest == /etc
/systemd
/system
/* ]]; then
86 if [[ -s /usr
/local
/src
/node-exporter-url
]]; then
87 installed_url
=$
(cat /usr
/local
/src
/node-exporter-url
)
89 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')
91 echo $0: error failed to get url
94 if [[ $url != "$installed_url" ]]; then
101 install $dir/node_exporter
/usr
/local
/bin
/prometheus-node-exporter
102 printf "%s\n" "$url" >/usr
/local
/src
/node-exporter-url
112 # taken from postinstall script
113 if ! getent passwd prometheus
&>/dev
/null
; then
114 adduser
--quiet --system --home /var
/lib
/prometheus
--no-create-home \
115 --group --gecos "Prometheus daemon" prometheus
119 # textfile collector dir
120 mkdir
-p /var
/lib
/prometheus
/node-exporter
122 chown prometheus
:prometheus
/var
/lib
/prometheus
125 i
/etc
/default
/prometheus-node-exporter
<<EOF
126 # Set the command-line arguments to pass to the server.
127 # Due to shell scaping, to pass backslashes for regexes, you need to double
128 # them (\\d for \d). If running under systemd, you need to double them again
129 # (\\\\d to mean \d), and escape newlines too.
131 # sometime before 1.5 systemd was a default collector
132 ARGS="--web.listen-address=${listen_address}:9100 --collector.textfile.directory=/var/lib/prometheus/node-exporter --collector.systemd"
133 # to see all possible args, run with --help
136 if [[ -d /etc
/systemd
/system
]]; then # we are using systemd
138 # this is just fixing a screwed up state we shouldnt get into normally.
139 if [[ -e /etc
/init.d
/prometheus-node-exporter
]]; then
141 rm -f /etc
/init.d
/prometheus-node-exporter
146 # this logic to handle both cases: The only differences of the
147 # packaged version in t11 is that it creates /var/log/prometheus and a
148 # logrotate, but nothing is logged there. And it depends on
149 # prometheus-node-exporter-collectors, which is useful, but that
150 # package itself depends on prometheus-node-exporter. We have no
151 # reason to install prometheus-node-exporter except that
152 # dependency. We could fix that, but this is easier. So, we keep
153 # handling both cases in case we fix that or maybe run a different
154 # distro that doesn't have it.
155 if [[ -e /lib
/systemd
/system
/prometheus-node-exporter.service
]]; then
157 if [[ -e /etc
/systemd
/system
/prometheus.service
]]; then
158 rm -f /etc
/systemd
/system
/prometheus.service
163 i
/etc
/systemd
/system
/prometheus-node-exporter.service.d
/override.conf
<<'EOF'
165 # needed to continually restart
166 StartLimitIntervalSec=0
170 # time to sleep before restarting a service
173 # empty signifies to replace the existing value
175 ExecStart=/usr/local/bin/prometheus-node-exporter $ARGS
177 else # we dont have the distro package installed
178 i
/etc
/systemd
/system
/prometheus-node-exporter.service
<<'EOF'
180 Description=Prometheus exporter for machine metrics
181 Documentation=https://github.com/prometheus/node_exporter
183 # addition to the distro package
184 StartLimitIntervalSec=0
190 EnvironmentFile=/etc/default/prometheus-node-exporter
191 ExecStart=/usr/local/bin/prometheus-node-exporter $ARGS
192 ExecReload=/bin/kill -HUP $MAINPID
196 # additions to the distro package
202 WantedBy=multi-user.target
205 if $sysd_reload; then
206 systemctl daemon-reload
208 systemctl
enable prometheus-node-exporter
209 if $installed_file; then
210 systemctl restart prometheus-node-exporter
212 else # not using systemd
213 i
/etc
/init.d
/prometheus-node-exporter
<<'EOF'
215 # kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
216 if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
217 set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
220 # Provides: prometheus-node-exporter
221 # Required-Start: $remote_fs
222 # Required-Stop: $remote_fs
223 # Default-Start: 2 3 4 5
224 # Default-Stop: 0 1 6
225 # Short-Description: Prometheus exporter for machine metrics
226 # Description: Prometheus exporter for machine metrics, written in Go
227 # with pluggable metric collectors.
230 # Author: Martina Ferrari <tina@debian.org>
231 # Author: Guillem Jover <gjover@sipwise.com>
233 DESC="Prometheus exporter for machine metrics"
234 NAME=prometheus-node-exporter
237 DAEMON=/usr/bin/$NAME
238 PIDFILE=/run/prometheus/$NAME.pid
239 LOGFILE=/var/log/prometheus/$NAME.log
241 START_ARGS="--no-close --background --make-pidfile"
242 STOP_ARGS="--remove-pidfile"
246 mkdir -p $(dirname $PIDFILE)
249 do_start_cmd_override()
251 start-stop-daemon --start --quiet --oknodo \
252 --exec $DAEMON --pidfile $PIDFILE --user $USER --group $GROUP \
253 --chuid $USER:$GROUP $START_ARGS -- $ARGS >>$LOGFILE 2>&1
256 do_stop_cmd_override()
258 start-stop-daemon --stop --quiet --oknodo --retry=TERM/30/KILL/5 \
259 --exec $DAEMON --pidfile $PIDFILE --user $USER $STOP_ARGS
262 alias do_reload=do_reload_sigusr1
264 chmod +x
/etc
/init.d
/prometheus-node-exporter
266 mkdir
-p /var
/log
/prometheus
267 chown prometheus
:prometheus
/var
/log
/prometheus
268 update-rc.d prometheus-node-exporter defaults
271 if type -t pgrep
&>/dev
/null
&& pgrep
-f prometheus-node-exporter
&>/dev
/null
; then
274 if $installed_file ||
! $running; then
275 /etc
/init.d
/prometheus-node-exporter restart
278 i
/etc
/logrotate.d
/prometheus-node-exporter
<<'EOF'
279 /var/log/prometheus/prometheus-node-exporter.log {