Ros3 Traffic Limit

From CTWUG Wiki
Jump to: navigation, search

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:10:00 please also read the section for the next script. As far as policies goes 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
# Version 1
# 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 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 day [:pick [/system clock get date] 4 6 ];
  :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 02/10/2009
# Version 1
# Script 2 of 2

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

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

# total usage on the interface
:local usageRaw [/interface get $interface bytes];

:local bool [/interface get $interface disabled];
:if ($bool = true) do={
  /interface set $interface disabled=no;
} else={
  :local usage (([:pick $usageRaw 0 [:find $usageRaw "/"]])+([:pick $usageRaw ([:find $usageRaw "/" ]+1) [:len $usageRaw]]));
  /file set $file contents=((($usage/1024)/1024));
}

Thanks for the help Tfyre and Mufasa! Hope this is of some use to you, even if it's just a string tutorial. There is still a lot that can be done with this script since this is just a simple script, but (for now) it's good enough for me. If you'd like to improve on this be my guest!