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