As requested by few , specially from PK who are using PTCL DSL links and on few packages, PTCL have embossed Quota limit of 300GB (or likewise) and if the user exceed this limit, some penalty or extra charges are added in the monthly bill which is in some cases creates extra financial burden for DCN. Following are few scripts which can help you in this particular case that if the link crosses specific percentage of Quota limit, the link can be disabled, or second link (if available) can be activated or whatever operator chooses can be done by modifying the script action.
[The script idea was taken from the Mikrotik forum but those scripts didn’t performed well, therefore I modified and re-created few sections in some parts ]
Hope it will help you. Leave your comments …
Regard’s
Syed Jahanzaib
Scenario:
Two DSL Links , Primary DSL (ether1) have 4mb link with 50 GB data limit, where as secondary (or backup) link (ether2) have 2mbps with no data limit. Our requirement is to use Primary DSL Link which have higher bandwidth capacity and if the 50GB quota is reached to 90%, it should give warning (or email or disable Primary Link interface which have distance value of 1 and ENABLE Secondary Link which have distance value of 2 to auto take over)
Primary DSL = ether1
Secondary DSL = ether2
Scripts Description: [Tested with Mikrotik 6.27 Only]
In this example I have created 3 Scripts.
Benefit of using scripting method is that it can save values in a text file, so even if a route reboots, the script can retrieve last recorded data form the text file , that’s the biggest advantage for DCN ;) YKWIM
1- monitor_data
This script will get gather data from the interface stats and add it to text file which will be later read by second script which does the required action based on the counters in this file
2- check_useage
This script will read values from the text file created by above script, (monitor_data) and act accordingly to either ignore if the data usage is below specific value, and to perform what action if the data usage is above specific value.
3- check_date
This script will check in which the data value should be set to ZERO, usually start of month. So that counters can be reset.
1- MONITOR_DATA [To get interface usage data]
Set this script in scheduler to run every hour. (collecting data every hour is reasonable IMHO]
# MONITOR_DATA Script (to get interface usage in file) # First Part of WAN Data Quota Monitoring System # Syed Jahanzaib / aacable@hotmail.com # https://aacable.wordpress.com # Set WAN interface you want to monitor :local INT ether1 # Gather Interface RX bytes :local counter [/interface get $INT rx-byte] # Set Counter DATA value gathered by Interface RX bytes and add TX bytes to get combined value :set $counter ($counter + [/interface get $INT tx-byte]) :local traffic # If previous data file not found, then create new one :if ([:len [/file find where name=wandatauseage.txt]] < 1 ) do={ /file print file=wandatauseage.txt where name=wandatauseage.txt; # Add some delay, for slow or high load routers /delay delay-time=2; # Adding Traffic Value in the file /file set wandatauseage.txt contents="0"; }; :local before value=[/file get wandatauseage.txt contents] :if ($counter > $before) do={ /file set wandatauseage.txt contents=$counter } else= { :set $traffic ($counter+$before) /file set wandatauseage.txt contents=$traffic };
2- check_usage [To read data usage from file, and act accordingly]
Set this script in scheduler to run every hour or two.
# CHECK_USAGE > CHECK DATA USAGE Script (to read from file and act accordingly) # Second Part of WAN Data Quota Monitoring System # Syed Jahanzaib / aacable@hotmail.com # https://aacable.wordpress.com</pre> local traffic ([/file get wandatauseage.txt contents] / 1024 / 1024 / 1024) # Set the WAN Interface Quota limit in GB :local limit 50 :local percent ($traffic*100 / $limit) # Set the percentage as required :if ($percent >= 90) do={ :log warning "WAN INTERFACE Quota Exceeded 90% of $limit GB / zaib" # OR Take specific action, either disable interface, change route, email , punch the fun-bags ; ) YKWIM ; ) etc etc # Example Email like , but first make sure you have configured your email client properly # /tool e-mail send to=YOUR_EMAIL_ADDRESS subject="WAN Traffic Quota warning: $percent% reached at $[/system clock get date], $[/system clock get time]" body="The traffic amount is $traffic GB \r\nThis is $percent% of the monthly limit\r\nQuota Monitor by Z" }
Example if the quota reaches to its limit.
Example of EMAIL if the quota reaches to its limit.
3- check-date [To reset counters in text file, if its 1st of the month]
Set this script in scheduler to run every daily in night at 1:00am, and if it found date to 1st of the month, it will reset the data counter file to zero.
# CHECK_DATE > CHECK DATE and RESET Counters on specific date ... # Third Part of WAN Data Quota Monitoring System # Syed Jahanzaib / aacable@hotmail.com # https://aacable.wordpress.com if ([:pick [/system clock get date] ([:find [/system clock get date] "/" ] + 1) 6 ] = "01") do={/file set wandatauseage.txt contents="0"}
Filed under: Mikrotik Related
