Page 1 of 1

Failover script not returning to eth1

Posted: Fri Mar 21, 2014 3:48 pm
by Pirlet
Hello,

I've used the failover script from
http://wiki.mikrotik.com/wiki/Failover_Scripting

Now my problem is that my failover automatically goes to eth2 and doesn't return.

My config:
# ------------------- header -------------------
# Script by Tomas Kirnak, version 1.0.7
# If you use this script, or edit and
# re-use it, please keep the header intact.
#
# For more information and details about
# this script please visit the wiki page at
# http://wiki.mikrotik.com/wiki/Failover_Scripting
# ------------------- header -------------------



# ------------- start editing here -------------
# Edit the variables below to suit your needs

# Please fill the WAN interface names
:local InterfaceISP1 ether1
:local InterfaceISP2 ether2

# Please fill the gateway IPs (or interface names in case of PPP)
:local GatewayISP1 192.168.0.1
:local GatewayISP2 192.168.1.1

# Please fill the ping check host - currently: www.google.com
:local PingTarget 173.194.113.55

# Please fill how many ping failures are allowed before fail-over happends
:local FailTreshold 3

# Define the distance increase of a route when it fails
:local DistanceIncrease 2

# Editing the script after this point may break it
# -------------- stop editing here --------------



# Declare the global variables
:global PingFailCountISP1
:global PingFailCountISP2

# This inicializes the PingFailCount variables, in case this is the 1st time the script has ran
:if ([:typeof $PingFailCountISP1] = "nothing") do={:set PingFailCountISP1 0}
:if ([:typeof $PingFailCountISP2] = "nothing") do={:set PingFailCountISP2 0}

# This variable will be used to keep results of individual ping attempts
:local PingResult



# Check ISP1
:set PingResult [ping $PingTarget count=1 interface=$InterfaceISP1]
:put $PingResult

:if ($PingResult = 0) do={
	:if ($PingFailCountISP1 < ($FailTreshold+2)) do={
		:set PingFailCountISP1 ($PingFailCountISP1 + 1)
		
		:if ($PingFailCountISP1 = $FailTreshold) do={
			:log warning "ISP1 has a problem en route to $PingTarget - increasing distance of routes."
			:foreach i in=[/ip route find gateway=$GatewayISP1 && static] do=\
				{/ip route set $i distance=([/ip route get $i distance] + $DistanceIncrease)}
			:log warning "Route distance increase finished."
		}
	}
}
:if ($PingResult = 1) do={
	:if ($PingFailCountISP1 > 0) do={
		:set PingFailCountISP1 ($PingFailCountISP1 - 1)
		
		:if ($PingFailCountISP1 = ($FailTreshold -1)) do={
			:log warning "ISP1 can reach $PingTarget again - bringing back original distance of routes."
			:foreach i in=[/ip route find gateway=$GatewayISP1 && static] do=\
				{/ip route set $i distance=([/ip route get $i distance] - $DistanceIncrease)}
			:log warning "Route distance decrease finished."
		}
	}
}



# Check ISP2
:set PingResult [ping $PingTarget count=1 interface=$InterfaceISP2]
:put $PingResult

:if ($PingResult = 0) do={
	:if ($PingFailCountISP2 < ($FailTreshold+2)) do={
		:set PingFailCountISP2 ($PingFailCountISP2 + 1)
		
		:if ($PingFailCountISP2 = $FailTreshold) do={
			:log warning "ISP2 has a problem en route to $PingTarget - increasing distance of routes."
			:foreach i in=[/ip route find gateway=$GatewayISP2 && static] do=\
				{/ip route set $i distance=([/ip route get $i distance] + $DistanceIncrease)}
			:log warning "Route distance increase finished."
		}
	}
}
:if ($PingResult = 1) do={
	:if ($PingFailCountISP2 > 0) do={
		:set PingFailCountISP2 ($PingFailCountISP2 - 1)
		
		:if ($PingFailCountISP2 = ($FailTreshold -1)) do={
			:log warning "ISP2 can reach $PingTarget again - bringing back original distance of routes."
			:foreach i in=[/ip route find gateway=$GatewayISP2 && static] do=\
				{/ip route set $i distance=([/ip route get $i distance] - $DistanceIncrease)}
			:log warning "Route distance decrease finished."
		}
	}
}

Re: Failover script not returning to eth1

Posted: Fri Mar 21, 2014 3:55 pm
by Pirlet
Nevermind, I got it to work, seemend like I had to use Masquerade for both ethernet1 and ethernet2 to my bridge for it to work :D

Re: Failover script not returning to eth1

Posted: Sat Apr 19, 2014 7:29 pm
by VaxCavalera
Nevermind, I got it to work, seemend like I had to use Masquerade for both ethernet1 and ethernet2 to my bridge for it to work :D
i had same problem. but i also has masquarade to both ethernets... so how u did it?

Re: Failover script not returning to eth1

Posted: Tue Nov 18, 2014 6:30 pm
by Antz
I am having the same issue. It seems to be a result of the ping command not being able to ping IP addresses through the gateway who's route has the higher distance. Meaning that once it increases the distance of the primary route it will never decrease it again.
Has anyone got more detail on how to get this to work?

Re: Failover script not returning to eth1

Posted: Tue Nov 18, 2014 7:23 pm
by jarda
Why to use such huge script?

Assuming you have two wan ports both masqueraded. For each port you have one default route, one with distance 12, second with distance 13.
Then you select some public ip that you want to check thru first gw. You need to set static route to it with distance 1. Also you set the static blackhole route to it with distance 99.

Then you set netwatch checking your testing ip address. When fails, you just change the distance 12 of the default route to 22 by netwatch in "down":

/ip route set distance=22 [find distance=12];

if the checking ip is reachable you just set the route back by the same netwatch in "up":
/ip route set distance=12 [find distance=22];

And that is all.

You can also do the same with second route or with whatever number of wan ports you want to failover.

Re: Failover script not returning to eth1

Posted: Wed Nov 19, 2014 4:58 pm
by enman
This would work, but why not work ping through the second interface at which the metric higher than the first? I have more than one Mikrotik and only one that works. For those who get internet on DHCP in the first case and in the other ISP - PPPoE. If interfaces have static addresses - it does not work. Why?

Re: Failover script not returning to eth1

Posted: Wed Nov 19, 2014 6:24 pm
by jarda
The routing procedure selects always the best fitting active route. If more routes fit the same then lower distance wins.