2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
5 # usage: runs once every 15 seconds unless any args are passed, or we
6 # then just runs once and have verbose output. On battery power, run
9 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
11 if [[ $EUID != 1000 ]]; then
12 echo "$0: error, expected to be user 1000"
16 source /a
/bin
/errhandle
/err
17 status_file
=/dev
/shm
/iank-status
23 for p
in ~
/.gem
/ruby
/*/bin
; do
37 p
() { printf "%s\n" "$*"; }
38 # log-once COUNT NAME [MESSAGE]
40 if type -p ifne
&>/dev
/null
; then
41 /usr
/local
/bin
/log-once
"$@" | ifne
mail -s "$HOSTNAME: system-status $2" root@localhost
46 if type -p ifne
&>/dev
/null
; then
47 /usr
/local
/bin
/log-once
"$@" | ifne
mail -s "$HOSTNAME: system-status $2" daylert@iankelling.org
57 # todo, consider migrating some of these alerts into prometheus
59 chars
=("${first_chars[@]}")
61 services
=( epanicclean
)
71 if systemctl show
-p SubState
--value ${services[@]} |
grep -E -v '^(running|)$' &>/dev
/null
; then
72 for s
in ${services[@]}; do
73 if [[ $
(systemctl show
-p SubState
--value $s 2>&1) != running
]]; then
79 p
${bads[*]} | lo
-240 mysers
86 prometheus-node-exporter
87 prometheus-alertmanager
91 if systemctl show
-p SubState
--value ${services[@]} |
grep -E -v '^(running|)$' &>/dev
/null
; then
92 for s
in ${services[@]}; do
93 if [[ $
(systemctl show
-p SubState
--value $s 2>&1) != running
]]; then
99 p
${bads[*]} | lo
-240 prom
104 # this section copied from servicepid()
106 pid
=$
(systemctl show
--property MainPID
--value $unit ||
:)
110 dir
=/sys
/fs
/cgroup
/system.slice
111 if [[ ! -d $dir ]]; then
112 dir
=/sys
/fs
/cgroup
/systemd
/system.slice
114 pid
=$
(head -n1 $dir/${unit%.service}.service
/cgroup.procs ||
:)
117 if [[ ! $pid ]]; then
122 if [[ -e /a
/bin
/bash_unpublished
/source-state
]]; then
123 # /a gets remounted due to btrbk, ignore error code for file doesnt exist
124 source /a
/bin
/bash_unpublished
/source-state ||
[[ $?
== 1 ]]
128 ## check if last snapshot was recent
129 old_snap_limit
=$
(( 3 * 60 * 60 ))
131 btrbk_root
=/mnt
/o
/btrbk
132 # this section generally copied from btrbk scripts, but
133 # this part modified to speed things up by about half a second.
134 # I'm not sure if its quite as reliable, but it looks pretty safe.
135 # Profiled it using time and also adding to the top of the file:
137 # PS4='+ $(date "+%2N") '
138 # allow failure in case there are no snapshots yet.
139 # shellcheck disable=SC2012
141 files
=($btrbk_root/$vol.20*)
143 if (( ! ${#files[@]} )); then
146 snaps
=($
(ls -1avdr "${files[@]}" 2>/dev
/null |
head -n1 ||
: ))
149 for s
in ${snaps[@]}; do
151 t
=$
(date -d $
(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${file#$vol.}) +%s
)
152 if (( t
> maxtime
)); then
157 last_snap_age
=$
(( now
- maxtime
))
158 last_snap_hours
=$
(( last_snap_age
/ 60 / 60 ))
159 if (( last_snap_age
> old_snap_limit
)); then
160 chars
+=(OLD-SNAP-
${last_snap_hours}h
)
161 snapshotmsg
="/$vol snapshot older than 4 hours"
162 if [[ $MAIL_HOST == "$HOSTNAME" ]]; then
163 p
"$snapshotmsg" | lo
-1 old-snapshot
165 # not bothering to get info on all volumes if we find an old one.
169 if [[ $MAIL_HOST == "$HOSTNAME" ]]; then
172 glob
=(/m
/md
/bounces
/new
/*)
173 if [[ -e ${glob[0]} ]]; then
175 bouncemsg
="message in /m/md/bounces/new"
177 p
$bouncemsg | loday
-1 bounce
178 # emails without the S (seen) flag. this only checks the last flag,
179 # but its good enough for me.
180 glob
=(/m
/md
/alerts
/{new
,cur
}/!(*,S
))
181 if [[ -e ${glob[0]} ]]; then
185 glob
=(/m
/md
/daylert
/{new
,cur
}/!(*,S
))
186 if [[ -e ${glob[0]} ]]; then
191 if [[ $
(systemctl is-active btrbk.timer
) != active
]]; then
195 p
"$bbkmsg" | lo
-480 btrbk.timer
199 # commented out, only using timetrap retrospectively.
200 # # clock us out in timetrap if are idle too long
201 # if [[ -e /p/.timetrap.db ]]; then
203 # if type -p xprintidle &>/dev/null && xidle=$(xprintidle 2>/dev/null); then
204 # if [[ $xidle == [0-9]* ]]; then
205 # sheet=$(sqlite3 /p/.timetrap.db "select sheet from entries where end is NULL;")
207 # if [[ $sheet == w ]]; then
210 # if [[ $sheet && $xidle -gt $idle ]]; then
216 else # end if $MAIL_HOST
217 rmg
/home
/iank
/cron-errors
/bounce
* \
218 /home
/iank
/cron-errors
/btrbk.timer
* \
219 /home
/iank
/cron-errors
/old-snapshot
*
222 if ip l show tunfsf
&>/dev
/null
; then
223 # this is for tracking dns over tls issue, which
224 # fixvpndns() in brc2 fixes.
225 stat
=$
(resolvectl dnsovertls tunfsf
2>/dev
/null ||
: )
226 read -r _ _ _ istls
<<<"$stat"
230 printf "%s\n" "$istls" | ts
>> /tmp
/istls.log
236 # We do this once every 5 minutes, since this is not a grave problem.
237 # For formatted elisp, see /b/ds/unsaved-buffers.el
238 elisp
='(format "%s" (-reduce-from (lambda (acc buf) (let ((bpath (buffer-file-name buf))) (if (and bpath (buffer-modified-p buf)) (cons bpath acc ) acc))) nil (buffer-list)))'
239 if [[ ! $last_emacs_check ||
$emacsfiles ]] ||
(( last_emacs_check
< EPOCHSECONDS
- 300 )); then
240 if pgrep
-G iank
-u iank
-f 'emacs --daemon' &>/dev
/null
; then
241 # i dun care if this fails
242 emacsfiles
="$(timeout 1 emacsclient -a /usr/bin/true --eval "$elisp" 2>/dev/null | sed '/^"nil
"$/d;s/^"(/E
: /;s
/)"$//' ||:)"
243 if [[ $emacsfiles ]]; then
244 chars
+=("$emacsfiles")
247 last_emacs_check
=$EPOCHSECONDS
251 glob
=(/nocow
/btrfs-stale
/*)
252 if [[ -e ${glob[0]} ]]; then
256 if [[ $
(find /var
/mail -type f \
! -empty -print -quit) ]]; then
257 var_mail_msg
="message in /var/mail"
259 p
$var_mail_msg | loday
-1 var_mail
261 # Note, early in install process, we dont have permission yet for exiqgrep.
263 # todo: don't do this every 15 seconds, more like once every 2 minutes to
266 # 2400 = 40 mins. This should allow for system restarts, and
267 # 30 minute message delay plus 10 minute queu runs.
268 qlen
=$
(/usr
/sbin
/exiqgrep
-o 2400 -c -b |
awk '{print $1}') ||
:
271 # Do sending of long delayed messages, and dont count them in our queue warnings.
272 for mid
in $
(exiqgrep
-o 2400 -zi); do
273 if exim
-Mvh $mid |
awk 'tolower($2) == "fdate:"' |
grep -q .
; then
275 if (( $
(date -d "$(exim -Mset $mid -be <<<'$h_date:' | sed -n 's/^> *//;/./p')" +%s
) < EPOCHSECONDS
)); then
276 if ip a show veth0-mail
&>/dev
/null
; then
277 pid
=$
(pgrep
-f "/usr/sbin/exim4 -bd -q30m -C /etc/exim4/my.conf"|
head -n1);
278 nsenter
-t $pid -n -m /usr
/sbin
/exim4
-C /etc
/exim
4/my.conf
-M $mid
280 /usr
/sbin
/exim4
-M $mid
287 qmsg
="queue length $qlen"
292 # No point in emailing about the mailq on a host where we don't
295 p
$qmsg | loday
-120 qlen
298 rmg
/home
/iank
/cron-errors
/qlen
*
304 # todo: make this robust to the case of /a not being mounted
305 if ! make -C /b
/ds
-q ~
/.local
/distro-begin
2>/dev
/null ||
[[ $
(<~
/.local
/distro-begin
) != 0 ]]; then
310 if ! make -C /b
/ds
-q ~
/.local
/distro-end
2>/dev
/null ||
[[ $
(<~
/.local
/distro-end
) != 0 ]]; then
314 # these conditions are so we dont have an overly verbose prompt
315 if $begin && $end; then
322 source /a
/bin
/ds
/script-files
324 # shellcheck disable=SC2043
329 # the / 60 makes it 0-59 seconds less strict, +1 to help make sure we
330 # dont have any false positives.
331 fmin
=$
(( (fsec
- now
+ 1 ) / 60 ))
332 fminplus
=$
(( fmin
+ 60*24 ))
333 # Filesystem files get copied, so find any newer than the last run.
334 # The rest are hueristics:
335 # Given the last time we added a file in git, is that newer than the last conflink run.
336 # Given new files not added to git, were they modified more recently than the last conflink? but,
337 # push their modification time back by a day so we can develop them before needing to add them to git.
339 all_dirs
=({/a
/bin
/ds
,/p
/c
}{/filesystem
,/machine_specific
/$HOSTNAME/filesystem
})
340 # This part is copied from conflink
341 for x
in /p
/c
/machine_specific
/*.hosts
/a
/bin
/ds
/machine_specific
/*.hosts
; do
342 if grep -qxF $HOSTNAME $x; then all_dirs
+=( ${x%.hosts} ); fi
345 # Just because i forget a lot, -mmin -NUM means files modified <= NUM minutes ago
346 if (( fmin
< 0 )) && [[ $
(find ${all_my_scripts[@]} ${all_dirs[@]} -mmin $fmin -type f
-print -quit 2>/dev
/null
) ]]; then
347 v conflink newer filesystem files
352 for d
in /a
/bin
/distro-setup
/p
/c
; do
353 [[ -d $d ]] ||
continue
355 if [[ ! -e .git
]]; then
356 # some hosts i dont push all of /p/c
359 if (( $
(date -d "$(git log --diff-filter=ACR --format=%aD -1)" +%s
) > fsec
)); then
360 v conflink
: newer files checked
in to git
368 done < <(git ls-files
-o --exclude-standard)
369 if [[ ${untracked[0]} && $
(find "${untracked[@]}" -mmin $fminplus -type f
-print -quit) ]]; then
370 v conflink
: untracked
in $d
378 if [[ ! -e $f || $
(<$f) != 0 ]]; then
379 v conflink
: last run not found or failed
386 # if [[ $(grep -v "exim user lost privilege for using -C option" /var/log/exim4/paniclog 2>/dev/null ||:) ]]; then
387 if [[ -s /var
/log
/exim
4/paniclog
]]; then
389 # leave it up to epanic-clean to send email notification
392 if [[ ! -e $status_file ||
-w $status_file ]]; then
393 if [[ -e /a
/bin
/bash_unpublished
/source-state
]]; then
394 cat /a
/bin
/bash_unpublished
/source-state
>$status_file
397 if [[ ${chars[*]} ]]; then
398 echo "ps_char=\"${chars[*]} \$ps_char\"" >>$status_file
403 # This prevents me having to mute notifications when I'm going to bed.
408 if lock_info
=$
(xscreensaver-command
-time); then
409 if [[ $lock_info != *non-blanked
* ]]; then
415 midnight
=$
(date -d 00:00 +%s
)
416 mdiff
=$
(( EPOCHSECONDS
- midnight
))
417 if $locked && (( mdiff
< 6 || mdiff
> 21 )); then
418 case $
(pactl get-sink-mute @DEFAULT_SINK@ |
awk '{print $2}') in
422 pactl set-sink-mute @DEFAULT_SINK@ true
426 if ! $locked && (( mdiff
> 6 || mdiff
< 12 )) && [[ ! -e /tmp
/ianknap
]]; then
427 case $
(pactl get-sink-mute @DEFAULT_SINK@ |
awk '{print $2}') in
431 pactl set-sink-mute @DEFAULT_SINK@ false
437 # use this if we want to do something just once per minute
449 if [[ -e /sys
/class
/power_supply
/AC
/online
&& $
(</sys
/class
/power_supply
/AC
/online
) == 0 ]]; then
454 if systemctl
-q is-active bitcoind
; then
466 # ensure our long operations are one line so we are not prone errors
467 # from this file being modified.