Community discussions

MikroTik App
 
jonah1810
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 98
Joined: Tue Jul 30, 2019 10:19 pm

Queue Tree Data Usage Script

Thu Jul 15, 2021 6:08 am

I have created a script to track customer data usage when using a queue tree. I based my queue tree off of:
https://mum.mikrotik.com/presentations/ ... _Megis.pdf
WARNING: as this script uses a lot of mangle rules it is not very cpu efficient, but I optimized it the best I knew how.

this script is customizable you just need to change the following:
1. The package name and data limit on line #4 ie
local listlimit value=[:toarray {"Surfer";107374182400;"Streamer";644245094400;"Binger";858993459200;"Business";1610611911367}]
package come first, followed by data limit. you can have as many packages as you like. value is in bytes.

2. The list name on line #7 ie
foreach k,v in=[ip firewall address-list print as-value where list=Surfer-List || list=Streamer-List || list=Binger-List || list=Business-List]
list must correspond exactly to your ip firewall address-list name.

this script creates a warning log that the ip address has gone over their limit. if you wish to have it warn you a different method it is on you to modify

for this script to work you must create a slight variation to the mangle in the QOS document above
here is an example of the change on my "Streamer" package
/ip firewall mangle
add action=mark-connection chain=forward comment="Streamer Traffic" \
    new-connection-mark=Streamer-Connection passthrough=yes src-address-list=\
    Streamer-List
add action=mark-packet chain=forward connection-mark=Streamer-Connection \
    new-packet-mark=Streamer-Packet passthrough=yes
add action=jump chain=forward jump-target=Streamer packet-mark=\
    Streamer-Packet
    
I will post the script below as when I put it in the code blocks on this same post again it goes all weird
Last edited by jonah1810 on Thu Jul 15, 2021 6:10 am, edited 1 time in total.
 
jonah1810
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 98
Joined: Tue Jul 30, 2019 10:19 pm

Re: Queue Tree Data Usage Script

Thu Jul 15, 2021 6:09 am

Here is the script
{
	local addr value=[:toarray ""]
	local listname value=[:toarray ""]
	local listlimit value=[:toarray {"Surfer";107374182400;"Streamer";644245094400;"Binger";858993459200;"Business";1610611911367}]

#loop through address package-lists to get address and current package
	foreach k,v in=[ip firewall address-list print as-value where list=Surfer-List || list=Streamer-List || list=Binger-List || list=Business-List] do={
		:set ($addr->$k) value=[pick [pick $v 1 2]]
		:set ($listname->$k) value=[:pick [:tostr [pick $v 4 5]] 5 [find [:tostr [pick $v 4 5]] "-L"]]
	}

#loop through every address to check mangle rule status
	foreach k,v in=$addr do={
# if mangle rule doesnt exist create one
		if ([len [ip firewall mangle find where dst-address=($addr->$k)]] = 0) do={
			/ip firewall mangle
			add action=accept chain=($listname->$k) comment=(($addr->$k) . " " . ($listname->$k) . " Download") dst-address=($addr->$k)
			add action=accept chain=($listname->$k) comment=(($addr->$k) . " " . ($listname->$k) . " Upload") src-address=($addr->$k)
		}

#if mangle rule exists but package has changed then set new chain/comment
		if (([len [ip firewall mangle find where dst-address=($addr->$k)]] > 0) && ([len [ip firewall mangle find where comment=(($addr->$k) . " " . ($listname->$k) . " Download")]] = 0)) do={
			/ip firewall mangle
			set [find where dst-address=($addr->$k)] chain=($listname->$k) comment=(($addr->$k) . " " . ($listname->$k) . " Download")
			set [find where src-address=($addr->$k)] chain=($listname->$k) comment=(($addr->$k) . " " . ($listname->$k) . " Upload")
		}
# if mangle rule exists check data usage against package limit
		if ([len [ip firewall mangle find where dst-address=($addr->$k)]] > 0) do={
			if (([ip firewall mangle get [find where src-address=($addr->$k)] bytes] + [ip firewall mangle get [find where dst-address=($addr->$k)] bytes]) > [:pick $listlimit ([find $listlimit ($listname->$k)] + 1)]) do={
				:log warning message=(($addr->$k) . " has Exceeded " . ($listname->$k) . " limit")
			}
		}
	}
}

Who is online

Users browsing this forum: Maxfadeev699 and 19 guests