Here are some resource scripts to handle routing.

IProute resource script will add or delete routes on startup
IPrule resource script will add or delete ip rules on startup
IProutesrc will change device route source on startup and remove it on stop.

IProutesrc is not able to handle defaultroute so IPsrcaddr is still
needed for default route handling.


-- 
Tuomo Soini <[EMAIL PROTECTED]>
Linux and network services
+358 40 5240030
Foobar Oy <http://foobar.fi/>
#!/bin/bash
#
#       $Id: IProute,v 1.17 2004/06/10 10:10:46 tis Exp $
#
#       Copyright (C) 2004 Tuomo Soini <[EMAIL PROTECTED]>
#
#       This script manages static routes
#
#       usage: $0 {add|del} {ifname} \
#               {start|stop|restart|condrestart|status}
#
#       Config file /etc/ha.d/route.d/route-{add|del}-{ifname}
#
#       The "start" arg adds or removes static routes.
#
#       Surprisingly, the "stop" may remove or add routes.
#
unset LANG
LC_ALL=C
export LC_ALL

# source function library
. /etc/ha.d/shellfuncs

IP2UTIL=/sbin/ip
VARLIB=/var/lib/heartbeat
VLDIR=$VARLIB/rsctmp/IProute
LOCKFILE=$VLDIR/route-$1-$2
RETVAL=0

doroute() {
        rtype=$1
        rcmd=$2
        rif=$3
        rc=0
        if
            [ -f /etc/ha.d/route.d/route-${rtype}-${rif} ]
        then
            cat /etc/ha.d/route.d/route-${rtype}-${rif} | grep -v '^#' \
                | sed -e 's/#.*//' | while read line
            do
                if
                    [ "X$line" = "X" ]
                then
                    : empty line
                else
                    CMD="$IP2UTIL route ${rcmd} $line dev ${rif}"
                    if
                        $CMD
                    then
                        ha_log "info: IProute ${rtype}: $CMD"
                    else
                        ha_log "WARN: IProute ${rtype}: $CMD"
                    fi
                fi
            done
            $IP2UTIL route flush cache
        else
            ha_log "ERROR: IProute ${rtype} config file \
                /etc/ha.d/route.d/route-${rtype}-${rif} is missing"
            rc=1
        fi
        return $rc
}

usage() {
        echo "usage: $0 {add|del} {ifname} \\"
        echo -e "\t{start|stop|restart|condrestart|status}"
        exit 1
}

start() {
        doroute $1 $2 $3
        if
            [ "$?" -eq 0 ]
        then
            if
                [ -d $VLDIR ]
            then
                : vldir is there
            else
                mkdir -p $VLDIR
            fi
            touch "$LOCKFILE"
            RETVAL=$?
        else
            RETVAL=1
        fi
}

stop() {
        doroute $1 $2 $3
        if
            [ "$?" -eq 0 ]
        then
            if
                [ -d $VLDIR ]
            then
                : vldir is there
            else
                mkdir -p $VLDIR
            fi
            rm -f "$LOCKFILE"
            RETVAL=$?
        else
            RETVAL=1
        fi
}

restart() {
        stop
        start
}

if
    [ $# -ne 3 ]
then
    usage
fi

ROUTECMD="$1"
ROUTEIF="$2"
INITCMD="$3"

case "$INITCMD" in
        start)
            case "$ROUTECMD" in
                add)
                    start $ROUTECMD add $ROUTEIF
                    ;;
                del)
                    start $ROUTECMD delete $ROUTEIF
                    ;;
                *)
                    usage
            esac
            ;;
        stop) 
            case "$ROUTECMD" in
                add)
                    stop $ROUTECMD delete $ROUTEIF
                    ;;
                del)
                    stop $ROUTECMD add $ROUTEIF
                    ;;
                *)
                    usage
            esac
            ;;
        restart|force-reload)
            restart
            ;;
        reload)
            ;;
        condrestart)
            [ -f "$LOCKFILE" ] && restart
            ;;
        status)
            if [ -f $LOCKFILE ]; then
                    echo "IProute is running..."
                    RETVAL=0
            else
                    echo "IProute is stopped"
                    RETVAL=3
            fi
            ;;
        *)
            usage
esac

exit $RETVAL

# EOF
#!/bin/bash
#
#       $Id: IProutesrc,v 1.11 2006/01/30 19:27:48 tis Exp $
#
#       Copyright (C) 2006 Tuomo Soini <[EMAIL PROTECTED]>
#
#       This script changes route source
#
#       usage: $0 {ifname} {ipaddr/prefix} [rt_table] \
#               {start|stop|restart|condrestart|status}
#
#       The "start" arg changes route source or add route if missing.
#
#       Surprisingly, the "stop" remove source from route.
#
unset LANG
LC_ALL=C
export LC_ALL

# source function library
. /etc/ha.d/shellfuncs

IP2UTIL=/sbin/ip
RETVAL=0

usage() {
        echo "usage: $0 {ifname} {ipaddr/prefix} [rt_table] \\"
        echo -e "\t{start|stop|restart|condrestart|status}"
        exit 1
}

start() {
        if
            [ -n "$RTTABLE" ]
        then
            RTTABLE=" table $RTTABLE"
        fi
        $IP2UTIL route change ${NETWORK}/${PREFIX} dev ${ROUTEIF} \
            $RTTABLE src $IPADDR 2>/dev/null
        RETVAL=$?
        if
            [ "$RETVAL" -gt 0 ]
        then
            # Route seems to be missing. Create one
            LOG="$($IP2UTIL route add ${NETWORK}/${PREFIX} dev 
${ROUTEIF}${RTTABLE} src $IPADDR 2>&1)"
            RETVAL=$?
            if
                [ "$RETVAL" -gt 0 ]
            then
                RETVAL=1
                [ -z "$LOG" ] && LOG="Silent error"
                ha_log "ERROR: cmd \"$IP2UTIL route add ${NETWORK}/${PREFIX} 
dev ${ROUTEIF}${RTTABLE} src ${IPADDR}\" failed with error: \"$LOG\""
            fi
        fi
}

stop() {
        if
            [ -n "$RTTABLE" ]
        then
            RTTABLE=" table $RTTABLE"
        fi
        $IP2UTIL route change ${NETWORK}/${PREFIX} dev ${ROUTEIF}${RTTABLE} 
2>/dev/null
        RETVAL=$?
        if
            [ "$RETVAL" -gt 0 ]
        then
            # Route seems to be missing. Create one
            LOG="$($IP2UTIL route add ${NETWORK}/${PREFIX} dev 
${ROUTEIF}${RTTABLE} 2>&1)"
            RETVAL=$?
            if
                [ "$RETVAL" -gt 0 ]
            then
                RETVAL=1
                [ -z "$LOG" ] && LOG="Silent error"
                ha_log "ERROR: cmd \"$IP2UTIL route add ${NETWORK}/${PREFIX} 
dev ${ROUTEIF}${RTTABLE}\" failed with error: \"$LOG\""
            fi
        else
            RETVAL=0
        fi
}

parseroute() {
        while [ $# -gt 0 ]
        do
            case $1 in
                src)
                    SOURCE=$2
                    shift; shift
                    ;;
                *)
                    shift
                    ;;
                esac
        done
}

status() {
        if
            [ -n "$RTTABLE" ]
        then
            RTTABLE=" table $RTTABLE"
        fi
        ROUTE="$($IP2UTIL route list ${NETWORK}/${PREFIX} dev 
${ROUTEIF}${RTTABLE})"
        parseroute $ROUTE
        if
            [ "$SOURCE" = "$IPADDR" ]
        then
            echo "IProutesrc is running..."
            RETVAL=0
        else
            echo "IProutesrc is stopped"
            RETVAL=3
        fi
}

restart() {
        stop
        start
}

if
    [ $# -eq 3 ]
then
    ROUTEIF="$1"
    # use /sbin/shorewall to calculate following variables:
    # CIDR
    # NETMASK
    # NETOWORK
    # BROADCAST
    eval $(/sbin/shorewall ipcalc $2)
    IPADDR=${CIDR%/*}
    PREFIX=${CIDR#*/}
    INITCMD="$3"
    RTTABLE=
    if
        [ -z "$ROUTEIF" -o -z "$PREFIX" -o -z "$NETWORK" -o -z "$IPADDR" ]
    then
        usage
    fi
else
    if
        [ $# -ne 4 ]
    then
        usage
    fi
    ROUTEIF="$1"
    # use /sbin/shorewall to calculate following variables:
    # CIDR
    # NETMASK
    # NETOWORK
    # BROADCAST
    eval $(/sbin/shorewall ipcalc $2)
    IPADDR=${CIDR%/*}
    PREFIX=${CIDR#*/}
    RTTABLE="$3"
    INITCMD="$4"
    if
        [ -z "$ROUTEIF" -o -z "$PREFIX" -o -z "$NETWORK" -o -z "$IPADDR" ]
    then
        usage
    fi
fi

case "$INITCMD" in
        start)
            start
            $IP2UTIL route flush cache
            ;;
        stop) 
            stop
            $IP2UTIL route flush cache
            ;;
        restart|force-reload)
            restart
            $IP2UTIL route flush cache
            ;;
        reload)
            ;;
        condrestart)
            restart
            $IP2UTIL route flush cache
            ;;
        status)
            status
            ;;
        *)
            usage
esac

exit $RETVAL

# EOF
#!/bin/bash
#
#       $Id: IPrule,v 1.5 2004/06/10 10:16:29 tis Exp $
#
#       Copyright (C) 2004 Tuomo Soini <[EMAIL PROTECTED]>
#
#       This script manages iproute2 rules
#
#       usage: IPrule {add|del} {rulesetname} \
#               {start|stop|restart|condrestart|status}
#
#       Config file /etc/ha.d/route.d/rule-{add|del}-{rulesetname}
#
#       The "start" arg adds or removes rules.
#
#       Surprisingly, the "stop" may remove or add rules.
#
unset LANG
LC_ALL=C
export LC_ALL

# source function library
. /etc/ha.d/shellfuncs

IP2UTIL=/sbin/ip
VARLIB=/var/lib/heartbeat
VLDIR=$VARLIB/rsctmp/IPrule
LOCKFILE=$VLDIR/rule-$1-$2
RETVAL=0

dorule() {
        rtype=$1
        rcmd=$2
        rset=$3
        rc=0
        if
            [ -f /etc/ha.d/route.d/rule-${rtype}-${rset} ]
        then
            cat /etc/ha.d/route.d/rule-${rtype}-${rset} | grep -v '^#' \
                | sed -e 's/#.*//' | while read line
            do
                if
                    [ "X$line" = "X" ]
                then
                    : empty line
                else
                    CMD="$IP2UTIL rule $rcmd $line"
                    if
                        $CMD
                    then
                        ha_log "info: IPrule ${rtype}: $CMD"
                    else
                        ha_log "WARN: IPrule ${rtype}: $CMD"
                    fi
                fi
            done
        else
            ha_log "ERROR: IPrule ${rtype}: config file \
                /etc/ha.d/route.d/rule-${rtype}-${rset} is missing"
            rc=1
        fi
        return $rc
}

usage() {
        echo "usage: $0 {add|del} {rulesetname} \\"
        echo -e "\t{start|stop|restart|condrestart|status}"
        exit 1
}

start() {
        dorule $1 $2 $3
        if
            [ "$?" -eq 0 ]
        then
            if
                [ -d $VLDIR ]
            then
                : vldir is there
            else
                mkdir -p $VLDIR
            fi
            touch "$LOCKFILE"
            RETVAL=$?
        else
            RETVAL=1
        fi
}

stop() {
        dorule $1 $2 $3
        if
            [ "$?" -eq 0 ]
        then
            if
                [ -d $VLDIR ]
            then
                : vldir is there
            else
                mkdir -p $VLDIR
            fi
            rm -f "$LOCKFILE"
            RETVAL=$?
        else
            RETVAL=1
        fi
}

restart() {
        stop
        start
}

if
    [ $# -ne 3 ]
then
    usage
fi

RULECMD="$1"
RSETNAME="$2"
INITCMD="$3"

case "$INITCMD" in
        start)
            case "$RULECMD" in
                add)
                    start $RULECMD add $RSETNAME
                    ;;
                del)
                    start $RULECMD delete $RSETNAME
                    ;;
                *)
                    usage
            esac
            ;;
        stop) 
            case "$RULECMD" in
                add)
                    stop $RULECMD delete $RSETNAME
                    ;;
                del)
                    stop $RULECMD add $RSETNAME
                    ;;
                *)
                    usage
            esac
            ;;
        restart|force-reload)
            restart
            ;;
        reload)
            ;;
        condrestart)
            [ -f "$LOCKFILE" ] && restart
            ;;
        status)
            if [ -f $LOCKFILE ]; then
                    echo "IPrule is running..."
                    RETVAL=0
            else
                    echo "IPrule is stopped"
                    RETVAL=3
            fi
            ;;
        *)
            usage
esac

exit $RETVAL

# EOF

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________________
Linux-HA-Dev: Linux-HA-Dev@lists.linux-ha.org
http://lists.linux-ha.org/mailman/listinfo/linux-ha-dev
Home Page: http://linux-ha.org/

Reply via email to