Community discussions

MikroTik App
 
stoser
Member Candidate
Member Candidate
Topic Author
Posts: 119
Joined: Sun Aug 21, 2016 12:04 am

Firewall Filter RATE: How to access value in script?

Sat Oct 21, 2017 6:01 pm

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,
 
stoser
Member Candidate
Member Candidate
Topic Author
Posts: 119
Joined: Sun Aug 21, 2016 12:04 am

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

Sat Oct 21, 2017 7:23 pm

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";
######################
 
inquiery
Frequent Visitor
Frequent Visitor
Posts: 51
Joined: Mon Oct 27, 2014 3:49 pm

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

Thu Mar 12, 2020 3:47 pm

No solutions for that yet? I was trying to get those rules rates also, with scripts, and couldn't find the properties.
 
FabioJr
just joined
Posts: 1
Joined: Thu May 28, 2020 4:46 am

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

Thu May 28, 2020 6:06 am

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
Last edited by FabioJr on Thu May 28, 2020 6:14 am, edited 2 times in total.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1789
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

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

Thu May 28, 2020 2:16 pm

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
 
How to use Splunk to monitor your MikroTik Router(s)

MikroTik->Splunk
 

Who is online

Users browsing this forum: No registered users and 18 guests