Community discussions

 
safiullahtariq
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 81
Joined: Sun Apr 06, 2014 9:21 pm
Location: Lahore Pakistan

Failover with email

Mon Jun 24, 2019 10:05 pm

Hello All,

I hope everyone is doing ok.

I have written a failover script, it checks for ping reply via WAN 1 every 5 mins and if replay is not received, it will send an email and switch to Backup. After that it will check for primary internet (WAN 1) after every 4 mins. And if internet is received via WAN !, it will send an email that Primary is up now.
Basically it is a scheduler


Just wanted to share something with the community. How it helps someone. Dont forget to change gateways, emails and password from this script

Regards,
/ip route
add comment=Main distance=1 gateway=198.42.12.1 scope=10
add comment=Backup distance=2 gateway=192.168.42.129
add check-gateway=ping comment=Primary disabled=yes distance=1 gateway=8.8.8.8
add comment=Secondary disabled=yes distance=2 gateway=192.168.42.129
add comment="Validate Primary" disabled=yes distance=1 dst-address=8.8.8.8/32 gateway=198.42.12.1 scope=10
add comment="BBC website" distance=1 dst-address=151.101.192.81/32 gateway=192.168.42.129
add comment="CNN website" distance=1 dst-address=151.101.193.67/32 gateway=198.42.12.1
/system clock
set time-zone-name=Asia/Karachi
/system scheduler
add comment="This will run after 4 mins" interval=5m name=main on-event="/syst\
    em scheduler set [find name=checker] disabled=yes\r\
    \n:local pinged [/ping address=151.101.192.81 count=5 interface=ether1 siz\
    e=50]\r\
    \n:if ((\$pinged=0)) do={\r\
    \n#send email\r\
    \n/tool e-mail send subject=(\"Backup is up and Primary is down\") \\\r\
    \nbody=(\"Backup is being used now \" . [/system clock get time]) \\\r\
    \nto=(\"XXXXX@gmail.com\")\r\
    \n/system scheduler set [find name=backupwan] disabled=no\r\
    \n}\r\
    \n" policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
    start-date=jun/21/2019 start-time=20:21:53
add disabled=yes interval=15s name=backupwan on-event="/ip route set [find com\
    ment=\"Main\"] distance=2\r\
    \n/ip route set [find comment=\"Backup\"] distance=1\r\
    \n/system scheduler set [find name=main] disabled=yes\r\
    \n/system scheduler set [find name=checker] disabled=no\r\

add comment="This will run after 5 mins" disabled=yes interval=5m25s name=\
    checker on-event="/system scheduler set [find name=backupwan] disabled=yes\
    \r\
    \n:local pinged [/ping address=151.101.193.67 count=5 interface=ether1 siz\
    e=50]\r\
    \n:if ((\$pinged=5)) do={\r\
    \n#send email\r\
    \n/tool e-mail send subject=(\"Primary is back\") \\\r\
    \nbody=(\"PTCL is being used at \" . [/system clock get time]) \\\r\
    \nto=(\"xxxxxxxx@gmail.com\")\r\
    \n\r\
    \n#change route to enable primary\r\
    \n\r\
    \n/ip route set [find comment=\"Main\"] distance=1\r\
    \n/ip route set [find comment=\"Backup\"] distance=2\r\
    \n#delay 5\r\
    \n/system scheduler set [find name=main] disabled=no\r\
    \n}" policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
    start-date=jun/21/2019 start-time=21:33:27
/tool bandwidth-server
set enabled=no
/tool e-mail
set address=74.125.206.109 from=XXXX@gmail.com password=\
    XXXXXXXXXX port=587 start-tls=yes user=XXXXXX@gmail.com
Last edited by safiullahtariq on Mon Jul 01, 2019 2:39 pm, edited 1 time in total.
 
anav
Forum Guru
Forum Guru
Posts: 3113
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada

Re: Failover with email

Wed Jun 26, 2019 7:04 pm

I am a bit confused what is the difference between the check ping in your IP ROUTES, and the script check ping?
If the route check ping is in the order of every few seconds, then the router will switch to the backup and nobody will know until the next script time check?
Further the router could switch back before X minutes to the primary and nobody would be the wiser?

Wouldn't you want to somewhat mirror the actual check ping and gateway switching that the router does automatically due to routing instructions??

On another note, I wouldnt be able to use your script as my primary is not cable (its fiber) and dynamic and thus the gateway will change and this is not automatically detected or inserted by the router (which is the case for cable). I have to manually go into ip client click on the connection and read the status (and new gateway) and plug that gateway into the IP route, for internet to come back up on the primary. If you can script for that ..... it would be amazing!!
I'd rather manage rats than software. Follow my advice at your own risk! (Sob & mkx forced me to write that!)
 
stoser
Member Candidate
Member Candidate
Posts: 107
Joined: Sun Aug 21, 2016 12:04 am

Re: Failover with email

Mon Jul 01, 2019 5:59 am

Perhaps a better way would be to let the mikrotik handle the pings and the failover as designed using recursive routes. You could just schedule a script to check to see if any active route has a distance > 1 (which would indicate that the primary route is down). If the condition is met, send an email and log the event.
 
safiullahtariq
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 81
Joined: Sun Apr 06, 2014 9:21 pm
Location: Lahore Pakistan

Re: Failover with email

Mon Jul 01, 2019 2:37 pm

Ok, may be (and surely) is didnt explain it well.

let me tell you what I have done - I didn't clear up the disabled routes which should have not been mentioned in this such as follows which is causing confusion:
add check-gateway=ping comment=Primary disabled=yes distance=1 gateway=8.8.8.8
add comment=Secondary disabled=yes distance=2 gateway=192.168.42.129
add comment="Validate Primary" disabled=yes distance=1 dst-address=8.8.8.8/32 gateway=198.42.12.1 scope=10
Now lets check the remaining script.

Following are the main gateways which interchange when the internet is available or not.\
/ip route
add comment=Main distance=1 gateway=198.42.12.1 scope=10
add comment=Backup distance=2 gateway=192.168.42.129
Following are the websites to whom I will check via specific gateway to know if internet is availabel or nto. You can use any other relaible website.
add comment="BBC website" distance=1 dst-address=151.101.192.81/32 gateway=192.168.42.129
add comment="CNN website" distance=1 dst-address=151.101.193.67/32 gateway=198.42.12.1
Make sure the clock is right
/system clock
set time-zone-name=Asia/Karachi
Now the schedulers to check for the internet. What the following will do is, it will ping the website after an interval and if the ping count is 5 and size is 50, is NOT received, it will send an email will ENABLE the next scheduler.
/system scheduler
add comment="This will run after 4 mins" interval=5m name=main on-event="/syst\
    em scheduler set [find name=checker] disabled=yes\r\
    \n:local pinged [/ping address=151.101.192.81 count=5 interface=ether1 siz\
    e=50]\r\
    \n:if ((\$pinged=0)) do={\r\
    \n#send email\r\
    \n/tool e-mail send subject=(\"Backup is up and Primary is down\") \\\r\
    \nbody=(\"Backup is being used now \" . [/system clock get time]) \\\r\
    \nto=(\"XXXXX@gmail.com\")\r\
    \n/system scheduler set [find name=backupwan] disabled=no\r\
    \n}\r\
    \n" policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
    start-date=jun/21/2019 start-time=20:21:53
The next Scheduler. It will change the distance in the routes "Main to distance 2 and Backup to distance 1". AND, it will disable the email sending scheduler (previous one) and the enable the next scheduler-handing the control to the next scheduler.
add disabled=yes interval=15s name=backupwan on-event="/ip route set [find com\
    ment=\"Main\"] distance=2\r\
    \n/ip route set [find comment=\"Backup\"] distance=1\r\
    \n/system scheduler set [find name=main] disabled=yes\r\
    \n/system scheduler set [find name=checker] disabled=no\r\
    
Following scheduler will check the availability of Ethernet via Main WAN via ping to the specific website after a specific interval, and if ping count 5 is received, it will change the route of Main to distance 1 and Backup to distance 2. Moreover, it will send and email and hand the enable to first scheduler. The job of first scheduler is already mentioned
add comment="This will run after 5 mins" disabled=yes interval=5m25s name=\
    checker on-event="/system scheduler set [find name=backupwan] disabled=yes\
    \r\
    \n:local pinged [/ping address=151.101.193.67 count=5 interface=ether1 siz\
    e=50]\r\
    \n:if ((\$pinged=5)) do={\r\
    \n#send email\r\
    \n/tool e-mail send subject=(\"Primary is back\") \\\r\
    \nbody=(\"PTCL is being used at \" . [/system clock get time]) \\\r\
    \nto=(\"XXXXXX@gmail.com\")\r\
    \n\r\
    \n#change route to enable primary\r\
    \n\r\
    \n/ip route set [find comment=\"Main\"] distance=1\r\
    \n/ip route set [find comment=\"Backup\"] distance=2\r\
    \n#delay 5\r\
    \n/system scheduler set [find name=main] disabled=no\r\
    \n}" policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
    start-date=jun/21/2019 start-time=21:33:27
Do setup your email
/tool e-mail
set address=74.125.206.109 from=XXXX@gmail.com password=\
    XXXXXXXXXX port=587 start-tls=yes user=XXXXXX@gmail.com
Important point.
The main (1st) scheduler must have smaller interval than the last (3rd) one. E.g. 3rs scheduler will have interval of 5 mins, but the first interval will have 4 mins. IF they have equal time or 1st has larger interval than the 3rd one, it will not shift the control.

Regards,
 
stoser
Member Candidate
Member Candidate
Posts: 107
Joined: Sun Aug 21, 2016 12:04 am

Re: Failover with email

Mon Jul 01, 2019 9:19 pm

I cannot help you with your current design, as I think that is is cumbersome and overly complex. However, I can point you in the direction of a simpler implementation. Have you seen this wiki?

https://wiki.mikrotik.com/wiki/Advanced ... _Scripting

It does everything you need minus the email. All you need to do is implement the Failover without scripting using recursive routing, as per the wiki, and write a simple scheduled script that checks to see which route is active, and sends an email if certain conditions are met (for example if the distance=2 condition is met). Here's an example of how to check an active route that has distance > 1, and to log it. You would need to add the email functionality, but you already have that figured out. Then it would be one simple scheduled task, without the compexity of timing the schedules, disabling schedules, etc. You would need some global variables as flags to indicate that an email has already been sent for this condition, so that you don't get multiple emails, but that is simple enough...

:foreach i in=[/ip route find where (distance>1 and active and !disabled and scope=30)] do={
:log warning ("RoutingMark -- " . [/ip route get $i value-name=routing-mark] . " -- is in failover to distance = " . [/ip route get $i value-name=distance]);
}.

Who is online

Users browsing this forum: Google [Bot] and 135 guests