Skip to content

Instantly share code, notes, and snippets.

@twursc
Last active April 23, 2021 03:06
Show Gist options
  • Save twursc/1fb0bf1a34800605fc2900f4f5cbb567 to your computer and use it in GitHub Desktop.
Save twursc/1fb0bf1a34800605fc2900f4f5cbb567 to your computer and use it in GitHub Desktop.
VPS Quota Watchdog
#!/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
#!/bin/sh
FU_USING_NIC=ens3 # Interface name?
FU_DIRECTION=tx # Accounting directions [both/tx/rx] ?
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
echo "total_mbytes=${total_mbytes}"
echo "$total_mbytes" > quotalimit_${FU_USING_NIC}.save

Manual

  • Put the two following .sh to /root or other place
  • Edit configurations at the head of the two .sh
  • chmod +x
  • Trigger quotalimit_reset.sh to save current rx/tx size of the specific nic
  • Add the following tasks to crontab:
  * * * * * /root/quotalimit_ctrl.sh
  m h d * * /root/quotalimit_reset.sh
  • Write message push & network block actions in quotalimit_ctrl.sh, in the sample, I created an iptables rule called block_forwarding_ports, this blocks some ports that running my network traffic.
  • That's all, you can run quotalimit_ctrl.sh to view the real-time quota information.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment