2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
5 # usage: runs 4 times every 15 seconds unless any args are passed, or we
6 # are on battery power, then just runs once.
8 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
10 source /a
/bin
/errhandle
/err
11 status_file
=/dev
/shm
/iank-status
25 lo
() { /usr
/local
/bin
/log-once
"$@"; }
28 chars
=("${first_chars[@]}")
30 glob
=(/nocow
/btrfs-stale
/*)
31 if [[ -e ${glob[0]} ]]; then
34 glob
=(/m
/md
/bounces
/new
/*)
35 if [[ -e ${glob[0]} ]]; then
37 bouncemsg
="message in /m/md/bounces/new"
39 lo
-1 bounce
$bouncemsg
40 glob
=(/m
/md
/alerts
/new
/* /m
/md
/alerts
/cur
/*)
41 if [[ -e ${glob[0]} ]]; then
44 if [[ -e /nocow
/user
/mailtest-failure
]]; then
48 qlen
=$
(/usr
/sbin
/exiqgrep
-o 60 -c -b |
awk '{print $1}')
54 if ! make -q ~
/.local
/distro-begin ||
[[ $
(<~
/.local
/distro-begin
) != 0 ]]; then
55 chars
+=("DISTRO-BEGIN!")
58 if ! make -q ~
/.local
/distro-end ||
[[ $
(<~
/.local
/distro-end
) != 0 ]]; then
59 chars
+=("DISTRO-END!")
67 fmin
=$
(( (fsec
- now
) / 60 + 1 ))
68 fminplus
=$
(( fmin
+ 60*24 ))
69 # Filesystem files get copied, so find any newer than the last run.
70 # The rest are hueristics:
71 # Given the last time we added a file in git, is that newer than the last conflink run.
72 # Given new files not added to git, were they modified more recently than the last conflink? but,
73 # push their modification time back by a day so we can develop them before needing to add them to git.
75 all_dirs
=({/a
/bin
/ds
,/p
/c
}{/filesystem
,/machine_specific
/$HOSTNAME/filesystem
})
76 # This part is copied from conflink
77 for x
in /p
/c
/machine_specific
/*.hosts
/a
/bin
/ds
/machine_specific
/*.hosts
; do
78 if grep -qxF $HOSTNAME $x; then all_dirs
+=( ${x%.hosts} ); fi
81 if (( $
(date -d "$(git log --diff-filter=ACR --format=%aD -1)" +%s
) > fsec
)) || \
82 [[ $
(find ${all_dirs[@]} -mmin $fmin -type f
-print -quit 2>/dev
/null
) ]]; then
83 v conflink newer git or newer filesystem files
88 for d
in /a
/bin
/distro-setup
/p
/c
; do
90 untracked
=$
(git ls-files
-o --exclude-standard)
91 if [[ $untracked && $
(find $untracked -mmin $fminplus -type f
-print -quit) ]]; then
92 v conflink
: untracked
in $d
99 if [[ ! -e $f || $
(<$f) != 0 ]]; then
100 v conflink
: last run not found or failed
107 ## Clean the paniclog, but only up to 4 times per day, or else we
108 ## should investigate.
109 loglog
=/tmp
/panicloglog-$
(date --rfc-3339=date)
110 if [[ -s $loglog ]]; then
111 spamcount
=$
(stat
-c%s
$loglog)
115 if (( spamcount
<= 4 )); then
116 if grep -q 'spam acl condition' /var
/log
/exim
4/paniclog
&>/dev
/null
; then
119 /a
/bin
/distro-setup
/epanic-clean
122 if [[ -s /var
/log
/exim
4/paniclog
]]; then
124 tail -n 20 /var
/log
/exim
4/paniclog | lo
-1 paniclog
129 source /a
/bin
/bash_unpublished
/source-state
130 if [[ $MAIL_HOST == "$HOSTNAME" ]]; then
132 if [[ $
(systemctl is-active btrbk.timer
) != active
]]; then
133 chars
+=("BTRBK.TIMER!")
134 bbkmsg
="btrbk.timer not enabled"
136 lo
-60 btrbk.timer
$bbkmsg
138 ## check if last snapshot was within an hour
140 # this section generally copied from btrbk scripts, but
141 # this part modified to speed things up by about half a second.
142 # I'm not sure if its quite as reliable, but it looks pretty safe.
143 # Profiled it using time and also adding to the top of the file:
145 # PS4='+ $(date "+%2N") '
146 snaps
=($
(ls -1avdr /mnt
/root
/btrbk
/$vol.20*|
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
156 if (( maxtime
< now
- 60*60 )); then
157 chars
+=("OLD-SNAPSHOT!")
158 snapshotmsg
="/o snapshot older than 1 hour"
160 lo
-1 old-snapshot
$snapshotmsg
163 cat /a
/bin
/bash_unpublished
/source-state
>$status_file
165 if [[ ${chars[*]} ]]; then
166 echo "ps_char=\"${chars[*]} \$ps_char\"" >>$status_file
170 # use this if we want to do something just once per minute
174 if [[ -e /sys
/class
/power_supply
/AC
/online
&& $
(</sys
/class
/power_supply
/AC
/online
) == 0 ]]; then
187 for ((i
=1; i
<=3; i
++)); do