mainly changes to keep systems up to date
[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
39 if ! make -q ~/.local/distro-begin || [[ $(<~/.local/distro-begin) != 0 ]]; then
40 chars+=("DISTRO-BEGIN!")
41 fi
42
43 if ! make -q ~/.local/distro-end || [[ $(<~/.local/distro-end) != 0 ]]; then
44 chars+=("DISTRO-END!")
45 fi
46
47 f=~/.local/conflink
48 if [[ -e $f ]]; then
49 cd /b/ds
50 now=$(date +%s)
51 fsec=$(stat -c%Y $f)
52 fmin=$(( (fsec - now ) / 60 + 1 ))
53 fminplus=$(( fmin + 60*24 ))
54 # Filesystem files get copied, so find any newer than the last run.
55 # The rest are hueristics:
56 # Given the last time we added a file in git, is that newer than the last conflink run.
57 # Given new files not added to git, were they modified more recently than the last conflink? but,
58 # push their modification time back by a day so we can develop them before needing to add them to git.
59 if (( $(date -d "$(git log --diff-filter=ACR --format=%aD -1)" +%s) > fsec )) || \
60 [[ $(find {/a/bin/ds,/p/c}{/filesystem,/machine_specific/$HOSTNAME/filesystem} -mmin $fmin -type f -print -quit 2>/dev/null) ]] \
61 || [[ $(find $(git ls-files -o --exclude-standard) -mmin $fminplus -type f -print -quit) ]]; then
62 chars+=("CONFLINK!")
63 fi
64 fi
65
66 if [[ ! -e $f || $(<$f) != 0 ]]; then
67 chars+=("CONFLINK!")
68 fi
69
70
71 ## Clean the paniclog, but only up to 4 times per day, or else we
72 ## should investigate.
73 loglog=/tmp/panicloglog-$(date --rfc-3339=date)
74 if [[ -s $loglog ]]; then
75 spamcount=$(stat -c%s $loglog)
76 else
77 spamcount=0
78 fi
79 if (( spamcount <= 4 )); then
80 if grep -q 'spam acl condition' /var/log/exim4/paniclog; then
81 printf . >>$loglog
82 fi
83 /a/bin/distro-setup/epanic-clean
84 fi
85
86 if [[ -s /var/log/exim4/paniclog ]]; then
87 chars+=("PANIC!")
88 tail -n 20 /var/log/exim4/paniclog | lo -1 paniclog
89 else
90 lo -1 paniclog
91 fi
92
93 source /a/bin/bash_unpublished/source-state
94 if [[ $MAIL_HOST == "$HOSTNAME" ]]; then
95 if [[ $(systemctl is-active btrbk.timer) != active ]]; then
96 chars+=("BTRBK.TIMER!")
97 bbkmsg="btrbk.timer not enabled"
98 fi
99 lo -60 btrbk.timer $bbkmsg
100
101 ## check if last snapshot was within an hour
102 vol=o
103 snaps=(/mnt/root/btrbk/$vol.20*)
104 now=$(date +%s)
105 maxtime=0
106 for s in ${snaps[@]}; do
107 file=${s##*/}
108 t=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${file#$vol.}) +%s)
109 if (( t > maxtime )); then
110 maxtime=$t
111 fi
112 done
113 if (( maxtime < now - 60*60 )); then
114 chars+=("OLD-SNAPSHOT!")
115 snapshotmsg="/o snapshot older than 1 hour"
116 fi
117 lo -1 old-snapshot $snapshotmsg
118 fi
119
120 cat /a/bin/bash_unpublished/source-state >$status_file
121
122 if [[ ${chars[*]} ]]; then
123 echo "ps_char=\"${chars[*]} \$ps_char\"" >>$status_file
124 fi
125
126 }
127 write-status
128 if [[ $1 ]]; then
129 cat $status_file
130 exit 0
131 fi
132 for ((i=1; i<=3; i++)); do
133 sleep 15
134 write-status
135 done