|
#!/bin/sh |
|
|
|
# Configuration |
|
FU_NODE_NAME=node # Node name? |
|
FU_USING_NIC=ens3 # Interface name? |
|
FU_RESET_DAY=29 # Quota resets on? |
|
FU_PURCHASED=95 # Quota purchased? |
|
FU_DIRECTION=tx # Accounting directions [both/tx/rx] ? |
|
# ############# |
|
|
|
|
|
FU_PURCHASED_MB=$(expr $FU_PURCHASED \* 1024) |
|
|
|
echo "reset_day=$FU_RESET_DAY, flow_monthly_max=${FU_PURCHASED_MB}m, direction=$FU_DIRECTION" |
|
|
|
rx_bytes=$(expr `ifconfig $FU_USING_NIC | grep RX\ packets | awk {'print $5'} ` / 1024 / 1024) |
|
tx_bytes=$(expr `ifconfig $FU_USING_NIC | grep TX\ packets | awk {'print $5'} ` / 1024 / 1024) |
|
|
|
echo "nic=$FU_USING_NIC, rx=${rx_bytes}m, tx=${tx_bytes}m" |
|
|
|
total_mbytes=0 |
|
if [ $FU_DIRECTION = "both" ] |
|
then |
|
total_mbytes=$(expr $rx_bytes + $tx_bytes) |
|
elif [ $FU_DIRECTION = "rx" ] |
|
then |
|
total_mbytes=$rx_bytes |
|
elif [ $FU_DIRECTION = "tx" ] |
|
then |
|
total_mbytes=$tx_bytes |
|
fi |
|
|
|
monthly_mbytes=`cat quotalimit_$FU_USING_NIC.save` |
|
limit_mbytes=$(expr $monthly_mbytes + $FU_PURCHASED_MB ) |
|
|
|
echo "current_mbytes=${total_mbytes}m, allowed_mbytes=${limit_mbytes}m" |
|
|
|
used_mbytes=$(expr $total_mbytes - $monthly_mbytes ) |
|
used_percentage=`echo "scale=3; $used_mbytes * 100 / $FU_PURCHASED_MB" | bc` |
|
echo "used_mbytes=${used_mbytes}m, percentage=${used_percentage}" |
|
|
|
# Qutoa currently used out |
|
if [ $total_mbytes -gt $limit_mbytes ] |
|
then |
|
# Check if already blocked |
|
test_ipt=`iptables -L OUTPUT | grep block_forwarding_ports` |
|
if [ ${#test_ipt} -gt 4 ] |
|
then |
|
echo "overlimit=yes, blocked=yes" |
|
else |
|
echo "overlimit=yes, blocked=no" |
|
# Do message push |
|
curl -s "https://notify.me/?chat=......&message=......"; |
|
# Block network |
|
iptables -t filter -I OUTPUT -j block_forwarding_ports |
|
fi |
|
else |
|
echo "overlimit=no" |
|
fi |