sabato , Novembre 27 2021
Ultime Notizie
Home / Wireless & Networking / Mikrotik / Mikrotik Scripting: Failover Routing for Asterisk PBX

Mikrotik Scripting: Failover Routing for Asterisk PBX

Hi Guys,

today i’ll show you some scripting tricks!

Assume we have 2 Internet connection and a Mikrotik device (e.g. Mikrotik 750, Mikrotik 750GL etc.) and we want to put those connection in failover mode.

Failover mode start when Primary connection fails (e.g. ADSL link down) so we need to switch all traffic to Secondary connection.

This is easy to do with some route command, but what’s happened to Asterisk VoIP PBX?

When Asterisk loose the Primary connection, all registration to sip providers not switch automatically to Secondary connection, so the VoIP PBX stop working but internet works!

How can we solve this thing?

Let’s go scripting!

First of all i’m going to explain you the idea. we’re going to put to scripts into our device.

One that periodically do some check ping to 2 ip address. When ping is ok, we’re fine and nothing happen.

When ping fails from both address, we’ve lost connection so this is the trigger for the script.

The first script, that we’re going to call “Check Script”, disable the Primary Internet GW and send an email to alert us.

After a 3 seconds delay reboot the device.

So this is the trigger for second script, that we’re going to call “Restart Script”, that after a 10 second delay(necessary to get the device up completely) check if the Primary Internet GW is disabled (maybe you reboot the device for other purpose?!?!?) .

If Primary GW is disabled, the script assign it a rounting distance of 3 (so the Secondary became the Master Internet GW) and enable it to check if the connection come back.

If pings are now ok, restore the Primary Interface as Master by changing the distance to 1 otherwise Primary link is down for some bad reason, and connection remains on with Secondary GW.

Under this Scenario Asterisk VoIP does not stop working but remains up!

Those are the two 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";
}

Enjoy!

Info Paolo Daniele

Ingegnere delle Telecomunicazioni, appassionato di informatica fin da piccolo ho trasformato la passione in lavoro. Con il PHP faccio tutto (o quasi...) ma non disprezzo altri linguaggi all'occorrenza. Se vi piace il mio sito, o vi è utile, o vi sto simpatico, offritemi una birra!

Ti potrebbe interessare

Mikrotik VLAN e Switch

Ciao Ragazzi, ritorno a scrivere dopo qualche tempo per parlarvi della “nuova” gestione delle VLAN …

3 Commenti

  1. Can you please tell me why you have to disable primary gateway rather than only change its distance in the first step? And why you have to reboot the device?

    • Paolo Daniele

      Hi Daniel,
      I do some test with Asterisk and see that the primary interface needs to be disabled and the router rebooted otherwise Asterisk connections remain appended and i have no line (but i have internet!). If you have better idea please tell me!

  2. No need to restart the router.
    Does this:
    /ip firewall connection tracking set enabled=no
    :delay 5s
    /ip firewall connection tracking set enabled=yes

    Spanish:
    No hace falta que reinicies el router.
    Haz esto:
    /ip firewall connection tracking set enabled=no
    :delay 5s
    /ip firewall connection tracking set enabled=yes

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *