mainly alerting improvements
[distro-setup] / mailtest-check
1 #!/bin/bash
2
3 # Usage: mail-test-check [slow] [anything]
4 #
5 # slow: do slow checks, like spamassassin
6 #
7 # anything: consider non-interactive, dont print unless something went
8 # wrong
9
10
11 source /b/errhandle/err
12
13 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
14
15 shopt -s nullglob
16
17 e() { $int || return 0; printf "mailtest-check: %s\n" "$*"; }
18
19 getspamdpid() {
20 if [[ ! $spamdpid || ! -d /proc/$spamdpid ]]; then
21 # try twice in case we are restarting, it happens.
22 for i in 1 2; do
23 spamdpid=$(systemctl show --property MainPID --value spamassassin | sed 's/^[10]$//' ||:)
24 if [[ $spamdpid ]]; then
25 break
26 fi
27 sleep 30
28 done
29 fi
30 }
31 pr() {
32 if [[ -e /var/lib/prometheus/node-exporter ]]; then
33 cat >>/var/lib/prometheus/node-exporter/mailtest-check.prom.$$
34 fi
35 }
36
37
38 #### begin arg processing ####
39 # spamassassin checking takes about 8 seconds.
40 slow=false
41 if [[ $1 == slow ]]; then
42 slow=true
43 shift
44 fi
45
46 int=false
47 if [[ $SUDO_USER || $SSH_CONNECTION ]]; then
48 int=true
49 fi
50
51 if [[ $1 == int ]]; then
52 int=true
53 fi
54
55 if [[ $1 == nonint ]]; then
56 int=false
57 fi
58 #### end arg processing ####
59
60
61 if ! $int; then
62 sleep 60
63 fi
64
65
66 # TODO, get je to deliver the local mailbox: /m/md/INBOX
67 # dovecot appears to setup, i can t be sure.
68
69
70
71 main() {
72
73 case $HOSTNAME in
74 bk)
75 folders=(/m/md/{expertpathologyreview.com,amnimal.ninja}/testignore)
76 froms=(ian@iankelling.org z@zroe.org testignore@je.b8.nz iank@gnu.org)
77 ;;
78 je)
79 froms=(ian@iankelling.org z@zroe.org testignore@expertpathologyreview.com testignore@amnimal.ninja)
80 folders=(/m/md/je.b8.nz/testignore)
81 ;;
82 *)
83 folders=(/m/md/l/testignore)
84 froms=(testignore@je.b8.nz testignore@expertpathologyreview.com testignore@amnimal.ninja ian@iankelling.org z@zroe.org iank@gnu.org)
85 if ! $int; then
86 timeout 120 rsync --chown iank:iank -e "ssh -oIdentitiesOnly=yes -F /dev/null -i /root/.ssh/jtuttle" -t --inplace -r 'jtuttle@fencepost.gnu.org:/home/j/jtuttle/Maildir/new/' /m/md/l/testignore/new
87 fi
88 ;;
89 esac
90
91
92 # avoid errors like this:
93 # Nov 8 08:16:05.439 [6080] warn: plugin: failed to parse plugin (from @INC): Can't locate Mail/SpamAssassin/Plugin/WLBLEval.pm: lib/Mail/SpamAssassin/Plugin/WLBLEval.pm: Permission denied at (eval 59) line 1.
94 #Nov 8 08:16:05.439 [6080] warn: plugin: failed to parse plugin (from @INC): Can't locate Mail/SpamAssassin/Plugin/VBounce.pm: lib/Mail/SpamAssassin/Plugin/VBounce.pm: Permission denied at (eval 60) line 1.
95 # i dont know why, i just found the solution online
96 cd /m/md
97
98
99 getspamdpid
100 # first time we write, overwrite anything existing
101 if [[ -e /var/lib/prometheus/node-exporter ]]; then
102 cat >/var/lib/prometheus/node-exporter/mailtest-check.prom.$$ <<EOF
103 mailtest_check_found_spamd_pid_bool $(( ${spamdpid:-0} > 0 ))
104 EOF
105 fi
106 e spamdpid: $spamdpid
107 if [[ ! $spamdpid ]]; then
108 echo mailtest spamd pid not found. systemctl status spamassassin:
109 systemctl status spamassassin
110 fi
111 tmpfile=$(mktemp)
112 declare -i unexpected=0
113 for folder in ${folders[@]}; do
114 for from in ${froms[@]}; do
115 latest=
116 last_sec=0
117
118 if ! grep -rlFx "From: $from" $folder/{new,cur} >$tmpfile; then
119 echo "no message found from: $from"
120 continue
121 fi
122 # webmail sends them to cur it seems
123 while read -r file; do
124 if [[ $file -nt $latest ]]; then
125 latest=$file
126 fi
127 done <$tmpfile
128
129 to=$(awk '/^Envelope-to: / {print $2}' $latest)
130 last_sec=$(awk '/^Subject: / {print $4}' $latest)
131
132 if $slow; then
133 if ! $int; then
134 find $folder/new $folder/cur -type f -mmin +1080 -delete
135 fi
136 getspamdpid
137 if [[ $spamdpid ]]; then
138 if [[ $(readlink /proc/$$/ns/net) != "$(readlink /proc/$spamdpid/ns/net)" ]]; then
139 spamcpre="nsenter -t $spamdpid -n -m"
140 fi
141
142 declare -A results
143 # pyzor fails for our test message, so dont put useless load on their
144 # servers.
145 # example line that sed is parsing:
146 # (-0.1 / 5.0 requ) DKIM_SIGNED=0.1,DKIM_VALID=-0.1,DKIM_VALID_AU=-0.1,SPF_HELO_PASS=-0.001,SPF_PASS=-0.001,TVD_SPACE_RATIO=0.001 autolearn=_AUTOLEARN
147 raw_results="$($spamcpre sudo -u Debian-exim spamassassin -t --cf='score PYZOR_CHECK 0' <"$latest" | tail -n2 | head -n1 | sed -r 's/^\([^)]*\) *//;s/=[^, ]*([, ]|$)/ /g')"
148 for r in $raw_results; do
149 case $r in
150 # got this in an update 2022-01. dun care
151 T_SCC_BODY_TEXT_LINE|SCC_BODY_SINGLE_WORD) : ;;
152 # we have a new domain, ignore this.
153 # it seems like some versions of spamassassin do BODY_SINGLE_WORD, others dont, we dun care.
154 # bayes_00 is a new one indicating ham, we dont care if its missing.
155 BAYES_00|BODY_SINGLE_WORD|FROM_FMBLA_NEWDOM*|autolearn) : ;;
156 SPF_HELO_NEUTRAL)
157 # some of my domains use neutral spf, treat them the same.
158 results[SPF_HELO_PASS]=t
159 ;;
160 *)
161 results[$r]=t
162 ;;
163 esac
164 done
165 # debugging
166 # e results = ${!results[@]}
167 missing=()
168
169 keys=(DKIM_SIGNED DKIM_VALID{,_AU,_EF} SPF_HELO_PASS SPF_PASS TVD_SPACE_RATIO)
170 if [[ $to == *@gnu.org && $from == *@gnu.org ]]; then
171 keys=(ALL_TRUSTED TVD_SPACE_RATIO)
172 elif [[ $to == *@gnu.org ]]; then
173 # eggs has RCVD_IN_DNSWL_MED
174 keys+=(RCVD_IN_DNSWL_MED)
175 elif [[ $from == *@gnu.org ]]; then
176 # eggs has these
177 keys+=(RCVD_IN_DNSWL_MED DKIMWL_WL_HIGH)
178 fi
179
180 for t in ${keys[@]}; do
181 if [[ ${results[$t]} ]]; then
182 unset "results[$t]"
183 elif [[ $t == DKIM_VALID_EF && $from == *@[^.]*.[^.]*.[^.]* ]]; then
184 :
185 # third level domains dont hit this. its because
186 # /usr/share/perl5/Mail/SpamAssassin/Plugin/DKIM.pm checks
187 # if its signed with the registryboundaries domain. afaik:
188 # we need the actual domain to sign it, this would result in
189 # a second signature. I only use second level domains for
190 # testing atm, fsf doesnt use them for anything but the
191 # forum and I dont expect that to have any deliverability
192 # problems. So, not bothering atm.
193 else
194 missing+=($t)
195 fi
196 done
197 if (( ${#results[@]} || ${#missing[@]} )); then
198 printf "$HOSTNAME spamtest %s/%s\n" "$latest"
199 if (( ${#results[@]} )); then
200 printf "unexpected %s" "${!results[*]} "
201 fi
202 if (( ${#missing[@]} )); then
203 printf "missing %s" "${missing[*]}"
204 fi
205 echo # ends our printf string buildup
206
207 if $int; then
208 echo mailtest-check: cat $latest:
209 cat $latest
210 echo mailtest-check: end of cat
211 echo "$(tput setaf 5 2>/dev/null ||:)█$(tput sgr0 2>/dev/null||:)%.0s" $(eval echo "{1..${COLUMNS:-60}}")
212 fi
213 fi
214 fi # if spamdpid
215 fi # if $slow
216
217 now=$EPOCHSECONDS
218 age_sec=$(( now - last_sec ))
219 e $((age_sec / 60)):$(( age_sec % 60 )) ago. to:$to from:$from $latest
220
221 # usec = unix seconds
222 pr <<EOF
223 mailtest_check_last_usec{folder="$folder",from="$from"} $last_sec
224 EOF
225 done
226 unexpected=$(( unexpected + ${#results[@]} + ${#missing[@]} ))
227 done
228 if $slow; then
229 pr <<EOF
230 mailtest_check_unexpected_spamd_results $unexpected
231 EOF
232 fi
233
234 dir=/var/lib/prometheus/node-exporter
235 if [[ -e $dir ]]; then
236 mv $dir/mailtest-check.prom.$$ $dir/mailtest-check.prom
237 # note: node_textfile_mtime_seconds will tell us when this last happened. useful for debugging.
238 fi
239 }
240
241 loop-main() {
242 while true; do
243 premain_sec=$EPOCHSECONDS
244 main
245 sleep $(( 300 - ( $EPOCHSECONDS - premain_sec ) ))
246 done
247 }
248
249
250 if [[ $INVOCATION_ID ]]; then
251 loop-main
252 else
253 main
254 fi
255
256 exit 0