Quota Measurement and Bandwidth usage control script

Here is a script to measure the bandwidth usage on a specific interface, you can enter the name of the interface on the script and if u have a limitation of bandwidth usage (for example 300GB per month) you can add that limitation and the script will show the remaining number of byte. used upload , used download, total usage and the remaining quota all that will be appear on the required interface comment, first apply this in the new terminal (i ll assume that ether1 is the interface on which the measurement will be done you can choose your own interface name by changing “ether1” to “xxx” where xxx is your interface name"):
/interface set comment=“Sinan @ Upload=0 MB - Download=0 MB - Total=0 MB or 0 GB Remaining=0 MB or 0 GB” ether1
you can change the word “Sinan” by any comment you wanted just put it before @
and now for the measurement process we should add these rules :
/ip firewall mangle add action=mark-packet chain=prerouting comment=“ether1 Down” in-interface=ether1 new-packet-mark=d
/ip firewall mangle add action=mark-packet chain=postrouting comment=“ether1 Up” new-packet-mark=U out-interface=ether1
one for the download and the other is for the download process “dont forget to change ether1 to your input name”
and now here is the script :
/system script add name=“Quota_Measurement " policy=
ftp,reboot,read,write,policy,test,password,sniff,sensitive source=”#$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
#\r
\n# BY Sinan AlChalabi #\r
\n# 23/Apr/2016 #
\r
\n# Measuring Bandwith Usage #\r
\n#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$#\r
\n{\r
\n#Input the name of the input ether at the end of the next line\r
\n:local in "ether1"\r
\n#Input the total limit quota at the end of the next line (in MB)\r
\n:local quota 307200\r
\n:local a [/interface get value-name=comment $in]\r
\n:local name [:pick $a 0 [:find $a "@"]]\r
\n:local up [:pick $a ([:find $a "Upload="]+7) [:find $a " MB"]]\r
\n:local down [:pick $a ([:find $a "Download="]+9) [:find $a " MB -
T"]]\r
\n:local upnew [/ip firewall mangle get value-name=bytes [find comment="
$in Up"]]\r
\n:local downnew [/ip firewall mangle get value-name=bytes [find comment=
"$in Down"]]\r
\n/ip firewall mangle reset-counters [find comment="$in Up"||comment="
$in Down"]\r
\n:set upnew [(($upnew/1024)/1024)]\r
\n:set downnew [(($downnew/1024)/1024)]\r
\n:set up ($up+$upnew)\r
\n:set down ($down+$downnew)\r
\n:local total ($up+$down)\r
\n:local totalg ($total/1024)\r
\n:local remain ($quota-$total)\r
\n:local remaing ($remain/1024)\r
\n:if ($remain<1024) do={\r
\n/log error message="The quota of $in is about to be finished"\r
\n}\r
\n:if ($remain<100) do={\r
\n#/interface disable [find name="$in"]\r
\n}\r
\n/interface set comment="$name@ Upload=$up MB - Download=$down MB - T
otal=$total MB or $totalg GB Remaining=$remain MB or $remaing GB" [fi
nd name="$in"]\r
\n/system scheduler set comment="Upload=$up MB - Download=$down MB - To
tal=$total MB or $totalg GB Remaining=$remain MB or $remaing GB" [fin
d name=$in]\r
\n}\r
\n"

when there will be only 1GB the script will start to warn you that the quota is about to be finished , when it reached 100MB the script will run something (now it runs nothing you can add your own script you want it to be run instead of this #/interface disable [find name="$in"]")
now you have to add a new scheduler to renew the measurement by an interval as you want , it could be each 5 mins for example:
/system scheduler add interval=5m name=ether1 on-event=
“/system script run "Quota_Measuring"\r
\n” policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive
start-date=apr/23/2016 start-time=00:00:00

Hope you enjoy it …

Perfect job.
I’m looking for it , thank you.
Can you make script to look in the WAN comment and disable the line at the specific quota ?

This looks good, is it possible to tell it the dates the quota starts / finishes ?