Community discussions

MikroTik App
 
User avatar
vader7071
newbie
Topic Author
Posts: 39
Joined: Tue Jan 07, 2014 9:44 pm

script to run manual bandwidth test and save to file

Thu Jan 11, 2018 10:18 pm

I am hoping some one can help get this correct.

Here is the desired end result. I want to be able to run a script (manually) where my CRS109 runs a bandwidth test and then saves the results to a file. I want the test to run only 10 seconds.

This line of code works from the terminal screen and running a script from the terminal screen:

Code: Select all

/tool bandwidth-test $target duration=10s direction=both local-udp-tx-size=1000 protocol=udp remote-udp-tx-size=1000 user=$user pass=$pass

I am sure everyone has seen that code run. I get the results:

Code: Select all

status: completetd
duration: 10s
tx-current: 0bps
.......(and so on)

What I would like here is to take all of those values, and dump them into a text file. Creating a test script, I have been able to use

Code: Select all

/file set $filename contents=$data
and have created just junk data into a file. So I know how to put data into a file, and I can run a test to get the data. Where I am having issues right now is getting the data to dump into the file.

Thank you in advance for your help.
 
User avatar
vader7071
newbie
Topic Author
Posts: 39
Joined: Tue Jan 07, 2014 9:44 pm

Re: script to run manual bandwidth test and save to file

Fri Jan 12, 2018 9:43 pm

An update to this. I tried the following:

Code: Select all

/file set report.txt contents=(/tool bandwidth-test $target duration=10s user=$user pass=$pass)
And what I get is "true" in the file. The file gets accessed and modified, but the only thing in it is "true". It also just spits it out. The test should take about 10 seconds, but this script is done in a fraction of a second.
 
boredwitless
just joined
Posts: 6
Joined: Tue Jul 10, 2018 12:19 pm

Re: script to run manual bandwidth test and save to file

Tue Jul 10, 2018 12:28 pm

You ever get anywhere with this? I'm pretty new to scripting so I've been trying to makes sense of other posts.

viewtopic.php?t=19775#
viewtopic.php?t=82357#
 
User avatar
vader7071
newbie
Topic Author
Posts: 39
Joined: Tue Jan 07, 2014 9:44 pm

Re: script to run manual bandwidth test and save to file

Tue Jul 10, 2018 2:18 pm

I did. I'm away from my router right now, but I will post how I got it to work tonight.
 
User avatar
vader7071
newbie
Topic Author
Posts: 39
Joined: Tue Jan 07, 2014 9:44 pm

Re: script to run manual bandwidth test and save to file

Tue Jul 10, 2018 2:48 pm

I was able to remote in.

Here is how I did it:

Create a script with this in it. I named this one "server0". Make sure to update the IP address of the test server. You can also make the test run longer than 15 seconds, but when I did this, i didn't want to bog anything down on the test server's end.
{
:local txc
:local txcA
:local txcB
:local txcC

:local rxc
:local rxcA
:local rxcB
:local rxcC

:local rxta
:local rxtaA
:local rxtaB
:local rxtaC

:local txta
:local txtaA
:local txtaB
:local txtaC

:local sysname [/system identity get name]
:local datetime "$[/system clock get date] $[/system clock get time]"
:local result
:local month
:local day
:local year
:local hour
:local min

:set month [:pick $datetime 0 3]
:set day [:pick $datetime 4 6]
:set year [:pick $datetime 7 11]
:set hour [:pick $datetime 12 14]
:set min [:pick $datetime 15 17]

/tool bandwidth-test xxx.xxx.xxx.xxx protocol=udp local-udp-tx-size=1000 remote-udp-tx-size=1000 direction=both duration=15s user=btest password=btest do={

:set txcA ($"tx-current" / 1000)
:set txcB ($txcA / 1000 * 1000)
:set txcC ($txcA - $txcB)
:set txcB ($txcB / 1000)
:set txc "$txcB.$txcC"

:set rxcA ($"rx-current" / 1000)
:set rxcB ($rxcA / 1000 * 1000)
:set rxcC ($rxcA - $rxcB)
:set rxcB ($rxcB / 1000)
:set rxc "$rxcB.$rxcC"

:set rxtaA ($"rx-total-average" / 1000)
:set rxtaB ($rxtaA / 1000 * 1000)
:set rxtaC ($rxtaA - $rxtaB)
:set rxtaB ($rxtaB / 1000)
:set rxta "$rxtaB.$rxtaC"

:set txtaA ($"tx-total-average" / 1000)
:set txtaB ($txtaA / 1000 * 1000)
:set txtaC ($txtaA - $txtaB)
:set txtaB ($txtaB / 1000)
:set txta "$txtaB.$txtaC"

:set result $status
}

/file print file="$year$month$day-$hour$min-report.txt"
/file set "$year$month$day-$hour$min-report.txt" contents="$sysname
$datetime

Result : $result
Server : xxx.xxx.xxx.xxx

Upload burst speed $txc Mbps/s
Download burst speed $rxc Mbps/s

Upload total average $txta Mbps/s
Download total average $rxta Mbps/s"
}
Once the script is done, then create a scheduler with this:
/system script run server0
The end run for me, I had 3 different server scripts (server0, server1, server2) set up with 3 different schedulers. Each script went to a different test server so i could get a good idea if I was having issues with my ISP. By testing against 3 servers, i could see a pattern of just one server always reporting low, or if all 3 reported low, it was me.

The schedules were setup to start at 00:00, 03:40, and 07:20. Each one ran recurring every 11 hours. This let me drift through the day and get a good feel for when I was getting slow service and when it was solid.

When the script ran, it would save a *.txt file on the router that I could download later on. The report looks like this:
{router name}
apr/01/2018 07:10:00

Result : done testing
Server : xxx.xxx.xxx.xxx 

Upload burst speed 4.935 Mbps/s
Download burst speed 11.253 Mbps/s

Upload total average 5.302 Mbps/s
Download total average 11.404 Mbps/s
FYI, this is one of the reports I showed to my ISP to prove they were not giving me the speeds I was promised.

Hope this helps.
 
jo2jo
Forum Guru
Forum Guru
Posts: 1007
Joined: Fri May 26, 2006 1:25 am

Re: script to run manual bandwidth test and save to file

Sun Nov 25, 2018 1:48 am

thank you for posting this! I already had a script that ran a BW test every 5 minutes, but i was then just using SNMP to "grab" the results (ie snmp traffic with short interval, and looking for the spike).

I can add this- for some of my mt scripts, one way to retrieve the value of a variable (or result from a script), if its a number (maybe integer only? not sure), is to have the script assign the value of the variableto a Simple Queue's name. And then with snmp you can pull the value of the simple queues name (and thus you can graph that script variable). I use this for graphing the number of active VPN sessions on a mt vpn server i have (or in another case, for snmp graphing the current number of "NAT connections open/tracked", via /ip fire connections print count) .
EDIT- i also have one of these setup to snmp graph the value of /ip neighbor count

below is script i wrote for that task. (i am going to modify your script to try to snmp graph the results of scheduled BW tests, i will post that if im successful in a few hours).

(i just have a single scheduler entry that runs every 5 minutes for this below):
code:
/queue simple set [find comment="SNMPget_vpnACTIVEconnections_Count_Holder"] name=[/ppp active print count-only]

or export:

/system scheduler
add interval=4m name=queueIPneighbor on-event=\
    "/queue simple set [find comment=\"SNMPget_vpnACTIVEconnections_Count_Holder\"] name=[/ppp active print count-only]" policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-time=startup
    
you need to create a dumb simple queue to hold this value, you can set it to disabled=yes (then use /queue simple print OID to get its snmp oid to query)


EDIT 2- well that was quick and easy! Credit ofcourse to OP - vader7071 . i just deleted some lines and added a few, he did the heavy work!
You do need to create 2x disabled simple queues named:
bwTX
and
bwRX
(and then can use /queue simple print oid and grab the "name=" oid from each of the 2x queues, and plug that into your SNMP graphing tool, i use PRTG).
and ofcourse set the IP of your BW test server (IP , not dns). Im using his txcB / rxcB varables as the sites im using this on, are are many megabits in speed. so if you need kb or a lower measurement scale, you can modify the varables accordingly (or use the direct output from the bw-test run, ie the $txc but remove the "/1000" part)
{
:local txc
:local txcA
:local txcB
:local txcC

:local rxc
:local rxcA
:local rxcB
:local rxcC

:local rxta
:local rxtaA
:local rxtaB
:local rxtaC

:local txta
:local txtaA
:local txtaB
:local txtaC

/tool bandwidth-test x.x.x.x protocol=tcp direction=both duration=5s do={

:set txcA ($"tx-current" / 1000)
:set txcB ($txcA / 1000 * 1000)
:set txcC ($txcA - $txcB)
:set txcB ($txcB / 1000)
:set txc "$txcB.$txcC"

:set rxcA ($"rx-current" / 1000)
:set rxcB ($rxcA / 1000 * 1000)
:set rxcC ($rxcA - $rxcB)
:set rxcB ($rxcB / 1000)
:set rxc "$rxcB.$rxcC"

:set rxtaA ($"rx-total-average" / 1000)
:set rxtaB ($rxtaA / 1000 * 1000)
:set rxtaC ($rxtaA - $rxtaB)
:set rxtaB ($rxtaB / 1000)
:set rxta "$rxtaB.$rxtaC"

:set txtaA ($"tx-total-average" / 1000)
:set txtaB ($txtaA / 1000 * 1000)
:set txtaC ($txtaA - $txtaB)
:set txtaB ($txtaB / 1000)
:set txta "$txtaB.$txtaC"

}

#:log warning "rxta:"
#:log warning ($rxtaB)
#:log warning "txcB:"
#:log warning ($txcB)

/queue simple set [find comment="bwRX"] name=($rxtaB)
/queue simple set [find comment="bwTX"] name=($txcB)

}
 
User avatar
ADahi
Member Candidate
Member Candidate
Posts: 209
Joined: Thu Sep 21, 2017 7:16 pm
Location: Iraq, Ninavah
Contact:

Re: script to run manual bandwidth test and save to file

Sun Nov 25, 2018 4:47 pm

 
User avatar
fmarais007
newbie
Posts: 26
Joined: Thu Jan 11, 2018 9:16 am

Re: script to run manual bandwidth test and save to file

Thu Jul 09, 2020 11:04 am

Hi,

I came across this script, but it wasn't working for me in ROS 6.47, so I had to tweak it a bit before it worked.
:local txc
:local txcA
:local txcB
:local txcC

:local rxc
:local rxcA
:local rxcB
:local rxcC

:local rxta
:local rxtaA
:local rxtaB
:local rxtaC

:local txta
:local txtaA
:local txtaB
:local txtaC

:local sysname [/system identity get name]
:local datetime "$[/system clock get date] $[/system clock get time]"

:local month
:local day
:local year
:local hour
:local min
:local filename
:local result

:set month [:pick $datetime 0 3]
:set day [:pick $datetime 4 6]
:set year [:pick $datetime 7 11]
:set hour [:pick $datetime 12 14]
:set min [:pick $datetime 15 17]
:set filename "$year$month$day-$hour$min-report.txt"

:log info "Bandwidth-test start"

/tool bandwidth-test protocol=udp direction=both duration=15s local-udp-tx-size=1000 remote-udp-tx-size=1000 address=xxx.xxx.xxx.xxx user=YOURUSERNAME password=YOURPASSWORD do={

:set $txcA ($"tx-current" / 1000)
:set $txcB ($txcA / 1000 * 1000)
:set $txcC ($txcA - $txcB)
:set $txcB ($txcB / 1000)
:set $txc "$txcB.$txcC"

:set $rxcA ($"rx-current" / 1000)
:set $rxcB ($rxcA / 1000 * 1000)
:set $rxcC ($rxcA - $rxcB)
:set $rxcB ($rxcB / 1000)
:set $rxc "$rxcB.$rxcC"

:set $rxtaA ($"rx-total-average" / 1000)
:set $rxtaB ($rxtaA / 1000 * 1000)
:set $rxtaC ($rxtaA - $rxtaB)
:set $rxtaB ($rxtaB / 1000)
:set $rxta "$rxtaB.$rxtaC"

:set $txtaA ($"tx-total-average" / 1000)
:set $txtaB ($txtaA / 1000 * 1000)
:set $txtaC ($txtaA - $txtaB)
:set $txtaB ($txtaB / 1000)
:set $txta "$txtaB.$txtaC"

:set result $status

}

:log info "Bandwidth-test done"

/file print file=$filename
:delay 5
/file set $filename contents="$sysname

$datetime



Result : $result

Server : xxx.xxx.xxx.xxx



Upload burst speed $txc Mbps/s
Download burst speed $rxc Mbps/s

Upload total average $txta Mbps/s
Download total average $rxta Mbps/s"

:log info "Speed test results written to $filename"

For some reason I kept getting a "no such item" error when running the script manually, so adding the delay ultimately fixed it.
Sometimes the local and remote udp-tx-size doesn't work, so you can remove those if necessary.
Anyway, hope this helps someone.

Cheers
 
creatin
Member Candidate
Member Candidate
Posts: 108
Joined: Sat Nov 23, 2019 2:59 am

Re: script to run manual bandwidth test and save to file

Fri Jul 10, 2020 3:21 pm

From which server are you pulling test file?
How does it work?

Who is online

Users browsing this forum: No registered users and 9 guests