Community discussions

MikroTik App
 
millenium7
Long time Member
Long time Member
Topic Author
Posts: 579
Joined: Wed Mar 16, 2016 6:12 am

BTest to multiple routers or all OSPF neighbors

Wed Nov 22, 2023 9:35 am

Needed a way to occasionally run a bandwidth test on every distribution router in the network to assess real-world link speeds, as well as net backhaul capacity out of the network. Finally get around to writing a script to accomplish this
Essentially this will let you specify 1 or more routers by filling out the "BTestTargets" field in the form of an Array
Alternatively leave this blank and will test every connected OSPF neighbor

- If BTestTargets is filled out, will test those routers. Provide in array form i.e. {1.1.1.1;2.2.2.2;3.3.3.3}
- If blank, will find all active OSPF neighbors and test to those instead. All adjacencies so will test primary/backup links
- Tests each target in sequence
- 1 direction at a time with a grace period. Designed with wireless links that have flexible up/down ratio's in mind
- Runs as a script. So if connection is interrupted you can still retrieve results from the log file

Example:

Image

This only reports the results of the btest tool (Useful if you want to assess remaining usable capacity). If you want the actual capacity of the interface this doesn't do it, either watch that interface in real-time or pull interface stats with SNMP


Designed to simply copy/paste into a router and run immediately, or ran remotely via SSH
Creates and runs a script (deletes after script has run), so that if the connection is interrupted you can still log in and retrieve results in the log file

# Runs Upload/Download bandwidth test for defined duration each way (gets final 10s average) and returns result
# Specify BTestTargets as an array i.e. {1.1.1.1;2.2.2.2;3.3.3.3} or leave blank (no curly braces)
# If no IP addresses are specified in BTestTargets, then will test all OSPF neighbors
/system script add name="AutoBTestScript" dont-require-permissions=yes source={
:local BTestTargets
:local BTestUser "TestUser"
:local BTestPass "Password123"
:local BTestDuration 15s
:local BTestConnCount 8
:local BTestProtocol udp
####################
:if ([:len $BTestTargets] < 1) do={
 :foreach i in=[/routing ospf neighbor find] do={
  :local a [/routing ospf neighbor get $i address]
  :local b [/routing ospf neighbor get $i interface]
  :local c [/routing ospf neighbor get $i router-id]
  :set BTestTargets ($BTestTargets,$a)
  :local Output ("Will BTest OSPF neighbor - $a ( $c ) | $b")
  :log warning $Output
  :put $Output
 }
}
:foreach i in=$BTestTargets do={
:local Output "BTesting to $i (Tx)"
:if ([/routing ospf neighbor find address=$i] != "") do={:set $Output ("$Output | *OSPF* via ".[/routing ospf neighbor get [find address=$i] interface])
} else={
  :if ([/ip route find where dst-address="$i/32"] != "") do={
   :local x [:tostr [/ip route get [find where dst-address="$i/32"] gateway-status]]
   :local o [:pick $x [:find $x "via"] [:len $x]]
   :set $Output ("$Output | ".$o)
  }
};
:put $Output ; :log info $Output
:delay 1s
:local t [/tool bandwidth-test connection-count=$BTestConnCount user=$BTestUser password=$BTestPass protocol=$BTestProtocol duration=$BTestDuration direction=transmit address=$i as-value]
:local BTestUpSpeed ((($t->"tx-10-second-average")/1000)/1000)
:local BTestUpCPULocal ($t->"local-cpu-load")
:local BTestUpCPURemote ($t->"remote-cpu-load")
:local BTestUpResult ($t->"status")
:put "Waiting..." ; :log info "Waiting..."
:delay 10s
:local Output "BTesting to $i (Rx)"
:if ([/routing ospf neighbor find address=$i] != "") do={:set $Output ("$Output | *OSPF* via ".[/routing ospf neighbor get [find address=$i] interface])
} else={
  :if ([/ip route find where dst-address="$i/32"] != "") do={
   :local x [:tostr [/ip route get [find where dst-address="$i/32"] gateway-status]]
   :local o [:pick $x [:find $x "via"] [:len $x]]
   :set $Output ("$Output | ".$o)
  }
};
:delay 1s
:put $Output ; :log info $Output
:local t [/tool bandwidth-test connection-count=$BTestConnCount user=$BTestUser password=$BTestPass protocol=$BTestProtocol duration=$BTestDuration direction=receive address=$i as-value]
:local BTestDownSpeed ((($t->"rx-10-second-average")/1000)/1000)
:local BTestDownCPULocal ($t->"local-cpu-load")
:local BTestDownCPURemote ($t->"remote-cpu-load")
:local BTestDownResult ($t->"status")
:local Output "BTest $BTestUpResult/$BTestDownResult to $i - Tx/Rx: $BTestUpSpeed/$BTestDownSpeed Mbps | CPU (Local/Remote) Tx: $BTestUpCPULocal/$BTestUpCPURemote% - Rx: $BTestDownCPULocal/$BTestDownCPURemote%"
:put $Output ; :log warning $Output
:local Output "=== Complete, Waiting..."
:put $Output ; :log info $Output
:delay 3s
}
:local Output "=== BTests Finished ==="
:put $Output ; :log info $Output
/system script remove [find where name="AutoBTestScript"]
};
/system script run "AutoBTestScript";
edit: slight update to boolean logic on older firmware
 
User avatar
chechito
Forum Guru
Forum Guru
Posts: 3135
Joined: Sun Aug 24, 2014 3:14 am
Location: Bogota Colombia
Contact:

Re: BTest to multiple routers or all OSPF neighbors

Thu Nov 23, 2023 5:11 am

be careful to not DOS yourself 8)

Who is online

Users browsing this forum: LouisdeBussy and 4 guests