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