Browse Source

增加 流量限制脚本

zjcqoo 6 years ago
parent
commit
c6d0e97de0
1 changed files with 24 additions and 0 deletions
  1. 24 0
      traffic-quota.sh

+ 24 - 0
traffic-quota.sh

@@ -0,0 +1,24 @@
+# 流量配额限制
+DEV=eth0
+
+# 上行流量 2TB
+TX_MAX=$((2 * 1024 ** 4))
+
+TX_USED=$(ifconfig $DEV | grep "TX packets" | awk '{ print $5 }')
+# TX_USED=$(iptables -nvxL OUTPUT | grep "Chain OUTPUT" | awk '{ print $7 }')
+
+TX_FREE=$(($TX_MAX - $TX_USED))
+TX_RATE=$((100 * $TX_USED / $TX_MAX))
+
+printf "tx used: %'d ($TX_RATE%%)\n" $TX_USED
+printf "tx free: %'d\n" $TX_FREE
+
+
+if [[ ! $(iptables -L | grep "tx quota") ]]; then
+  iptables \
+    -m comment --comment "tx quota" \
+    -A OUTPUT \
+    -m set ! --match-set ngx-ban-dstip dst \
+    -m quota ! --quota $TX_FREE \
+    -j DROP
+fi