Community discussions

MUM Europe 2020
 
tomtom80
newbie
Topic Author
Posts: 43
Joined: Sun Jan 06, 2008 12:48 am

A working dynamic bandwidth script. Test it!

Sat Oct 16, 2010 1:27 pm

Hallo,

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 ..
#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
		}
	}
}
This example script works with a queue named like this and there you can see the calculated up an down rates.
screen.png
20GB-queuename__0 (so have to look new queues with the current settings of the script when you want to test)
You do not have the required permissions to view the files attached to this post.
 
tomtom80
newbie
Topic Author
Posts: 43
Joined: Sun Jan 06, 2008 12:48 am

Re: A working dynamic bandwidth script. Test it!

Sat Oct 16, 2010 2:01 pm

New Version with Traffic estimate runs on v3.30.

Image
My configuration is to limit traffic at 25GB in 30 days (853MB / day) (ignore the Queunenames with 20GB).
When you look at the Queue with the Name 20GB-neu19__11052--25941 you can see the value 11052. This says the the user have 11052 MB too much traffic. Every day when the user uses more than 853 MB this value will rise and will fall when the user uses less traffic. If the value of to much traffic is too high then the speed will slow down. It can slow down so much that the user can't generate more traffic than the chosen limit.

The new part in this version is something that estimate the traffic amount off an user. For the Queue 20GB-neu19__11052--25941 the second value says that the user had about 25941 MB traffic in the last 30 days. This value is above 25GB (25400MB) because the user can burst traffic but in average the user can't have more the the 25GB traffic in 30 days.

So maybe this script is for someone useful...
#Your Settings Here ------------------------
#Max traffic volume in MB per TimePeriod (TotalBytes, up and download are counted)
:local MaxTrafficVolume 25600
#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
:local TrafficBurst 5120
#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%
:local SlowDownVolume 10000
#Speed in kbit 
:local MaxSpeedDown 800
:local MaxSpeedUp 128
#if set too high MaxTrafficVolume-Limit can easily be exceed
:local MinSpeedDown 130
:local MinSpeedUp 96
# Runtime 15 means that all 15 minutes new bandwidth values are calculated (you have to configure the scheduler with this time!)
:local RunTime 1440
#only queues which name start with this String are modified by this script
:local KeyString "20GB-"
# after this string the traffic is stored in queuename (this string can only one time part of the queue name!) 
:local TrafficKeyString "__"
:local MonthTrafficKeyString "--"
#part in percent of the new estimated traffic 
#(values between  1 and 5 are useful at 12h runtime (at 2% after 30 days runtime of the script the estimated traffic value seems to be good)))
:local NewTrafficPart 4

#END Your Settings ----------------------------

#only for simulation, when working this value have to be 0 !!!
:local TestTraffic 0

:local MonthTraffic
:local NewPoints
:local OldPoints
:local Traffic
:local DecreasePerStep
:local NewSpeed
:local NewSpeedDown
:local NewSpeedUp
:local NewName
:local SpeedMultiplier

:set DecreasePerStep ($MaxTrafficVolume * $RunTime / $TimePeriod / 60);
: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 MB
		:set OldPoints ([:pick [/queue simple get $i name] ([:find [/queue simple get $i name] $TrafficKeyString] + 2) ([:find [/queue simple get $i name] $MonthTrafficKeyString])]);
		:set MonthTraffic ([:pick [/queue simple get $i name] ([:find [/queue simple get $i name] $MonthTrafficKeyString] + 2) [:len [/queue simple get $i name]]]);;
#read new traffic from
		:set Traffic ([/queue simple get $i bytes])
                                :set Traffic ((([:pick $Traffic ([:find $Traffic "/"]  +1) ([:len $Traffic])]) + ([:pick $Traffic 0 ([:find $Traffic "/"] )]))/1048576);
		:set Traffic ($Traffic + $TestTraffic);
		:put $MonthTraffic
#calculate/estimate the traffic of the last 30 days
		:set MonthTraffic ((($MonthTraffic * (100 - $NewTrafficPart)) + ($Traffic * (1440 / $RunTime) * 30 * $NewTrafficPart)) / 100);
		/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 too much traffic then decrease speed
		:if ($NewPoints > $TrafficBurst) do={
			:if ($SlowDownVolume < 1) do={:set SlowDownVolume 1;}
#little bit math, linear slowdown if traffic over traffic burst			
			:set SpeedMultiplier (10000 - ((($NewPoints - ($TrafficBurst))*10000) / ($SlowDownVolume)))
			: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)) . $MonthTrafficKeyString . (tostr($MonthTraffic)));
		/queue simple set [/queue simple get $i name]  name=$NewName
		}
	}
}
You do not have the required permissions to view the files attached to this post.
 
IlCarletto
newbie
Posts: 30
Joined: Mon May 10, 2010 3:09 pm
Location: Milan
Contact:

Re: A working dynamic bandwidth script. Test it!

Fri Oct 22, 2010 11:01 am

hi
i'm doing something like this but in fact i need to monitor a UMTS sim with 20GB / month of traffic available ( 20GB up&down)
i activate a simple queue on the specific interface, but everytime i reboot the RB , the counter 'total-bytes' restart from 0.

now i write a script that send me an email with the running value, but i'd like to improve sometinh that accumulate the traffica, can you help me?


my script (working on 4.11 rb333)

ContatoreEth1 is the simple queue on ether1 (is a test-board)

/tool e-mail send to="myemail@mydomain.it" subject=([/system identity get name] . " - " . [/system clock get date]. " - " .[/system clock get time]. " - " .[/queue simple get ContatoreEth1 total-bytes] . \
" Bytes di Traffico") body=">>Dettagli Traffico RB 333 Test<<

Sistema: $[/system resource get platform]
Nome Sistema: $[/system identity get name]
Routerboard: $[/system resource get board-name]
Versione OS: $[/system resource get version]
Uptime: $[/system resource get uptime]
Data: $[/system clock get date]
Ora: $[/system clock get time]

Bytes Totali su Eth1: $[/queue simple get ContatoreEth1 total-bytes]

Bytes Download/Upload su Eth1: $[/queue simple get ContatoreEth1 bytes]" from=myemail@mydomain.it

bye
RB433 / R52n + R52n-M - 6.10rc
 
tomtom80
newbie
Topic Author
Posts: 43
Joined: Sun Jan 06, 2008 12:48 am

Re: A working dynamic bandwidth script. Test it!

Fri Oct 22, 2010 11:29 am

hi
i'm doing something like this but in fact i need to monitor a UMTS sim with 20GB / month of traffic available ( 20GB up&down)
i activate a simple queue on the specific interface, but everytime i reboot the RB , the counter 'total-bytes' restart from 0.
bye
That the counter restart from 0 after reboot that was also my problem. The only sollution I found was to store the traffic in the Queue-Name (see the numbers in queue-name).

All you need to rename the queue and store the traffic in it you can find in my script.

I don't know how often your Router reboot. So you have to choose how often you want to save the traffic info to the queue name. I run the script usually one time a day.
 
IlCarletto
newbie
Posts: 30
Joined: Mon May 10, 2010 3:09 pm
Location: Milan
Contact:

Re: A working dynamic bandwidth script. Test it!

Fri Oct 22, 2010 11:42 am

i use the RB some days a week for 4-6 hours

i can run the script every one hour. i'll try
RB433 / R52n + R52n-M - 6.10rc
 
Cameron Earnshaw
Frequent Visitor
Frequent Visitor
Posts: 91
Joined: Sun May 30, 2004 6:46 pm
Contact:

Re: A working dynamic bandwidth script. Test it!

Tue Dec 14, 2010 1:22 am

I like this idea! I'm just curious how this has been working for you now that you have had a couple months to test it?

Cam
 
tomtom80
newbie
Topic Author
Posts: 43
Joined: Sun Jan 06, 2008 12:48 am

Re: A working dynamic bandwidth script. Test it!

Mon Jan 17, 2011 7:58 pm

I like this idea! I'm just curious how this has been working for you now that you have had a couple months to test it?
Cam
Yes. Now it runs for 2 years. I had no problems and it do its job very well!!

Maybe some customers don't like it because they don't have full speed ;-)
 
Cameron Earnshaw
Frequent Visitor
Frequent Visitor
Posts: 91
Joined: Sun May 30, 2004 6:46 pm
Contact:

Re: A working dynamic bandwidth script. Test it!

Wed Nov 30, 2011 8:10 am

Do you know if this script will work with O/S V4.x or 5.x?

Cam
 
takoateli
Member Candidate
Member Candidate
Posts: 104
Joined: Sat Oct 24, 2009 9:10 pm

Re: A working dynamic bandwidth script. Test it!

Mon Apr 30, 2012 11:26 pm

Does anyone have this working under v5?

Thanks!
Greg
 
mesquito
just joined
Posts: 16
Joined: Sat Apr 28, 2012 2:54 am

Re: A working dynamic bandwidth script. Test it!

Tue Jun 05, 2012 9:19 am

I just set this up on 5.15 and it seems to be working.
[Edited - seems to be working fine].

Who is online

Users browsing this forum: No registered users and 14 guests