Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Search
    • Register
    • Login
    Introducing Netgate Nexus: Multi-Instance Management at Your Fingertips.

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

    Scheduled Pinned Locked Moved Routing and Multi WAN
    1 Posts 1 Posters 2.3k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • X
      xibalba
      last edited by

      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

      1 Reply Last reply
      Reply Quote 0

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      • First post
        Last post
      Copyright 2026 Rubicon Communications LLC (Netgate). All rights reserved.
      Privacy Policy · Cookie Policy