Initial commit
This commit is contained in:
commit
a7afed13fb
3 changed files with 101 additions and 0 deletions
21
README
Normal file
21
README
Normal file
|
@ -0,0 +1,21 @@
|
|||
Track traffic across interface and device restarts and limit
|
||||
consumption to a donation-backed amount.
|
||||
|
||||
To increase SSD lifetime persistent storage is only touched every
|
||||
$MaxPerTrafficDiff bytes (currently 100 MB). So tracking can be
|
||||
trivially avoided by frequent router reboot.
|
||||
|
||||
* Usage
|
||||
- call traffic_track via cron as often as possible
|
||||
- call traffic_account_donation to increase the traffic limit
|
||||
after you put the coin in the treasure chest
|
||||
|
||||
* Internals
|
||||
** Counters
|
||||
- Max :: traffic limit
|
||||
- Per :: traffic accounted to disk
|
||||
- Eph :: traffic acounted to /tmp
|
||||
- Cur :: traffic directly obtained from `ip -s`
|
||||
- Obs :: part of Cur already accounted
|
||||
** Caveats
|
||||
- scripts are non-reentrant
|
28
src/traffic_account_donation
Executable file
28
src/traffic_account_donation
Executable file
|
@ -0,0 +1,28 @@
|
|||
#!/bin/sh
|
||||
|
||||
# config
|
||||
TMaxFile=/etc/traffic_limit
|
||||
BytesPerCent=5368709 # 1 GiB for 2 EUR
|
||||
|
||||
# annoy user
|
||||
echo "How many CENT have you donated to the collecting box?"
|
||||
read Donation
|
||||
|
||||
# check value sanity
|
||||
if [ "$Donation" -lt 0 ] 2>/dev/null || [ $? -eq 2 ]; then
|
||||
echo "$Donation does not look #like a sane number"
|
||||
exit -1
|
||||
fi
|
||||
|
||||
# calc and store new value
|
||||
BytesDonated=$(( $Donation * $BytesPerCent ))
|
||||
TMax=$(cat $TMaxFile || echo 0)
|
||||
TMax=$((TMax + $BytesDonated))
|
||||
echo $TMax > $TMaxFile
|
||||
|
||||
# TODO: log donation
|
||||
# TODO: log remote, once connection is up
|
||||
|
||||
# cheer user up
|
||||
echo "You have donated $((BytesDonated / 1024 / 1024)) MB." \
|
||||
"The new limit is $((TMax / 1024 / 1024)) MB."
|
52
src/traffic_track
Executable file
52
src/traffic_track
Executable file
|
@ -0,0 +1,52 @@
|
|||
#!/bin/sh
|
||||
|
||||
# config
|
||||
IF=eth0.1
|
||||
TMaxFile=/etc/traffic_limit
|
||||
TPerFile=/etc/traffic_consumed
|
||||
TEphFile=/tmp/traffic_to_be_accounted
|
||||
TObsFile=/tmp/traffic_observed_since_ifup
|
||||
|
||||
# TODO: MaxPerTimeGap=3600
|
||||
MaxPerTrafficDiff=$((100 * 1024 * 1024))
|
||||
|
||||
# gather different traffic counters
|
||||
TMax=$(cat $TMaxFile 2>/dev/null || echo 0)
|
||||
TPer=$(cat $TPerFile 2>/dev/null || echo 0)
|
||||
TEph=$(cat $TEphFile 2>/dev/null || echo 0)
|
||||
TObs=$(cat $TObsFile 2>/dev/null || echo 0)
|
||||
TCur=$((
|
||||
$(ip -s link show eth0.1 \
|
||||
| awk '{ if (FNR == 4) print $1 " + "; if (FNR == 6) print $1 }' \
|
||||
| tr -d "\n")
|
||||
))
|
||||
# test for invalid cur values (negative number or invalid number format (ret=2))
|
||||
if [ "$TCur" -lt 0 ] 2>/dev/null || [ $? -eq 2 ]; then
|
||||
# TODO: log error
|
||||
exit -1
|
||||
fi
|
||||
|
||||
|
||||
# update traffic counters
|
||||
UpdatePer=false
|
||||
if [ $TCur -lt $TObs ]; then
|
||||
# interface counter has been reset
|
||||
TEph=$(($TEph + $TCur))
|
||||
# TODO: log warning
|
||||
else
|
||||
TEph=$(($TEph + $TCur - $TObs))
|
||||
fi
|
||||
TObs=$TCur
|
||||
TPer=$(($TPer + $TEph))
|
||||
# TODO: also lock _once_ if TPer exceeds TMax
|
||||
if [ $TEph -gt $MaxPerTrafficDiff ]; then
|
||||
TEph=0
|
||||
echo $TPer > $TPerFile
|
||||
fi
|
||||
echo $TEph > $TEphFile
|
||||
echo $TObs > $TObsFile
|
||||
|
||||
# act if traffic max is reached
|
||||
if [ $TPer -gt $TMax ]; then
|
||||
echo "TODO: shut down internet interface"
|
||||
fi
|
Reference in a new issue