Hi all!
I don’t know if anyone else has gone down this path before, but I use distance=2 static routes (mostly in combination with EoIP tunnels) and wanted to be notified every time the distance=1 route was unreachable. Hence the script below, which sends an e-mail to up to 4 recepients if a gateway has been unreachable for over 10 minutes. (It will wait for an hour before sending a second e-mail alert.) The idea is that very short term problems with internet or wireless connections are ignored, but you get notified of more persistent ones. Provided, that is, that your router has an alternative way of accessing the internet, for sending the e-mails! (You will probably also need to select a “Check Gateway” method for your route list to update the gateway status to “unreachable”.)
What I like about this approach is that there is no need to identify specific routes, interfaces, or IP addresses for pinging, as all routes are checked every time the script is run.
# MUST BE RUN EVERY 1 MINUTE
# FIRST ALERT AFTER 10 MINUTES, SECOND ALERT AFTER 1 HOUR
:local emlFrom "~~~~~e.g._Mikrotik_Alerter_<from@e.mail>~~~~~"
:local emlRec1 "~~~~~e.g._firstname_surname_<no1@e.mail>~~~~~"
:local emlRec2 "~~~~~e.g._firstname_surname_<no2@e.mail>~~~~~"
:local emlRec3 "~~~~~e.g._firstname_surname_<no1@e.mail>~~~~~"
:local emlRec4 0
# ALL PRECEDING ENTRIES MUST BE REPLACED BY ACTUAL DATA INSIDE DOUBLE QUOTES OR ZEROS
:local emlSmtp [:resolve smtp.myisp.tld]
# ACTUAL SMTP SERVER TO BE INSERTED ABOVE
:global gwur10m
:global gwur50m
:local aa
:local emlRecs [:toarray ($emlRec1,$emlRec2,$emlRec3,$emlRec4)]
:local emlText (" unreachable on ".[/system identity get name])
:local unrable 0
:if ( [:typeof $gwur10m]!="num" ) do={ :set gwur10m -1 }
:if ( [:typeof $gwur50m]!="num" ) do={ :set gwur50m 50 } else={ :set gwur50m ($gwur50m+1) }
:if ( $gwur50m>49 ) do={
:foreach pp in=[/ip route find] do={
:foreach qq in=[/ip route get $pp gateway-status] do={
:set aa [:find $qq "reachable"]
:if ( [:pick $qq ($aa-2) $aa]="un" ) do={ :set unrable ($unrable+1) }
} }
:if ( $unrable=0 ) do={
:set gwur10m -1
} else={
:if ( $gwur10m<9 ) do={
:set gwur10m ($gwur10m+1)
} else={
:set gwur10m -1
:set gwur50m -1
:if ( $unrable=1 ) do={ :set aa "Gateway" } else={ :set aa ($unrable." gateways") }
:log info "••• Unr_able: $aa unreachable, e-mail alert sent."
:foreach pp in=$emlRecs do={
:if ( $pp!=0 ) do={
/tool e-mail send body="Gateway(s)$emlText for over 10 minutes." from=$emlFrom server=$emlSmtp subject=">>> $aa$emlText <<<" to=$pp
} } } } }
I hope that others find this script helpful. All comments and ideas for improvement are most welcome.