If anyone is interested:
Queue tree is quite simple. For sorting purposes, names of items are IP addresses ending with 3 digit number (meaning 192.168.1.1 rule has name 192.168.1.001)
This results in 3 for-loops since i check the name of queue items (i have an idea to fix this but am lazy to do it)
I have one for the complete /24 range for upload and for download.
This script reports bandwidth usage per IP address. It reports the hostname too if the DHCP is on the same device.
Unfortunatley i don'tt know how and if mikrotik can get hostnames from IP addresses any other way. (note for another feature request)
Here is the script:
:log error "Queue Stats Start"
:local traf;
:local megsu "0";
:local megsd "0";
:local resulter "";
:local logmsg;
:local hname "";
/queue tree
:for i from=1 to=9 do={
:set logmsg "";
:if ( [/ip dhcp-server lease find address="192.168.1.$i"] != "" ) do={
:if ( [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name] != "") do={
:set hname [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name]
} else={
:set hname "unknown"
}
} else={
:set hname "n/a"
}
:if ([/queue tree find name="192.168.1.00$i_D"]!="") do={:set traf [get [find name="192.168.1.00$i_D"] bytes]; :set megsd ($traf / 1000);}
:if ([/queue tree find name="192.168.1.00$i_U"]!="") do={:set traf [get [find name="192.168.1.00$i_U"] bytes]; :set megsu ($traf / 1000);}
:if ($megsd > 0) do={
:if ($megsd > 1000) do={ :set megsd ($megsd / 1000); :set megsd "$megsd Mb" } else={ :set megsd "$megsd Kb"}
:set logmsg ("192.168.1.$i Down: $megsd");
:set resulter ("$resulter \n IP: 192.168.1.00$i Host: $hname Down: $megsd")
}
:if ($megsu > 0) do={
:if ($megsu > 1000) do={ :set megsu ($megsu / 1000); :set megsu "$megsu Mb" } else={ :set megsu "$megsu Kb"}
:if ($logmsg!="") do={
:set resulter ("$resulter Up: $megsu")
:set logmsg ("$logmsg Up: $megsu")
} else={
:set resulter ("$resulter \n Ip: 192.168.1.00$i Host: $hname Up: $megsu")
:set logmsg ("192.168.1.$i Up: $megsu");
}
}
:if ($logmsg!="") do={ :log info ($logmsg) }
}
:for i from=10 to=99 do={
:set logmsg "";
:if ( [/ip dhcp-server lease find address="192.168.1.$i"] != "" ) do={
:if ( [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name] != "") do={
:set hname [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name]
} else={
:set hname "unknown"
}
} else={
:set hname "n/a"
}
:if ([/queue tree find name="192.168.1.0$i_D"]!="") do={:set traf [get [find name="192.168.1.0$i_D"] bytes]; :set megsd ($traf / 1000);}
:if ([/queue tree find name="192.168.1.0$i_U"]!="") do={:set traf [get [find name="192.168.1.0$i_U"] bytes]; :set megsu ($traf / 1000);}
:if ($megsd > 0) do={
:if ($megsd > 1000) do={ :set megsd ($megsd / 1000); :set megsd "$megsd Mb" } else={ :set megsd "$megsd Kb"}
:set logmsg ("192.168.1.$i Down: $megsd");
:set resulter ("$resulter \n IP: 192.168.1.0$i Host: $hname Down: $megsd")
}
:if ($megsu > 0) do={
:if ($megsu > 1000) do={ :set megsu ($megsu / 1000); :set megsu "$megsu Mb" } else={ :set megsu "$megsu Kb"}
:if ($logmsg!="") do={
:set resulter ("$resulter Up: $megsu")
:set logmsg ("$logmsg Up: $megsu")
} else={
:set resulter ("$resulter \n Ip: 192.168.1.0$i Host: $hname Up: $megsu")
:set logmsg ("192.168.1.$i Up: $megsu");
}
}
:if ($logmsg!="") do={ :log info ($logmsg) }
}
:for i from=100 to=254 do={
:set logmsg "";
:if ( [/ip dhcp-server lease find address="192.168.1.$i"] != "" ) do={
:if ( [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name] != "") do={
:set hname [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name]
} else={
:set hname "unknown"
}
} else={
:set hname "n/a"
}
:if ([/queue tree find name="192.168.1.$i_D"]!="") do={:set traf [get [find name="192.168.1.$i_D"] bytes]; :set megsd ($traf / 1000);}
:if ([/queue tree find name="192.168.1.$i_U"]!="") do={:set traf [get [find name="192.168.1.$i_U"] bytes]; :set megsu ($traf / 1000);}
:if ($megsd > 0) do={
:if ($megsd > 1000) do={ :set megsd ($megsd / 1000); :set megsd "$megsd Mb" } else={ :set megsd "$megsd Kb"}
:set logmsg ("192.168.1.$i Down: $megsd");
:set resulter ("$resulter \n IP: 192.168.1.$i Host: $hname Down: $megsd")
}
:if ($megsu > 0) do={
:if ($megsu > 1000) do={ :set megsu ($megsu / 1000); :set megsu "$megsu Mb" } else={ :set megsu "$megsu Kb"}
:if ($logmsg!="") do={
:set resulter ("$resulter Up: $megsu")
:set logmsg ("$logmsg Up: $megsu")
} else={
:set resulter ("$resulter \n Ip: 192.168.1.$i Host: $hname Up: $megsu")
:set logmsg ("192.168.1.$i Up: $megsu");
}
}
:if ($logmsg!="") do={ :log info ($logmsg) }
}
/tool e-mail send body="$[/system identity get name] queue stats report on: $[/system clock get date]@$[/system clock get time]\n\n$resulter" subject="Queue stats" to="me@myserver.com" from="$[/system identity get name]@myserver.com" ;
/queue tree reset-counters-all ;
:log error "Queue Stats End"
Scheduler runs this script every midnight. The script, as is, takes about 1 minute to execute on a pentium computer so be careful if putting on a routerboard (especially since it requires 500 mangle rules and 500 queue tree rules)
It will end up on the wiki once i removed the excess code