Community discussions

 
Askey307
newbie
Topic Author
Posts: 49
Joined: Fri Sep 29, 2017 10:20 am
Location: South Africa
Contact:

FUP Script with Auto Carry over.

Thu Apr 25, 2019 11:04 am

Good Day

I need some assistance with a script of mine.
Basically it is a Fair Usage Policy Script with usage cap as well on 24 hours.
Currently everything is working, however, the one thing that baffles us is how to carry over left over cap/data to the next day.

Let's say you have 1gb of data for 24 hours and only use 300mb, the left over 700mb add to the next days 1gb (1.7gb).

PM me for the script:)
Or mail me dewetcm@gmail.com

Once script is working, I'll post it here:)

All and any help would be appreciated.

Kind Regards
 
User avatar
sebastia
Forum Guru
Forum Guru
Posts: 1776
Joined: Tue Oct 12, 2010 3:23 am
Location: Antwerp, BE

Re: FUP Script with Auto Carry over.

Thu Apr 25, 2019 11:53 am

Hey

If you're planning on posting the script anyway, why don't you post already what you have, so people will chip in?
 
Askey307
newbie
Topic Author
Posts: 49
Joined: Fri Sep 29, 2017 10:20 am
Location: South Africa
Contact:

Re: FUP Script with Auto Carry over.

Thu Apr 25, 2019 12:10 pm

Yeah, why not. See attached the file:):)
4M 30 Gig Cap Daily FUP for Scheduler (1).txt
#### Script Name: SiteID-ReadBytes ####
#This first script is scheduled to run every 15 minutes. It collects the traffic of VLAN1700 (sums up tx and rx bytes)
#and writes different data to multiple files.
#
###########################################
##### Parameters to change per client #####
###########################################
:local custname "SiteID"
:local interfmon "ether1-vlan1700"
:local interfblock "ether1-vlan1700"
:local limit 1024
:local recipients { "abcd@abcd.com"; "abcd@abcd.com" }

# Comment the unit that is not relevant
:local limitunit "MB"
#:local limitunit "GB"

###########################################
#
:local fileref ""
:local filename ""
:local filewarn ""
:set $fileref ($custname."-refcount.txt")
:set $filename ($custname."-counter.txt")
:set $filewarn ($custname."-warning.txt")
# Read Interface data
:local newdata
:local tx [/interface get $interfmon tx-byte] 
:local rx [/interface get $interfmon rx-byte] 
:set $newdata ( $rx + $tx )
:local curtime [/system clock get time]
#
# See if files exist
:if ([:len [/file find where name=$fileref]] < 1 ) do={
	/file print file=$fileref where name=$fileref;
	/delay delay-time=2;
	/file set $fileref contents=0;
};
:if ([:len [/file find where name=$filename]] < 1 ) do={
	/file print file=$filename where name=$filename;
	/delay delay-time=2;
	/file set $filename contents=0;
}
#
#Read Reference File
:local ref
:set $ref [/file get $fileref contents]
:if ( $ref = 0 ) do={
	/file set $fileref contents=$newdata
	/delay delay-time=2;
};
#
# Calculate new data usage
:local before
:local correct
:set $before [/file get $filename contents]
:set $ref [/file get $fileref contents]
:if ( $newdata < $ref  ) do={ :set $ref 0 }
:set $correct ( $before + $newdata - $ref )
/file set $filename contents=$correct
/file set $fileref contents=$newdata
#
# Check the traffic usage
:local traffic ([/file get $filename contents])
:if ( $limitunit = "MB" ) do={
   :set $traffic ($traffic / 1024 / 1024)
} else={
   :set $traffic ($traffic / 1024 / 1024 / 1024)
};
:local percent ($traffic*100 / $limit)
:log info "CAP-$custname: $curtime - ReadBytes $custname: $traffic $limitunit (Limit is $limit $limitunit) - $percent %"
:put "CAP-$custname: $curtime - ReadBytes $custname: $traffic $limitunit (Limit is $limit $limitunit) - $percent %"
:if ([:len [/file find where name=$filewarn]] < 1 ) do={
	/file print file=$filewarn where name=$filewarn
	/delay delay-time=2
	/file set $filewarn contents=$percent
};
:if ($percent < 50) do={
	/file set $filewarn contents=$percent
} else={
     :if ($percent < 80) do={
        /file set $filewarn contents=$percent
     } else={
        :if (($percent >=80) && ($traffic < $limit)) do={
			:if ([/file get $filewarn contents] <> 99) do={
				:foreach r in=[:toarray $recipients] do={
					/tool e-mail send to=[:tostr $r] subject="Traffic warning - $custname: $percent% reached at $[/system clock get date], $[/system clock get time]" body="The actual traffic amount is $traffic $limitunit \r\nThis is $percent% of the monthly limit\r\n"
					:log warning "CAP-$custname: Warning message sent for $interfmon at $percent%"
				}
			}
				/file set $filewarn contents=99
		} else={
			:if ($percent >= 100) do={
				:if ([/file get $filewarn contents] < 100) do={
					:foreach r in=[:toarray $recipients] do={
						/tool e-mail send to=[:tostr $r] subject="Traffic warning - $custname: $percent% reached at $[/system clock get date], $[/system clock get time]" body="The actual traffic amount is $traffic $limitunit \r\nThis is $percent% of the monthly limit and communication is now blocked\r\n"
					}
					/queue simple enable [/queue simple find]
					:log warning "CAP-$custname: Interface $queue enabled due to reached data limit - email send"
					:put "Traffic at $percent % (100% or over) -stop interface- email send"
				};
				/file set $filewarn contents=100
				:log warning "CAP-$custname: Interface $queue enabled due to reached data limit"
			};
		};
	};
};












You do not have the required permissions to view the files attached to this post.
 
User avatar
sebastia
Forum Guru
Forum Guru
Posts: 1776
Joined: Tue Oct 12, 2010 3:23 am
Location: Antwerp, BE

Re: FUP Script with Auto Carry over.

Fri Apr 26, 2019 2:56 pm

The idea is simple:
currently the daily quota is fixed in script. Make it a dynamic field, and store similar to other params in a file.
On roll-over, at midnight, run another script which adds the unused bandwidth to allowed quota parameter / file and resets the counter to 0.

Make sure you "first" script is not run at midnight, to prevent file conflicts.

Who is online

Users browsing this forum: MSN [Bot] and 3 guests