Re: Daily quota based on remaining monthly quota
Posted: Sat Feb 11, 2017 12:55 pm
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
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