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.
130 ARGS="--web.listen-address=${listen_address}:9100 --collector.textfile.directory=/var/lib/prometheus/node-exporter"
131 # to see all possible args, run with --help
134 if [[ -d /etc
/systemd
/system
]]; then # we are using systemd
136 # this is just fixing a screwed up state we shouldnt get into normally.
137 if [[ -e /etc
/init.d
/prometheus-node-exporter
]]; then
139 rm -f /etc
/init.d
/prometheus-node-exporter
144 # this logic to handle both cases: The only differences of the
145 # packaged version in t11 is that it creates /var/log/prometheus and a
146 # logrotate, but nothing is logged there. And it depends on
147 # prometheus-node-exporter-collectors, which is useful, but that
148 # package itself depends on prometheus-node-exporter. We have no
149 # reason to install prometheus-node-exporter except that
150 # dependency. We could fix that, but this is easier. So, we keep
151 # handling both cases in case we fix that or maybe run a different
152 # distro that doesn't have it.
153 if [[ -e /lib
/systemd
/system
/prometheus-node-exporter.service
]]; then
155 if [[ -e /etc
/systemd
/system
/prometheus.service
]]; then
156 rm -f /etc
/systemd
/system
/prometheus.service
161 i
/etc
/systemd
/system
/prometheus.service.d
/override.conf
<<'EOF'
163 # needed to continually restart
164 StartLimitIntervalSec=0
168 # time to sleep before restarting a service
171 # empty signifies to replace the existing value
173 ExecStart=/usr/local/bin/prometheus $ARGS
175 else # we dont have the distro package installed
176 i
/etc
/systemd
/system
/prometheus.service
<<'EOF'
178 Description=Prometheus exporter for machine metrics
179 Documentation=https://github.com/prometheus/node_exporter
181 # addition to the distro package
182 StartLimitIntervalSec=0
188 EnvironmentFile=/etc/default/prometheus-node-exporter
189 ExecStart=/usr/local/bin/prometheus-node-exporter $ARGS
190 ExecReload=/bin/kill -HUP $MAINPID
194 # additions to the distro package
200 WantedBy=multi-user.target
203 if $sysd_reload; then
204 systemctl daemon-reload
206 systemctl
enable prometheus-node-exporter
207 if $installed_file; then
208 systemctl restart prometheus-node-exporter
210 else # not using systemd
211 i
/etc
/init.d
/prometheus-node-exporter
<<'EOF'
213 # kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
214 if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
215 set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
218 # Provides: prometheus-node-exporter
219 # Required-Start: $remote_fs
220 # Required-Stop: $remote_fs
221 # Default-Start: 2 3 4 5
222 # Default-Stop: 0 1 6
223 # Short-Description: Prometheus exporter for machine metrics
224 # Description: Prometheus exporter for machine metrics, written in Go
225 # with pluggable metric collectors.
228 # Author: Martina Ferrari <tina@debian.org>
229 # Author: Guillem Jover <gjover@sipwise.com>
231 DESC="Prometheus exporter for machine metrics"
232 NAME=prometheus-node-exporter
235 DAEMON=/usr/bin/$NAME
236 PIDFILE=/run/prometheus/$NAME.pid
237 LOGFILE=/var/log/prometheus/$NAME.log
239 START_ARGS="--no-close --background --make-pidfile"
240 STOP_ARGS="--remove-pidfile"
244 mkdir -p $(dirname $PIDFILE)
247 do_start_cmd_override()
249 start-stop-daemon --start --quiet --oknodo \
250 --exec $DAEMON --pidfile $PIDFILE --user $USER --group $GROUP \
251 --chuid $USER:$GROUP $START_ARGS -- $ARGS >>$LOGFILE 2>&1
254 do_stop_cmd_override()
256 start-stop-daemon --stop --quiet --oknodo --retry=TERM/30/KILL/5 \
257 --exec $DAEMON --pidfile $PIDFILE --user $USER $STOP_ARGS
260 alias do_reload=do_reload_sigusr1
262 chmod +x
/etc
/init.d
/prometheus-node-exporter
264 mkdir
-p /var
/log
/prometheus
265 chown prometheus
:prometheus
/var
/log
/prometheus
266 update-rc.d prometheus-node-exporter defaults
269 if type -t pgrep
&>/dev
/null
&& pgrep
-f prometheus-node-exporter
&>/dev
/null
; then
272 if $installed_file ||
! $running; then
273 /etc
/init.d
/prometheus-node-exporter restart
276 i
/etc
/logrotate.d
/prometheus-node-exporter
<<'EOF'
277 /var/log/prometheus/prometheus-node-exporter.log {