Mktik Failover Script

# ——————- 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 ——————-

# ————- Modification ——————-
# Modified by Andrés López
# Failover using Firewall Mangle Rules
# ———————————————-

# Please fill the WAN interface names
:local canal_a ether1
:local canal_b ether2
:local canal_c ether3

# routing net marks
:local redadmin 0
:local redinvitados 1

# check gateways
:local PingTarget 8.8.8.8
:local FailTreshold 10

# Declare the global variables
:global PingFailCountISP1
:global PingFailCountISP2

# Status
:global canal_a_estado
:global canal_b_estado

# 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=$canal_a]
:put $PingResult

:if ($PingResult = 0) do={
:if ($PingFailCountISP1 < ($FailTreshold+2)) do={
:set PingFailCountISP1 ($PingFailCountISP1 + 1)

:if ($PingFailCountISP1 = $FailTreshold) do={
:log warning “Canal a is offline”
:set canal_a_estado 1
}
}
}

:if ($PingResult = 1) do={
:if ($PingFailCountISP1 > 0) do={
:set PingFailCountISP1 ($PingFailCountISP1-1)

:if ($PingFailCountISP1 = ($FailTreshold – 1)) do={
:log warning “Canal a is back”
:set canal_a_estado 0
}
}
}

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

:if ($PingResult = 0) do={
:if ($PingFailCountISP2 < ($FailTreshold+2)) do={
:set PingFailCountISP2 ($PingFailCountISP2 + 1)

:if ($PingFailCountISP2 = $FailTreshold) do={
:log warning “Canal b is offline”
:set canal_b_estado 1
}
}
}

:if ($PingResult = 1) do={
:if ($PingFailCountISP2 > 0) do={
:set PingFailCountISP2 ($PingFailCountISP2-1)

:if ($PingFailCountISP2 = ($FailTreshold – 1)) do={
:log warning “Canal b is back”
:set canal_b_estado 0
}
}
}

# Indication flags
:put $canal_a_estado
:put $canal_b_estado

# Failover actions
:if ($canal_b_estado=1) do={
:log warning “Enviando redinvitados por canal c”
ip firewall mangle set new-routing-mark=canal_c $redinvitados
}

:if ($canal_a_estado=1) do={
:log warning “Enviando redadmin por canal c”
ip firewall mangle set new-routing-mark=canal_c $redadmin
}

:if ($canal_b_estado=0) do={
:log warning “Canal b estable”
ip firewall mangle set new-routing-mark=canal_b $redinvitados
}

:if ($canal_a_estado=0) do={
:log warning “Canal a estable”
ip firewall mangle set new-routing-mark=canal_a $redadmin
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s