Rabu, 03 April 2013

Improved Load Balancing over Multiple Gateways Failover Script


hi guys


This is my first script for fail-over with two load-balanced line and third backup sleeping line. can you take a look and think of how can enhance it? or maybe you can use it if you need.
scheduler:
/ system scheduler 
add name="ping_google_link_A-up" on-event=schedule_A_up start-date=jan/01/1970 \
    start-time=00:00:00 interval=5s comment="" disabled=yes 
add name="ping_google_link_A-down" on-event=schedule_A_down \
    start-date=jan/01/1970 start-time=00:00:00 interval=5s comment="" \
    disabled=no 
scripts go up:
#set variables
:local pingcount  3
:local ipA 211.24.149.42
:local GatewayA 211.24.149.41
:local ipB 211.24.139.114
:local GatewayB 211.24.139.113
:local GatewayC 192.168.0.250

#ping gateways with src
:local pingresultA [/ping www.google.com src-address=$ipA count=$pingcount]
:local pingresultB [/ping www.google.com src-address=$ipB count=$pingcount]


#if link_A is DOWN and link_B is UP then:
:if (($pingresultA=0) && ($pingresultB=3)) do={
/ip route set [find comment="Default Gateway"] gateway=$GatewayB 
}

#if link_A is UP and link_B is DOWN then:
:if (($pingresultA=3) && ($pingresultB=0)) do={
/ip route set [find comment="Default Gateway"] gateway=$GatewayA
}

#if both link DOWN:
:if (($pingresultA=0) && ($pingresultB=0)) do={
/ip route set [find comment="Default Gateway"] gateway=$GatewayC
}

# Link A or Link B both are UP:
:if (($pingresultA=$pingcount) && ($pingresultB=$pingcount)) do={

#send alert email
/tool e-mail send subject=($ipA . "is up now") \
body=("the gateway is backed up at: " . [/system clock get date]) \ to=("zirnevis@gmail.com")

#disable all multi-session balancing mangles
/ip firewall mangle set [find chain=prerouting in-interface=LAN \
connection-mark=even action=mark-routing passthrough=no] disabled=no
/ip firewall mangle set [find chain=prerouting in-interface=LAN \
connection-state=new new-connection-mark=even passthrough=yes \
action=mark-connection nth=1,1,1] disabled=no
/ip firewall mangle set [find chain=prerouting in-interface=LAN \
connection-mark=odd action=mark-routing new-routing-mark=odd \
passthrough=no] disabled=no
/ip firewall mangle set [find chain=prerouting in-interface=LAN nth=1,1,0 \
action=mark-connection new-connection-mark=odd \
passthrough=yes] disabled=no

#disable current scheduler
/system scheduler set [find name=ping_google_link_A-down] disabled=no

#enable -up scheduler
/system scheduler set [find name=ping_google_link_A-up] disabled=yes

#set gateway back to GatewayB
/ip route set [find comment="Default Gateway"] gateway=$GatewayB
}

script go down:
#set variables
:local pingcount  3
:local ipA 211.24.149.42
:local GatewayA 211.24.149.41
:local ipB 211.24.139.114
:local GatewayB 211.24.139.113
:global state no

#ping gateways with src
:local pingresultA [/ping www.google.com src-address=$ipA count=$pingcount]
:local pingresultB [/ping www.google.com src-address=$ipB count=$pingcount]

# Link A or Link B is down then>
:if (($pingresultA=0) || ($pingresultB=0)) do={
#send email
/tool e-mail send subject=($ipA . " is down") \
body=("the gateway is down now at: " . [/system clock get date]) \ to=("zirnevis@gmail.com")

#disable all multi-session balancing mangles
/ip firewall mangle set [find chain=prerouting in-interface=LAN \
connection-mark=even action=mark-routing passthrough=no] disabled=yes
/ip firewall mangle set [find chain=prerouting in-interface=LAN \
connection-state=new new-connection-mark=even passthrough=yes \
action=mark-connection nth=1,1,1] disabled=yes
/ip firewall mangle set [find chain=prerouting in-interface=LAN \
connection-mark=odd action=mark-routing new-routing-mark=odd \
passthrough=no] disabled=yes
/ip firewall mangle set [find chain=prerouting in-interface=LAN nth=1,1,0 \
action=mark-connection new-connection-mark=odd \
passthrough=yes] disabled=yes

#disable current scheduler
/system scheduler set [find name=ping_google_link_A-down] disabled=yes

#enable -up scheduler
/system scheduler set [find name=ping_google_link_A-up] disabled=no

#set gateway to secondary or tertiary

#if link_A is DOWN and link_B is UP then:
:if (($pingresultA=0) && ($pingresultB>0)) do={
/ip route set [find comment="Default Gateway"] gateway=211.24.139.113 
}

#if link_A is UP and link_B is DOWN then:
:if (($pingresultA>0) && ($pingresultB=0)) do={
/ip route set [find comment="Default Gateway"] gateway=211.24.149.41
}

#if link_A and link_B both were DOWN then:
:if (($pingresultA=0) && ($pingresultB=0)) do={
/ip route set [find comment="Default Gateway"] gateway=192.168.0.250
}
}

Source : http://wiki.mikrotik.com/wiki/Improved_Load_Balancing_over_Multiple_Gateways_Failover_Script



May Be useful

Regards
Merah Putih Segoroasat
Thanks

Tidak ada komentar :

Posting Komentar