SD-WAN

nevermind wind, no matter rain

Velocloud Virtual Edge HA in Alibaba Cloud with HaVip

The python script running on secondary VCE

In this test, the script is called failover_exp.py and placed in /opt/vc/bin folder. The failover_exp.py script content is at the code box below.

#!/usr/bin/python3
import subprocess
import time
from datetime import datetime

# Looking for the following in main and adjust the IP address and interface to suit your environment
#    target_ip = "10.200.203.10"
#    This target_ip is the primary VCE WAN IP (but use the IP address on the interface, not the public IP address)
#    source_ip = "10.200.203.11"
#    This source_ip is the secondary VCE WAN IP (again, use the IP address on the interface, not the public IP address)
#    lan_default_gateway = "10.200.202.253"
#    lan_default_gateway is the LAN side Alibaba vSwitch default gateway
#    interface = "eth1"
#    interface is the LAN side interface, that is the interface where the HaVip located at
#

def ping_ip(destination_ip, source_ip):
    """
    Pings the destination IP once using source_ip as the origin.
    The '-c 1' flag sends only one packet.
    Returns True if ping is successful (exit code 0), otherwise False.
    """
    try:
        result = subprocess.run(
            ["ping", "-I", source_ip, "-c", "1", destination_ip],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL
        )
        return result.returncode == 0
    except Exception as e:
        print(f"Error pinging {destination_ip} from {source_ip}: {e}")
        return False

def is_interface_up(interface):
    """
    Checks whether the given network interface is up by invoking 'ifconfig <interface>'
    and searching for the 'RUNNING' flag in its output.
    Returns True if 'RUNNING' is present, otherwise False.
    """
    try:
        result = subprocess.run(
            ["ifconfig", interface],
            capture_output=True,
            text=True
        )
        if result.returncode != 0:
            # If getting interface info fails, treat the interface as down.
            return False
        # The 'RUNNING' flag generally indicates the interface is active.
        return "RUNNING" in result.stdout
    except Exception as e:
        print(f"Error checking interface {interface}: {e}")
        return False

def bring_interface_up(interface):
    """
    Brings the given network interface up using 'ifconfig <interface> up'.
    """
    try:
        subprocess.run(["ifconfig", interface, "up"])
        print(f"Interface {interface} has been brought up.")
    except Exception as e:
        print(f"Error bringing up interface {interface}: {e}")

def bring_interface_down(interface):
    """
    Brings the given network interface down using 'ifconfig <interface> down'.
    """
    try:
        subprocess.run(["ifconfig", interface, "down"])
        print(f"Interface {interface} has been brought down.")
    except Exception as e:
        print(f"Error bringing down interface {interface}: {e}")

def main():
    target_ip = "10.200.203.10"
    source_ip = "10.200.203.11"
    lan_default_gateway = "10.200.202.253"
    interface = "eth1"

    # Log the event
    with open("/var/log/failover_exp.log", "a") as log_file:
        current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        log_file.write(f"{current_time} - failover_exp started\n")

    while True:
        if ping_ip(target_ip, source_ip):
            print(f"Ping to {target_ip} is successful from {source_ip}.")
            if is_interface_up(interface):
                print(f"Interface {interface} is currently up. Sleeping for 30 seconds...")
                time.sleep(60)

                # Perform a second ping after sleeping.
                if ping_ip(target_ip, source_ip):
                    print(f"Second ping to {target_ip} is also successful.")
                    time.sleep(60)
                    if ping_ip(target_ip, source_ip):
                        print(f"Third ping to {target_ip} is also successful. Bringing interface {interface} down...")
                        bring_interface_down(interface)
                        # Log the event
                        with open("/var/log/failover_exp.log", "a") as log_file:
                            current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                            log_file.write(f"{current_time} - Interface {interface} has been brought down. Which means failover back to primary\n")
                else:
                    print(f"Second ping to {target_ip} failed. Keeping interface {interface} up.")
            else:
                print(f"Interface {interface} is already down.")
        else:
            print(f"Ping to {target_ip} failed from {source_ip}.")
            # Check the state of the interface if the ping fails.
            if is_interface_up(interface):
                print(f"Interface {interface} is already up.")
            else:
                print(f"Interface {interface} is currently down. Bringing it up...")
                bring_interface_up(interface)
                # Log the event
                with open("/var/log/failover_exp.log", "a") as log_file:
                    current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                    log_file.write(f"{current_time} - Interface {interface} has been brought up. Which means secondary take over as primary\n")
                time.sleep(1)
                subprocess.run(["ping", "-c", "2", lan_default_gateway])

        # Pause for a second before the next check.
        time.sleep(2)

if __name__ == "__main__":
    main()
Velocloud Virtual Edge HA in Alibaba Cloud with HaVip

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top