0ab88b4d533a4ee4f2d01e2a0751f6a63f658458
[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
16 lo() { /usr/local/bin/log-once "$@"; }
17
18 write-status() {
19 chars=("${first_chars[@]}")
20
21 glob=(/nocow/btrfs-stale/*)
22 if [[ -e ${glob[0]} ]]; then
23 chars+=("STALE!")
24 fi
25 glob=(/m/md/bounces/new/*)
26 if [[ -e ${glob[0]} ]]; then
27 chars+=("BOUNCE!")
28 bouncemsg="message in /m/md/bounces/new"
29 fi
30 lo -1 bounce $bouncemsg
31 glob=(/m/md/alerts/new/* /m/md/alerts/cur/*)
32 if [[ -e ${glob[0]} ]]; then
33 chars+=("ALERT!")
34 fi
35 if [[ -e /nocow/user/mailtest-failure ]]; then
36 chars+=("MAILPING!")
37 fi
38
39 qlen=$(/usr/sbin/exiqgrep -o 60 -c -b | awk '{print $1}')
40 if ((qlen)); then
41 chars+=("q $qlen")
42 fi
43
44 if ! make -q ~/.local/distro-begin || [[ $(<~/.local/distro-begin) != 0 ]]; then
45 chars+=("DISTRO-BEGIN!")
46 fi
47
48 if ! make -q ~/.local/distro-end || [[ $(<~/.local/distro-end) != 0 ]]; then
49 chars+=("DISTRO-END!")
50 fi
51
52 f=~/.local/conflink
53 if [[ -e $f ]]; then
54 cd /b/ds
55 now=$(date +%s)
56 fsec=$(stat -c%Y $f)
57 fmin=$(( (fsec - now ) / 60 + 1 ))
58 fminplus=$(( fmin + 60*24 ))
59 # Filesystem files get copied, so find any newer than the last run.
60 # The rest are hueristics:
61 # Given the last time we added a file in git, is that newer than the last conflink run.
62 # Given new files not added to git, were they modified more recently than the last conflink? but,
63 # push their modification time back by a day so we can develop them before needing to add them to git.
64 if (( $(date -d "$(git log --diff-filter=ACR --format=%aD -1)" +%s) > fsec )) || \
65 [[ $(find {/a/bin/ds,/p/c}{/filesystem,/machine_specific/$HOSTNAME/filesystem} -mmin $fmin -type f -print -quit 2>/dev/null) ]] \
66 || [[ $(find $(git ls-files -o --exclude-standard) -mmin $fminplus -type f -print -quit) ]]; then
67 chars+=("CONFLINK!")
68 fi
69 fi
70
71 if [[ ! -e $f || $(<$f) != 0 ]]; then
72 chars+=("CONFLINK!")
73 fi
74
75
76 ## Clean the paniclog, but only up to 4 times per day, or else we
77 ## should investigate.
78 loglog=/tmp/panicloglog-$(date --rfc-3339=date)
79 if [[ -s $loglog ]]; then
80 spamcount=$(stat -c%s $loglog)
81 else
82 spamcount=0
83 fi
84 if (( spamcount <= 4 )); then
85 if grep -q 'spam acl condition' /var/log/exim4/paniclog &>/dev/null; then
86 printf . >>$loglog
87 fi
88 /a/bin/distro-setup/epanic-clean
89 fi
90
91 if [[ -s /var/log/exim4/paniclog ]]; then
92 chars+=("PANIC!")
93 tail -n 20 /var/log/exim4/paniclog | lo -1 paniclog
94 else
95 lo -1 paniclog
96 fi
97
98 source /a/bin/bash_unpublished/source-state
99 if [[ $MAIL_HOST == "$HOSTNAME" ]]; then
100 bbkmsg=
101 if [[ $(systemctl is-active btrbk.timer) != active ]]; then
102 chars+=("BTRBK.TIMER!")
103 bbkmsg="btrbk.timer not enabled"
104 fi
105 lo -60 btrbk.timer $bbkmsg
106
107 ## check if last snapshot was within an hour
108 vol=o
109 # this section generally copied from btrbk scripts, but
110 # this part modified to speed things up by about half a second.
111 # I'm not sure if its quite as reliable, but it looks pretty safe.
112 # Profiled it using time and also adding to the top of the file:
113 # set -x
114 # PS4='+ $(date "+%2N") '
115 snaps=($(ls -1avdr /mnt/root/btrbk/$vol.20*|head -n1))
116 now=$(date +%s)
117 maxtime=0
118 for s in ${snaps[@]}; do
119 file=${s##*/}
120 t=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${file#$vol.}) +%s)
121 if (( t > maxtime )); then
122 maxtime=$t
123 fi
124 done
125 if (( maxtime < now - 60*60 )); then
126 chars+=("OLD-SNAPSHOT!")
127 snapshotmsg="/o snapshot older than 1 hour"
128 fi
129 lo -1 old-snapshot $snapshotmsg
130 fi
131
132 cat /a/bin/bash_unpublished/source-state >$status_file
133
134 if [[ ${chars[*]} ]]; then
135 echo "ps_char=\"${chars[*]} \$ps_char\"" >>$status_file
136 fi
137
138 }
139 # use this if we want to do something just once per minute
140 first_chars=()
141
142 power=true
143 if [[ -e /sys/class/power_supply/AC/online && $(</sys/class/power_supply/AC/online) == 0 ]]; then
144 power=false
145 fi
146
147 write-status
148 if [[ $1 ]]; then
149 cat $status_file
150 exit 0
151 fi
152
153 if ! $power; then
154 exit 0
155 fi
156 for ((i=1; i<=3; i++)); do
157 sleep 15
158 write-status
159 done