fix root mail on bk, nonroot stuff, misc prom updates
[distro-setup] / mailtest-check
index 3fdefffb1050afca706edf6622ee5e061dc33185..2dcc7b215a7bc4cb748c98191f0953d809756f5b 100755 (executable)
@@ -21,7 +21,7 @@ e() { $int || return 0; printf "mailtest-check: %s\n" "$*"; }
 # We run this cronjob along with sending the test email every 5 minutes,
 # so give it 1 minute to arrive, then if the latest email is older than
 # 7 minutes, the last 2 haven't arrived in a reasonable amount of time.
-min_limit=7
+min_limit=8
 
 
 # spamassassin checking takes about 8 seconds. only do that every
@@ -76,16 +76,25 @@ esac
 
 getspamdpid() {
   if [[ ! $spamdpid || ! -d /proc/$spamdpid ]]; then
-    spamdpid=$(systemctl status spamassassin| sed -n '/^ *Main PID:/s/[^0-9]//gp' ||:)
+    spamdpid=$(systemctl show --property MainPID --value spamassassin | sed 's/^1$//' ||:)
   fi
 }
 getspamdpid
+pr() {
+  if [[ -e /var/lib/prometheus/node-exporter ]]; then
+    cat >>/var/lib/prometheus/node-exporter/mailtest-check.prom.$$
+  fi
+}
+pr <<EOF
+mailtest_check_found_spamd_pid_bool $(( ${spamdpid:-0} > 0 ))
+EOF
 e spamdpid: $spamdpid
 if [[ ! $spamdpid ]]; then
   echo $HOSTNAME mailtest spamd pid not found. systemctl status spamassassin:
   systemctl status spamassassin
 fi
 tmpfile=$(mktemp)
+declare -i unexpected=0
 for folder in ${folders[@]}; do
   for from in ${froms[@]}; do
     latest=
@@ -102,7 +111,10 @@ for folder in ${folders[@]}; do
       fi
     done <$tmpfile
 
-    if [[ $latest ]]; then
+    if [[ ! $latest ]]; then
+      # 10 is an arbitrary bad value
+      unexpected+=10
+    else
       to=$(awk '/^Envelope-to: / {print $2}' $latest)
       last_sec=$(awk '/^Subject: / {print $4}' $latest)
 
@@ -196,5 +208,19 @@ for folder in ${folders[@]}; do
     if (( last_sec <= limit )); then
       echo $HOSTNAME mailtest $folder $from $(date -d @$last_sec +'%a %m-%d %H:%M')
     fi
+    # usec = unix seconds
+    pr <<EOF
+mailtest_check_last_usec{folder="$folder",from="$from"} $last_sec
+EOF
   done
 done
+if $slow; then
+  pr <<EOF
+mailtest_check_unexpected_spamd_results $unexpected
+EOF
+fi
+
+dir=/var/lib/prometheus/node-exporter
+if [[ -e $dir  ]]; then
+  mv $dir/mailtest-check.prom.$$ $dir/mailtest-check.prom
+fi