f25510ef094ce28df9d5cba7e0633bad961aac32
[log-quiet] / sysd-prom-fail-install
1 #!/bin/bash
2 # Copyright (C) 2022 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # usage: since this is a counter, in prometheus alert manager you will
17 # need to test something like: rate(node_systemd_unit_result_fail_count[30m]) > 0
18
19 source /a/bin/bash-bear-trap/bash-bear
20 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
21
22 ser_name=$1
23
24 mkdir -p /var/lib/prometheus/node-exporter
25
26 f=/var/local/sysd-prom-fail/${ser_name}-result.prom
27
28
29 if [[ ! -s $f ]]; then
30 SERVICE_RESULT=success /usr/local/bin/sysd-prom-fail $ser_name
31 fi
32
33 tmp=$(mktemp)
34
35 cat >>$tmp <<EOF
36 [Service]
37 ExecStopPost=+/usr/local/bin/sysd-prom-fail $ser_name
38 EOF
39
40 dir=/etc/systemd/system/$ser_name.service.d
41 mkdir -p $dir
42 out=$(rsync -cipgo --chmod=644 --chown=root:root $tmp $dir/sysd-prom-fail.conf)
43 if [[ $out ]]; then
44 echo $0: systemctl daemon-reload
45 systemctl daemon-reload
46 if systemctl is-active $ser_name &>/dev/null; then
47 echo $0: systemctl restart $ser_name
48 systemctl restart $ser_name ||:
49 fi
50 fi
51
52 # todo: make uninstall script