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. On battery power, run once per minute.
8 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
10 if [[ $EUID != 1000 ]]; then
11 echo "$0: error, expected to be user 1000"
15 source /a
/bin
/errhandle
/err
16 status_file
=/dev
/shm
/iank-status
22 for p
in ~
/.gem
/ruby
/*/bin
; do
36 p
() { printf "%s\n" "$*"; }
37 # log-once COUNT NAME [MESSAGE]
39 if type -p ifne
&>/dev
/null
; then
40 /usr
/local
/bin
/log-once
"$@" | ifne
mail -s "$HOSTNAME: system-status $2" root@localhost
45 if type -p ifne
&>/dev
/null
; then
46 /usr
/local
/bin
/log-once
"$@" | ifne
mail -s "$HOSTNAME: system-status $2" daylert@iankelling.org
50 # todo, consider migrating some of these alerts into prometheus
52 chars
=("${first_chars[@]}")
54 services
=( epanicclean
)
64 if systemctl show
-p SubState
--value ${services[@]} |
egrep -v '^(running|)$' &>/dev
/null
; then
65 for s
in ${services[@]}; do
66 if [[ $
(systemctl show
-p SubState
--value $s 2>&1) != running
]]; then
72 p
${bads[*]} | lo
-240 mysers
79 prometheus-node-exporter
80 prometheus-alertmanager
84 if systemctl show
-p SubState
--value ${services[@]} |
egrep -v '^(running|)$' &>/dev
/null
; then
85 for s
in ${services[@]}; do
86 if [[ $
(systemctl show
-p SubState
--value $s 2>&1) != running
]]; then
92 p
${bads[*]} | lo
-240 prom
97 if [[ -e /a
/bin
/bash_unpublished
/source-state
]]; then
98 # /a gets remounted due to btrbk, ignore error code for file doesnt exist
99 source /a
/bin
/bash_unpublished
/source-state ||
[[ $?
== 1 ]]
101 if [[ $MAIL_HOST == "$HOSTNAME" ]]; then
104 glob
=(/m
/md
/bounces
/new
/*)
105 if [[ -e ${glob[0]} ]]; then
107 bouncemsg
="message in /m/md/bounces/new"
109 p
$bouncemsg | loday
-1 bounce
110 # emails without the S (seen) flag. this only checks the last flag,
111 # but its good enough for me.
112 glob
=(/m
/md
/alerts
/{new
,cur
}/!(*,S
))
113 if [[ -e ${glob[0]} ]]; then
117 glob
=(/m
/md
/daylert
/{new
,cur
}/!(*,S
))
118 if [[ -e ${glob[0]} ]]; then
123 if [[ $
(systemctl is-active btrbk.timer
) != active
]]; then
127 p
"$bbkmsg" | lo
-480 btrbk.timer
129 ## check if last snapshot was within an hour
131 # this section generally copied from btrbk scripts, but
132 # this part modified to speed things up by about half a second.
133 # I'm not sure if its quite as reliable, but it looks pretty safe.
134 # Profiled it using time and also adding to the top of the file:
136 # PS4='+ $(date "+%2N") '
137 # allow failure in case there are no snapshots yet.
138 # shellcheck disable=SC2012
140 files
=(/mnt
/root
/btrbk
/$vol.20*)
143 if (( ${#files[@]} )); then
144 snaps
=($
(ls -1avdr "${files[@]}" 2>/dev
/null |
head -n1 ||
: ))
148 for s
in ${snaps[@]}; do
150 t
=$
(date -d $
(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${file#$vol.}) +%s
)
151 if (( t
> maxtime
)); then
156 if (( maxtime
< now
- 4*60*60 )); then
158 snapshotmsg
="/o snapshot older than 4 hours"
160 p
"$snapshotmsg" | lo
-1 old-snapshot
163 # commented out, only using timetrap retrospectively.
164 # # clock us out in timetrap if are idle too long
165 # if [[ -e /p/.timetrap.db ]]; then
167 # if type -p xprintidle &>/dev/null && xidle=$(xprintidle 2>/dev/null); then
168 # if [[ $xidle == [0-9]* ]]; then
169 # sheet=$(sqlite3 /p/.timetrap.db "select sheet from entries where end is NULL;")
171 # if [[ $sheet == w ]]; then
174 # if [[ $sheet && $xidle -gt $idle ]]; then
183 if ip l show tunfsf
&>/dev
/null
; then
184 # this is for tracking dns over tls issue, which
185 # fixvpndns() in brc2 fixes.
186 stat
=$
(resolvectl dnsovertls tunfsf
2>/dev
/null ||
: )
187 read _ _ _ istls
<<<"$stat"
191 printf "%s\n" "$istls" | ts
>> /tmp
/istls.log
197 # We do this once every 5 minutes, since this is not a grave problem.
198 # For formatted elisp, see /b/ds/unsaved-buffers.el
199 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)))'
200 if [[ ! $last_emacs_check ||
$emacsfiles ]] ||
(( last_emacs_check
< EPOCHSECONDS
- 300 )); then
201 if pgrep
-G iank
-u iank
-f 'emacs --daemon' &>/dev
/null
; then
202 # i dun care if this fails
203 emacsfiles
="$(timeout 1 emacsclient --eval "$elisp"| sed '/^"nil
"$/d;s/^"(/E
: /;s
/)"$//' ||:)"
204 if [[ $emacsfiles ]]; then
205 chars
+=("$emacsfiles")
208 last_emacs_check
=$EPOCHSECONDS
212 glob
=(/nocow
/btrfs-stale
/*)
213 if [[ -e ${glob[0]} ]]; then
217 if [[ $
(find /var
/mail -type f \
! -empty -print -quit) ]]; then
218 var_mail_msg
="message in /var/mail"
220 p
$var_mail_msg | loday
-1 var_mail
223 tmp
=(/var
/local
/cron-errors
/mailtest-check
*)
224 if (( ${#tmp[@]} )); then
227 tmp
=(/var
/local
/cron-errors
/mailtest-slow
*)
228 if (( ${#tmp[@]} )); then
232 # early in install process, we dont have permission yet for exiqgrep.
233 # 1100 helps allow for system restarts
234 qlen
=$
(/usr
/sbin
/exiqgrep
-o 1100 -c -b |
awk '{print $1}') ||
:
237 qmsg
="queue length $qlen"
241 # No point in emailing about the mailq on a host where we don't
244 p
$qmsg | loday
-120 qlen
250 if ! make -C /b
/ds
-q ~
/.local
/distro-begin
2>/dev
/null ||
[[ $
(<~
/.local
/distro-begin
) != 0 ]]; then
255 if ! make -C /b
/ds
-q ~
/.local
/distro-end
2>/dev
/null ||
[[ $
(<~
/.local
/distro-end
) != 0 ]]; then
259 # these conditions are so we dont have an overly verbose prompt
260 if $begin && $end; then
268 # shellcheck disable=SC2043
273 # the / 60 makes it 0-59 seconds less strict, +1 to help make sure we
274 # dont have any false positives.
275 fmin
=$
(( (fsec
- now
+ 1 ) / 60 ))
276 fminplus
=$
(( fmin
+ 60*24 ))
277 # Filesystem files get copied, so find any newer than the last run.
278 # The rest are hueristics:
279 # Given the last time we added a file in git, is that newer than the last conflink run.
280 # Given new files not added to git, were they modified more recently than the last conflink? but,
281 # push their modification time back by a day so we can develop them before needing to add them to git.
283 all_dirs
=({/a
/bin
/ds
,/p
/c
}{/filesystem
,/machine_specific
/$HOSTNAME/filesystem
})
284 # This part is copied from conflink
285 for x
in /p
/c
/machine_specific
/*.hosts
/a
/bin
/ds
/machine_specific
/*.hosts
; do
286 if grep -qxF $HOSTNAME $x; then all_dirs
+=( ${x%.hosts} ); fi
289 # Just because i forget a lot, -mmin -NUM means files modified <= NUM minutes ago
290 if (( fmin
< 0 )) && [[ $
(find ${all_dirs[@]} -mmin $fmin -type f
-print -quit 2>/dev
/null
) ]]; then
291 v conflink newer filesystem files
296 for d
in /a
/bin
/distro-setup
/p
/c
; do
297 [[ -d $d ]] ||
continue
299 if [[ ! -e .git
]]; then
300 # some hosts i dont push all of /p/c
303 if (( $
(date -d "$(git log --diff-filter=ACR --format=%aD -1)" +%s
) > fsec
)); then
304 v conflink
: newer files checked
in to git
312 done < <(git ls-files
-o --exclude-standard)
313 if [[ ${untracked[0]} && $
(find "${untracked[@]}" -mmin $fminplus -type f
-print -quit) ]]; then
314 v conflink
: untracked
in $d
322 if [[ ! -e $f || $
(<$f) != 0 ]]; then
323 v conflink
: last run not found or failed
330 # if [[ $(grep -v "exim user lost privilege for using -C option" /var/log/exim4/paniclog 2>/dev/null ||:) ]]; then
331 if [[ -s /var
/log
/exim
4/paniclog
]]; then
333 # leave it up to epanic-clean to send email notification
336 if [[ ! -e $status_file ||
-w $status_file ]]; then
337 if [[ -e /a
/bin
/bash_unpublished
/source-state
]]; then
338 cat /a
/bin
/bash_unpublished
/source-state
>$status_file
341 if [[ ${chars[*]} ]]; then
342 echo "ps_char=\"${chars[*]} \$ps_char\"" >>$status_file
346 # use this if we want to do something just once per minute
359 if [[ -e /sys
/class
/power_supply
/AC
/online
&& $
(</sys
/class
/power_supply
/AC
/online
) == 0 ]]; then
372 # ensure our long operations are one line so we are not prone errors
373 # from this file being modified.