61fe700a6e6b5b29d623f14a6be816a916db9d53
[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 #why have
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
156
157 if [[ -e /etc/systemd/system/prometheus.service ]]; then
158 rm -f /etc/systemd/system/prometheus.service
159 sysd_reload=true
160 fi
161
162
163 i /etc/systemd/system/prometheus-node-exporter.service.d/override.conf <<'EOF'
164 [Unit]
165 # needed to continually restart
166 StartLimitIntervalSec=0
167
168 [Service]
169 Restart=always
170 # time to sleep before restarting a service
171 RestartSec=600
172
173 # empty signifies to replace the existing value
174 ExecStart=
175 ExecStart=/usr/local/bin/prometheus-node-exporter $ARGS
176 EOF
177 else # we dont have the distro package installed
178 i /etc/systemd/system/prometheus-node-exporter.service <<'EOF'
179 [Unit]
180 Description=Prometheus exporter for machine metrics
181 Documentation=https://github.com/prometheus/node_exporter
182
183 # addition to the distro package
184 StartLimitIntervalSec=0
185
186
187 [Service]
188 Restart=on-failure
189 User=prometheus
190 EnvironmentFile=/etc/default/prometheus-node-exporter
191 ExecStart=/usr/local/bin/prometheus-node-exporter $ARGS
192 ExecReload=/bin/kill -HUP $MAINPID
193 TimeoutStopSec=20s
194 SendSIGKILL=no
195
196 # additions to the distro package
197 Restart=always
198 RestartSec=600
199
200
201 [Install]
202 WantedBy=multi-user.target
203 EOF
204 fi
205 if $sysd_reload; then
206 systemctl daemon-reload
207 fi
208 systemctl enable prometheus-node-exporter
209 if $installed_file; then
210 systemctl restart prometheus-node-exporter
211 fi
212 else # not using systemd
213 i /etc/init.d/prometheus-node-exporter <<'EOF'
214 #!/bin/sh
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
218 fi
219 ### BEGIN INIT INFO
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.
228 ### END INIT INFO
229
230 # Author: Martina Ferrari <tina@debian.org>
231 # Author: Guillem Jover <gjover@sipwise.com>
232
233 DESC="Prometheus exporter for machine metrics"
234 NAME=prometheus-node-exporter
235 USER=prometheus
236 GROUP=$USER
237 DAEMON=/usr/bin/$NAME
238 PIDFILE=/run/prometheus/$NAME.pid
239 LOGFILE=/var/log/prometheus/$NAME.log
240
241 START_ARGS="--no-close --background --make-pidfile"
242 STOP_ARGS="--remove-pidfile"
243
244 do_start_prepare()
245 {
246 mkdir -p $(dirname $PIDFILE)
247 }
248
249 do_start_cmd_override()
250 {
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
254 }
255
256 do_stop_cmd_override()
257 {
258 start-stop-daemon --stop --quiet --oknodo --retry=TERM/30/KILL/5 \
259 --exec $DAEMON --pidfile $PIDFILE --user $USER $STOP_ARGS
260 }
261
262 alias do_reload=do_reload_sigusr1
263 EOF
264 chmod +x /etc/init.d/prometheus-node-exporter
265
266 mkdir -p /var/log/prometheus
267 chown prometheus:prometheus /var/log/prometheus
268 update-rc.d prometheus-node-exporter defaults
269
270 running=false
271 if type -t pgrep &>/dev/null && pgrep -f prometheus-node-exporter &>/dev/null; then
272 running=true
273 fi
274 if $installed_file || ! $running; then
275 /etc/init.d/prometheus-node-exporter restart
276 fi
277
278 i /etc/logrotate.d/prometheus-node-exporter <<'EOF'
279 /var/log/prometheus/prometheus-node-exporter.log {
280 weekly
281 rotate 10
282 copytruncate
283 compress
284 delaycompress
285 notifempty
286 missingok
287 }
288 EOF
289
290 fi