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