version 1.2, 2009/12/17 11:16:34
|
version 1.5, 2010/01/09 04:25:42
|
Line 28 HOSTSET_FILE="$CONFIG_PATH/hostset"
|
Line 28 HOSTSET_FILE="$CONFIG_PATH/hostset"
|
GATEWAYS_FILE="$CONFIG_PATH/gateways" |
GATEWAYS_FILE="$CONFIG_PATH/gateways" |
|
|
# path to fping |
# path to fping |
PING=/usr/sbin/fping |
PING=/usr/bin/fping |
|
|
# answer of fping to reachable hosts |
# answer of fping to reachable hosts |
ALIVE_ANSWER="is alive" |
ALIVE_ANSWER="is alive" |
Line 36 ALIVE_ANSWER="is alive"
|
Line 36 ALIVE_ANSWER="is alive"
|
# set to an empty string to avoid debug output |
# set to an empty string to avoid debug output |
# to "low" for a few output and to anything else |
# to "low" for a few output and to anything else |
# for verbose output |
# for verbose output |
DEBUG= |
DEBUG=low |
|
|
|
|
# -------- Do not edit below this line -------- |
# -------- Do not edit below this line -------- |
Line 47 PINGARGS="-A -p25 -t100"
|
Line 47 PINGARGS="-A -p25 -t100"
|
## Function declarations |
## Function declarations |
# |
# |
|
|
|
# parse commandline switches |
|
function parse_commandline { |
|
|
|
# check rest |
|
while [ "$#" -gt 0 ]; do |
|
|
|
case "$1" in |
|
|
|
-d|--debug) |
|
# debug switch (another hidden feature) |
|
shift |
|
if [ "$1" != "low" ]; then |
|
DEBUG="choke" |
|
echo "Debug mode on." |
|
else |
|
#echo "Info mode on." |
|
DEBUG="low" |
|
shift |
|
fi |
|
;; |
|
|
|
-C) |
|
# Parse writeable dir request (hidden feature) |
|
echo "$DHCP_TMP_DIR" |
|
exit 0 |
|
;; |
|
|
|
-q|--query) |
|
# Parse query for current gw (yet another hidden feature) |
|
shift |
|
parse_commandline "$@" |
|
getcurrentgw |
|
echo "$CURRENTGW" |
|
exit 0 |
|
;; |
|
|
|
-s|--set) |
|
# Set gateway to number n (yet another hidden feature) |
|
shift |
|
set_to_gw="$1" |
|
shift |
|
parse_commandline "$@" |
|
set_gw |
|
exit 0 |
|
;; |
|
|
|
*) |
|
echo "Invalid argument \"$1\"." |
|
exit 1 |
|
;; |
|
|
|
esac |
|
done |
|
} |
|
|
# Read gateways from file |
# Read gateways from file |
function readgwlist { |
function readgwlist { |
if [ -f "$GATEWAYS_FILE" ]; then |
if [ -f "$GATEWAYS_FILE" ]; then |
Line 85 function readgwlist {
|
Line 140 function readgwlist {
|
unset alldhcpgates |
unset alldhcpgates |
done |
done |
fi |
fi |
|
|
if [ "$number_of_gws" -eq 0 ]; then |
if [ "$number_of_gws" -eq 0 ]; then |
echo "No gateways configured. Please edit $GATEWAYS_FILE." |
echo "No gateways configured. Please edit $GATEWAYS_FILE." |
exit 1 |
exit 1 |
Line 121 function checkconnection {
|
Line 176 function checkconnection {
|
if [ -n "$DEBUG" ]; then |
if [ -n "$DEBUG" ]; then |
if [ -n "$UP" ]; then |
if [ -n "$UP" ]; then |
echo "The connection is up." |
echo "The connection is up." |
else |
else |
echo "The connection is down." |
echo "The connection is down." |
fi |
fi |
fi |
fi |
Line 134 function checkgw {
|
Line 189 function checkgw {
|
echo "$NEWGW is reachable." |
echo "$NEWGW is reachable." |
fi |
fi |
else |
else |
if [ -n "$DEBUG" ]; then |
if [ -n "$DEBUG" ]; then |
echo "$NEWGW is not reachable." |
echo "$NEWGW is not reachable." |
fi |
fi |
NEWGW="" |
NEWGW="" |
fi |
fi |
} |
} |
|
|
|
# Get the current default gateway (or set it, if there was none set) |
function getcurrentgw { |
function getcurrentgw { |
iproute_default_gw_txt="$(ip route show scope global)" |
iproute_default_gw_txt="$(ip route show scope global)" |
if [ `echo $iproute_default_gw_txt | grep -c default` -gt 1 ]; then |
if [ `echo $iproute_default_gw_txt | grep -c default` -gt 1 ]; then |
Line 148 function getcurrentgw {
|
Line 204 function getcurrentgw {
|
exit 1 |
exit 1 |
else |
else |
iproute_default_gw_txt="$(echo "$iproute_default_gw_txt" | grep "default via")" |
iproute_default_gw_txt="$(echo "$iproute_default_gw_txt" | grep "default via")" |
fi |
fi |
if [ -z "$iproute_default_gw_txt" ]; then |
if [ -z "$iproute_default_gw_txt" ]; then |
# no default gateway currently set, set to the highest index, to land on GW0 |
# no default gateway currently set, set to the highest index, to land on GW0 |
CURRENTGW_ID=0 |
CURRENTGW_ID=0 |
CURRENTGW=${GW[$CURRENTGW_ID]} |
CURRENTGW=${GW[$CURRENTGW_ID]} |
CURRENTGW_DEV="unknown" |
|
echo "No default gateway currently set. Setting it now to $CURRENTGW." |
echo "No default gateway currently set. Setting it now to $CURRENTGW." |
ip route add default via $CURRENTGW |
ip route add default via $CURRENTGW |
|
if [ $? -eq 2 ]; then |
|
echo "Not able to switch default gateway. Is your interface up?" |
|
exit 1 |
|
fi |
else |
else |
CURRENTGW=`echo $iproute_default_gw_txt | sed "s/^default via \(\([0-9]\+\.\?\)\{4\}\).*\$/\1/"` |
CURRENTGW=`echo $iproute_default_gw_txt | sed "s/^default via \(\([0-9]\+\.\?\)\{4\}\).*\$/\1/"` |
CURRENTGW_DEV=`echo $iproute_default_gw_txt | sed "s/^default via .* dev \(eth[0-9]\+\).*\$/\1/"` |
|
fi |
fi |
|
CURRENTGW_DEV=`echo $iproute_default_gw_txt | sed "s/^default via .* dev \(eth[0-9]\+\).*\$/\1/"` |
|
|
# get index of current gateway |
# get index of current gateway |
index=-1 |
index=-1 |
Line 201 function getnextgw {
|
Line 260 function getnextgw {
|
function getnewgw { |
function getnewgw { |
getnextgw |
getnextgw |
# Check wheter all gateways are already tested and failed |
# Check wheter all gateways are already tested and failed |
if [ "$NEWGW_ID" -eq "$CURRENTGW_ID" ]; then |
if [[ "$NEWGW_ID" -eq "$CURRENTGW_ID" && -z $set_to_gw ]]; then |
if [ -n "$DEBUG" ]; then |
if [ -n "$DEBUG" ]; then |
echo "All gateways tried." |
echo "All gateways tried." |
fi |
fi |
Line 227 function getnewgw {
|
Line 286 function getnewgw {
|
fi |
fi |
} |
} |
|
|
|
# Switch the gateway to a desired gateway |
|
function set_gw { |
|
|
|
# get name of gateway |
|
if [[ ! "$set_to_gw" =~ ^[0-9]{1,}$ ]]; then |
|
echo "Only positive numbers allowed. Invalid value \"$set_to_gw\"." |
|
exit 1 |
|
fi |
|
|
|
NEWGW=${GW[$set_to_gw]} |
|
if [ -z "$NEWGW" ]; then |
|
echo "The gateway number $set_to_gw is not in the list of available gateways." |
|
echo "Valid gateways are:" |
|
for ((gw=0; gw<${#GW[*]}; gw++)); do |
|
echo -e "\t$gw (${GW[$gw]})" |
|
done |
|
exit 1 |
|
fi |
|
if [ -n "$DEBUG" -a "$DEBUG" != "low" ]; then |
|
echo "Default gateway desired: $NEWGW ($set_to_gw)" |
|
fi |
|
|
|
getcurrentgw |
|
NEWGW_ID=$[ $set_to_gw - 1 ] |
|
switchgw |
|
} |
|
|
function switchgw { |
function switchgw { |
getnewgw |
getnewgw |
if [ -n "$NEWGW" ]; then |
if [ -n "$NEWGW" ]; then |
if [ "$DEBUG" = "low" ]; then |
if [ "$DEBUG" = "low" ]; then |
echo -n "Switching default gateway now to $NEWGW ($NEWGW_ID)..." |
echo -n "Switching default gateway now to $NEWGW ($NEWGW_ID)..." |
fi |
fi |
|
|
iproute_msg="$(ip route change default via $NEWGW 2>&1 )" |
iproute_msg="$(ip route change default via $NEWGW 2>&1 )" |
iproute_exit=$? |
iproute_exit=$? |
|
|
Line 241 function switchgw {
|
Line 327 function switchgw {
|
sleep 1 |
sleep 1 |
GARBAGE=`$PING -q -c 5 -t100 -p50 -A -f $HOSTSET_FILE 2> /dev/null` |
GARBAGE=`$PING -q -c 5 -t100 -p50 -A -f $HOSTSET_FILE 2> /dev/null` |
sleep 1 |
sleep 1 |
|
|
if [ "$DEBUG" = "low" ]; then |
if [ "$DEBUG" = "low" ]; then |
echo "done." |
echo "done." |
fi |
fi |
Line 274 function switchgw {
|
Line 360 function switchgw {
|
## MAIN |
## MAIN |
# |
# |
|
|
# Parse writeable dir request (hidden feature) |
|
if [ "$1" = "-C" ]; then |
|
echo "$DHCP_TMP_DIR" |
|
exit 0 |
|
fi |
|
|
|
# Parse debug switch (another hidden feature) |
# Check fping existence |
if [ "$1" = "-d" ]; then |
if [ ! -x "$PING" ]; then |
if [ "$2" != "low" ]; then |
echo -e "Error: Ping program not executable or not found at $PING.\nPlease configure the full path in $0." |
echo "Debug mode on." |
exit 1 |
DEBUG="choke" |
|
else |
|
#echo "Info mode on." |
|
DEBUG="low" |
|
fi |
|
fi |
fi |
|
|
# Get all gateways, we can forward traffic |
# Get all gateways, we can forward traffic |
readgwlist |
readgwlist |
|
|
# Parse query for current gw (yet another hidden feature) |
# parse commandline switches |
if [ "$1" = "-q" ]; then |
parse_commandline "$@" |
DEBUG= |
|
getcurrentgw |
|
echo "$CURRENTGW" |
|
exit 0 |
|
fi |
|
|
|
# get current gw |
# get current gw |
getcurrentgw |
getcurrentgw |
Line 321 if [ -z "$UP" ]; then
|
Line 392 if [ -z "$UP" ]; then
|
checkgw |
checkgw |
if [ -z "$NEWGW" ]; then |
if [ -z "$NEWGW" ]; then |
echo "Gateway $CURRENTGW is down." |
echo "Gateway $CURRENTGW is down." |
else |
else |
echo "No connection through gateway $CURRENTGW." |
echo "No connection through gateway $CURRENTGW." |
fi |
fi |
|
|
NEWGW_ID=$CURRENTGW_ID |
NEWGW_ID=$CURRENTGW_ID |
switchgw |
switchgw |
|
|
else |
else |
# If the current gateway is not the standard default gateway, try if the connection |
# If the current gateway is not the standard default gateway, try if the connection |
# through the standard default gateway is back |
# through the standard default gateway is back |
Line 347 fi
|
Line 418 fi
|
|
|
# send an eMail if the connection is down or limited |
# send an eMail if the connection is down or limited |
if [ -z "$UP" ]; then |
if [ -z "$UP" ]; then |
EMAIL_BODY="Switchgate on $(hostname -f) reports: No connection!\n\n\nTime:\t\t$(date)\nGateway:\t$CURRENTGW ($CURRENTGW_ID)" |
EMAIL_BODY="Switchgate on $(hostname -f) reports: No connection!\n\nTime:\t\t$(date)\nGateway:\t$CURRENTGW ($CURRENTGW_ID)" |
EMAIL_SUBJECT="Attention: $(hostname -f) has no connection!" |
EMAIL_SUBJECT="Attention: $(hostname -f) has no connection!" |
echo -e "$EMAIL_BODY" | mail -s "$EMAIL_SUBJECT" "$ALERT_EMAIL" |
echo -e "$EMAIL_BODY" | mail -s "$EMAIL_SUBJECT" "$ALERT_EMAIL" |
if [[ -n "$DEBUG" && "$DEBUG" != "low" ]]; then |
if [[ -n "$DEBUG" && "$DEBUG" != "low" ]]; then |
echo "Alert message sent to $ALERT_EMAIL." |
echo "Alert message sent to $ALERT_EMAIL." |
fi |
fi |
elif [ "$CURRENTGW_ID" != 0 ]; then |
elif [ "$CURRENTGW_ID" != 0 ]; then |
EMAIL_BODY="Switchgate on $(hostname -f) reports: Not operating on standard default gateway!\n\n\nTime:\t\t$(date)\nGateway:\t$CURRENTGW ($CURRENTGW_ID)" |
EMAIL_BODY="Switchgate on $(hostname -f) reports: Not operating on standard default gateway!\n\nTime:\t\t$(date)\nGateway:\t$CURRENTGW ($CURRENTGW_ID)" |
EMAIL_SUBJECT="Attention: $(hostname -f) Not operating on standard default gateway!" |
EMAIL_SUBJECT="Attention: $(hostname -f) not operating on standard default gateway!" |
echo -e "$EMAIL_BODY" | mail -s "$EMAIL_SUBJECT" "$ALERT_EMAIL" |
echo -e "$EMAIL_BODY" | mail -s "$EMAIL_SUBJECT" "$ALERT_EMAIL" |
if [[ -n "$DEBUG" && "$DEBUG" != "low" ]]; then |
if [[ -n "$DEBUG" && "$DEBUG" != "low" ]]; then |
echo "Alert message sent to $ALERT_EMAIL." |
echo "Alert message sent to $ALERT_EMAIL." |