I testet a lot with queues and with burst with long bursttime. But that doesn't work!
It is coded in v3.2 and seem to work. I'm a newbie in MT scripting so there maybe better ways to code ..
To the script in short sentences..
- it can limit the amount of traffic in specific period of time
- it can slowdown the speed slowly if user comes to the given amount of traffic
- need no usermanager
- can remember the traffic after reboot, (trafficinfo stored in queuename, maybe there is a better way.. please tell me when)
- speed of queues can adjust from every minute to unlimited (need scheduler)
- adjust only specific queues (names of queues must start with a special string you can choose)
- many parameters to adjust for your needs
- many comments i hope you understand it..
please..
- test it
- tell me bugs
- lets improve it
- tell me what you think about ..
Code: Select all
#Variables
:local MaxTrafficVolume
:local TimePeriod
:local TrafficBurst
:local MaxSpeedDown
:local MaxSpeedUp
:local MinSpeedDown
:local MinSpeedUp
:local SlowDownVolume
:local RunTime
:local KeyString
:local TrafficKeyString
#Your Settings Here ------------------------
#Max traffic volume in MB per TimePeriod (TotalBytes, up and download are counted)
:set MaxTrafficVolume 20480
#time in hours, in this time the MaxTrafficVolume can be downloaded/uploaded (720 = 30 days)
:local TimePeriod 720
# similar to Burst in MB, MaxTrafficVolume + TrafficBurst traffic can occur in TimePeriod without effect a speed slowdown
:set TrafficBurst 5000
#in MB, this value causes a SLOW slowdown of the speed till minspeed, value=0 no SLOW slowdown (like in "normal" bursts)
#if X MB more traffic than MaxTrafficVolume + TrafficBurst in TimePeriod
#if SlowDownVolume=5000 and X=2000, (2000/5000 *100)% = 40%, 60% left till Slowdownvolume is reached, Speed = MaxSpeed * 60%
#if SlowDownVolume=5000 and X=4000, (4000/5000 *100)% = 80%, 20% left till Slowdownvolume is reached, Speed = MaxSpeed * 20%
:set SlowDownVolume 5000
#Speed in kbit
:set MaxSpeedDown 800
:set MaxSpeedUp 128
#if set to high MaxTrafficVolume-Limit can easily be exceed
:set MinSpeedDown 32
:set MinSpeedUp 32
# Runtime 15 means that all 15 minutes new bandwidth values are calculated (you have to configure the scheduler with this time!)
:set RunTime 15
#only queues which name start with this String are modified by this script
:set KeyString "20GB-"
# after this string the traffic is stored in queuename (this string can only one time part of the queue name!)
:set TrafficKeyString "__"
#END Your Settings ----------------------------
:local NewPoints
:local OldPoints
:local Traffic
:local DecreasePerStep
:local NewSpeed
:local NewSpeedDown
:local NewSpeedUp
:local NewName
:local SpeedMultiplier
:set DecreasePerStep ($MaxTrafficVolume * 1024 / $TimePeriod / 60 * $RunTime);
:set MaxSpeedDown ($MaxSpeedDown * 1000);
:set MaxSpeedUp ($MaxSpeedUp * 1000);
:set MinSpeedDown ($MinSpeedDown * 1000);
:set MinSpeedUp ($MinSpeedUp * 1000);
:foreach i in=[/queue simple find] do={
#if queue is active and not dynamic and queuename start with keystring
:if ((![/queue simple get $i disabled]) && (![/queue simple get $i dynamic]) && ([:find [/queue simple get $i name] $KeyString] = 0) ) do={
#if it is a queue with traffic info (points)
:if ([:find [/queue simple get $i name] $TrafficKeyString] >= 0) do={
#read trafficinfo from Queuename in KB
:set OldPoints ([:pick [/queue simple get $i name] ([:find [/queue simple get $i name] $TrafficKeyString] + 2) [:len [/queue simple get $i name]]]);
#read new traffic from
:set Traffic ([/queue simple get $i total-bytes] / 1024)
#reset traffic
/queue simple reset-counters [/queue simple get $i name]
:set NewPoints ($OldPoints-$DecreasePerStep+$Traffic);
:if ($NewPoints < 0) do={:set NewPoints 0;}
:set NewSpeedDown $MaxSpeedDown;
:set NewSpeedUp $MaxSpeedUp;
#if to much traffic then decrease speed
:if ($NewPoints > $TrafficBurst * 1024) do={
:if ($SlowDownVolume < 1) do={:set SlowDownVolume 1;}
#little bit math, linear slowdown if traffic over traffic burst
:set SpeedMultiplier (10000 - ((($NewPoints - ($TrafficBurst * 1024))*10000) / ($SlowDownVolume * 1024)))
:set NewSpeedDown (($SpeedMultiplier * $MaxSpeedDown) / 10000)
:set NewSpeedUp (($SpeedMultiplier * $MaxSpeedUp) / 10000)
#speed under minspeed then set minspeed
:if ($NewSpeedDown < $MinSpeedDown) do={:set NewSpeedDown $MinSpeedDown;}
:if ($NewSpeedUp < $MinSpeedUp) do={:set NewSpeedUp $MinSpeedUp;}
}
:set NewSpeed ($NewSpeedUp . "/" . $NewSpeedDown)
:put $NewSpeed;
/queue simple set [/queue simple get $i name] max-limit=$NewSpeed;
#new name of the queue with traffic info
:set NewName ([:pick [/queue simple get $i name] 0 ([:find [/queue simple get $i name] $TrafficKeyString] + 2)] . (tostr($NewPoints)));
/queue simple set [/queue simple get $i name] name=$NewName
}
}
}