fix beetag
[distro-setup] / mailtest-check
index cce5908fb78267d53c06a8bb224df0248b40daca..06ddec68063f9415a05db1a7f5ae51cb8d0227a9 100755 (executable)
@@ -28,11 +28,6 @@ getspamdpid() {
     done
   fi
 }
-pr() {
-  if $doprom && [[ -e /var/lib/prometheus/node-exporter ]]; then
-    cat >>/var/lib/prometheus/node-exporter/mailtest-check.prom.$$
-  fi
-}
 
 
 #### begin arg processing ####
@@ -81,6 +76,7 @@ esac
 
 main() {
 
+  local -a unexpected_spamd_results missing_dnswl last_usec
   case $HOSTNAME in
     bk)
       folders=(/m/md/{expertpathologyreview.com,amnimal.ninja}/testignore)
@@ -104,6 +100,7 @@ main() {
           try_left=$(( try_limit - ( EPOCHSECONDS - try_start_time) ))
           timeout=120 # somewhat arbitrary value
           if (( try_left < 0 )); then
+            echo "mailtest-check: failed to rsync fencepost > $try_limit seconds"
             break
           fi
           if (( try_left < timeout )); then
@@ -145,10 +142,11 @@ EOF
   fi
   tmpfile=$(mktemp)
   declare -i unexpected=0
-  declare -i missing_dnswl=0
-  declare -i dnsfail=0
   for folder in ${folders[@]}; do
     for from in ${froms[@]}; do
+      declare -i missing_dnswl=0
+      declare -i dnsfail=0
+      declare -i unexpected=0
       latest=
       last_sec=0
 
@@ -217,8 +215,8 @@ EOF
             # eggs has RCVD_IN_DNSWL_MED
             keys+=(RCVD_IN_DNSWL_MED)
           elif [[ $from == *@gnu.org ]]; then
-            # eggs has these
-            keys+=(RCVD_IN_DNSWL_MED DKIMWL_WL_HIGH)
+            # eggs has this. it used to have DKIMWL_WL_HIGH sometime in 2022
+            keys+=(RCVD_IN_DNSWL_MED)
           fi
 
           for t in  ${keys[@]}; do
@@ -289,6 +287,12 @@ EOF
                 ;;
             esac
           done
+          mapfile -O ${#missing_dnswl[@]} -t missing_dnswl <<EOF
+mailtest_check_missing_dnswl{folder="$folder",from="$from"} $missing_dnswl
+EOF
+          mapfile -O ${#unexpected_spamd_results[@]} -t unexpected_spamd_results <<EOF
+mailtest_check_unexpected_spamd_results{folder="$folder",from="$from"} $unexpected
+EOF
         fi # if spamdpid
       fi # if $slow
 
@@ -297,21 +301,25 @@ EOF
       e $((age_sec / 60)):$(( age_sec % 60 )) ago. to:$to from:$from $latest
 
       # usec = unix seconds
-      pr <<EOF
+      mapfile -O ${#last_usec[@]} -t last_usec <<EOF
 mailtest_check_last_usec{folder="$folder",from="$from"} $last_sec
 EOF
     done # end for from in ${froms[@]}
   done # end for folder in ${folders[@]}
-  if $slow; then
-    pr <<EOF
-mailtest_check_missing_dnswl $missing_dnswl
-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
+  path=$dir/mailtest-check.prom.$$
+  if $doprom && [[ -e $dir  ]]; then
+    for l in "${unexpected_spamd_results[@]}"; do
+      printf "%s\n" "$l" >>$path
+      done
+    for l in "${missing_dnswl[@]}"; do
+      printf "%s\n" "$l" >>$path
+    done
+    for l in "${last_usec[@]}"; do
+      printf "%s\n" "$l" >>$path
+    done
+    mv $path $dir/mailtest-check.prom
     # note: node_textfile_mtime_seconds will tell us when this last happened. useful for debugging.
   fi
 }