Page 1 of 1

tx-bytes rx-bytes have spaces and are unusable. Please help (edited and added more info)

Posted: Sun Jun 11, 2017 5:31 pm
by maikel
Hello everyone,

I'm transforming a scipt from one of the users on this forum (viewtopic.php?t=87565#) to be compatible with the newest version.
I use RouterOS v6.38.5 on Winbox v3.11 on hEX Routerboard.
Therefore I'm trying to use tx-bytes and rx-bytes but I get a value with spaces back from routerOS. I cannot use this value, when I want to add them, I get
41 045 997;41 045 997
This is the code that I want to use;
:local counterrx [/interface ethernet get eth2-master rx-bytes]
:put "counterrx:"
:put $counterrx
:local countertx [/interface ethernet get eth2-master tx-bytes]
:put "countertx:"
:put $countertx
:tonum $counterrx
:tonum $countertx
set $counter ( $counterrx + $countertx )
:put "counter:"
:put $counter
my output is:
counterrx:
41 278 785
countertx:
391 157 213
counter:
41 278 785;391 157 213
I would expect to get numbers (integer) like 41278785 (without spaces) and 391157213 for rx-bytes ($counterrx) and tx-bytes ($countertx)
I tried to debug the following;
'tonum' doesn't change
I made a script to delete spaces, however, the script cannot find any spaces in the integers $counterrx and $countertx
Also it shows me that the length of the number is 1 ???
I used this script;
:local counterrx [/interface ethernet get eth2-master rx-bytes]
:put "counterrx:"
:put $counterrx
:local Replacechar do={
  :for i from=0 to=([:len $1] - 1) do={
    :local char [:pick $1 $i]
    :if ($char = $2) do={
      :set $char $3
    }
    :set $output ($output . $char)
  }
  :return $output
}

:local counterrx2 [ $Replacechar $counterrx " " "" ]
:put $counterrx2
:put "counterrx length:"
:put [ len $counterrx ]
:local blub "blubblub"
:put "blubblub length:"
:put [ len $blub ]
output is:
counterrx:
41 946 248
41 946 248
counterrx length:
1
blubblub length:
8
As last thing I checked if the number $counterrx is an array and counted the elements;
:local counterrx [/interface ethernet get eth2-master rx-bytes]
:foreach nr,xxx in={$counterrx} do={
:put ("$nr=$xxx")
};
output:
0=42 147 960
Only the first number gets counted?

I tried to find spaces and remove them with this code;
:local posspace [ :find $counterrx " " -1]
:put $posspace
:local counterrx2 [:pick $counterrx 0 $posspace]
:put $counterrx2
but that also doesn't work. posspace is empty and counterrx2 doesn't change at all. I also tried to find , but same behavior.
I found out that the value is actually correct (when I verify stats in the interface list in Winbox)

Can someone please help me?

Thank you very much,
Maikel

Edit: I edited almost the whole post to make it much more clear, hopes it helps understanding the situation and to let you see I tried. Also I would like to say that I use RouterOS programming language (C?) for four hours now :)

Re: tx-bytes rx-bytes have spaces and are unusable. Please help (edited and added more info)

Posted: Mon Jun 12, 2017 3:35 pm
by maikel
Could someone please help me with this
Thank you very much in advance

Re: tx-bytes rx-bytes have spaces and are unusable. Please help (edited and added more info)

Posted: Fri Jun 16, 2017 8:40 pm
by maikel

Re: tx-bytes rx-bytes have spaces and are unusable. Please help (edited and added more info)

Posted: Thu Jul 20, 2017 8:02 pm
by LaZyLion
I'm having this issue as well. I'm using 6.39.2

The manual clearly states these numbers should be returned as integers: https://wiki.mikrotik.com/wiki/Manual:Interface

but
:put [:typeof [/interface ethernet get ether1 rx-bytes]]
shows that an array is being returned. The spaces make this extra unhelpful.

You can convert this to a string using
[:pick  [/interface ethernet get ether1 rx-bytes] 0 ]
and then use a for loop to find and eliminate the spaces, but that seems like a cumbersome solution
when you have to pull stats for several interfaces. A lot of extra processing for no good reason.


I've submitted a bug report. Hopefully they correct this in a new version.

Re: tx-bytes rx-bytes have spaces and are unusable. Please help (edited and added more info)

Posted: Thu Jul 20, 2017 10:13 pm
by IntrusDave
I use this code to fix it. The Global creates a reusable function, then use the "$removeSpace t=" to call the function
:global removeSpace do={ :local temp;
    :for i from=0 to=([:len $t] - 1) do={ :local char [:pick $t $i];
        :if ($char = " ") do={ :set $char ""; }
        :set temp ($temp . $char); }
    :return $temp; }

:put [$removeSpace t=[:tostr [/interface ethernet get value-name=tx-bytes eth2-master]]]

Re: tx-bytes rx-bytes have spaces and are unusable. Please help (edited and added more info)

Posted: Sat Oct 02, 2021 4:39 am
by PortalNET
Hi guys..

i know its a litle bit late on ansewers, but its better late then never right, ok so my working version of the code to fetch on newer Versions rOS 6.46, 6.47 the total RX-BYTES and TX-BYTES

working code below..
 if ($API->connect($iphost, $userhost, $pass, $port)) {
 $stats = $API->comm('/interface/print', array('stats-detail' => '', 'from' => '0')); // here on 0 you can change it in order to select the correct Interface or vlan-interface in your mikrotik 
print_r($stats);
		   $meme = $stats['0'];
		   echo "<td>" . $meme['rx-byte']. "</td><br />";
		   echo "<td>" . $meme['tx-byte'] . "</td><br />";
		   
           $API->disconnect();
		   
	   
         }
 

then we need to convert it from bits to Gigabits using the following code
<?php
$bytes = $meme['rx-byte'];
function isa_bytes_to_gb($bytes, $decimal_places = 1 )
{
return number_format($bytes / 1073741824, $decimal_places);
}
$gigabytes = isa_bytes_to_gb($bytes);
echo "<td>" . $gigabytes . " GB</td><br />";
?>

Re: tx-bytes rx-bytes have spaces and are unusable. Please help (edited and added more info)

Posted: Sat Oct 02, 2021 12:00 pm
by rextended
And... why API? I do not see on any post on this thread something about API...


Simply I see a wrong approach...

This is the solution.....
:put [/interface ethernet get ether1 rx-bytes]
2 778 266 326

:put ([/interface get ether1]->"rx-byte")
2739389845

Re: tx-bytes rx-bytes have spaces and are unusable. Please help (edited and added more info)

Posted: Tue May 31, 2022 2:04 pm
by kirac
Hello,

I can't use this method. How can I follow a path with the command I specified below?
> :put ([/interface ethernet get ether1]->"rx-fcs-error")
5 297
I need to remove the spaces
Thanks