#!/bin/bash # 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, 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 source /a/bin/errhandle/err status_file=/dev/shm/iank-status shopt -s nullglob shopt -s dotglob shopt -s extglob for p in ~/.gem/ruby/*/bin; do PATH="$PATH:$p" done verbose=false if [[ $1 ]]; then verbose=true fi v() { if $verbose; then printf "%s\n" "$*" fi } # log-once COUNT NAME [MESSAGE] lo() { /usr/local/bin/log-once "$@" | ifne mail -s "$HOSTNAME: system-status $2" root@localhost } write-status() { chars=("${first_chars[@]}") # clock us out in timetrap if are idle too long if [[ -e /p/.timetrap.db ]]; then export DISPLAY=:0 if type -p xprintidle &>/dev/null && xidle=$(xprintidle 2>/dev/null); then if [[ $xidle == [0-9]* ]]; then sheet=$(sqlite3 /p/.timetrap.db "select sheet from entries where end is NULL;") idle=300000 if [[ $sheet == w ]]; then idle=900000 fi if [[ $sheet && $xidle -gt $idle ]]; then timetrap out fi fi fi fi if pgrep -f 'emacs --daemon' &>/dev/null; then emacsfiles="$(emacsclient --eval "$(cat /a/bin/ds/unsaved-buffers.el)"| sed '/^"nil"$/d;s/^"(/E: /;s/)"$//')" if [[ $emacsfiles ]]; then chars+=("$emacsfiles") fi fi glob=(/nocow/btrfs-stale/*) if [[ -e ${glob[0]} ]]; then chars+=("STALE") fi if [[ $(find /var/mail -type f \! -empty -print -quit) ]]; then var_mail_msg="message in /var/mail" fi lo -1 var_mail $var_mail_msg glob=(/m/md/bounces/new/*) if [[ -e ${glob[0]} ]]; then chars+=("BOUNCE") bouncemsg="message in /m/md/bounces/new" fi lo -1 bounce $bouncemsg # emails without the S (seen) flag. this only checks the last flag, # but its good enough for me. glob=(/m/md/alerts/{new,cur}/!(*,S)) if [[ -e ${glob[0]} ]]; then chars+=("A") fi tmp=(/var/local/cron-errors/mailtest-check*) if (( ${#tmp[@]} )); then chars+=("MAILPING") fi tmp=(/var/local/cron-errors/mailtest-slow*) if (( ${#tmp[@]} )); then chars+=("SPAMD") fi # early in install process, we dont have permission yet for exiqgrep qlen=$(/usr/sbin/exiqgrep -o 600 -c -b | awk '{print $1}') ||: if ((qlen)); then qmsg="queue length $qlen" chars+=("q $qlen") fi case $HOSTNAME in # No point in emailing about the mailq on a host where we don't # check email. $MAIL_HOST|bk) lo -120 qlen $qmsg ;; esac begin=false if ! make -C /b/ds -q ~/.local/distro-begin || [[ $(<~/.local/distro-begin) != 0 ]]; then begin=true fi end=false if ! make -C /b/ds -q ~/.local/distro-end || [[ $(<~/.local/distro-end) != 0 ]]; then end=true fi # these conditions are so we dont have an overly verbose prompt if $begin && $end; then chars+=("D") elif $begin; then chars+=("DB") elif $end; then chars+=("DE") else f=~/.local/conflink # shellcheck disable=SC2043 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 to help make sure we # dont have any false positives. fmin=$(( (fsec - now + 1 ) / 60 )) 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 (( fmin < 0 )) && [[ $(find ${all_dirs[@]} -mmin $fmin -type f -print -quit 2>/dev/null) ]]; then v conflink 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 if (( $(date -d "$(git log --diff-filter=ACR --format=%aD -1)" +%s) > fsec )); then v conflink: newer files checked in to git chars+=("CONFLINK") break fi untracked=() while read -r l; do untracked+=("$l") done < <(git ls-files -o --exclude-standard) if [[ ${untracked[0]} && $(find "${untracked[@]}" -mmin $fminplus -type f -print -quit) ]]; then v conflink: untracked in $d chars+=("CONFLINK") break fi done cd / fi if [[ ! -e $f || $(<$f) != 0 ]]; then v conflink: last run not found or failed chars+=("CONFLINK") break fi done fi if [[ -s /var/log/exim4/paniclog ]]; then chars+=("PANIC!") # leave it up to epanic-clean to send email notification fi source /a/bin/bash_unpublished/source-state if [[ $MAIL_HOST == "$HOSTNAME" ]]; then bbkmsg= if [[ $(systemctl is-active btrbk.timer) != active ]]; then chars+=("BTRBK.TIMER") bbkmsg="btrbk.timer not enabled" fi lo -960 btrbk.timer $bbkmsg ## check if last snapshot was within an hour vol=o # 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") ' # allow failure in case there are no snapshots yet. # shellcheck disable=SC2012 shopt -u nullglob files=(/mnt/root/btrbk/$vol.20*) shopt -s nullglob snaps=() if (( ${#files[@]} )); then snaps=($(ls -1avdr "${files[@]}" 2>/dev/null |head -n1 || : )) fi 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 - 4*60*60 )); then chars+=("OLD-SNAP") snapshotmsg="/o snapshot older than 4 hours" 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 } # use this if we want to do something just once per minute first_chars=() power=true if [[ -e /sys/class/power_supply/AC/online && $(