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