consistent license, various updates
[buildscripts] / prom-node-exporter
1 #!/bin/bash
2
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
5 set -eE -o pipefail
6 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR
7
8 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
9
10
11 usage() {
12 cat <<EOF
13 Usage: ${0##*/} [-d]
14 Install prometheus-node-exporter for debian based system with sysv or systemd
15
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.
20
21 For downloading, requires curl wget and jq
22
23 EOF
24 exit $1
25 }
26
27 ##### begin command line parsing ########
28
29
30 # defaults
31 dl_bin=true
32 do_setup=true
33 listen_address=0.0.0.0
34
35 while [[ $1 ]]; do
36 case $1 in
37 -d) dl_bin=false ;;
38 -s) do_setup=false ;;
39 -l) listen_address=127.0.0.1 ;;
40 -h|--help) usage 0 ;;
41 esac
42 shift
43 done
44 readonly dl_bin do_setup
45
46 ##### end command line parsing ########
47
48 if $dl_bin; then
49 for p in curl wget jq; do
50 if ! type -t $p &>/dev/null; then
51 apt-get -y install $p
52 fi
53 done
54 fi
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
58 fi
59
60 sysd_reload=false
61 installed_file=false
62
63 i() { # install file
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
69 mkdir -p "$dir"
70 fi
71 tmpdir=$(mktemp -d)
72 cat >$tmpdir/"$base"
73 tmp=$(rsync -ic $tmpdir/"$base" "$dest")
74 if [[ $tmp ]]; then
75 printf "%s\n" "$tmp"
76 installed_file=true
77 if [[ $dest == /etc/systemd/system/* ]]; then
78 sysd_reload=true
79 fi
80 fi
81 rm -rf $tmpdir
82 }
83
84
85 if $dl_bin; then
86 if [[ -s /usr/local/src/node-exporter-url ]]; then
87 installed_url=$(cat /usr/local/src/node-exporter-url)
88 fi
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')
90 if [[ ! $url ]]; then
91 echo $0: error failed to get url
92 exit 1
93 fi
94 if [[ $url != "$installed_url" ]]; then
95 tmpdir=$(mktemp -d)
96 cd $tmpdir
97 f=${url##*/}
98 wget -nv $url
99 tar -xf $f
100 dir=${f%.tar.gz}
101 install $dir/node_exporter /usr/local/bin/prometheus-node-exporter
102 printf "%s\n" "$url" >/usr/local/src/node-exporter-url
103 cd
104 rm -rf $tmpdir
105 fi
106 fi
107
108 if ! $do_setup; then
109 exit 0
110 fi
111
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
116 fi
117
118
119 # textfile collector dir
120 mkdir -p /var/lib/prometheus/node-exporter
121
122 chown prometheus:prometheus /var/lib/prometheus
123
124
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
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
134 EOF
135
136 if [[ -d /etc/systemd/system ]]; then # we are using systemd
137
138 # this is just fixing a screwed up state we shouldnt get into normally.
139 if [[ -e /etc/init.d/prometheus-node-exporter ]]; then
140 sysd_reload=true
141 rm -f /etc/init.d/prometheus-node-exporter
142 fi
143
144
145 # I used to have logic to handle having the distro package installed, which would then just override it's service file in some ways. The only differences of the
146 # packaged version in t11 is that it creates /var/log/prometheus and a
147 # logrotate, but nothing is logged there. And it depends on
148 # prometheus-node-exporter-collectors, which is useful, but
149 # depends on prometheus-node-exporter. We would have no
150 # reason to install prometheus-node-exporter except that
151 # dependency. But since the collectors package is just a few
152 # small scripts we don't even currently use, and of course are not
153 # there on old versions, lets not try to use that package, and
154 # just manually install the files we want from upstream.
155 # https://github.com/prometheus-community/node-exporter-textfile-collector-scripts
156
157 if [[ -e /lib/systemd/system/prometheus-node-exporter.service ]]; then
158 apt -y remove --purge prometheus-node-exporter
159 rm -fv /lib/systemd/system/prometheus-node-exporter.service
160 fi
161
162 i /etc/systemd/system/prometheus-node-exporter.service <<'EOF'
163 [Unit]
164 Description=Prometheus exporter for machine metrics
165 Documentation=https://github.com/prometheus/node_exporter
166
167 # addition to the distro package
168 StartLimitIntervalSec=0
169
170
171 [Service]
172 Restart=on-failure
173 User=prometheus
174 EnvironmentFile=/etc/default/prometheus-node-exporter
175 ExecStart=/usr/local/bin/prometheus-node-exporter $ARGS
176 ExecReload=/bin/kill -HUP $MAINPID
177 TimeoutStopSec=20s
178 SendSIGKILL=no
179
180 # additions to the distro package
181 Restart=always
182 RestartSec=600
183
184
185 [Install]
186 WantedBy=multi-user.target
187 EOF
188 if $sysd_reload; then
189 systemctl daemon-reload
190 fi
191 systemctl enable prometheus-node-exporter
192 if $installed_file; then
193 systemctl restart prometheus-node-exporter
194 fi
195 else # not using systemd
196 i /etc/init.d/prometheus-node-exporter <<'EOF'
197 #!/bin/sh
198 # kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
199 if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
200 set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
201 fi
202 ### BEGIN INIT INFO
203 # Provides: prometheus-node-exporter
204 # Required-Start: $remote_fs
205 # Required-Stop: $remote_fs
206 # Default-Start: 2 3 4 5
207 # Default-Stop: 0 1 6
208 # Short-Description: Prometheus exporter for machine metrics
209 # Description: Prometheus exporter for machine metrics, written in Go
210 # with pluggable metric collectors.
211 ### END INIT INFO
212
213 # Author: Martina Ferrari <tina@debian.org>
214 # Author: Guillem Jover <gjover@sipwise.com>
215
216 DESC="Prometheus exporter for machine metrics"
217 NAME=prometheus-node-exporter
218 USER=prometheus
219 GROUP=$USER
220 DAEMON=/usr/bin/$NAME
221 PIDFILE=/run/prometheus/$NAME.pid
222 LOGFILE=/var/log/prometheus/$NAME.log
223
224 START_ARGS="--no-close --background --make-pidfile"
225 STOP_ARGS="--remove-pidfile"
226
227 do_start_prepare()
228 {
229 mkdir -p $(dirname $PIDFILE)
230 }
231
232 do_start_cmd_override()
233 {
234 start-stop-daemon --start --quiet --oknodo \
235 --exec $DAEMON --pidfile $PIDFILE --user $USER --group $GROUP \
236 --chuid $USER:$GROUP $START_ARGS -- $ARGS >>$LOGFILE 2>&1
237 }
238
239 do_stop_cmd_override()
240 {
241 start-stop-daemon --stop --quiet --oknodo --retry=TERM/30/KILL/5 \
242 --exec $DAEMON --pidfile $PIDFILE --user $USER $STOP_ARGS
243 }
244
245 alias do_reload=do_reload_sigusr1
246 EOF
247 chmod +x /etc/init.d/prometheus-node-exporter
248
249 mkdir -p /var/log/prometheus
250 chown prometheus:prometheus /var/log/prometheus
251 update-rc.d prometheus-node-exporter defaults
252
253 running=false
254 if type -t pgrep &>/dev/null && pgrep -f prometheus-node-exporter &>/dev/null; then
255 running=true
256 fi
257 if $installed_file || ! $running; then
258 /etc/init.d/prometheus-node-exporter restart
259 fi
260
261 i /etc/logrotate.d/prometheus-node-exporter <<'EOF'
262 /var/log/prometheus/prometheus-node-exporter.log {
263 weekly
264 rotate 10
265 copytruncate
266 compress
267 delaycompress
268 notifempty
269 missingok
270 }
271 EOF
272
273 fi