Load Balancer / Multi-WAN - Gateway wont change [workaround]



  • Hey Everyone on the board,
    I looked around here quite a bit, read the multi-wan documentation on the wiki/docs areas but still was unable to get it working.
    First let me explain what I was trying to accomplish. I wanted a CARP router solution with Failover to WAN2 only if WAN1 was down. I did not want to load balance these connections because one was a T3 and the other a T1. After many failed attempts I finally just setup 1 load balancer pool with both my gateways.
    I set it up as follows

    Type: Gateway
    Behaviour: Failover
    Monitor IP #1 : WAN 1 Gateway
    Interface #1  : WAN
    Monitor IP #2 : WAN 2 Gateway
    Interface #2  : WAN2

    Then I just used the script outlined below. This script still needs a-lot of work, like another to check that it  is still running, heartbeat, etc… If someone wants to add-on to it that would be great.

    #!/usr/local/bin/php -f

    01/18/2008 Copyright Reza Ambler reza@rndcomputing.com# All rights reserved.

    while(true)
    {
      check_gateways();
      sleep(2);
    }

    function check_gateways()
    {
      // lb is the name of the load balancer pool that
      // has the gateways for wan1 and wan2 in it
      // line #1 in this pool file is the currently working gateway

    $working_gw = exec ( "head -n 1 /tmp/lb.pool" ) ;

    // figure out the current default gateway

    exec("netstat -nr -f inet", $routes);
      $routes = implode("\n", $routes);

    if (preg_match("/^default\s+([\d.]+)/m", $routes, $gateway) == 1) {
          $current_gw = $gateway[1];
      }

    // if working_gw is not equal, to the current default gateway
      // in netstat then we know the current default gw is down
      // proceed to switch the default gw and flush the states

    if ($working_gw == $gateway[1])
      {
          echo "State unchanged\n";
      }
      else
      {
          //change gw, clear states
          echo "NOTICE: Changing route to $working_gw\n";
          echo "NOTICE: Clearing state tables\n";
          exec("route change default $working_gw");
          exec("pfctl -F state");
      }
    }
    ?>

    Please let me know your thoughts. If you think it needs major modification, let me know I have only had brief time to test it but it did seem to work.

    I forgot to add that I put this file in /usr/local/bin/mon-gw, set chmod 755 /usr/local/bin/mon-gw, and executed it./reza@rndcomputing.com


Log in to reply