Community discussions

MikroTik App
 
ashpri
Member Candidate
Member Candidate
Topic Author
Posts: 143
Joined: Sun Oct 14, 2018 3:11 am

Script for recursive failover based on packet loss

Fri Sep 23, 2022 4:22 am

Recursive failover has been working perfect for my connection with 2 ISPs. Last week my main ISP experienced intermittent issues. You could still ping but it was intermittent with latency spikes. I ended up having to disable (renumber) the route manually.

How would I apply recursive failover by checking for packet loss?

I suppose the script logic would be:
1. Check 8.8.8.8 (recursive for ISP1) & 8.8.4.4 (recursive for ISP2)
2. Ping 8.8.8.8 or 8.8.4.4 ten times
3. If packet loss >=20% then find route by comment and disable
4. If packet loss <20% then find route by comment and enable

Are there past topics that you can refer me to.

Thank you.
Last edited by ashpri on Fri Sep 23, 2022 4:51 am, edited 1 time in total.
 
ashpri
Member Candidate
Member Candidate
Topic Author
Posts: 143
Joined: Sun Oct 14, 2018 3:11 am

Re: Script for recursive failover based on packet loss

Fri Sep 23, 2022 4:39 am

That logic would not work. Once you disable a route you can't check whether it is back to normal.

Updated logic:

1. Ping 8.8.8.8 (recursive for ISP-1) 20 times

2. If packet loss = 0%,
do nothing

3. Else If packet loss >= 20% and route to ISP-1 (find by comment) distance = 1,
find route to ISP-1 (find by comment) and change distance to 91

4. Else if packet loss = 0% and route to ISP-1 distance != 1,
find route to ISP-1 and change distance to 1

I would then have a similar script for ISP-2 except the distances would be different.

Do you agree with the script logic?
 
ashpri
Member Candidate
Member Candidate
Topic Author
Posts: 143
Joined: Sun Oct 14, 2018 3:11 am

Re: Script for recursive failover based on packet loss

Fri Sep 23, 2022 9:33 am

FYI this is the relevant screen in fortigate to achieve the above.
You do not have the required permissions to view the files attached to this post.
 
User avatar
hughrobbie
just joined
Posts: 9
Joined: Wed Apr 15, 2015 2:29 pm
Location: KZN
Contact:

Re: Script for recursive failover based on packet loss

Thu Nov 24, 2022 3:57 pm

You could always do something like this:
:if ([/ping XXX.XXX.XXX.XXX count=100] = 0) do={
you can change it to greater than or less than, so if you send 100 pings and the returned packets are 95 or less you can do something.
 
User avatar
hughrobbie
just joined
Posts: 9
Joined: Wed Apr 15, 2015 2:29 pm
Location: KZN
Contact:

Re: Script for recursive failover based on packet loss

Thu Nov 24, 2022 4:01 pm

You could always do something like this:
:if ([/ping XXX.XXX.XXX.XXX count=100] = 0) do={
you can change it to greater than or less than, so if you send 100 pings and the returned packets are 95 or less you can do something.
I think i saw something about ROS v7 having a new netwatch tool that can do packet loss detection.
 
dfdf
just joined
Posts: 13
Joined: Wed Dec 08, 2021 3:51 pm

Re: Script for recursive failover based on packet loss

Fri Nov 25, 2022 11:12 pm

:local "stat_period" (5*60);
:local "traffic";
:local "tx-packets-per-second";
:local "rx-packets-per-second";
:local "packet_tx_drops_rate";
:local "packet_rx_drops_rate";
#:local "packet_tx_errors_rate";
#:local "packet_rx_errors_rate";

:for "count" from=1 to=$"stat_period" do={
 :set $"traffic" [/interface/monitor-traffic interface=current_wan as-value once];
 :set $"packet_tx_drops_rate" ($"packet_tx_drops_rate"+($"traffic"->"tx-queue-drops-per-second"));
 :set $"packet_rx_drops_rate" ($"packet_rx_drops_rate"+($"traffic"->"rx-drops-per-second"));
# :set $"packet_tx_errors_rate" ($"packet_tx_errors_rate"+($"traffic"->"tx-errors-per-second"));
# :set $"packet_rx_errors_rate" ($"packet_rx_errors_rate"+($"traffic"->"rx-errors-per-second"));
 :set $"tx-packets-per-second" ($"tx-packets-per-second"+($"traffic"->"tx-packets-per-second"));
 :set $"rx-packets-per-second" ($"rx-packets-per-second"+($"traffic"->"rx-packets-per-second"));

 :delay 1s;
}

:set $"packet_tx_drops_rate" (($"packet_tx_drops"/$"stat_period")*100)/($"tx-packets-per-second");
:set $"packet_rx_drops_rate" (($"packet_rx_drops"/$"stat_period")*100)/($"rx-packets-per-second");
#:set $"packet_tx_errors_rate" ($"packet_tx_errors_rate"/$"stat_period");
#:set $"packet_rx_errors_rate" ($"packet_rx_errors_rate"/$"stat_period");

than evaluate failover criteria based on collected values and switch gateway by manipulating routing table...
 
User avatar
chechito
Forum Guru
Forum Guru
Posts: 2447
Joined: Sun Aug 24, 2014 3:14 am
Location: Bogota Colombia
Contact:

Re: Script for recursive failover based on packet loss

Fri Nov 25, 2022 11:34 pm

i think new implementation of netwatch in Routeros 7.4 and later can help

https://help.mikrotik.com/docs/display/ROS/Netwatch
netwatch.png
You do not have the required permissions to view the files attached to this post.
 
User avatar
vklpt
newbie
Posts: 36
Joined: Mon Feb 18, 2019 1:13 pm
Location: Izhevsk
Contact:

Re: Script for recursive failover based on packet loss

Tue Nov 29, 2022 6:45 pm

Who is online

Users browsing this forum: Xtreme512 and 3 guests