M
M
Maxim Chornopolsky2014-05-22 09:24:14
Mikrotik
Maxim Chornopolsky, 2014-05-22 09:24:14

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";}}
}}

2) a script that collects queue statistics from queues.
: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;

Unfortunately, Mikrotik cannot handle files longer than 4096 bytes, which is slightly inconvenient, but still enough.
3) no longer a script, but simply a command in the scheduler - / system reboot;
Reboots the Mikrotik and resets the counters. I know that the counters are reset with a simple command, but this way it is more reliable, in my opinion. And reboots from time to time are needed by Mikrotik, they sometimes hang with me.
So that's the question. At the moment, Mikrotik does an excellent job of counting traffic and there is only one, significant, drawback in this system. If Mikrotik is restarted, traffic statistics are reset, respectively, Mikrotik starts accumulating data from scratch. This can be fraught with going beyond the daily traffic limit, and restarting the Mikrotik one way or another at the point is not a problem, and the light goes out periodically.
I just can't think of an algorithm for saving daily traffic statistics to files on Mikrotik, so that he can "not look back" at reboots. Maybe you can suggest something?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2014-05-23
@Voiddancer

In there mikrotik.ru/forum/viewtopic.php?f=3&t=4612 there was a discussion of writing values ​​to a file and working with it already.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question