MikroTik Scripting: Failover Routing for Asterisk PBX

Hi guys,

This is my second article and I wanted to raise the difficulty level of my tutorials!

We work a lot with Asterisk PBX and MikroTik and we’ve encountered a problem when we have 2 internet connection with a MikroTik using WAN failover, our Asterisk PBX would stop working when the primary connections fail; we finally  figured out why.

In this case, the Asterisk connections would remain appended to the primary gateway, and Asterisk doesn’t understand the change of connection; so we came up with a solution using 2 MikroTik scripts.

First, the “Check Script” checks if the primary internet connection is working or not. If fails, use the secondary internet link and reboot the router. You are probably asking why we would do this…I know!

Because after the reboot I can run a “Restart Script” to check if primary connection is still out, or came back.

In this way, by rebooting the router, the asterisk pbx client loose for a moment the registration and reconnect with the new gateway so everything works.
Actually we’re testing other solutions to avoid the router reboot…when I’ve found that I’ll update this post and let you know!

Editors note: This could be achieved in a few ways, I’ll leave it as a test for our readers to see what improvements you can come up with!

Here are the scripts:

Check Script

:global strDate [/system clock get date]
:global strTime [/system clock get time]
:global strSystemName [/system identity get name]

:if ([/ping 10.104.7.187 interface=pppoe-out1 count=5] = 0 && [/ping 8.8.4.4 interface=pppoe-out1 count=5] = 0 && [/ip route get [find comment="Primary"] disabled]=false) do={
    :log info "Disabling Primary";
    /ip route set [find comment="Primary"] disabled=yes
    /tool e-mail send from="[email protected]" to="[email protected]" subject="Route Failover - $strDate $strTime - $strSystemName" body="Failover to Telecom occurred at $strDate $strTime on $strSystemName"
    :delay 3
    /system reboot

} else= {
    :log info "No Failover Necessary";
}

Restart Script

:delay 10;
:if ([/ip route get [find comment="Primary"] disabled]=true) do={
    /interface ethernet set numbers=4 disabled=no

     /ip route set [find comment="Primary"] disabled=no
     /ip route set [find comment="Primary"] distance=3
    :delay 10
    :if ([/ping 10.104.7.187 routing-table=Primary count=5] > 0 && [/ping 8.8.4.4 routing-table=Primary count=5] > 0) do={

       /ip route set [find comment="Primary"] distance=1
       /system reboot
    }
     else= {

        /ip route set [find comment="Primary"] distance=3

    }
} else= {
    :log info "No Failover Necessary";
}

Written by Razorblade, edited by Omega-00

Advertisement

About razorblade

Internet and Networking Expert, work a lot with Virtualization on Debian server and Mikrotik Devices. Php software developer.

3 thoughts on “MikroTik Scripting: Failover Routing for Asterisk PBX

  1. Dear Poster,

    Yes this would work. We use s slightly less severe method. When one of our interfaces. Specifically the interface related to the VOIP (asterisk) link drop we look through the connection tracking information on the MT and simply delete all the connection tracking related to the Asterisk box VOIP.

    /ip firewall connection for details.

  2. Hello razorblade. I am in need of this script for our hosted Asterisk pbx but I am not very familiar with mikrotik. I have failover working perfectly. ether1 is a static ip and ether2 is a cellular dhcp. I need to force the mikrotik to reboot upon failover. I tried copying your script and changing ppoe to ether1. What else do I need to do? What am I missing? I would really appreciate your help….

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.