Daily quota based on remaining monthly quota

Report issues relating to bandwith monitoring, bandwidth quotas or QoS in this forum.

Moderator: Moderators

emmexx
Posts: 11
Joined: Thu Sep 29, 2016 6:01 am

Re: Daily quota based on remaining monthly quota

Post by emmexx »

I made a small modification to my script because the renewal hour of the monthly (28 days) quota is uncertain. The last day before renewal I limit the daily quota to a fixed value of QUOTA / RENEWAL DAYS.

The update script follows.

bye

maxx

Code: Select all

#!/bin/sh

# This program is copyright © 2016 Massimo Conter
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.   
#
# https://www.gnu.org/licenses/gpl-2.0.html

#customise the following 3 variables
RENEWAL_DATE='2016-09-18'       #last date ISP reset the quota
RENEWAL_DAYS=28                #renewal every xxx days
QUOTA=$(( 5 * 1073741824 ))    #bytes

# if the script is run without parameters it outputs QUOTA and RENEWAL_DAYS, remaining quota for the period,
# remaining days before reset, current quota
# if the script is run with any parameter (e.g. quota_period.sh -c) it resets and updates gargoyle quota
#
# how to use
# In gargoyle GUI / Firewall / Quotas add a new quota. Set the parameters to:
# Applies to: Entire Local Network
# Max Total Up+Down: 100 MB
# Quota Resets: Every Day
# Reset Hour: 00:00
# Quota is Active: Always
# When Exceeded: choose if you want to shutdown Internet access or throttle bandwith (to a very low and unusable value)
# Click on Add New Quota and on Save Changes
#
# Run the script every day, e.g.:
# ssh to the router and run crontab -e
# add a line such as
# 5 0 0 0 0 /root/quota_period.sh -c >/dev/null 2>&1
# the script runs every day at 00:05
# add execute permission to the file

# nothing to change beyond this line!

MPHR=60    # Minutes per hour.
HPD=24     # Hours per day.

diff () {
        printf '%s' $(( $(date -u +%s) -
                        $(date -u -d "$RENEWAL_DATE" +%s)))
#        printf '%s' $(( $(date -u -d "2016-11-12" +%s) -                           
#                        $(date -u -d "$RENEWAL_DATE" +%s)))                           
}

get_bandwith () {
  LINE="$(/usr/bin/bw_get -i $1 -h 2> /dev/null | /bin/grep -v '^$' | /usr/bin/head -n -1 | /usr/bin/tail -$2)"

  used=0
  while read -r bw other
    do
      if [ "$bw" -eq "$bw" ] 2>/dev/null
      then
        used=$(( used+bw ))
      fi
    done <<EOF
    $LINE
EOF
   
  echo $used  #do not remove this line, it is the return value of the function
}

past_days=$(( $(diff) /$MPHR /$MPHR /$HPD % $RENEWAL_DAYS )) # days since last quota reset
remaining_days=$(( RENEWAL_DAYS-past_days)) # days to next quota reset

download=$(get_bandwith "total5-download-day-365" "$past_days")
upload=$(get_bandwith "total5-upload-day-365" "$past_days")

bw_used=$((download+upload))


if [ $bw_used -ge $QUOTA ]                                           
then                               
  quotaToday=0                                                   
else
  if [ $remaining_days -eq 1 ]
  then
    quotaToday=$((QUOTA/RENEWAL_DAYS))   #the hour of the renewal is uncertain. So the last day we limit the quota
  else
    quotaToday=$(((QUOTA-bw_used)/remaining_days)) 
  fi
fi

if [ "$#" -eq "0" ]
then
  echo
  echo "Plan: $QUOTA Bytes / $RENEWAL_DAYS days"
  echo "Remaining: $((QUOTA-bw_used)) Bytes / $remaining_days day(s)"
  echo "Quota for today: $(uci get firewall.quota_1.combined_limit)"
  exit
fi 

uci set firewall.quota_1.combined_limit="$quotaToday"
uci commit

sh /usr/lib/gargoyle/restart_firewall.sh
if [ -d "/usr/data/quotas/" ] ; then rm -rf /usr/data/quotas/* ; fi ;
/usr/bin/backup_quotas

exit

ispyisail
Moderator
Posts: 4809
Joined: Mon Apr 06, 2009 3:15 am
Location: New Zealand

Re: Daily quota based on remaining monthly quota

Post by ispyisail »

Thanks

Much appreciated

chadycom
Posts: 11
Joined: Fri Jun 09, 2017 9:29 am

Re: Daily quota based on remaining monthly quota

Post by chadycom »

Hi,
Do you mind if i contact you on PM Please?
I would surely use a help from you based on what you have posted. I am not an expert in scripting and i am facing some challenges with having the quota work properly. I already posted a new message but still under approval

thank you

kokowa
Posts: 1
Joined: Sun Oct 02, 2016 9:27 am

Re: Daily quota based on remaining monthly quota

Post by kokowa »

I faced a similar problem as the OP, but with multiple users:
A fixed quota should be distributed for multiple users over time such that they are not able to use there own quota in the first days and cannot use the whole quota such that the internet is throttled for everyone for the remaining month..

In addition the script is capping the free quota each user can accumulate and redistributes the remaining one, such that power user can use more of the overall data plan :)

So inspired from the already posted scripts I created a github repository:
https://github.com/hb128/gargoyle-flexible-quotas

I'm open for any comments, bug reports or feature requests :)

ABza
Posts: 2
Joined: Sun Nov 19, 2017 5:02 am

Re: Daily quota based on remaining monthly quota

Post by ABza »

Hi Maxx,
Thanks for the tips. This is ver close to what I need.
Can you help with the command to convert bw_get output to CSV file? What would also be great as well if it can be exactly in the "bandwidth.csv" format with the columns?
Thanks
ABza

poveiro
Posts: 6
Joined: Thu Nov 30, 2017 11:48 am

Re: Daily quota based on remaining monthly quota

Post by poveiro »

I have tried to set the quota on the gargoyle but some how doesn't work.
I wanted to set 50gb day time, from 6:00 to 24:00
and
from 24:00 to 6:00 60gb
the problem is that it doesn't take the 24 hour clock, so I'm not sure how it can be done.
Now on searching have found this, not sure how a script like this is used and how to use it as monthly, because I see the date to be changed, amount of days (problem the months dont have the same quantity of days) and the quota that I believe as been set to 5 times, (total 640 megabytes)
Can some one please make it clear to a newbie ;-)

poveiro
Posts: 6
Joined: Thu Nov 30, 2017 11:48 am

Re: Daily quota based on remaining monthly quota

Post by poveiro »

poveiro wrote:I have tried to set the quota on the gargoyle but some how doesn't work.
I wanted to set 50gb day time, from 6:00 to 24:00
and
from 24:00 to 6:00 60gb
the problem is that it doesn't take the 24 hour clock, so I'm not sure how it can be done.
Now on searching have found this, not sure how a script like this is used and how to use it as monthly, because I see the date to be changed, amount of days (problem the months dont have the same quantity of days) and the quota that I believe as been set to 5 times, (total 640 megabytes)
Can some one please make it clear to a newbie ;-)

got it working on the gargoyle, it had to be 00:00 instead of 24:00
But would be nice to learn the script.

atrac
Posts: 13
Joined: Sat Nov 02, 2019 9:26 am

Re: Daily quota based on remaining monthly quota

Post by atrac »

Hello, nice work!
In my country a recent law has established that telecom companies have to invoice on the same day for each month, instead of every 28 days as in the past. Therefore, each month the renewal duration is different because if I initially pay on the 13th for example I get the renewal every 13th of the month.
Do you have any suggestion in order to set the renewal date as the Nth day of the month?
Thank you

Hendryk
Posts: 4
Joined: Mon Mar 23, 2020 8:16 am

Re: Daily quota based on remaining monthly quota

Post by Hendryk »

kokowa wrote:
Sat Jun 10, 2017 4:39 pm

So inspired from the already posted scripts I created a github repository:
Thanks a lot for posting this on github!

Post Reply