How to Limit the Bandwidth Speed for a User After A Cap Has Been Reached

by JP Coetzee 0 Comments

This is not used on the WUG, so do not worry about it!


Based on https://wiki.mikrotik.com/wiki/Limiting_a_user_to_a_given_amount_of_traffic_II

Modified the script for Cavelynx to help manage his internet users at a guest house. Fixed for latest ROS (6.41), added variables.

First create the simple queues you need (create script, run ONCE only):

:for e from=20 to=253 do={
/queue simple add name="user $e" target="192.168.1.$e" queue=default/default total-queue=default
}

Then create a schedule to run this daily (23:59):

/que simple reset-counters

The script itself I’d recommend running every 5-20 minutes, depending on your RB’s CPU(s):

:local traf;
:local throttledSpeed;
:local unthrottledSpeed;
:local cap;

# cap in megaBYTEs
:local theCap 200;
# speed limit in megaBIT/s
:local throttledSpeedLimit 5;
:local unthrottledSpeedLimit 20;
:local upThrottledSpeedLimit 5;
:local upUnthrottledSpeedLimit 20;

# the ip range to use
:local cLevel 1;
:local startRange 20;
:local endRange 253;

:local dTemp ($throttledSpeedLimit * 1000000);
:local uTemp ($upThrottledSpeedLimit * 1000000);

:local eTemp ($unthrottledSpeedLimit * 1000000);
:local vTemp ($upUnthrottledSpeedLimit * 1000000);

:set throttledSpeed  "$dTemp/$uTemp";
:set unthrottledSpeed "$eTemp/$vTemp";
:set cap  ($theCap * 1000000 * 8);

/queue simple
  :for i from=$startRange to= $endRange do={
    :if ([/queue simple find target=("192.168." . $cLevel . "." . $i . "/32")] != "") do={
      :set traf [get [find target=("192.168." . $cLevel . "." . $i . "/32")] total-bytes]
# limit people who have reached the "cap"
      :if ($traf  > $cap) do={
:log warning "User $i has reached cap, throttling."
        set [find target=("192.168." . $cLevel . "." . $i . "/32")] max-limit=$throttledSpeed
      }
# we still want to limit people who have not reached the "cap"
      :if ($traf  < $cap) do={
        set [find target=("192.168." . $cLevel . "." . $i . "/32")] max-limit=$unthrottledSpeed
      }
    }
  }

Leave a reply

Your email address will not be published.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>