#!/bin/bash basename=$(basename $0) supervision_status="/dev/shm/${basename}${USER:+.$USER}.status" supervision_disabled="/dev/shm/${basename}${USER:+.$USER}.disabled" touch $supervision_status $supervision_disabled now=$(date +%s) function _err () { test $# -eq 2 || return err[${#err[@]}]="$1" err[${#err[@]}]="$2%0a" } function _load () { local conf= local l= local d= local l_ok= local count_loaded= local msg= for conf in $*; do count_loaded=0 if grep -q "^@$conf$" $supervision_disabled; then disabled[${#disabled[@]}]=$conf else for dir in $HOME/.config/$basename ${0}.d /usr/local/share/${basename}; do test -r "$dir/$conf" && source "$dir/$conf" && let count_loaded++ done test $count_loaded -gt 0 && loaded[${#loaded[@]}]=$conf fi done if test ${#loaded[@]} -ne $#; then for conf in $*; do l_ok=0 for l in ${loaded[@]}; do test $conf == $l && l_ok=1 && break done if test $l_ok -eq 0; then for d in ${disabled[@]}; do test $conf == $d && msg+="$conf (disabled)%0a" && l_ok=1 && break done fi test $l_ok -eq 0 && msg+="$conf%0a" done _err "_load" "config not loaded:%0a${msg/%%0a}" fi } function _do_tests () { for t in $(declare -f | sed -rn '/^[[:alpha:]]/s/^([[:alnum:]_]+)\s*\(\)\s*$/\1/p'); do msg= if grep -q "^${t}$" $supervision_disabled; then echo supervision by $t disabled else msg=$( $t | sed 's/$/%0a/g' ) if test -n "$msg"; then echo "problem: ${t}" err[${#err[@]}]=$t err[${#err[@]}]="${msg}" else if test -n "${!t}"; then sed -ri "/^$t=.+$/d" $supervision_status fi fi fi done } function _do_manage_errors () { source $supervision_status if test ${#err[@]} -gt 0; then errors="Supervision $HOSTNAME (${loaded[@]}):%0a" notification=0 for ((i=0; i<${#err[@]}; i++)); do t=${err[i]} msg=${err[++i]} errors="${errors}"'*'" [${t}] ${msg}" if test -n "${!t}"; then if test ${!t} -lt $(( $now - 86400 )); then sed -ri "s/^(${t}=).+$/\1$now/" $supervision_status let notification++ fi else echo "${t}=$now" >> $supervision_status let notification++ fi done if test "$SHLVL" -eq 1; then if test $notification -gt 0; then $(dirname "$0")/smsapi "$errors" fi else echo -e ${errors//\%0a/\\n} fi fi } _load ${*:-common-tests $HOSTNAME} _do_tests _do_manage_errors