diff --git a/src/traffic_account_donation b/src/traffic_account_donation index c1eff94..97fd8bf 100755 --- a/src/traffic_account_donation +++ b/src/traffic_account_donation @@ -1,8 +1,6 @@ #!/bin/sh -# config -TMaxFile=/etc/traffic_limit -BytesPerCent=5368709 # 1 GiB for 2 EUR +source $(dirname $0)/traffic_common.sh # annoy user echo "How many CENT have you donated to the collecting box?" @@ -15,13 +13,11 @@ if [ "$Donation" -lt 0 ] 2>/dev/null || [ $? -eq 2 ]; then fi # calc and store new value -BytesDonated=$(( $Donation * $BytesPerCent )) -TMax=$(cat $TMaxFile || echo 0) -TMax=$((TMax + $BytesDonated)) +BytesDonated=$(( $Donation * $BytesPerCent )) +TMax=$(($TMax + $BytesDonated)) echo $TMax > $TMaxFile -# TODO: log donation -# TODO: log remote, once connection is up +# TODO: log donation to some remote place # cheer user up echo "You have donated $((BytesDonated / 1024 / 1024)) MB." \ diff --git a/src/traffic_common.sh b/src/traffic_common.sh new file mode 100644 index 0000000..3f52c2f --- /dev/null +++ b/src/traffic_common.sh @@ -0,0 +1,16 @@ +# config +IF=eth0.1 +TMaxFile=/etc/traffic_limit +TPerFile=/etc/traffic_consumed +TEphFile=/tmp/traffic_to_be_accounted +TObsFile=/tmp/traffic_observed_since_ifup +BytesPerCent=5368709 # 1 GiB for 2 EUR + +# 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) diff --git a/src/traffic_track b/src/traffic_track index 37e4e7b..4a851b7 100755 --- a/src/traffic_track +++ b/src/traffic_track @@ -1,20 +1,8 @@ #!/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 +source $(dirname $0)/traffic_common.sh -# 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) +# update traffic counters TCur=$(( $(ip -s link show $IF \ | awk '{ if (FNR == 4) print $1 " + "; if (FNR == 6) print $1 }' \ @@ -25,9 +13,6 @@ 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 @@ -38,8 +23,8 @@ else fi TObs=$TCur TPer=$(($TPer + $TEph)) -# TODO: also lock _once_ if TPer exceeds TMax if [ $TEph -gt $MaxPerTrafficDiff ]; then + # TODO: log _once_ if TPer exceeds TMax TEph=0 echo $TPer > $TPerFile fi