#!/bin/bash -e
# 初始阈值为1000M,单位为字节
gosgos=$((5000 * 1024 * 1024)) # 5G
max=$((300 * 1024 * 1024 * 1024)) # 300G
nettag=$(cat /sys/class/net/eth0/statistics/tx_bytes)
end=$((nettag + gosgos)) # 初始阈值为10M
echo "speedlog" > /var/speed.log
# 循环执行
while true; do
current_traffic=$nettag
sleep 1m
new_traffic=$(cat /sys/class/net/eth0/statistics/tx_bytes)
traffic_increase=$((new_traffic - current_traffic))
echo "本小时上行流量: $((traffic_increase / 1048576)) MB; 起始量:$((current_traffic / 1048576)) MB 阈值:$((end / 1048576)) MB" >> /var/speed.log
# 检查是否超过阈值
if [ $new_traffic -ge $end ]; then
/usr/bin/python3 /root/auto/msg.py $((traffic_increase / 1048576)) $((new_traffic / 1048576))
echo "超过 $((gosgos / 1048576)) MB,告警告警。" >> /var/speed.log
nettag=$(cat /sys/class/net/eth0/statistics/tx_bytes) # 更新起始值
end=$((nettag + gosgos))
else
echo "未超过 $((gosgos / 1048576)) MB,继续监控。"
fi
# 检查是否超过阈值
if [ $new_traffic -ge $max ]; then
/usr/bin/python3 /root/auto/msg.py 即将关机 poweroff
shutdown -h +5
echo "上行流量超过 $((max / 1073741824)) GB,将执行关机操作。" >> /var/speed.log.bk
else
echo "上行流量未超过 $((max / 1073741824)) GB,继续监控。"
fi
if [ $(date +%M) == "00" ]; then
nettag=$(cat /sys/class/net/eth0/statistics/tx_bytes) # 每1小时起始值
end=$((nettag + gosgos)) # 阈值
echo "新阈值为 $((end / 1048576)) MB。" >> /var/speed.log
fi
# 在每天午夜执行ifconfig命令
if [ $(date +%H:%M) == "18:02" ]; then
/usr/bin/python3 /root/auto/msg.py 每日流量情况 $((new_traffic / 1048576)) MB
fi
done