improve alerts
[distro-setup] / system-status
1 #!/bin/bash
2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4
5 # usage: runs 4 times every 15 seconds unless any args are passed, then just runs once
6
7 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
8
9 set -eE -o pipefail
10 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
11
12 status_file=/dev/shm/iank-status
13
14 shopt -s nullglob
15 shopt -s dotglob
16
17 lo() { /usr/local/bin/log-once -1 "$@"; }
18
19 write-status() {
20 chars=()
21
22 glob=(/nocow/btrfs-stale/*)
23 if [[ -e $glob ]] ; then
24 chars+=("STALE!")
25 fi
26 glob=(/m/md/bounces/new/*)
27 if [[ -e $glob ]]; then
28 chars+=("BOUNCE!")
29 lo bounce "message in /m/md/bounces/new"
30 fi
31 glob=(/m/md/alerts/new/* /m/md/alerts/cur/*)
32 if [[ -e $glob ]]; then
33 chars+=("ALERT!")
34 fi
35 if [[ -e /nocow/user/mailtest-failure ]]; then
36 chars+=("MAILPING!")
37 lo mailping "mailtest-check didnt see mail from send-test-forward"
38 fi
39 if [[ -s /var/log/exim4/paniclog ]]; then
40 chars+=("PANIC!")
41 tail /var/log/exim4/paniclog | lo paniclog
42 fi
43
44 source /a/bin/bash_unpublished/source-state
45 if [[ $MAIL_HOST == $HOSTNAME ]]; then
46 if [[ $(systemctl is-active btrbk.timer) != active ]]; then
47 chars+=("BTRBK-TIMER!")
48 fi
49
50 ## check if last snapshot was within an hour
51 vol=o
52 snaps=(/mnt/root/btrbk/$vol.20*)
53 now=$(date +%s)
54 maxtime=0
55 for s in ${snaps[@]}; do
56 file=${s##*/}
57 t=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${file#$vol.}) +%s)
58 if (( t > maxtime )); then
59 maxtime=$t
60 fi
61 done
62 if (( maxtime < now - 60*60 )); then
63 chars+=("OLD-SNAPSHOT!")
64 fi
65 fi
66
67 cat /a/bin/bash_unpublished/source-state >$status_file
68
69 if [[ $chars ]]; then
70 echo "ps_char=\"${chars[*]} \$ps_char\"" >>$status_file
71 fi
72
73 }
74 write-status
75 if [[ $@ ]]; then
76 cat $status_file
77 exit 0
78 fi
79 for ((i=1; i<=3; i++)); do
80 sleep 15
81 write-status
82 done