c6f3fe7f5144d3c3661196289cc8cfbb062059ed
[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 source /a/bin/errhandle/err
10 status_file=/dev/shm/iank-status
11
12 shopt -s nullglob
13 shopt -s dotglob
14
15 lo() { /usr/local/bin/log-once "$@"; }
16
17 write-status() {
18 chars=()
19
20 glob=(/nocow/btrfs-stale/*)
21 if [[ -e ${glob[0]} ]]; then
22 chars+=("STALE!")
23 fi
24 glob=(/m/md/bounces/new/*)
25 if [[ -e ${glob[0]} ]]; then
26 chars+=("BOUNCE!")
27 bouncemsg="message in /m/md/bounces/new"
28 fi
29 lo -1 bounce $bouncemsg
30 glob=(/m/md/alerts/new/* /m/md/alerts/cur/*)
31 if [[ -e ${glob[0]} ]]; then
32 chars+=("ALERT!")
33 fi
34 if [[ -e /nocow/user/mailtest-failure ]]; then
35 chars+=("MAILPING!")
36 fi
37
38 ## Clean the paniclog, but only up to 4 times per day, or else we
39 ## should investigate.
40 loglog=/tmp/panicloglog-$(date --rfc-3339=date)
41 if [[ -s $loglog ]]; then
42 spamcount=$(stat -c%s $loglog)
43 else
44 spamcount=0
45 fi
46 if (( spamcount <= 4 )); then
47 if grep -q 'spam acl condition' /var/log/exim4/paniclog; then
48 printf . >>$loglog
49 fi
50 /a/bin/distro-setup/epanic-clean
51 fi
52
53 if [[ -s /var/log/exim4/paniclog ]]; then
54 chars+=("PANIC!")
55 tail -n 20 /var/log/exim4/paniclog | lo -1 paniclog
56 else
57 lo -1 paniclog
58 fi
59
60 source /a/bin/bash_unpublished/source-state
61 if [[ $MAIL_HOST == "$HOSTNAME" ]]; then
62 if [[ $(systemctl is-active btrbk.timer) != active ]]; then
63 chars+=("BTRBK.TIMER!")
64 bbkmsg="btrbk.timer not enabled"
65 fi
66 lo -60 btrbk.timer $bbkmsg
67
68 ## check if last snapshot was within an hour
69 vol=o
70 snaps=(/mnt/root/btrbk/$vol.20*)
71 now=$(date +%s)
72 maxtime=0
73 for s in ${snaps[@]}; do
74 file=${s##*/}
75 t=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${file#$vol.}) +%s)
76 if (( t > maxtime )); then
77 maxtime=$t
78 fi
79 done
80 if (( maxtime < now - 60*60 )); then
81 chars+=("OLD-SNAPSHOT!")
82 snapshotmsg="/o snapshot older than 1 hour"
83 fi
84 lo -1 old-snapshot $snapshotmsg
85 fi
86
87 cat /a/bin/bash_unpublished/source-state >$status_file
88
89 if [[ ${chars[*]} ]]; then
90 echo "ps_char=\"${chars[*]} \$ps_char\"" >>$status_file
91 fi
92
93 }
94 write-status
95 if [[ $1 ]]; then
96 cat $status_file
97 exit 0
98 fi
99 for ((i=1; i<=3; i++)); do
100 sleep 15
101 write-status
102 done