minor bug fix
[distro-setup] / system-status
index a36240ca24c631296d5d944da3a6d2251ab02c87..b3c22e4ea8126f1a24330b039aceef3803ed1d03 100755 (executable)
 # 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
+# usage: runs 4 times every 15 seconds unless any args are passed, or we
+# are on battery power, 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
 
+verbose=false
+if [[ $1 ]]; then
+  verbose=true
+fi
+v() {
+  if $verbose; then
+    printf "%s\n" "$*"
+  fi
+}
+lo() { /usr/local/bin/log-once "$@"; }
+
 write-status() {
-  chars=()
+  chars=("${first_chars[@]}")
 
   glob=(/nocow/btrfs-stale/*)
-  if [[ -e $glob ]] ; then
+  if [[ -e ${glob[0]} ]]; then
     chars+=("STALE!")
   fi
   glob=(/m/md/bounces/new/*)
-  if [[ -e $glob ]]; then
+  if [[ -e ${glob[0]} ]]; then
     chars+=("BOUNCE!")
+    bouncemsg="message in /m/md/bounces/new"
   fi
-  glob=(/m/md/alerts/new/*)
-  if [[ -e $glob ]]; then
+  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
+
+  if ! qlen=$(/usr/sbin/exiqgrep -o 60 -c -b | awk '{print $1}'); then
+    # early in install process, we dont have permission yet for exiqgrep
+    qlen=$(sudo /usr/sbin/exiqgrep -o 60 -c -b | awk '{print $1}')
+  fi
+  if ((qlen)); then
+    chars+=("q $qlen")
+  fi
+
+  begin=false
+  cd /b/ds
+  if ! make -q ~/.local/distro-begin || [[ $(<~/.local/distro-begin) != 0 ]]; then
+    begin=true
+  fi
+
+  end=false
+  if ! make -q ~/.local/distro-end || [[ $(<~/.local/distro-end) != 0 ]]; then
+    end=true
+  fi
+
+  if $begin && $end; then
+    chars+=("DISTRO!")
+  elif $begin; then
+    chars+=("DISTRO-BEGIN!")
+  elif $end; then
+    chars+=("DISTRO-END!")
+  fi
+
+  f=~/.local/conflink
+  for _ in 1; do
+    if [[ -e $f ]]; then
+      now=$(date +%s)
+      fsec=$(stat -c%Y $f)
+      # the / 60 makes it 0-59 seconds less strict, +1 makes sure we
+      # dont have any false positives.
+      fmin=$(( (fsec - now ) / 60 + 1 ))
+      fminplus=$(( fmin + 60*24 ))
+      # Filesystem files get copied, so find any newer than the last run.
+      # The rest are hueristics:
+      # Given the last time we added a file in git, is that newer than the last conflink run.
+      # Given new files not added to git, were they modified more recently than the last conflink? but,
+      # push their modification time back by a day so we can develop them before needing to add them to git.
+
+      all_dirs=({/a/bin/ds,/p/c}{/filesystem,/machine_specific/$HOSTNAME/filesystem})
+      # This part is copied from conflink
+      for x in /p/c/machine_specific/*.hosts /a/bin/ds/machine_specific/*.hosts; do
+        if grep -qxF $HOSTNAME $x; then all_dirs+=( ${x%.hosts} ); fi
+      done
+
+      # just because i forget a lot, -mmin -NUM means files modified <= NUM minutes ago
+      if (( $(date -d "$(git log --diff-filter=ACR --format=%aD -1)" +%s) > fsec )) || \
+           [[ $(find ${all_dirs[@]} -mmin $fmin -type f -print -quit 2>/dev/null) ]]; then
+        v conflink newer git or newer filesystem files
+        chars+=("CONFLINK!")
+        break
+      fi
+
+      for d in /a/bin/distro-setup /p/c; do
+        cd $d
+        if [[ ! -e .git ]]; then
+          # some hosts i dont push all of /p/c
+          continue
+        fi
+        untracked=$(git ls-files -o --exclude-standard)
+        if [[ $untracked && $(find $untracked -mmin $fminplus -type f -print -quit) ]]; then
+          v conflink: untracked in $d
+          chars+=("CONFLINK!")
+          break
+        fi
+      done
+
+    fi
+    if [[ ! -e $f || $(<$f) != 0 ]]; then
+      v conflink: last run not found or failed
+      chars+=("CONFLINK!")
+      break
+    fi
+  done
+
+
+  ## 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 &>/dev/null; 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
 
   source /a/bin/bash_unpublished/source-state
-  if [[ $MAIL_HOST == $HOSTNAME ]]; then
+  if [[ $MAIL_HOST == "$HOSTNAME" ]]; then
+    bbkmsg=
     if [[ $(systemctl is-active btrbk.timer) != active ]]; then
-      chars+=("BTRBK-TIMER!")
+      chars+=("BTRBK.TIMER!")
+      bbkmsg="btrbk.timer not enabled"
     fi
+    lo -60 btrbk.timer $bbkmsg
 
     ## check if last snapshot was within an hour
     vol=o
-    snaps=(/mnt/root/btrbk/$vol.20*)
+    # this section generally copied from btrbk scripts, but
+    # this part modified to speed things up by about half a second.
+    # I'm not sure if its quite as reliable, but it looks pretty safe.
+    # Profiled it using time and also adding to the top of the file:
+    # set -x
+    # PS4='+ $(date "+%2N") '
+    snaps=($(ls -1avdr /mnt/root/btrbk/$vol.20*|head -n1))
     now=$(date +%s)
     maxtime=0
     for s in ${snaps[@]}; do
@@ -55,22 +174,36 @@ write-status() {
       fi
     done
     if (( maxtime < now - 60*60 )); then
-      chars+=("OLD-SNAPSHOT!")
+      chars+=("OLD-SNAP!")
+      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
+  if [[ ${chars[*]} ]]; then
     echo "ps_char=\"${chars[*]} \$ps_char\"" >>$status_file
   fi
 
 }
+# use this if we want to do something just once per minute
+first_chars=()
+
+power=true
+if [[ -e /sys/class/power_supply/AC/online && $(</sys/class/power_supply/AC/online) == 0 ]]; then
+  power=false
+fi
+
 write-status
-if [[ $@ ]]; then
+if [[ $1 ]]; then
   cat $status_file
   exit 0
 fi
+
+if ! $power; then
+  exit 0
+fi
 for ((i=1; i<=3; i++)); do
   sleep 15
   write-status