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
|
#!/bin/sh
|
||||||
|
|
||||||
# config
|
source $(dirname $0)/traffic_common.sh
|
||||||
TMaxFile=/etc/traffic_limit
|
|
||||||
BytesPerCent=5368709 # 1 GiB for 2 EUR
|
|
||||||
|
|
||||||
# annoy user
|
# annoy user
|
||||||
echo "How many CENT have you donated to the collecting box?"
|
echo "How many CENT have you donated to the collecting box?"
|
||||||
|
@ -16,12 +14,10 @@ fi
|
||||||
|
|
||||||
# calc and store new value
|
# calc and store new value
|
||||||
BytesDonated=$(( $Donation * $BytesPerCent ))
|
BytesDonated=$(( $Donation * $BytesPerCent ))
|
||||||
TMax=$(cat $TMaxFile || echo 0)
|
TMax=$(($TMax + $BytesDonated))
|
||||||
TMax=$((TMax + $BytesDonated))
|
|
||||||
echo $TMax > $TMaxFile
|
echo $TMax > $TMaxFile
|
||||||
|
|
||||||
# TODO: log donation
|
# TODO: log donation to some remote place
|
||||||
# TODO: log remote, once connection is up
|
|
||||||
|
|
||||||
# cheer user up
|
# cheer user up
|
||||||
echo "You have donated $((BytesDonated / 1024 / 1024)) MB." \
|
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
|
#!/bin/sh
|
||||||
|
|
||||||
# config
|
source $(dirname $0)/traffic_common.sh
|
||||||
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
|
# update traffic counters
|
||||||
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=$((
|
TCur=$((
|
||||||
$(ip -s link show $IF \
|
$(ip -s link show $IF \
|
||||||
| awk '{ if (FNR == 4) print $1 " + "; if (FNR == 6) print $1 }' \
|
| 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
|
# TODO: log error
|
||||||
exit -1
|
exit -1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
# update traffic counters
|
|
||||||
UpdatePer=false
|
UpdatePer=false
|
||||||
if [ $TCur -lt $TObs ]; then
|
if [ $TCur -lt $TObs ]; then
|
||||||
# interface counter has been reset
|
# interface counter has been reset
|
||||||
|
@ -38,8 +23,8 @@ else
|
||||||
fi
|
fi
|
||||||
TObs=$TCur
|
TObs=$TCur
|
||||||
TPer=$(($TPer + $TEph))
|
TPer=$(($TPer + $TEph))
|
||||||
# TODO: also lock _once_ if TPer exceeds TMax
|
|
||||||
if [ $TEph -gt $MaxPerTrafficDiff ]; then
|
if [ $TEph -gt $MaxPerTrafficDiff ]; then
|
||||||
|
# TODO: log _once_ if TPer exceeds TMax
|
||||||
TEph=0
|
TEph=0
|
||||||
echo $TPer > $TPerFile
|
echo $TPer > $TPerFile
|
||||||
fi
|
fi
|
||||||
|
|
Reference in a new issue