fix false positive conflink detection
[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, or we
6 # are on battery power, then just runs once.
7
8 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
9
10 source /a/bin/errhandle/err
11 status_file=/dev/shm/iank-status
12
13 shopt -s nullglob
14 shopt -s dotglob
15
16 verbose=false
17 if [[ $1 ]]; then
18 verbose=true
19 fi
20 v() {
21 if $verbose; then
22 printf "%s\n" "$*"
23 fi
24 }
25 lo() { /usr/local/bin/log-once "$@"; }
26
27 write-status() {
28 chars=("${first_chars[@]}")
29
30 glob=(/nocow/btrfs-stale/*)
31 if [[ -e ${glob[0]} ]]; then
32 chars+=("STALE!")
33 fi
34 glob=(/m/md/bounces/new/*)
35 if [[ -e ${glob[0]} ]]; then
36 chars+=("BOUNCE!")
37 bouncemsg="message in /m/md/bounces/new"
38 fi
39 lo -1 bounce $bouncemsg
40 glob=(/m/md/alerts/new/* /m/md/alerts/cur/*)
41 if [[ -e ${glob[0]} ]]; then
42 chars+=("ALERT!")
43 fi
44 if [[ -e /nocow/user/mailtest-failure ]]; then
45 chars+=("MAILPING!")
46 fi
47
48 qlen=$(/usr/sbin/exiqgrep -o 60 -c -b | awk '{print $1}')
49 if ((qlen)); then
50 chars+=("q $qlen")
51 fi
52
53 cd /b/ds
54 if ! make -q ~/.local/distro-begin || [[ $(<~/.local/distro-begin) != 0 ]]; then
55 chars+=("DISTRO-BEGIN!")
56 fi
57
58 if ! make -q ~/.local/distro-end || [[ $(<~/.local/distro-end) != 0 ]]; then
59 chars+=("DISTRO-END!")
60 fi
61
62 f=~/.local/conflink
63 for _ in 1; do
64 if [[ -e $f ]]; then
65 now=$(date +%s)
66 fsec=$(stat -c%Y $f)
67 # the / 60 makes it 0-59 seconds less strict, +1 makes sure we
68 # dont have any false positives.
69 fmin=$(( (fsec - now ) / 60 + 1 ))
70 fminplus=$(( fmin + 60*24 ))
71 # Filesystem files get copied, so find any newer than the last run.
72 # The rest are hueristics:
73 # Given the last time we added a file in git, is that newer than the last conflink run.
74 # Given new files not added to git, were they modified more recently than the last conflink? but,
75 # push their modification time back by a day so we can develop them before needing to add them to git.
76
77 all_dirs=({/a/bin/ds,/p/c}{/filesystem,/machine_specific/$HOSTNAME/filesystem})
78 # This part is copied from conflink
79 for x in /p/c/machine_specific/*.hosts /a/bin/ds/machine_specific/*.hosts; do
80 if grep -qxF $HOSTNAME $x; then all_dirs+=( ${x%.hosts} ); fi
81 done
82
83 # just because i forget a lot, -mmin -NUM means files modified <= NUM minutes ago
84 if (( $(date -d "$(git log --diff-filter=ACR --format=%aD -1)" +%s) > fsec )) || \
85 [[ $(find ${all_dirs[@]} -mmin $fmin -type f -print -quit 2>/dev/null) ]]; then
86 v conflink newer git or newer filesystem files
87 chars+=("CONFLINK!")
88 break
89 fi
90
91 for d in /a/bin/distro-setup /p/c; do
92 cd $d
93 untracked=$(git ls-files -o --exclude-standard)
94 if [[ $untracked && $(find $untracked -mmin $fminplus -type f -print -quit) ]]; then
95 v conflink: untracked in $d
96 chars+=("CONFLINK!")
97 break
98 fi
99 done
100
101 fi
102 if [[ ! -e $f || $(<$f) != 0 ]]; then
103 v conflink: last run not found or failed
104 chars+=("CONFLINK!")
105 break
106 fi
107 done
108
109
110 ## Clean the paniclog, but only up to 4 times per day, or else we
111 ## should investigate.
112 loglog=/tmp/panicloglog-$(date --rfc-3339=date)
113 if [[ -s $loglog ]]; then
114 spamcount=$(stat -c%s $loglog)
115 else
116 spamcount=0
117 fi
118 if (( spamcount <= 4 )); then
119 if grep -q 'spam acl condition' /var/log/exim4/paniclog &>/dev/null; then
120 printf . >>$loglog
121 fi
122 /a/bin/distro-setup/epanic-clean
123 fi
124
125 if [[ -s /var/log/exim4/paniclog ]]; then
126 chars+=("PANIC!")
127 tail -n 20 /var/log/exim4/paniclog | lo -1 paniclog
128 else
129 lo -1 paniclog
130 fi
131
132 source /a/bin/bash_unpublished/source-state
133 if [[ $MAIL_HOST == "$HOSTNAME" ]]; then
134 bbkmsg=
135 if [[ $(systemctl is-active btrbk.timer) != active ]]; then
136 chars+=("BTRBK.TIMER!")
137 bbkmsg="btrbk.timer not enabled"
138 fi
139 lo -60 btrbk.timer $bbkmsg
140
141 ## check if last snapshot was within an hour
142 vol=o
143 # this section generally copied from btrbk scripts, but
144 # this part modified to speed things up by about half a second.
145 # I'm not sure if its quite as reliable, but it looks pretty safe.
146 # Profiled it using time and also adding to the top of the file:
147 # set -x
148 # PS4='+ $(date "+%2N") '
149 snaps=($(ls -1avdr /mnt/root/btrbk/$vol.20*|head -n1))
150 now=$(date +%s)
151 maxtime=0
152 for s in ${snaps[@]}; do
153 file=${s##*/}
154 t=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${file#$vol.}) +%s)
155 if (( t > maxtime )); then
156 maxtime=$t
157 fi
158 done
159 if (( maxtime < now - 60*60 )); then
160 chars+=("OLD-SNAPSHOT!")
161 snapshotmsg="/o snapshot older than 1 hour"
162 fi
163 lo -1 old-snapshot $snapshotmsg
164 fi
165
166 cat /a/bin/bash_unpublished/source-state >$status_file
167
168 if [[ ${chars[*]} ]]; then
169 echo "ps_char=\"${chars[*]} \$ps_char\"" >>$status_file
170 fi
171
172 }
173 # use this if we want to do something just once per minute
174 first_chars=()
175
176 power=true
177 if [[ -e /sys/class/power_supply/AC/online && $(</sys/class/power_supply/AC/online) == 0 ]]; then
178 power=false
179 fi
180
181 write-status
182 if [[ $1 ]]; then
183 cat $status_file
184 exit 0
185 fi
186
187 if ! $power; then
188 exit 0
189 fi
190 for ((i=1; i<=3; i++)); do
191 sleep 15
192 write-status
193 done