#!/bin/bash # canardien 0.0.3 # (c) 2005 under GPL by Adrian Zaugg # canardiens pings ente.limmat.ch to determine wether she is gone diving. # ToDo: multi host, multi eMail ## Settings # # Host to ping. It's ente.limmat.ch HOST="ente.limmat.ch" # eMail Alerts # Send alert email messages to the following address(es). Leave empty # for no alert. For multiple destinations use a comma separated list. ALERT_TO="root" # Subject of alert email ALERT_SUBJECT="Attention: no answer from $HOST anymore!" # Text of Message (But in 'single quotes' to protect variables. They should get # expanded at the time the message is sent!) ALERT_TEXT='"\n[$TIME_STAMP]\n\nALERT!!\n\n\t$HOST is down!\n\nYou should probably do something, please.\n\nKind regards, $PINGHOST."' # path to fping PING=`which fping` # answer of fping to reachable hosts ALIVE_ANSWER="is alive" # Temporary file path TMPDIR="/tmp" # This hosts name PINGHOST=`uname -n` # set to an empty string to avoid debug output, # to "low" for a few, output and to anything else # for verbose output DEBUG=loud # for silent (non-error) operation set to anything, # comment out to enable text output # (migration to use shout is in progress) SILENT=shshsh # -----------functions----------- # If ente.limmat.ch responds to pings, she is considered up. function checkconnection { unset UP if [ ${#PING} -eq 0 ]; then echo "Error: fping external program not in path. Exitting." exit 1 fi PING_ANSWER="$($PING "$HOST")" PING_ERRNUM=$? if [ $PING_ERRNUM -gt 2 ]; then echo "Error $PING_ERRNUM in fping $(head -1 "$PING_ANSWER") . Exitting." exit 1 fi 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 is up." fi else if [ -n "$DEBUG" ]; then shout "The connection is down." fi fi } # Rember recent state of ente.limmat.ch, trigger alert function rememberstate { # Put a simple time stamp in a tmp file, when host gets down if [ -z "$UP" ]; then if [ ! -e "$TMPDIR/.canardien-$PINGHOST-$HOST" ]; then # set time stamp echo -ne "$TIME_STAMP" > "$TMPDIR/.canardien-$PINGHOST-$HOST" # send alert sendalert elif [ -n "$DEBUG" ]; then shout "Down since `cat $TMPDIR/.canardien-$PINGHOST-$HOST`." fi 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 # delete tmp file rm "$TMPDIR/.canardien-$PINGHOST-$HOST" else # still running if [ ! "$DEBUG" = "low" ] && [ -n "$DEBUG" ]; then shout "$HOST is still up." fi fi fi } # Send an email alert function sendalert { # send mail if [ ! -z "$ALERT_TO" ]; then eval MSG=\$$ALERT_TEXT echo -e -n "$MSG" | mail -s "$ALERT_SUBJECT" "$ALERT_TO" if [ -n "$DEBUG" ]; then shout "Alert sent to $ALERT_TO." fi fi } # To avoid any output in case of a silent operation, # shout instead of echo. function shout() { if [ -z "$SILENT" ]; then echo -e "$1" fi } # -----------main----------- checkconnection rememberstate exit 0