Community discussions

MikroTik App
 
karaYusuf
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 54
Joined: Wed Oct 12, 2022 11:07 am

Getting avg-rtt of ping

Wed Mar 08, 2023 10:52 am

Hello guys,
I am sorry for this stupid question but I tried for some hours and do not get it.
I wrote script, which should get the "avg-rtt" of a ping but it does not work.
This is my Code:
:local a [:ping 1.1.1.1 count=3 as-value]
:local avg ($a->"avg-rtt");
:put $avg;

If I run this in the terminal I get nothing as output, but if I ping in the terminal I can clearly see that there is an avg-rtt output.
What am I doing wrong?
I appreciate every advice :)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Getting avg-rtt of ping

Wed Mar 08, 2023 11:54 am

You do not specify RouterOS version, probably v7 because v6 do not have as-value for ping.

But on v7 as-value still no return stats, but only single pings (you can calc it sum all 3 times and divide by 3)...

Alternative for v6 (work also on v7)
:put ([/tool flood-ping [:resolve "www.mikrotik.com"] count=3 as-value]->"avg-rtt")

For v7
{
:local test [:ping [:resolve "www.mikrotik.com"] count=3 as-value]
:local avg 0
:foreach try in=$test do={:set avg ($avg + ($try->"time"))}
:set avg ($avg / [:len $test])
:put $avg
}

The first method provide more human readable values (50), the second not so easy:
00:00:00.050994
Trimming the string, can be extracted only the interested part:
{
:local test [:ping [:resolve "www.mikrotik.com"] count=3 as-value]
:local avg 0
:foreach try in=$test do={:set avg ($avg + ($try->"time"))}
:set avg ($avg / [:len $test])
:local out  ""
:set out ( ([:tonum [:pick $avg 6   8]] * 1000000) + \
           ([:tonum [:pick $avg 10 12]] *    1000) + \
            [:tonum [:pick $avg 12 15]]              \
         )
:put "$($out / 1000)ms"
}
 
karaYusuf
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 54
Joined: Wed Oct 12, 2022 11:07 am

Re: Getting avg-rtt of ping

Wed Mar 08, 2023 2:16 pm

For v7
{
:local test [:ping [:resolve "www.mikrotik.com"] count=3 as-value]
:local avg 0
:foreach try in=$test do={:set avg ($avg + ($try->"time"))}
:set avg ($avg / [:len $test])
:put $avg
}
That is exactly what I searched for. Thank you very much.
It is also very usefull to know how to trimm it.
If I wanted to get packet-loss, should I do it in a similar way or is there something smarter?
Just that you know if it does matter, I would need the routing-table element in the ping, so flood-ping is not an option, if I understood it correctly.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Getting avg-rtt of ping

Wed Mar 08, 2023 3:07 pm

Everytime the "status" filed is one string, is one failed ping (because containing the error code:

all parameters code

{
:local test [:ping [:resolve "www.mikrotik.com"] count=3 as-value]
:local avg 0
:local fails 0
:local min 00:00:10
:local max 00:00:00
:foreach try in=$test do={
    :set avg ($avg + ($try->"time"))
    :if ([:typeof ($try->"status")] = "str") do={
        :set fails ($fails + 1)
    } else={
        :if (($try->"time") < $min) do={:set min ($try->"time")}
        :if (($try->"time") > $max) do={:set max ($try->"time")}
    }
}
:set avg ($avg / [:len $test])
:set avg [:pick $avg 0 15]
:put "sent=$[:len $test] received=$([:len $test]-$fails) packet-loss=$(($fails*100)/[:len $test])% min-rtt=$min avg-rtt=$avg max-rtt=$max"
}
Percentage is hard to calc precisely because MikroTik scripting do not support decimals.
Adding decimals multiply the number by 100 and splitting with :put is possible, but on this case is excessive...
 
karaYusuf
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 54
Joined: Wed Oct 12, 2022 11:07 am

Re: Getting avg-rtt of ping

Wed Mar 08, 2023 5:28 pm

Everytime the "status" filed is one string, is one failed ping (because containing the error code:

all parameters code

{
:local test [:ping [:resolve "www.mikrotik.com"] count=3 as-value]
:local avg 0
:local fails 0
:local min 00:00:10
:local max 00:00:00
:foreach try in=$test do={
    :set avg ($avg + ($try->"time"))
    :if ([:typeof ($try->"status")] = "str") do={
        :set fails ($fails + 1)
    } else={
        :if (($try->"time") < $min) do={:set min ($try->"time")}
        :if (($try->"time") > $max) do={:set max ($try->"time")}
    }
}
:set avg ($avg / [:len $test])
:set avg [:pick $avg 0 15]
:put "sent=$[:len $test] received=$([:len $test]-$fails) packet-loss=$(($fails*100)/[:len $test])% min-rtt=$min avg-rtt=$avg max-rtt=$max"
}
Percentage is hard to calc precisely because MikroTik scripting do not support decimals.
Adding decimals multiply the number by 100 and splitting with :put is possible, but on this case is excessive...
Your script is more then I even needed. Thank you very much for your time and effort :)
 
Guntis
MikroTik Support
MikroTik Support
Posts: 157
Joined: Fri Jul 20, 2018 1:40 pm

Re: Getting avg-rtt of ping

Wed Mar 08, 2023 6:23 pm

In case it helps, Netwatch ICMP probe offers built-in rtt-avg timer:
https://help.mikrotik.com/docs/display/ ... obeoptions
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3250
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Getting avg-rtt of ping

Wed Mar 08, 2023 8:37 pm

LOL. I was going to suggest this:
In case it helps, Netwatch ICMP probe offers built-in rtt-avg timer:
And a script can trigger based on the result, or even on just on "test" itself. The new netwatch in recent V7 is quite nice feature...

Who is online

Users browsing this forum: JDF and 26 guests