mikrotik:scripting:queue_stats_to_email

Send Queue tree stats to e-mail

If you have Queues set up per IP address, you can send yourself Queue stats via email. You might want to see where your bandwidth is going, or you might want to detect who's been making a backup of the internet and preventing other users from using the Internet.

Copy this code and create a scheduler which will run at designated time (eg. midnight)

It will mail you the current stats, and reset all counters, so next time you have a new clean stats.

: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="<your email here>" from="$[/system identity get name]@example.com" ;
/queue tree reset-counters-all ;
:log error "Queue Stats End"

Do not forget to enter your own e-mail address

  • mikrotik/scripting/queue_stats_to_email.txt
  • Last modified: 2019/10/31 09:05
  • by 127.0.0.1