From a7afed13fbbd8d0223be254dfb7d117e79acc7fe Mon Sep 17 00:00:00 2001 From: Peter Packult Date: Fri, 9 Sep 2011 20:14:33 +0200 Subject: [PATCH] Initial commit --- README | 21 +++++++++++++++ src/traffic_account_donation | 28 +++++++++++++++++++ src/traffic_track | 52 ++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 README create mode 100755 src/traffic_account_donation create mode 100755 src/traffic_track diff --git a/README b/README new file mode 100644 index 0000000..afa282f --- /dev/null +++ b/README @@ -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 diff --git a/src/traffic_account_donation b/src/traffic_account_donation new file mode 100755 index 0000000..c1eff94 --- /dev/null +++ b/src/traffic_account_donation @@ -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." diff --git a/src/traffic_track b/src/traffic_track new file mode 100755 index 0000000..50da9e1 --- /dev/null +++ b/src/traffic_track @@ -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