--- badi/public_scripts/canardien/canardien 2019/04/16 07:28:47 1.5 +++ badi/public_scripts/canardien/canardien 2023/08/06 20:06:51 1.6 @@ -1,12 +1,12 @@ #!/bin/bash -# canardien 0.0.4 -# (c) 2005-2019 under GPL by Adrian Zaugg +# canardien 0.0.6 +# (c) 2005-2021 under GPL by Adrian Zaugg # canardien [ [ ...]] -# canardiens pings a machine, originally ente.limmat.ch thus its name, +# canardiens pings a machine, originally ente.limmat.ch thus its name, # to determine wether she is gone diving. @@ -38,7 +38,7 @@ ALIVE_ANSWER="is alive" # Max number of pakets to send before giving up. Time increases exponentially, # use a number < 7. -RETRIES=4 +RETRIES=5 # Temporary file path TMPDIR="/tmp" @@ -78,46 +78,47 @@ function init() { if [ -n "$*" ]; then HOSTS="$*" fi + + # remove multiple spaces and trim + HOSTS="$(echo "$HOSTS" | sed -e "s/ \{1,\}/ /g" -e "s/^ //" -e "s/ $//")" + if [ -z "$HOSTS" ]; then echo "Error: No host to ping." >&2 - exit 0 + exit 1 fi } # If a host responds to pings, it is considered up. function checkconnection() { - - while read -r HOST; do - unset UP - # ping host - PING_ANSWER="$($PING -R -B 2 -r $RETRIES -p 50 "$HOST" 2>&1)" - PING_ERRNUM=$? - if [ $PING_ERRNUM -gt 2 ]; then - echo "Error: Got error $PING_ERRNUM from fping $(head -1 "$PING_ANSWER"). Disregarding $HOST." >&2 - continue; - fi + unset UP - # parse answer - if [ $(echo "$PING_ANSWER" | grep -c "$ALIVE_ANSWER") -gt 0 ]; then - UP=true + # ping host + PING_ANSWER="$($PING -R -B 2 -r $RETRIES -p 50 "$HOST" 2>&1)" + PING_ERRNUM=$? + if [ $PING_ERRNUM -gt 2 ]; then + echo "Error: Got error $PING_ERRNUM from fping $(head -1 "$PING_ANSWER"). Disregarding $HOST." >&2 + continue; + fi + + # parse answer + if [ $(echo "$PING_ANSWER" | grep -c "$ALIVE_ANSWER") -gt 0 ]; then + UP=true + fi + TIME_STAMP="$(date +"%a %e.%m.%y %H:%M:%S")" + if [ -n "$UP" ]; then + if [ ! "$DEBUG" = "low" ] && [ -n "$DEBUG" ]; then + shout "The connection to $HOST is up." fi - TIME_STAMP="$(date +"%a %e.%m.%y %H:%M:%S")" - if [ -n "$UP" ]; then - if [ ! "$DEBUG" = "low" ] && [ -n "$DEBUG" ]; then - shout "The connection to $HOST is up." - fi - else - if [ -n "$DEBUG" ]; then - shout "The connection to $HOST is down." - fi + else + if [ -n "$DEBUG" ]; then + shout "The connection to $HOST is down." fi - - # remember a hosts state - rememberstate - - done <<< "$(echo "$HOSTS" | tr ' ' '\n')" + fi + + # remember a hosts state + rememberstate } @@ -125,7 +126,7 @@ function checkconnection() { function rememberstate() { # Put a simple time stamp in a tmp file, when host is detected as down for the first time if [ -z "$UP" ]; then - if [ ! -e "$TMPDIR/.canardien-$PINGHOST-$HOST" ]; then + if [ ! -e "$TMPDIR/.canardien-$PINGHOST-$HOST" ]; then # set time stamp echo -ne "$TIME_STAMP" > "$TMPDIR/.canardien-$PINGHOST-$HOST" # send alert @@ -133,19 +134,19 @@ function rememberstate() { elif [ -n "$DEBUG" ]; then shout "Down since `cat $TMPDIR/.canardien-$PINGHOST-$HOST`." fi - else + else # Host is up if [ -e "$TMPDIR/.canardien-$PINGHOST-$HOST" ]; then if [ -n "$DEBUG" ]; then shout "$HOST is up again." fi - # send alert - ALERT_SUBJECT='"The host $HOST answers again!"' - ALERT_TEXT='"[$(date +"%a %e.%m.%y %H:%M:%S")]\n\n\n$HOST is up again.\n--------------------------------------\n(downtime began $(cat "$TMPDIR/.canardien-$PINGHOST-$HOST"))\n\n\n Kind regards, $PINGHOST."' - sendalert + # send alert + ALERT_SUBJECT='"The host $HOST answers again!"' + ALERT_TEXT='"[$(date +"%a %e.%m.%y %H:%M:%S")]\n\n\n$HOST is up again.\n--------------------------------------\n(downtime began $(cat "$TMPDIR/.canardien-$PINGHOST-$HOST"))\n\n\n Kind regards, $PINGHOST."' + sendalert # delete tmp file rm "$TMPDIR/.canardien-$PINGHOST-$HOST" - else + else # still running if [ ! "$DEBUG" = "low" ] && [ -n "$DEBUG" ]; then shout "$HOST is up." @@ -181,5 +182,23 @@ function shout() { # -----------main----------- init "$*" -checkconnection + +# call an instance for each host, when multiple hosts are given +if [[ "$HOSTS" =~ " " ]]; then + while read -r HOST; do + "$0" "$HOST" & + done <<< "$(echo "$HOSTS" | tr ' ' '\n')" + exit 0 +else + # process a single host + HOST="$HOSTS" + checkconnection +fi + exit 0 + +# todo: Switches ( -q, --debug, ...) +# grace period: do not send a mail if down time shorter than X minutes +# repeat down info after x hours +# test port instead of ping or in addition +