Community discussions

MikroTik App
 
PhireSideZA
just joined
Topic Author
Posts: 2
Joined: Sun Dec 11, 2022 9:39 pm

Scripting help needed with queues and netwatch (Newbie alert)

Mon Dec 19, 2022 5:49 pm

Hi everyone,

I am quite new to the Mikrotik world, having gotten my HeX S about a year ago after I figured that my EdgeRouter X was not cutting it anymore.

I very briefly delved into Mikrotik years ago when Squid proxies were still a thing, but I'm definitely far from a power user when it comes to scripting.

I have a situation where I have two WAN connections, configured with PCC and failover which is working pretty flawlessly for the most part. I also have queues that I dynamically adjust the bandwidth on as these WAN connections go online and offline quite frequently due to power outages. Ideally, if both connections are up, I want the limits to be 100M/100M, but when WAN1 is down, I want it to be 50M/50M and when WAN2 is down, it has to be 15M/15M due to the WAN speeds being asymmetric (WAN1=30mbps, WAN2=100mbps)

This used to work fine when my WAN1 was quite stable and only WAN2 would go offline often, but lately, even WAN1 is going offline quite a bit, and my issue is that my script isn't smart enough to cover all scenarios and sometimes the wrong queue speeds are applied.

What I had in mind was a script that can run every ten seconds and check which connections are up, either by checking the Netwatch status or the route status. I would then like to assign a value to each WAN link (ie. 1 for WAN1 and 2 for WAN2) so that if the variable value is 1, I know WAN1 is up but WAN2 is down, and if the value is 2, I know that WAN1 is down and WAN2 is up, and finally when the value is 3, then I know both links are up and I can apply the queues accordingly.

What I have managed to do is get a value of 1 or 0 depending on the WAN link status with this snippet of code:
[:len [/tool netwatch find comment=wan2 and status=up]]
This is the code that I need to articulate to be able to work in my script, but I know that my syntax is way off and needs work. This is just to put my ideas onto paper, as it were:
if $wan_status=1 do /queue simple set [find comment="limit"] max-limit=15M/15M

if $wan_status=2 do /queue simple set [find comment="limit"] max-limit 50M/50M

if $wan_status=3 do /queue simple set [find comment="limit"] max-limit 100M/100M

TL;DR:

I need assistance in writing a script to do the following:

Take values from netwatch, transform the variables to something I can use (ie. 1 for WAN1 and 2 for WAN2), script it so that it knows which queue speeds to apply given the current WAN situation. Apologies for any brevity and my overall lack of scripting experience; I know what I want to achieve but I lack the know-how of making it machine-readable and I would appreciate any tips or hints if there is another way of handing this!
 
PhireSideZA
just joined
Topic Author
Posts: 2
Joined: Sun Dec 11, 2022 9:39 pm

Re: Scripting help needed with queues and netwatch (Newbie alert)  [SOLVED]

Wed Dec 21, 2022 4:38 pm

Solved this with some clever Netwatch scripts:

WAN1 UP:
/log error "*** Frogfoot is UP ***"
:if [/ip route find where comment="ISP2_TEST" and active=yes] do={
/queue simple set [find comment=limit] max-limit=100M/100M
} else={
/queue simple set [find comment=limit] max-limit=15M/15M
}
WAN1 DOWN:
/log error "*** Frogfoot is DOWN ***"
:if [/ip route find where comment="ISP2_TEST" and active=yes] do={
/queue simple set [find comment=limit] max-limit=50M/50M
} else={
/queue simple set [find comment=limit] max-limit=100M/100M
}
WAN2 UP:
/log error "*** Rain is UP ***"
:if [/ip route find where comment="ISP1_TEST" and active=yes] do={
/queue simple set [find comment=limit] max-limit=100M/100M
} else={
/queue simple set [find comment=limit] max-limit=15M/15M
}
WAN2 DOWN:
/log error "*** Rain is DOWN ***"
:if [/ip route find where comment="ISP1_TEST" and active=yes] do={
/queue simple set [find comment=limit] max-limit=15M/15M
} else={
/queue simple set [find comment=limit] max-limit=100M/100M
}

Who is online

Users browsing this forum: EmuAGR and 25 guests