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