Benutzer:Martin:Routechecker

aus PUG, der Penguin User Group
Wechseln zu: Navigation, Suche
#!/bin/sh

# Route Checker: Checks accessibility of external sites
# and falls back to a different default gateway if needed.

PATH=/sbin:/usr/sbin:/bin:/usr/bin

DEFAULT_GATEWAY=192.168.2.1
FALLBACK_GATEWAY=192.168.3.1

MAIL_FROM=routechecker@example.com
MAIL_TO=admins@example.com

SENDMAIL=/usr/sbin/sendmail 

#            vortex.f00.net pns.dtag.de  ns1.msft.net   nserver.apple.com
CHECK_HOSTS="80.242.134.130 194.25.0.125 207.68.160.190 17.254.0.50"

# No user servicable parts beyond this point.
 
FAILED_SEMAPHORE=/var/run/routechecker-failed

notice () {
        TEXT=$1
        $SENDMAIL -f $MAIL_FROM -t <<EOF
From: Route-Checker auf `hostname` <$MAIL_FROM>
To: $MAIL_TO
Subject: $TEXT

$TEXT
EOF
}


OKAY=0
for HOST in $CHECK_HOSTS
do
        echo "+++ Checking $HOST:"
        ip route add $HOST/32 via $DEFAULT_GATEWAY
        dig +short . SOA @$HOST
        if [ $? -eq 0 ]
        then
                OKAY=1
                echo "+++ OK"
        else
                echo "--- FAIL"
        fi
        ip route del $HOST/32 via $DEFAULT_GATEWAY
done
if [ $OKAY -eq 0 ]
then
        echo "Replacing default route $DEFAULT_GATEWAY with $FALLBACK_GATEWAY"
        ip route replace default via $FALLBACK_GATEWAY
        if [ ! -f $FAILED_SEMAPHORE ]
        then
                touch $FAILED_SEMAPHORE
                logger -p user.notice -t routechecker "Fallback to $FALLBACK_GATEWAY activated."
                notice "Fallback-Route aktiviert"
        fi
else
        if [ -f $FAILED_SEMAPHORE  ]
        then
                echo "Replacing fallback route $FALLBACK_GATEWAY with $DEFAULT_GATEWAY" 
                ip route replace default via $DEFAULT_GATEWAY
                rm $FAILED_SEMAPHORE
                logger -p user.notice -t routechecker "Fallback to $FALLBACK_GATEWAY deactivated."
                notice "Fallback-Route deaktiviert"
        fi
fi