monitoring internet connection

Hi, I’ve written script which monitors main connection using ping and switches to backup when problem is detected. When main connection works again, script switches back traffic from backup.

Idea is simple. Script (I run it every 2 minutes via scheduler) sends ping to 3 defined locations. When all 3 locations are dead it supposses that connection is broken somewhere outside. In this case script enables firewall mangle rule that marks packets with ‘failover’ label. In routing table you have to add routing for ‘failover’ label using your backup interface. You have to add ‘failover’ routing for all your local interfaces - otherwise you local routing won’t work when main connection will be dead.

You have to setup few variables at the beginning of the script and ‘backupinterfacename’ in the script body. I’ve tried to make ‘backupinterfacename’ variable but script reports error when it finds " in-interface=!$variable " (! is the problem)

Here’s the script:

# START #

:local cwemail "myemail@mydomain.com"
:local cwemailfrom "router@myrouter.com"
:local cwstmphost "192.168.1.1"
:local cwmessage ""

# CHECK #
:local cwfwrulenum [/ip firewall mangle find new-routing-mark=failover]
if ( [ :len $cwfwrulenum ] > 1 ) do={
   /tool e-mail send server=$cwstmphost body="Too many firewall rules. Remove them manually..." from=$cwemailfrom to=$cwemail subject=" [ ROUTER ] [ ERROR ] CONNECTION WATCH"
   :error "Too many firewall rules. Remove them manually..."
}

:if ( $cwfwrulenum = "" ) do={
   :put "Adding firewall rule..."
   /ip firewall mangle add action=mark-routing chain=prerouting in-interface=!backupinterfacename comment="MARKING RULE FOR ROUTING PACKETS THROUGH NEOSTRADA CONNECTION (SEE ROUTING RULES) - DISABLED BY DEFAULT" disabled=yes new-routing-mark=failover passthrough=yes
   :set cwfwrulenum [/ip firewall mangle find new-routing-mark=failover]
}
:local cwfwruledisabled [/ip firewall mangle get $cwfwrulenum disabled]

# TEST DESTINATIONS
:put "Testing onet.pl through main connection"
:local cwcount1 [/ping routing-table=main count=5 213.180.146.27]
:put "Testing yahoo.com through main connection"
:local cwcount2 [/ping routing-table=main count=5 98.139.183.24]
:put "Testing google.pl through main connection"
:local cwcount3 [/ping routing-table=main count=5 209.85.148.103]

# CHECK RESULTS
:if ( $cwcount1<4 ) do={ :set cwmessage ($cwmessage.(100-($cwcount1*100)/5)."% loss to onet.pl\r\n") }
:if ( $cwcount2<4 ) do={ :set cwmessage ($cwmessage.(100-($cwcount2*100)/5)."% loss to yahoo.com\r\n") }
:if ( $cwcount3<4 ) do={ :set cwmessage ($cwmessage.(100-($cwcount3*100)/5)."% loss to google.pl\r\n") }
:put $cwmessage

:if ($cwcount1<2 && $cwcount2<2 && $cwcount3<2) do={
   if ($cwfwruledisabled) do={
      :put "Switching to backup connection"
      :log info "Switching to backup connection"
      /ip firewall mangle set $cwfwrulenum disabled=no
      :set cwmessage ($cwmessage."\r\nSwitching to backup connection")
   } else={
      :put "Backup connection is already active"
   }
} else={
   if (!$cwfwruledisabled) do={
      :put "Switching to main connection"
      :log info "Switching to main connection"
      /ip firewall mangle set $cwfwrulenum disabled=yes
      :set cwmessage ($cwmessage."\r\nSwitching to main connection")
   } else={
      :put "Already using main connection"
   }
}

if ( $cwmessage != "" ) do={
   /tool e-mail send server=$cwstmphost body=$cwmessage from=$cwemailfrom to=$cwemail subject=" [ ROUTER ] CONNECTION WATCH"
}

You can find it at https://github.com/codepill/script-utilities/blob/master/mikrotik/connection-watch as well.