Answer the question
In order to leave comments, you need to log in
How to organize billing using Mikrotik?
Hello.
I have several points on the satellite Internet in my "wards". You have to constantly monitor the volume of traffic so that the telephone connection via sip does not fall. I tried to use mikro-bill, in principle a very flexible thing, but no one now expects that traffic should be counted per megabyte, and even incoming / outgoing. Well, if the server with billing dies, no one limits the traffic, which is fraught. And in the taiga with electricity and uninterruptible power supplies, anything can happen.
Thus, I came up with a script system of the following form:
1) a script that bypasses queues (/queue simple) and where traffic is registered in the comments, it compares the number from the comments with the queue counter, if necessary, turns off the client entry in the arp table (on the points, devices that are not listed in this table do not have Internet access, bridge-local set to arp reply-only). If the queue traffic is less - includes. The script runs around all available queues, before each queue had its own script, but it's more reliable this way.
Excess put left from the time of debugging the script :)
:local n;
:local upload;
:local download;
:local totaltraf;
:local realtraff;
:local limit;
:local clientip;
:local clientiparray;
:for y from=0 to=([/queue simple print count-only]-1) do={
:put "==============================================================================================";
:put $y;
:set $upload 0;
:set $download 0;
:set $totaltraf 0;
:set $realtraff 0;
:set $limit 0;
:set n [/queue simple get number=$y name];
:put $n;
:set $realtraff [/queue simple get "$n" byte];
:put $realtraff;
:set $limit [/queue simple get "$n" comment];
:set $upload [:pick $realtraff 0 ([:find $realtraff "/"])];
:put ($upload / 1048576);
:set download [:pick $realtraff ([:find $realtraff "/"]+1) [:len $realtraff]];
:put ($download / 1048576 );
:set totaltraf (($upload + $download) / 1048576 + $totaltraf);
:put ("traffic: ".$totaltraf);
:put ("limit: ".$limit);
:if (limit != "no") do={:if ($totaltraf >= $limit) do={:set clientiparray [/queue simple get number=$y target]; for i from 0 to=([:len [/queue simple get number=$y target]]-1) do={:set clientip [:pick $clientiparray $i]; /ip arp set [find address=[:pick $clientip 0 [:find $clientip "/32"]]] disable=yes; put "zablokirovano";}} else={:set clientiparray [/queue simple get number=$y target]; for i from 0 to=([:len [/queue simple get number=$y target]]-1) do={:set clientip [:pick $clientiparray $i];:set clientip [:pick $clientip 0 [find $clientip "/32"]]; /ip arp set [find address=$clientip] disable=no; put "razblokirovano";}}
}}
:local inFile;
:local toFile;
:local upload;
:local download;
:local totaltraf;
:local realtraff;
:local limit;
:local name [/system identity get name];
:for y from=0 to=([/queue simple print count-only]-1) do={
:set realtraff [/queue simple get number=$y byte];
:set upload [:pick $realtraff 0 ([:find $realtraff "/"])];
:set download [:pick $realtraff ([:find $realtraff "/"]+1) [:len $realtraff]];
:set totaltraf (($upload + $download) / 1048576);
:set inFile [/queue simple get number=$y name];
:put "$inFile\r\t\t$totaltraf\tMB";
:set toFile "$toFile\r\n$inFile\r\t\t$totaltraf\tMB\n";
};
:local date [/system clock get date];
:local filename ($name."_"."traffic"."_".[:pick $date 7 11] . [:pick $date 0 3] . [:pick $date 4 6].".log" );
:local date [/system clock get date];
:local time [/system clock get time];
:local datetime ("$date\r\n$time");
/file print file="$filename";
:put "delay on one";
:delay delay-time=2;
/file set "$filename" contents="$datetime\r\n$toFile";
:delay delay-time=2;
/tool fetch address=192.168.1.36 src-path="$filename" user=mikrotik mode=ftp password=***** dst-path="home/trafficstat/$filename" upload=yes;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question