Skip to content

Commit d8d0564

Browse files
authored
Add files via upload
1 parent 2377edf commit d8d0564

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

check_traffic.sh

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# 使用此脚本前先执行 apt install -y net-tools bc 安装依赖
2+
# */10 * * * * /bin/bash /root/check_trafic1.sh >> /root/check.log 2>&1 cron任务,每10分钟运行检查一次
3+
#!/bin/bash
4+
5+
# 定义颜色
6+
green="\e[1;32m"
7+
yellow="\e[1;33m"
8+
re="\033[0m"
9+
10+
# 设置流量阈值,单位为 GB
11+
threshold_traffic=1000
12+
13+
14+
# 获取 lxdbr0和eth0分支流量
15+
rx_bytes=$(/sbin/ifconfig enp1s0 | grep "RX packets" | awk '{print $5}')
16+
tx_bytes=$(/sbin/ifconfig enp1s0 | grep "TX packets" | awk '{print $5}')
17+
18+
# 将字节转换为 GB
19+
rx_gb=$(echo "scale=2; $rx_bytes / (1024^3)" | bc)
20+
tx_gb=$(echo "scale=2; $tx_bytes / (1024^3)" | bc)
21+
22+
echo -e "${yellow}RX 流量: $(echo "$rx_gb" | bc)GB${re}"
23+
echo -e "${yellow}TX 流量: $(echo "$tx_gb" | bc)GB${re}"
24+
25+
# 计算总流量
26+
total_gb=$(echo "$rx_gb + $tx_gb" | bc)
27+
28+
echo -e "${yellow}总共使用流量: ${total_gb}GB${re}"
29+
30+
# 检查流量是否超过阈值
31+
if (( $(echo "$total_gb >= $threshold_traffic" | bc -l) )); then
32+
echo -e "${yellow}流量已超过阈值,执行关机操作...${re}"
33+
shutdown -h now
34+
else
35+
echo -e "${green}流量未超过阈值,继续运行...${re}"
36+
fi

0 commit comments

Comments
 (0)