Website-Icon Daniel Jörg Schuppelius

Loadbalancing für Endian 2.4

#!/bin/bash
##############################################################
#                                                            #
# Filename:       loadbalancing                              #
# Author:         Daniel Schuppelius                         #
# Email:          support@schuppelius.org                    #
# Organisation:   Daniel Jörg Schuppelius                    #
# Version / Date: 1.0 / 31.01.2012                           #
# License:        GNU GPL 2.0 or above                       #
# Description:    Loadbalancing Script for Endian with       #
#                 checking of active uplinks                 #
#                                                            #
##############################################################

export uplinkDirectory="/var/efw/uplinks/";
export uplinkActiveFile="active";
export uplinkActive=0;

# localhost
export ifLO="lo";
export ntLO="127.0.0.0/8";

# loadbalancing check
export checkInterfaceList="";
export routeInterfaceList="";
export runCommand="ip route replace default scope global";

SetExport(){
  if [ "$1" != "" ] ; then
    if [ "$2" != "" ] ; then
      export $1="$2";
    else
      export $1="";
    fi
  fi
}

GenerateUplinkVars(){
  for FILE in $uplinkDirectory* ; do
    if [ -d $FILE ]; then
      if [ -f "$FILE/$uplinkActiveFile" ] ; then
        tmpifILAN=`cat "$FILE/data" | grep "interface="`;
        tmpipILAN=`cat "$FILE/data" | grep "ip_address="`;
        tmpgwILAN=`cat "$FILE/data" | grep "gateway="`;
        tmpntILAN=`cat "$FILE/data" | grep "ips="`;
        tmpckILAN=`cat "$FILE/settings" | grep "CHECKHOSTS="`;
        tmpck=`cat "$FILE/settings" | grep "LINKCHECK="`;
        if [ "${tmpckILAN:11}" == "" ] || [ "${tmpck:10}" != "on" ] ; then
          tmpckILAN=`cat "$FILE/data" | grep "domain_name_servers="`;
          SetExport "ckILAN$uplinkActive" "${tmpckILAN:20}";
        else
          SetExport "ckILAN$uplinkActive" "${tmpckILAN:11}";
        fi
        SetExport "ifILAN$uplinkActive" "${tmpifILAN:10}";
        SetExport "ipILAN$uplinkActive" "${tmpipILAN:11}";
        SetExport "gwILAN$uplinkActive" "${tmpgwILAN:8}";
        SetExport "ntILAN$uplinkActive" "${tmpntILAN:4}";
        SetExport "tbILAN$uplinkActive" "uplink-${FILE##/*/}";
        uplinkActive=$[$uplinkActive+1];
      fi
    fi
  done
  FillRunVars;
}

# $1=net $2=iface $3=ip $4=gw $5=table
InitializeUplink(){
  ip route add $1 dev $2 src $3 table $5 > /dev/null 2>&1;
  ip route add default via $4 table $5 > /dev/null 2>&1;
  ip route add $1 dev $2 src $3 > /dev/null 2>&1;
  ip rule add from $3 table $5 > /dev/null 2>&1;
  LoadIfaceToUplink $5
}

FillRunVars() {
  for ((i=0; i<$uplinkActive; i++)); do
    checkInterfaceList="${checkInterfaceList} $(eval echo \$ifILAN$i)";
    ipToPing="$(eval echo \$ckILAN$i)";
    if [ "$ipToPing" != "" ] ; then
      MultiPing $(eval echo \$ifILAN$i) $ipToPing;
      pingResult=$?;
    else
      MultiPing $(eval echo \$ifILAN$i) $(ifconfig | egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}' | egrep -v '0|255|(127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})');
      pingResult=$?;
    fi
    if [ $pingResult -eq 0 ] ; then
      routeInterfaceList="${routeInterfaceList} $(eval echo \$ifILAN$i)";
      runCommand="${runCommand} nexthop via $(eval echo \$gwILAN$i) dev  $(eval echo \$ifILAN$i) weight 1";
    fi
  done
}

LoadIfaceToUplink(){
  for ((i=0; i<$uplinkActive; i++)); do
    ip route add  $(eval echo \$ntILAN$i) dev $(eval echo \$ifILAN$i) table $1 > /dev/null 2>&1;
  done
  ip route add $ntLO dev $ifLO table $1 > /dev/null 2>&1;
}

CleanDefaultRoutes() {
  while [ $? -eq 0 ]
    do ip route delete default > /dev/null 2>&1;
  done
}

MultiPing(){
  for ip in `echo ${2}`
    do echo $ip;
ping -w 1 -c 1 -t 1 -I $1 $ip > /dev/null 2>&1;
    if [ $? -eq 0 ] ; then return 0; fi
  done
  return 1;
}

IsRouteChanged(){
  tmpRoute=`ip route | grep "nexthop via"`;
  if [ "$tmpRoute" == "" ] ; then
    if [ $uplinkActive -ge 2 ] ; then return 1; fi
    tmpRoute=`ip route | grep "default"`;
  fi
  for devi in `echo ${checkInterfaceList}`
    do if [[ $tmpRoute == *$devi* ]] ; then
      if [[ $routeInterfaceList != *$devi* ]] ; then return 1; fi
    fi
  done
  return 0;
}

PrepareUplinks(){
  echo "Cleaning default routes...";
  CleanDefaultRoutes;
  echo "Initializing uplinks...";
  for ((i=0; i<$uplinkActive; i++)); do
    InitializeUplink $(eval echo \$ntILAN$i) $(eval echo \$ifILAN$i) $(eval echo \$ipILAN$i) $(eval echo \$gwILAN$i) $(eval echo \$tbILAN$i);
  done
}

LoadbalanceRoute(){
  echo "Setting up loadbalancing...";
  $runCommand # > /dev/null 2>&1;
  echo "...${runCommand:20}";
}

DefaultRoute(){
  runCommand="ip route add default via ${gwILAN0} dev ${ifILAN0}";
  $runCommand > /dev/null 2>&1;
}

main(){
  echo "Checking uplinks..."
  GenerateUplinkVars;
  IsRouteChanged;
  if [ $? -eq 1 ] ; then
    PrepareUplinks
    if [ $uplinkActive -ge 2 ] ; then
      LoadbalanceRoute
    elif [ $uplinkActive -eq 1 ] ; then
      DefaultRoute
    fi
  fi
}

main
exit 0

Loadbalancing Skript für Endian 2.4.x – 2.5.x mit Überprüfung auf aktive Uplinks (Als Cron-Job installieren)

Die mobile Version verlassen