Page 1 of 1

Firewall Filter RATE: How to access value in script?

Posted: Sat Oct 21, 2017 6:01 pm
by stoser
I need to access the transfer rates associated with a firewall filter rule. The properties that I am referring to can be seen in winbox in the following place: ip/firewall/filter/ ... choose a filter rule ... go to the Statistics tab ... Under that tab there is a graph displayed, and four numerical fields. I can access the "Bytes" and "Packets" fields with scripting. But I have not found any documentation on how to access the "Rate" field and "Packet Rate" field that can both be seen in winbox. In fact, they don't even appear in the wiki as properties. But there must be some way to access them, since winbox displays them, or to calculate them.

In brief what I am trying to do is to periodically capture the the download data rate in Mb/s that is associated with a Connection Mark. Based on that data rate, I need to make certain decisions. Any suggestions are appreciated.

Kind regards,

Re: Firewall Filter RATE: How to access value in script?

Posted: Sat Oct 21, 2017 7:23 pm
by stoser
Okay ... So I scripted a workaround, where I run a script on a schedule every few seconds, and calculate the avg Rate in kbps over that period. See below. It's self explanatory.

If by some chance anyone knows how to access the "Rate" property in the Filter Rules directly, please share, as it would be one less script and shedule to run. Otherwise, I this is a fairly good workaround.


########################
:log info "START TEST SCRIPT";

:global Bytes0;
:global Bytes1;
:global Rate;

:set Bytes0 ($Bytes1);
:set Bytes1 ([/ip firewall filter get [/ip firewall filter find comment="COMMENT_OF_FILTER_RULE"] bytes]);

:log info ("Test Script Bytes0 = " . $Bytes0) ;
:log info ("Test Script Bytes1 = " . $Bytes1) ;

:local Interval;

#Get the value of the seconds in the time field. It is assumed that the schedule will have zero minutes and hours
:set Interval ( [:pick [/system scheduler get [/system scheduler find name="NAME_OF_SCHEDULE"] interval] 6 8 ]);
:tonum $Interval;
:log info ("Test Script Interval = " . $Interval) ;

#Rate variable set to avg rate over time period in kbps
:set Rate ( ( ( ($Bytes1 - $Bytes0) / $Interval) * 8) / 1024);
:log info ("Test Script Rate = " . $Rate) ;


:log info "END TEST SCRIPT";
######################

Re: Firewall Filter RATE: How to access value in script?

Posted: Thu Mar 12, 2020 3:47 pm
by inquiery
No solutions for that yet? I was trying to get those rules rates also, with scripts, and couldn't find the properties.

Re: Firewall Filter RATE: How to access value in script?

Posted: Thu May 28, 2020 6:06 am
by FabioJr
To collect the Rate in "/ip firewall filter", I am using the following script:


#START

:local comm "COMMENT";

:local time 1;
:local bt0 [/ip firewall filter get [find comment=$comm] bytes];
:delay $time;
:local bt1 [/ip firewall filter get [find comment=$comm] bytes];
:local rate ((($bt1 - $bt0) / $time)*8);

:put $rate;

#FORMAT "bitrate"

:local unit "bps";
:if ($rate > 1073741824) do={
:set rate ($rate / 1073741824);
:set unit "Gbps";
} else={
:if ($rate > 1048576) do={
:set rate ($rate / 1048576);
:set unit "Mbps";
} else={
:if ($rate > 1024) do={
:set rate ($rate / 1024);
:set unit "Kbps";
} else={
};
};
};
:set rate "$rate$unit";

:put $rate;

#END

Re: Firewall Filter RATE: How to access value in script?

Posted: Thu May 28, 2020 2:16 pm
by Jotne
Please use code tags for code. Click </> button when code is selected
; is not neded, so removed. (only needed when multiple commands on same line)
and extra not needed else removed
Tab added to better see strukture.
#START

:local comm "COMMENT"
:local time 1
:local bt0 [/ip firewall filter get [find comment=$comm] bytes]
:delay $time
:local bt1 [/ip firewall filter get [find comment=$comm] bytes]
:local rate ((($bt1 - $bt0) / $time)*8)

:put $rate

#FORMAT "bitrate"

:local unit "bps"
:if ($rate > 1073741824) do={
	:set rate ($rate / 1073741824)
	:set unit "Gbps"
} else={
	:if ($rate > 1048576) do={
		:set rate ($rate / 1048576)
		:set unit "Mbps"
	} else={
		:if ($rate > 1024) do={
			:set rate ($rate / 1024)
			:set unit "Kbps"
		}	
	}
}
:set rate "$rate$unit"

:put $rate

#END