enhance this 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

script for any link 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=("youremail@youremail.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
}
}

script for when any link 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=("youremail@youremail.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
}

I need script to get ping result to http://www.yahoo.com e.g 60ms or 60, how to get this?

:put [/ping http://www.google.com count=1]
result is 1

although this is my first script, I don’t know any way to do that.

Try this:

/tool flood-ping [ :resolve www.google.com ] count=1 do={ :global PingTime $avg-rtt }
:put $PingTime

–Eric

here’s the latest status:

schedule_a_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
:local website www.yahoo.com 


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

:local timenow ([/system clock get date] . " / " . [/system clock get time])

#if link_A is UP and link_B is DOWN then:
:if (($pingresultA=$pingcount) && ($pingresultB=0)) do={
:if ([/ip route get [find comment="Default Gateway"] gateway]!=$GatewayA) \
do={/ip route set [find comment="Default Gateway"] gateway=$GatewayA}
/ip firewall mangle set [find comment="faisal"] disabled=yes
}

#if link_A is DOWN and link_B is UP then:
:if (($pingresultA=0) && ($pingresultB=$pingcount)) do={
:if ([/ip route get [find comment="Default Gateway"] gateway]!=$GatewayB) \
do={/ip route set [find comment="Default Gateway"] gateway=$GatewayB}
/ip firewall mangle set [find comment="faisal"] disabled=no
}

#if both link DOWN:
:if (($pingresultA=0) && ($pingresultB=0)) do={
#if it is not already set then do it
:if ([/ip route get [find comment="Default Gateway"] gateway]!=$GatewayC) \
do={/ip route set [find comment="Default Gateway"] gateway=$GatewayC
/ip firewall mangle set [find comment="faisal"] disabled=yes}

}

# Link A and Link B both are UP:
:if (($pingresultA=$pingcount) && ($pingresultB=$pingcount)) do={
#send alert email
:local msg ("the gateway is backed up at: " . $timenow)
:local subject ($ipA . " / " . $ipB . " are up at: " . $timenow)
/tool e-mail send subject=($subject) body=($msg) 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
/ip firewall mangle set [find comment="faisal"] 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
}

schedule_a_down.pl

#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 website www.yahoo.com 

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

# Link A or Link B is down then>
:if (($pingresultA=0) || ($pingresultB=0)) do={
#send email

:if (($pingresultA=0) && ($pingresultB=0)) do={
:local msg ("both of the gateways are down now at: " . [/system clock get date] . " / " . [/system clock get time])
/tool e-mail send subject=($ipA . " / " . $ipB . " are down") \
body=($msg) \ to=("zirnevis@gmail.com")
}

:if (($pingresultA=0) && ($pingresultB>0)) do={
:local msg ("The gateway is down now at: " . [/system clock get date] . " / " . [/system clock get time])
/tool e-mail send subject=($ipA . " is down") \
body=($msg) to=("zirnevis@gmail.com")
}

:if (($pingresultA>0) && ($pingresultB=0)) do={
:local msg ("The gateway is down now at: " . [/system clock get date] . " / " . [/system clock get time])
/tool e-mail send subject=($ipB . " is down") body=($msg) \
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

/ip firewall mangle set [find comment="faisal"] 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=$GatewayB
/ip firewall mangle set [find comment="faisal"] disabled=no
}

#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=$GatewayA
/ip firewall mangle set [find comment="faisal"] disabled=yes
}

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

so guys! the load balancing for 2 WAN and auto-failover works with this script!

please advise me.. cause i’ve been working on this issue for more than 2 weeks and I need to set it up v. soon on my network
regards