bunch of updates and fixes
[distro-setup] / system-status
index 231e61083f0a03eb4f4168b01ec257896b2a4443..c6f3fe7f5144d3c3661196289cc8cfbb062059ed 100755 (executable)
@@ -2,37 +2,98 @@
 # Copyright (C) 2019 Ian Kelling
 # SPDX-License-Identifier: AGPL-3.0-or-later
 
+# usage: runs 4 times every 15 seconds unless any args are passed, then just runs once
+
 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
 
-set -eE -o pipefail
-trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
+source /a/bin/errhandle/err
+status_file=/dev/shm/iank-status
+
+shopt -s nullglob
+shopt -s dotglob
 
-f=/dev/shm/iank-status
+lo() { /usr/local/bin/log-once "$@"; }
 
 write-status() {
   chars=()
 
-  if test -e /nocow/btrfs-stale/*; then
+  glob=(/nocow/btrfs-stale/*)
+  if [[ -e ${glob[0]} ]]; then
     chars+=("STALE!")
   fi
-  if test -e /m/md/bounces/new/*; then
+  glob=(/m/md/bounces/new/*)
+  if [[ -e ${glob[0]} ]]; then
     chars+=("BOUNCE!")
+    bouncemsg="message in /m/md/bounces/new"
+  fi
+  lo -1 bounce $bouncemsg
+  glob=(/m/md/alerts/new/* /m/md/alerts/cur/*)
+  if [[ -e ${glob[0]} ]]; then
+    chars+=("ALERT!")
   fi
   if [[ -e /nocow/user/mailtest-failure ]]; then
     chars+=("MAILPING!")
   fi
+
+  ## Clean the paniclog, but only up to 4 times per day, or else we
+  ## should investigate.
+  loglog=/tmp/panicloglog-$(date --rfc-3339=date)
+  if [[ -s $loglog ]]; then
+    spamcount=$(stat -c%s $loglog)
+  else
+    spamcount=0
+  fi
+  if (( spamcount <= 4 )); then
+    if grep -q 'spam acl condition' /var/log/exim4/paniclog; then
+      printf . >>$loglog
+    fi
+    /a/bin/distro-setup/epanic-clean
+  fi
+
   if [[ -s /var/log/exim4/paniclog ]]; then
     chars+=("PANIC!")
+    tail -n 20 /var/log/exim4/paniclog | lo -1 paniclog
+  else
+    lo -1 paniclog
   fi
 
-  cat /a/bin/bash_unpublished/source-state >$f
+  source /a/bin/bash_unpublished/source-state
+  if [[ $MAIL_HOST == "$HOSTNAME" ]]; then
+    if [[ $(systemctl is-active btrbk.timer) != active ]]; then
+      chars+=("BTRBK.TIMER!")
+      bbkmsg="btrbk.timer not enabled"
+    fi
+    lo -60 btrbk.timer $bbkmsg
 
-  if [[ $chars ]]; then
-    echo "ps_char=\"${chars[*]} \$ps_char\"" >>$f
+    ## check if last snapshot was within an hour
+    vol=o
+    snaps=(/mnt/root/btrbk/$vol.20*)
+    now=$(date +%s)
+    maxtime=0
+    for s in ${snaps[@]}; do
+      file=${s##*/}
+      t=$(date -d $(sed -r  's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${file#$vol.}) +%s)
+      if (( t > maxtime )); then
+        maxtime=$t
+      fi
+    done
+    if (( maxtime < now - 60*60 )); then
+      chars+=("OLD-SNAPSHOT!")
+      snapshotmsg="/o snapshot older than 1 hour"
+    fi
+    lo -1 old-snapshot $snapshotmsg
   fi
+
+  cat /a/bin/bash_unpublished/source-state >$status_file
+
+  if [[ ${chars[*]} ]]; then
+    echo "ps_char=\"${chars[*]} \$ps_char\"" >>$status_file
+  fi
+
 }
 write-status
-if [[ $@ ]]; then
+if [[ $1 ]]; then
+  cat $status_file
   exit 0
 fi
 for ((i=1; i<=3; i++)); do