mostly start using prometheus
[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
20 ## Minutes before we give error.
21 # We run this cronjob along with sending the test email every 5 minutes,
22 # so give it 1 minute to arrive, then if the latest email is older than
23 # 7 minutes, the last 2 haven't arrived in a reasonable amount of time.
24 min_limit=7
25
26
27 # spamassassin checking takes about 8 seconds. only do that every
28 # once in a while.
29 slow=false
30 if [[ $1 == slow ]]; then
31 slow=true
32 shift
33 fi
34
35 int=false
36 if [[ $SUDO_USER || $SSH_CONNECTION ]]; then
37 int=true
38 fi
39
40 if [[ $1 == int ]]; then
41 int=true
42 fi
43
44 if [[ $1 == nonint ]]; then
45 int=false
46 fi
47
48
49 if ! $int; then
50 sleep 60
51 fi
52
53 # avoid errors like this:
54 # 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.
55 #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.
56 # i dont know why, i just found the solution online
57 cd /m/md
58
59 case $HOSTNAME in
60 bk)
61 folders=(/m/md/{expertpathologyreview.com,amnimal.ninja}/testignore)
62 froms=(ian@iankelling.org z@zroe.org testignore@je.b8.nz iank@gnu.org)
63 ;;
64 je)
65 froms=(ian@iankelling.org z@zroe.org testignore@expertpathologyreview.com testignore@amnimal.ninja)
66 folders=(/m/md/je.b8.nz/testignore)
67 ;;
68 *)
69 folders=(/m/md/l/testignore)
70 froms=(testignore@je.b8.nz testignore@expertpathologyreview.com testignore@amnimal.ninja ian@iankelling.org z@zroe.org iank@gnu.org)
71 if ! $int; then
72 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
73 fi
74 ;;
75 esac
76
77 getspamdpid() {
78 if [[ ! $spamdpid || ! -d /proc/$spamdpid ]]; then
79 spamdpid=$(systemctl show --property MainPID --value spamassassin | sed 's/^1$//' ||:)
80 fi
81 }
82 getspamdpid
83 pr() {
84 cat >>/var/lib/prometheus/node-exporter/mailtest-check.prom.$$
85 }
86 pr <<EOF
87 mailtest_check_found_spamd_pid_bool $(( ${spamdpid:-0} > 0 ))
88 EOF
89 e spamdpid: $spamdpid
90 if [[ ! $spamdpid ]]; then
91 echo $HOSTNAME mailtest spamd pid not found. systemctl status spamassassin:
92 systemctl status spamassassin
93 fi
94 tmpfile=$(mktemp)
95 declare -i unexpected=0
96 for folder in ${folders[@]}; do
97 for from in ${froms[@]}; do
98 latest=
99 last_sec=0
100
101 if ! grep -rlFx "From: $from" $folder/{new,cur} >$tmpfile; then
102 e "no message found from: $from"
103 continue
104 fi
105 # webmail sends them to cur it seems
106 while read -r file; do
107 if [[ $file -nt $latest ]]; then
108 latest=$file
109 fi
110 done <$tmpfile
111
112 if [[ ! $latest ]]; then
113 # 10 is an arbitrary bad value
114 unexpected+=10
115 else
116 to=$(awk '/^Envelope-to: / {print $2}' $latest)
117 last_sec=$(awk '/^Subject: / {print $4}' $latest)
118
119 if $slow; then
120 if ! $int; then
121 find $folder/new $folder/cur -type f -mmin +1080 -delete
122 fi
123 getspamdpid
124 if [[ $spamdpid ]]; then
125 if [[ $(readlink /proc/$$/ns/net) != "$(readlink /proc/$spamdpid/ns/net)" ]]; then
126 spamcpre="nsenter -t $spamdpid -n -m"
127 fi
128
129 declare -A results
130 # pyzor fails for our test message, so dont put useless load on their
131 # servers.
132 # example line that sed is parsing:
133 # (-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
134 for r in $($spamcpre sudo -u Debian-exim spamassassin -t --cf='score PYZOR_CHECK 0' <"$latest" | tail -n2 | head -n1 | sed -r 's/^\([^)]*\) *//;s/=[^, ]*([, ]|$)/ /g'); do
135 case $r in
136 # got this in an update 2022-01. dun care
137 T_SCC_BODY_TEXT_LINE|SCC_BODY_SINGLE_WORD) : ;;
138 # we have a new domain, ignore this.
139 # it seems like some versions of spamassassin do BODY_SINGLE_WORD, others dont, we dun care.
140 # bayes_00 is a new one indicating ham, we dont care if its missing.
141 BAYES_00|BODY_SINGLE_WORD|FROM_FMBLA_NEWDOM*|autolearn) : ;;
142 SPF_HELO_NEUTRAL)
143 # some of my domains use neutral spf, treat them the same.
144 results[SPF_HELO_PASS]=t
145 ;;
146 *)
147 results[$r]=t
148 ;;
149 esac
150 done
151 # debugging
152 # e results = ${!results[@]}
153 missing=()
154
155 keys=(DKIM_SIGNED DKIM_VALID{,_AU,_EF} SPF_HELO_PASS SPF_PASS TVD_SPACE_RATIO)
156 if [[ $to == *@gnu.org && $from == *@gnu.org ]]; then
157 keys=(ALL_TRUSTED TVD_SPACE_RATIO)
158 elif [[ $to == *@gnu.org ]]; then
159 # eggs has RCVD_IN_DNSWL_MED
160 keys+=(RCVD_IN_DNSWL_MED)
161 elif [[ $from == *@gnu.org ]]; then
162 # eggs has these
163 keys+=(RCVD_IN_DNSWL_MED DKIMWL_WL_HIGH)
164 fi
165
166 for t in ${keys[@]}; do
167 if [[ ${results[$t]} ]]; then
168 unset "results[$t]"
169 elif [[ $t == DKIM_VALID_EF && $from == *@[^.]*.[^.]*.[^.]* ]]; then
170 :
171 # third level domains dont hit this. its because
172 # /usr/share/perl5/Mail/SpamAssassin/Plugin/DKIM.pm checks
173 # if its signed with the registryboundaries domain. afaik:
174 # we need the actual domain to sign it, this would result in
175 # a second signature. I only use second level domains for
176 # testing atm, fsf doesnt use them for anything but the
177 # forum and I dont expect that to have any deliverability
178 # problems. So, not bothering atm.
179 else
180 missing+=($t)
181 fi
182 done
183 if (( ${#results[@]} || ${#missing[@]} )); then
184 printf "$HOSTNAME spamtest %s/%s\n" "$latest"
185 if (( ${#results[@]} )); then
186 printf "unexpected %s" "${!results[*]} "
187 fi
188 if (( ${#missing[@]} )); then
189 printf "missing %s" "${missing[*]}"
190 fi
191 echo
192 echo mailtest-check: cat $latest:
193 cat $latest
194 echo mailtest-check: end of cat
195 printf "$(tput setaf 5 2>/dev/null ||:)█$(tput sgr0 2>/dev/null||:)%.0s" $(eval echo "{1..${COLUMNS:-60}}")
196 fi
197 fi # if spamdpid
198 fi # if $slow
199 fi # if [[ $latest ]]
200
201 now=$(date +%s)
202 limit=$(( now - 60 * min_limit ))
203 age_sec=$(( now - last_sec ))
204 e $((age_sec / 60)):$(( age_sec % 60 )) ago. to:$to from:$from $latest
205
206 if (( last_sec <= limit )); then
207 echo $HOSTNAME mailtest $folder $from $(date -d @$last_sec +'%a %m-%d %H:%M')
208 fi
209 # usec = unix seconds
210 pr <<EOF
211 mailtest_check_last_usec{folder="$folder",from="$from"} $last_sec
212 EOF
213 done
214 done
215 if $slow; then
216 pr <<EOF
217 mailtest_check_unexpected_spamd_results $unexpected
218 EOF
219 fi
220 mv /var/lib/prometheus/node-exporter/mailtest-check.prom.$$ /var/lib/prometheus/node-exporter/mailtest-check.prom