At the start of the script there are 3 variables that must be changed to fit your ISP package.
Code: Select all
RENEWAL_DATE is the date when your ISP resets, or will reset, the quota
RENEWAL_DAYS is the number of days of the ISP package (quota resets every xxx days)
QUOTA is the available bandwidth for the period
In my case the last renewal date was yesterday 2016-10-16 and the quota is reset every 28 days, the quota is 5 GB. So:
Code: Select all
RENEWAL_DATE='2016-09-18' #last date ISP reset the quota
RENEWAL_DAYS=28 #renewal every xxx days
QUOTA=$(( 5 * 1073741824 )) #bytes
I'm not a script master so everything could be written better but it works as expected.
The script is not integrated in gargoyle, so if you upgrade or change something, you'll have to set again everything.
The script changes every day the quota available for the day, based on the available bandwidth for the entire period minus the bandwidth already used in the period.
That means that the daily quota available will probably grow every day up to the reset day.
But the aim of the script is to keep a minimum quota available every day for the entire period.
Pre-conditions:
- You should add a quota using Gargoyle GUI before using the script. The script only updates the quota value.
- The script sums the combined upload and download bandwidth of the previous xx days. The script works only for the entire network bandwidth.
How to use it
- Use Gargoyle GUI to add a Firewall / Quotas / quota. See the script.
- Copy the script to the router using ssh, change the 3 variables aforementioned, change the execution permission of the script.
- Check if the script works by running it without parameters. It should output the values of the available bandwidth and period, the remaining bandwidth and the days before quota reset, the current quota value.
- Run crontab -e and add a line to run the script everyday, some minute after the quota reset. I use
Code: Select all
5 0 0 0 0 /root/quota_period.sh -c >/dev/null 2>&1
Since the script uses the upload and download values to calculate the remaining quota, it works only if the reset time is midnight. If the reset hour is in another moment of the day, the script should be modified.
Use the script at your own risk.
bye
maxx
p.s.attachments are not allowed. The script is here:
quota_period.sh
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=$(( 28-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
quotaToday=$(((QUOTA-bw_used)/remaining_days))
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