refractor out common.sh
This commit is contained in:
parent
6a0dfcbc72
commit
cf7c274589
3 changed files with 23 additions and 26 deletions
|
@ -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." \
|
||||
|
|
16
src/traffic_common.sh
Normal file
16
src/traffic_common.sh
Normal file
|
@ -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)
|
|
@ -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
|
||||
|
|
Reference in a new issue