From cc291b2ea4681baa4306fd79261862ac711aeb40 Mon Sep 17 00:00:00 2001 From: Rob Pearce Date: Fri, 6 Nov 2020 19:20:35 +1100 Subject: [PATCH] Add -T option to use formatting suitable for Telegram messages. --- bare.sh | 148 ++++++++++++++++++++++++++++++++++-------------------- report.sh | 2 +- 2 files changed, 94 insertions(+), 56 deletions(-) diff --git a/bare.sh b/bare.sh index 6325354..0e61cf4 100755 --- a/bare.sh +++ b/bare.sh @@ -25,6 +25,7 @@ DATE=/bin/date DOALL=0 REPO_LIST="" TESTMODE=0 +TELEGRAM=0 VERBOSE=0 function cecho() { @@ -61,7 +62,11 @@ function log() { else col="$CYAN" fi - echo -e "$CYAN${now} <${mode}> $col$*$PLAIN" >>${LOG} + if [[ $TELEGRAM -eq 1 ]]; then + echo -e "__${now}__ **<${mode}>** \`$*\`" >>${LOG} + else + echo -e "$CYAN${now} <${mode}> $col$*$PLAIN" >>${LOG} + fi } function updatestats() { @@ -136,6 +141,7 @@ function usage() { echo " -x num Use 'num' simultaneous connections (default: 20)" echo " -c Cron mode - log to ${LOGFILE}" echo " -t Test mode - dump what would be done then exit." + echo " -T Telegram fornatting mode" echo "" echo "Valid commands are:" echo " $VALIDCOMMANDS" @@ -452,8 +458,25 @@ function is_mounted() { } +function check_repomount_needed() { + local path + path=`gettagval "$f" premount` + if [[ $? -eq 0 ]]; then + grep -q $path /etc/fstab + if [[ $? -ne 0 ]]; then + log "Error: repo '$REPO' has premount tag but $path not in fstab." + return 1 + fi + + MOUNTREPO=1 + else + MOUNTREPO=0 + fi + return 0 +} + # Handle args -ARGS="acd:hs:tvx:" +ARGS="acd:hs:tTvx:" while getopts "$ARGS" i; do case "$i" in @@ -480,6 +503,9 @@ while getopts "$ARGS" i; do t) TESTMODE=1 ;; + T) + TELEGRAM=1 + ;; v) VERBOSE=1 ;; @@ -696,18 +722,8 @@ for f in $REPO_LIST; do USENFS=0 fi - path=`gettagval "$f" premount` - if [[ $? -eq 0 ]]; then - grep -q $path /etc/fstab - if [[ $? -ne 0 ]]; then - log "Error: repo '$REPO' has premount tag but $path not in fstab." - continue - fi - - MOUNTREPO=1 - else - MOUNTREPO=0 - fi + check_repomount_needed "$f" # sets $MOUNTREPO + [[ $? -ne 0 ]] && continue checktag "$f" localmount if [[ $? -eq 0 ]]; then @@ -747,6 +763,7 @@ for f in $REPO_LIST; do fi fi + if [[ -z $GOODREPOS ]]; then GOODREPOS="$f" else @@ -816,23 +833,6 @@ for f in $REPO_LIST; do else USINGDEFAULT=1 fi - - if [[ $MOUNTREPO -eq 1 ]]; then - path=`gettagval "$f" premount` - [[ $VERBOSE -eq 1 ]] && log "debug: doing repo premount at $path via fstab" - if is_mounted $path; then - log "Pre-mount of '$path': already mounted" - else - mount $path - if [[ $? -eq 0 ]]; then - log "Pre-mount of '$path': success" - else - log "Pre-mount of '$path': FAILED" - continue - fi - fi - fi - if [[ $CMD == "go" ]]; then NEEDMOUNT=1 elif [[ $CMD == "get" ]]; then @@ -843,6 +843,22 @@ for f in $REPO_LIST; do NEEDMOUNT=0 fi + check_repomount_needed "$f" # sets $MOUNTREPO + if [[ $MOUNTREPO -eq 1 ]]; then + path=`gettagval "$f" premount` + [[ $VERBOSE -eq 1 ]] && log "debug: doing repo premount at $path via fstab" + if is_mounted $path; then + [[ ! $CMD == "check" ]] && log "Pre-mount of '$path': already mounted" + else + mount $path + if [[ $? -eq 0 ]]; then + [[ ! $CMD == "check" ]] && log "Pre-mount of '$path': success" + else + [[ ! $CMD == "check" ]] && log "Pre-mount of '$path': FAILED" + continue + fi + fi + fi mode=`getmode "$f"` @@ -968,20 +984,36 @@ for f in $REPO_LIST; do oneday=$( echo "24 * 60 * 60" | bc) if [[ $code -eq 0 ]]; then if [[ $age -le $oneday ]]; then - cecho "$GREEN" "OK: Last backup for '$REPO' succeeded on $humanstamp." + if [[ $TELEGRAM -eq 1 ]]; then + echo "**OK**: Last backup for __${REPO}__ succeeded on $humanstamp." + else + cecho "$GREEN" "OK: Last backup for __${REPO}__ succeeded on $humanstamp." + fi rv=0 else - cecho "$RED" "CRITICAL: Last backup for '$REPO' succeeded on $humanstamp (age $age not in last 24 hours)." + if [[ $TELEGRAM -eq 1 ]]; then + echo "**CRITICAL**: Last backup for __${REPO}__ succeeded on $humanstamp (age $age not in last 24 hours)." + else + cecho "$RED" "CRITICAL: Last backup for __${REPO}__ succeeded on $humanstamp (age $age not in last 24 hours)." + fi errcount=$(( $errcount + 1)) rv=2 fi else - cecho "$RED" "CRITICAL: Last backup for '$REPO' failed on $humanstamp." + if [[ $TELEGRAM -eq 1 ]]; then + echo "**CRITICAL**: Last backup for __${REPO}__ failed on $humanstamp." + else + cecho "$RED" "CRITICAL: Last backup for __${REPO}__ failed on $humanstamp." + fi rv=2 errcount=$(( $errcount + 1)) fi else - cecho "$YELLOW" "WARNING: No history found for repo '$REPO'" + if [[ $TELEGRAM -eq 1 ]]; then + echo "**WARNING**: No history found for repo '$REPO'" + else + cecho "$YELLOW" "WARNING: No history found for repo '$REPO'" + fi rv=1 warncount=$(( $warncount + 1)) fi @@ -1349,34 +1381,40 @@ for f in $REPO_LIST; do fi fi + if [[ $NEEDMOUNT -eq 1 ]]; then + do_umount + fi + + if [[ ! $CMD == "check" ]]; then # Record result updatestats $f $CMD $rv - fi + # did our command succeed? + if [[ $rv -ne 0 ]]; then + log "Error: '$CMD' on repo '$REPO' failed" + if [[ ! -z $errtext ]]; then + log "$errtext" + fi + fi + log "Finished '$CMD' on repo '$REPO'" + fi +done + +# Clean up +for f in $REPO_LIST; do + check_repomount_needed "$f" # sets $MOUNTREPO if [[ $MOUNTREPO -eq 1 ]]; then path=`gettagval "$f" premount` - umount $path - if [[ $? -eq 0 ]]; then - log "Un-mount of '$path': success" - else - log "Un-mount of '$path': FAILED" + if is_mounted $path; then + umount $path + if [[ $? -eq 0 ]]; then + [[ ! $CMD == "check" ]] && log "Un-mount of '$path': success" + else + [[ ! $CMD == "check" ]] && log "Un-mount of '$path': FAILED" + fi fi fi - if [[ $rv -eq 0 ]]; then - if [[ $NEEDMOUNT -eq 1 ]]; then - do_umount - fi - elif [[ ! $CMD == "check" ]]; then - log "Error: '$CMD' on repo '$REPO' failed" - if [[ ! -z $errtext ]]; then - log "$errtext" - fi - fi - - if [[ ! $CMD == "check" ]]; then - log "Finished '$CMD' on repo '$REPO'" - fi done if [[ $CMD == "check" ]]; then diff --git a/report.sh b/report.sh index f7af1ac..9dd996a 100755 --- a/report.sh +++ b/report.sh @@ -5,7 +5,7 @@ SENDER=/usr/local/bin/zabbix_sender CONF=/etc/zabbix/zabbix_agentd.conf ZABBIXHOST=gridbug.nethack.net -TEXT="`$BARE -a check`" +TEXT="`$BARE -T -a check`" RV=$?