0781ff4cb6d99eff36351d8968ff5c06c54ff4a1
[log-quiet] / sysd-prom-fail
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
17 # requires 1.4.0 to run with multiple units due to bug before that:
18 # https://github.com/prometheus/node_exporter/pull/2475
19
20 set -eE -o pipefail
21 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
22
23 if [[ ! $SERVICE_RESULT ]]; then
24 echo "$0: error, no SERVICE_RESULT var."
25 exit 1
26 fi
27
28 ser_name="$1"
29
30 if [[ ! $ser_name ]]; then
31 echo "$0: error, no arg service name passed."
32 exit 1
33 fi
34
35 dir=/var/lib/prometheus/node-exporter
36 if [[ ! -e $dir ]]; then
37 exit 0
38 fi
39
40 f=$dir/${ser_name}-result.prom
41 ftmp=$f.$$
42
43
44 write_count=false
45 if [[ -s $f ]]; then
46 # https://www.freedesktop.org/software/systemd/man/systemd.exec.html
47 if [[ $SERVICE_RESULT != success ]]; then
48 write_count=true
49 read -r _ count <$f
50 case $count in
51 ''|*[!0-9]*)
52 count=0
53 ;;
54 *)
55 count=$(( count + 1 ))
56 ;;
57 esac
58 fi
59 else
60 count=0
61 write_count=true
62 fi
63
64 if $write_count; then
65 printf 'node_systemd_unit_result_fail_count{name="%s"} %s\n' "$ser_name" "$count" >$ftmp
66 mv $ftmp $f
67 fi