Difference between revisions of "Ros3 Traffic Limit"

From CTWUG Wiki
Jump to: navigation, search
Line 1: Line 1:
I hope this article will grow as people read it. For now this is how I limit my pptp and yes I'm refusing to use radius.
+
==Simple Interface Traffic Limiter==
 
+
This is the script checks how much data has been used on the interface, if it has reached the limit it will disable the interface. Below is the first script. If you want to run this script every 5min you can create a scheduler, simply write paste the name of your script in the newly created schedule and set the interval to 00:05:00 please also read the section for the next script. As far as policies are concerned the scripts will need read and write and the schedules will need read, write and test.
==Scar's Interface traffic limiter (Fail cake edition)==
 
This is the script checks how much data has been used on the interface, if it is over the limit it will disable the interface.
 
 
<pre>
 
<pre>
# Interface limit for pptp
+
# Simple Interface limit - written for pptp
 
# Written by Willem "ScarFreewill" Dreyer
 
# Written by Willem "ScarFreewill" Dreyer
# Date 28/09/2009
+
# Date 30/09/2009
 +
# Script 1 of 2
  
# interface you want to limit
+
# Interface you want to limit
:local interface "yourinterfacegoeshere";
+
:local interface "adsl";
# limit in mb
+
# Limit in MB for each day
 
:local limit 30;
 
:local limit 30;
 +
# file name to store data - the used traffic
 +
:local file "simplelimit.txt";
  
 +
# Checks if interface is already disabled
 
:local bool [/interface get $interface disabled];
 
:local bool [/interface get $interface disabled];
 
:if ($bool = false) do={
 
:if ($bool = false) do={
 
   :local usage [/interface get $interface bytes];
 
   :local usage [/interface get $interface bytes];
 
   :local ulen [:len $usage];
 
   :local ulen [:len $usage];
 +
#  :local day [:pick [/system clock get date] 4 6 ];
 
   :local rx;
 
   :local rx;
 
   :local tx;
 
   :local tx;
  :local total;
 
 
   :local totalmb;
 
   :local totalmb;
  
#   Mufasa's example next line:
+
# Gets the current usage on the interface
#  :put [:pick [ /interface get $interface bytes] 0  [ :find [ /interface get interface bytes ] "/" ] ];
+
#  :put [:pick [ /interface get $interface bytes] 0  [ :find [ /interface get $interface bytes ] "/" ] ];
 
   :for j from=( $ulen ) to=0 do={
 
   :for j from=( $ulen ) to=0 do={
 
     :if ( [:pick $usage $j] = "/") do={
 
     :if ( [:pick $usage $j] = "/") do={
Line 29: Line 31:
 
       :set tx [:pick $usage ($j+1) $ulen];
 
       :set tx [:pick $usage ($j+1) $ulen];
 
     }
 
     }
 +
  }
 +
  :set totalmb ((($rx+tx)/1024)/1024);
 +
 +
# Checks if file exists
 +
  :if ( [/file find name=$file] = "" ) do={
 +
    :log error ("Could not find " . $file . " creating it...");
 +
    /file print file=$file;
 +
    :delay 1;
 +
    /file set $file contents=($totalmb);
 
   }
 
   }
  
  :set total ($rx+tx);
+
# Gets total of previous day
   :set totalmb (($total/1024)/1024);
+
   :local ydaysUsage [/file get $file contents];
   :log info ($interface . ": rx:" . (($rx/1024)/1024) . "mb + tx:" . (($tx/1024)/1024) . "mb = total:" . $totalmb . "mb");
+
   :local todaysUsage ($totalmb-$ydaysUsage);
 +
:put ($ydaysUsage." ".$todaysUsage);
  
   :if ($totalmb>$limit) do={
+
# Checks if the interface reached the limit
    :log info ($interface . ": is over the limit, disabling...");
+
   :if ($todaysUsage>$limit) do={
 +
#    :log info ($interface . ": rx:" . (($rx/1024)/1024) . "mb + tx:" . (($tx/1024)/1024) . "mb = total:" . $totalmb . "mb");
 +
    :log warning ($interface . ": has reached the limit");
 
     /interface set $interface disabled=yes;
 
     /interface set $interface disabled=yes;
 +
    /file set $file contents=($totalmb);   
 +
  } else {
 +
#    :log info ($interface . ": is still under the limit");
 
   }
 
   }
  
Line 43: Line 60:
 
#  :log info ($interface . ": disabled");
 
#  :log info ($interface . ": disabled");
 
}
 
}
 +
</pre>
 +
This second script is to do the carry overs at the end of the day. At first it was about "over the limit" now it is about "carry overs" this is not good! The purpose is to either enable the disabled interface (in the case of it reaching the limit) or add the day's traffic to the file that is saved on the router e.g. "simplelimit.txt". The interval of the scheduler for the second script is important I used 1d 00:00:00 the starting time is not a major thing I used 23:56:00 ... PS: <b>Make sure both scripts have the same hard coded values!!</b>
 +
<pre>
 +
# Simple Interface limit - written for pptp
 +
# Written by Willem "ScarFreewill" Dreyer
 +
# Date 30/09/2009
 +
# Script 2 of 2
 +
 +
# Interface you want to limit
 +
:local interface "adsl";
 +
 +
# file name to store data - the used traffic
 +
:local file "simplelimit.txt";
  
 +
# TODO Tomorrow
 
</pre>
 
</pre>
 +
Thanks for the help Tfyre and Mufasa!

Revision as of 04:26, 30 September 2009

Simple Interface Traffic Limiter

This is the script checks how much data has been used on the interface, if it has reached the limit it will disable the interface. Below is the first script. If you want to run this script every 5min you can create a scheduler, simply write paste the name of your script in the newly created schedule and set the interval to 00:05:00 please also read the section for the next script. As far as policies are concerned the scripts will need read and write and the schedules will need read, write and test.

# Simple Interface limit - written for pptp
# Written by Willem "ScarFreewill" Dreyer
# Date 30/09/2009
# Script 1 of 2

# Interface you want to limit
:local interface "adsl";
# Limit in MB for each day
:local limit 30;
# file name to store data - the used traffic
:local file "simplelimit.txt";

# Checks if interface is already disabled
:local bool [/interface get $interface disabled];
:if ($bool = false) do={
  :local usage [/interface get $interface bytes];
  :local ulen [:len $usage];
#  :local day [:pick [/system clock get date] 4 6 ]; 
  :local rx;
  :local tx;
  :local totalmb;

# Gets the current usage on the interface
#  :put [:pick [ /interface get $interface bytes] 0  [ :find [ /interface get $interface bytes ] "/" ] ];
  :for j from=( $ulen ) to=0 do={
    :if ( [:pick $usage $j] = "/") do={
      :set rx [:pick $usage 0 $j];
      :set tx [:pick $usage ($j+1) $ulen];
    }
  }
  :set totalmb ((($rx+tx)/1024)/1024);

# Checks if file exists
  :if ( [/file find name=$file] = "" ) do={
    :log error ("Could not find " . $file . " creating it...");
    /file print file=$file;
    :delay 1;
    /file set $file contents=($totalmb);
  }

# Gets total of previous day
  :local ydaysUsage [/file get $file contents];
  :local todaysUsage ($totalmb-$ydaysUsage);
#  :put ($ydaysUsage." ".$todaysUsage);

# Checks if the interface reached the limit
  :if ($todaysUsage>$limit) do={
#    :log info ($interface . ": rx:" . (($rx/1024)/1024) . "mb + tx:" . (($tx/1024)/1024) . "mb = total:" . $totalmb . "mb");
    :log warning ($interface . ": has reached the limit");
    /interface set $interface disabled=yes;
    /file set $file contents=($totalmb);    
  } else {
#    :log info ($interface . ": is still under the limit");
  }

} else={
#  :log info ($interface . ": disabled");
}

This second script is to do the carry overs at the end of the day. At first it was about "over the limit" now it is about "carry overs" this is not good! The purpose is to either enable the disabled interface (in the case of it reaching the limit) or add the day's traffic to the file that is saved on the router e.g. "simplelimit.txt". The interval of the scheduler for the second script is important I used 1d 00:00:00 the starting time is not a major thing I used 23:56:00 ... PS: Make sure both scripts have the same hard coded values!!

# Simple Interface limit - written for pptp
# Written by Willem "ScarFreewill" Dreyer
# Date 30/09/2009
# Script 2 of 2

# Interface you want to limit
:local interface "adsl";

# file name to store data - the used traffic
:local file "simplelimit.txt";

# TODO Tomorrow

Thanks for the help Tfyre and Mufasa!