misc fixes and improvements
[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 shopt -s extglob
16
17 for p in ~/.gem/ruby/*/bin; do
18 PATH="$PATH:$p"
19 done
20
21
22 verbose=false
23 if [[ $1 ]]; then
24 verbose=true
25 fi
26 v() {
27 if $verbose; then
28 printf "%s\n" "$*"
29 fi
30 }
31 # log-once COUNT NAME [MESSAGE]
32 lo() {
33 /usr/local/bin/log-once "$@" | ifne mail -s "$HOSTNAME: system-status $2" root@localhost
34 }
35
36 write-status() {
37 chars=("${first_chars[@]}")
38
39 # clock us out in timetrap if are idle too long
40 if [[ -e /p/.timetrap.db ]]; then
41 export DISPLAY=:0
42 if type -p xprintidle &>/dev/null && xidle=$(xprintidle 2>/dev/null); then
43 if [[ $xidle == [0-9]* ]]; then
44 sheet=$(sqlite3 /p/.timetrap.db "select sheet from entries where end is NULL;")
45 idle=300000
46 if [[ $sheet == w ]]; then
47 idle=900000
48 fi
49 if [[ $sheet && $xidle -gt $idle ]]; then
50 timetrap out
51 fi
52 fi
53 fi
54 fi
55
56
57 if ip l show tunfsf &>/dev/null; then
58 # this is for tracking dns over tls issue, which
59 # fixvpndns() in brc2 fixes.
60 stat=$(resolvectl dnsovertls tunfsf 2>/dev/null ||: )
61 read _ _ _ istls <<<"$stat"
62 case $istls in
63 no) : ;;
64 *)
65 printf "%s\n" "$istls" | ts >> /tmp/istls.log
66 chars+=("T:$istls")
67 ;;
68 esac
69 fi
70
71
72 if pgrep -G iank -u iank -f 'emacs --daemon' &>/dev/null; then
73 emacsfiles="$(emacsclient --eval "$(cat /usr/local/bin/unsaved-buffers.el)"| sed '/^"nil"$/d;s/^"(/E: /;s/)"$//')"
74 if [[ $emacsfiles ]]; then
75 chars+=("$emacsfiles")
76 fi
77 fi
78
79 glob=(/nocow/btrfs-stale/*)
80 if [[ -e ${glob[0]} ]]; then
81 chars+=("STALE")
82 fi
83 if [[ $(find /var/mail -type f \! -empty -print -quit) ]]; then
84 var_mail_msg="message in /var/mail"
85 fi
86 lo -1 var_mail $var_mail_msg
87 glob=(/m/md/bounces/new/*)
88 if [[ -e ${glob[0]} ]]; then
89 chars+=("BOUNCE")
90 bouncemsg="message in /m/md/bounces/new"
91 fi
92 lo -1 bounce $bouncemsg
93 # emails without the S (seen) flag. this only checks the last flag,
94 # but its good enough for me.
95 glob=(/m/md/alerts/{new,cur}/!(*,S))
96 if [[ -e ${glob[0]} ]]; then
97 chars+=("A")
98 fi
99 tmp=(/var/local/cron-errors/mailtest-check*)
100 if (( ${#tmp[@]} )); then
101 chars+=("MAILPING")
102 fi
103 tmp=(/var/local/cron-errors/mailtest-slow*)
104 if (( ${#tmp[@]} )); then
105 chars+=("SPAMD")
106 fi
107
108 # early in install process, we dont have permission yet for exiqgrep
109 qlen=$(/usr/sbin/exiqgrep -o 600 -c -b | awk '{print $1}') ||:
110 if ((qlen)); then
111 qmsg="queue length $qlen"
112 chars+=("q $qlen")
113 fi
114 case $HOSTNAME in
115 # No point in emailing about the mailq on a host where we don't
116 # check email.
117 $MAIL_HOST|bk)
118 lo -120 qlen $qmsg
119 ;;
120 esac
121
122 begin=false
123 if ! make -C /b/ds -q ~/.local/distro-begin || [[ $(<~/.local/distro-begin) != 0 ]]; then
124 begin=true
125 fi
126
127 end=false
128 if ! make -C /b/ds -q ~/.local/distro-end || [[ $(<~/.local/distro-end) != 0 ]]; then
129 end=true
130 fi
131
132 # these conditions are so we dont have an overly verbose prompt
133 if $begin && $end; then
134 chars+=("D")
135 elif $begin; then
136 chars+=("DB")
137 elif $end; then
138 chars+=("DE")
139 else
140 f=~/.local/conflink
141 # shellcheck disable=SC2043
142 for _ in 1; do
143 if [[ -e $f ]]; then
144 now=$(date +%s)
145 fsec=$(stat -c%Y $f)
146 # the / 60 makes it 0-59 seconds less strict, +1 to help make sure we
147 # dont have any false positives.
148 fmin=$(( (fsec - now + 1 ) / 60 ))
149 fminplus=$(( fmin + 60*24 ))
150 # Filesystem files get copied, so find any newer than the last run.
151 # The rest are hueristics:
152 # Given the last time we added a file in git, is that newer than the last conflink run.
153 # Given new files not added to git, were they modified more recently than the last conflink? but,
154 # push their modification time back by a day so we can develop them before needing to add them to git.
155
156 all_dirs=({/a/bin/ds,/p/c}{/filesystem,/machine_specific/$HOSTNAME/filesystem})
157 # This part is copied from conflink
158 for x in /p/c/machine_specific/*.hosts /a/bin/ds/machine_specific/*.hosts; do
159 if grep -qxF $HOSTNAME $x; then all_dirs+=( ${x%.hosts} ); fi
160 done
161
162 # Just because i forget a lot, -mmin -NUM means files modified <= NUM minutes ago
163 if (( fmin < 0 )) && [[ $(find ${all_dirs[@]} -mmin $fmin -type f -print -quit 2>/dev/null) ]]; then
164 v conflink newer filesystem files
165 chars+=("CONFLINK")
166 break
167 fi
168
169 for d in /a/bin/distro-setup /p/c; do
170 [[ -d $d ]] || continue
171 cd $d
172 if [[ ! -e .git ]]; then
173 # some hosts i dont push all of /p/c
174 continue
175 fi
176 if (( $(date -d "$(git log --diff-filter=ACR --format=%aD -1)" +%s) > fsec )); then
177 v conflink: newer files checked in to git
178 chars+=("CONFLINK")
179 break
180 fi
181
182 untracked=()
183 while read -r l; do
184 untracked+=("$l")
185 done < <(git ls-files -o --exclude-standard)
186 if [[ ${untracked[0]} && $(find "${untracked[@]}" -mmin $fminplus -type f -print -quit) ]]; then
187 v conflink: untracked in $d
188 chars+=("CONFLINK")
189 break
190 fi
191 done
192 cd /
193
194 fi
195 if [[ ! -e $f || $(<$f) != 0 ]]; then
196 v conflink: last run not found or failed
197 chars+=("CONFLINK")
198 break
199 fi
200 done
201 fi
202
203 if [[ -s /var/log/exim4/paniclog ]]; then
204 chars+=("PANIC!")
205 # leave it up to epanic-clean to send email notification
206 fi
207
208 source /a/bin/bash_unpublished/source-state
209 if [[ $MAIL_HOST == "$HOSTNAME" ]]; then
210 bbkmsg=
211 if [[ $(systemctl is-active btrbk.timer) != active ]]; then
212 chars+=("BTRBK.TIMER")
213 bbkmsg="btrbk.timer not enabled"
214 fi
215 lo -960 btrbk.timer $bbkmsg
216
217 ## check if last snapshot was within an hour
218 vol=o
219 # this section generally copied from btrbk scripts, but
220 # this part modified to speed things up by about half a second.
221 # I'm not sure if its quite as reliable, but it looks pretty safe.
222 # Profiled it using time and also adding to the top of the file:
223 # set -x
224 # PS4='+ $(date "+%2N") '
225 # allow failure in case there are no snapshots yet.
226 # shellcheck disable=SC2012
227 shopt -u nullglob
228 files=(/mnt/root/btrbk/$vol.20*)
229 shopt -s nullglob
230 snaps=()
231 if (( ${#files[@]} )); then
232 snaps=($(ls -1avdr "${files[@]}" 2>/dev/null |head -n1 || : ))
233 fi
234 now=$(date +%s)
235 maxtime=0
236 for s in ${snaps[@]}; do
237 file=${s##*/}
238 t=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${file#$vol.}) +%s)
239 if (( t > maxtime )); then
240 maxtime=$t
241 fi
242 done
243 if (( maxtime < now - 4*60*60 )); then
244 chars+=("OLD-SNAP")
245 snapshotmsg="/o snapshot older than 4 hours"
246 fi
247 lo -1 old-snapshot $snapshotmsg
248 fi
249
250 cat /a/bin/bash_unpublished/source-state >$status_file
251
252 if [[ ${chars[*]} ]]; then
253 echo "ps_char=\"${chars[*]} \$ps_char\"" >>$status_file
254 fi
255
256 }
257 # use this if we want to do something just once per minute
258 first_chars=()
259
260 power=true
261 if [[ -e /sys/class/power_supply/AC/online && $(</sys/class/power_supply/AC/online) == 0 ]]; then
262 power=false
263 fi
264
265 write-status
266 if [[ $1 ]]; then
267 cat $status_file
268 exit 0
269 fi
270
271 if ! $power; then
272 exit 0
273 fi
274
275 # about 15 minutes
276 for ((i=1; i<=60; i++)); do
277 sleep 15
278 write-status
279 done