NOT FOR GENERAL PUBLIC
SHARING IT FOR PERSONAL REFERENCE
Following is an script that switches connectivity between two interfaces (usually wan).
Example: ISP x have provided internet connectivity via fiber link . as a backup / redundancy link ISP have provided second fiber line. Both link have same ip address with same gateway. There are other ways too , this is just one way of achieving task. it can be customized in many ways like forcing specific hosts to be monitored by ping and act accordingly , or by having bridge etc.
IP address example:
in /ip address >>>
sfp1 = 10.0.0.1 (with comments “PRIMARY-SFP1”)
[remains enabled until next status changed]sfp2 = 10.0.0.1 (with comments “SECONDARY-SFP2”)
[remains enabled until next status changed]
The script will do following.
Check the SFP1 Status, if it does not found “link–ok” reply, it will log event, send email/sms alert to admin, and switch to SFP2 port by enabling IP of SFP2 and enable ip on SFP1.
On next run, if it found SFP1 link ok, then it will log event, send email/sms alert to admin, and switch back to SFP1 port by enabling IP of SFP1 and disable ip on SFP2.
# Mikrotik SPF Link monitoring SCRIPT with optional Email and SMS Alert, # We are using local KANNEL as SMS gateway and GMAIL as email sender # by Syed Jahanzaib # https://aacable.wordpress.com # Email : aacable at hotmail dot com # Script Last Modified : 25-NOV-2016 / 1400 hours # PRIMARY FIBER LINK :local INT "sfp1" # SECONDARY BACKUP FIBER LINK :local INT2 "sfp2" :local i 0; :local F 0; :local date; :local time; :global sub1 ([/system identity get name]) :global sub2 ([/system clock get time]) :global sub3 ([/system clock get date]) :set date [/system clock get date]; :set time [/system clock get time]; :local cell1 "03333021909" :global FIBERnetstatus; :global FIBERnetlastchange; :global FIBERIP; # Company Name, do not use spaces in it :local COMPANY "ZZZ" # Setting GMAIL config :local sub1 ([/system identity get name]) :local sub2 ([/system clock get date]) :local sub3 ([/system clock get time]) :local TO1 "RECIPIENT-1@hotmail.com" :local gmailid "YOUR-GMAIL-ID@gmail.com" #If you dont have kannel sms gateway ignore this. :local KURL "http://10.0.0.1:13013/cgi-bin/sendsms" :local KID "kannel" :local KPASS "KANNEL-PASS" # SMS DOWN status Msg format for Kannel SMS gateway (donot use spaces in it) :local MSGDOWNSMS "$COMPANY+FIBER+ALERT:%0A$INT+fiber+is+now+DOWN.%0ASwitching+to+$INT2+backup+fiber+link" # SMS UP status Msg format for Kannel SMS gateway (donot use spaces in it) :local MSGUPSMS "$COMPANY+FIBER+INFO:%0A$INT+fiber+is+now+UP.%0ASwitching+back+to+$INT" # EMAIL Msg format for FIBER DOWN :local MSGDOWNEMAIL "$COMPANY FIBER ALERT: $INT fiber is now DOWN at $sub1 $sub2 $sub3 . Switching to $INT2 backup link. Please Verify it." # EMAIL Msg format for FIBER UP :local MSGUPEMAIL "$COMPANY FIBER INFO: $INT fiber is now UP at $sub1 $sub2 $sub3 . Switching back to $INT primary link. Please Verify it." # LOG error :local DOWNLOG1 "$COMPANY FIBER ALERT: $INT fiber is now DOWN at $sub1 $sub2 $sub3 . Switching to $INT2 backup link. Please Verify it." :local UPLOG1 "$COMPANY FIBER INFO: $INT fiber is now UP at $sub1 $sub2 $sub3 . Switching back to $INT primary link. Please Verify it" ###################################### ########## Start the SCRIPT ############### ########## DONOT EDIT BELOW ############ ###################################### local link; /interface ethernet cable-test $INT once do={ :set link $"status"; }; :if ($link != "link-ok") do={ :log error "$INT SFP DOWN" :if (($FIBERnetstatus="UP")) do={ :set FIBERnetstatus "DOWN"; # Also add status in global variables to be used as tracking :set date [/system clock get date]; :set time [/system clock get time]; :set FIBERnetlastchange ($time . " " . $date); ################################################## ####### FOR DOWN STATUS, CHANGE THE RULES ######## ################################################## # If the link is down, then LOG info and warning in Mikrotik LOG window [Zaib] :log error "$DOWNLOG1" # DOWN ACTION , shifting to SFP2 , backup link /ip address set disabled=yes [find comment="PRIMARY-SFP1"] /ip address set disabled=no [find comment="BACKUP-SFP2"] # Adding delay so gateway should be reachable properly :delay 5s; :global gmailsmtp :set gmailsmtp [:resolve "smtp.gmail.com"]; # "Emailing the DOWN status. . . " /tool e-mail send to=$TO1 subject="$MSGDOWNEMAIL" start-tls=yes body="$MSGDOWNEMAIL" # Send SMS the DOWN status via KANNEL /tool fetch url="$KURL\?username=$KID&password=$KPASS&to=$cell1&text=$MSGDOWNSMS" ################################################## ####### FOR UP STATUS, CHANGE THE RULES ######## ################################################## } else={:set FIBERnetstatus "DOWN";} } else={ :if (($FIBERnetstatus="DOWN")) do={ :set FIBERnetstatus "UP"; # If link is UP, then LOG info and warning in Mikrotik LOG window [Zaib] :log warning "$UPLOG1" # UP ACTION , shifting back to SFP1 /ip address set disabled=no [find comment="PRIMARY-SFP1"] /ip address set disabled=yes [find comment="BACKUP-SFP2"] # Adding delay so gateway should be reachable properly :delay 5s; :global gmailsmtp :set gmailsmtp [:resolve "smtp.gmail.com"]; # "Emailing the UP status. . . " /tool e-mail send to=$TO1 subject="$MSGUPEMAIL" start-tls=yes body="$MSGUPEMAIL" # Send SMS via KANNEL Gateway /tool fetch url="$KURL\?username=$KID&password=$KPASS&to=$cell1&text=$MSGUPSMS" :set date [/system clock get date]; :set time [/system clock get time]; :set FIBERnetlastchange ($time . " " . $date); } else={:set FIBERnetstatus "UP";} }
Filed under: Mikrotik Related, Uncategorized
![](http://pixel.wp.com/b.gif?host=aacable.wordpress.com&blog=10450829&post=7641&subd=aacable&ref=&feed=1)