Community discussions

MikroTik App
 
QuesT
just joined
Topic Author
Posts: 13
Joined: Wed Sep 25, 2013 5:05 pm

Failover Scripting

Fri Jan 09, 2015 9:19 pm

Hello,

i have setup a Bandwidth-based load-balancing . (http://mum.mikrotik.com/presentations/US12/tomas.pdf)
It works.

Now i will set up Failover http://wiki.mikrotik.com/wiki/Failover_Scripting but this not work only the Distance of Wan2 get higher and higher but the Wan2 ist ok.
# ------------------- 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 ISP_1
:local InterfaceISP2 ISP_2

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

# Please fill the ping check host - currently: resolver1.opendns.com
:local PingTarget 8.8.8.8

# 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."
		}
	}
}
 
bingo220
Member Candidate
Member Candidate
Posts: 126
Joined: Sun Sep 22, 2013 9:30 pm
Location: Ukraine

Re: Failover Scripting

Sat Jan 10, 2015 12:16 am

 
QuesT
just joined
Topic Author
Posts: 13
Joined: Wed Sep 25, 2013 5:05 pm

Re: Failover Scripting

Sat Jan 10, 2015 1:31 am

Thanks

witch is the better way?

/ip route
add dst-address=8.8.8.8 gateway=10.0.0.138 scope=10
add dst-address=8.8.4.4 gateway=192.168.2.1 scope=10
/ip route
add distance=1 gateway=8.8.8.8 routing-mark=ISP1_Route check-gateway=ping
add distance=2 gateway=8.8.4.4 routing-mark=ISP1_Route check-gateway=ping
/ip route
add distance=1 gateway=8.8.4.4 routing-mark=ISP2_Route check-gateway=ping
add distance=2 gateway=8.8.8.8 routing-mark=ISP2_Route check-gateway=ping
or
/ip route
add dst-address=8.8.8.8 gateway=10.0.0.138 scope=10
add dst-address=8.8.4.4 gateway=192.168.2.1 scope=10
/ip route
add dst-address=10.1.1.1 gateway=8.8.8.8 scope=10 target-scope=10 check-gateway=ping
add dst-address=10.2.2.2 gateway=8.8.4.4 scope=10 target-scope=10 check-gateway=ping
/ip route
add distance=1 gateway=10.1.1.1 routing-mark=ISP1_Route
add distance=2 gateway=10.2.2.2 routing-mark=ISP1_Route
add distance=1 gateway=10.2.2.2 routing-mark=ISP2_Route
add distance=2 gateway=10.1.1.1 routing-mark=ISP2_Route
 
bingo220
Member Candidate
Member Candidate
Posts: 126
Joined: Sun Sep 22, 2013 9:30 pm
Location: Ukraine

Re: Failover Scripting

Sat Jan 10, 2015 12:25 pm

 
QuesT
just joined
Topic Author
Posts: 13
Joined: Wed Sep 25, 2013 5:05 pm

Re: Failover Scripting

Sun Jan 11, 2015 9:06 pm

Hello again,

with the Failoverscript from you SNTP and check for updates on the Routerboard dont work anymore. (No Internetconnection) :( :( :(
 
bingo220
Member Candidate
Member Candidate
Posts: 126
Joined: Sun Sep 22, 2013 9:30 pm
Location: Ukraine

Re: Failover Scripting

Sun Jan 11, 2015 9:26 pm

I think it is because your connection is marked, in my example default-routes are non-marked.