嘟嘟社区

阿里云alpine系统,检测流量达标则关机脚本


本帖最后由 调查员 于 2024-9-18 19:18 编辑

此脚本是之前坛友发的,
我稍加修改,
使流量检测更准。
(之前的非常不准,我这个脚本,经过我多天检测,跟阿里云后台的流量变动几乎一致)

添加了五分钟后关机的命令。(怕开机后死循环)

建议定时任务,设为10分钟运行一次。
*/10    *       *       *       *       ash /root/aliyun_traffic.sh >/dev/null
(alpine系统,是用ash代替bash)

  1. #!/bin/bash
  2. # 获取当前脚本的绝对路径
  3. SCRIPT_PATH=$(realpath "$0")
  4. # 保存流量数据的目录和文件
  5. TRAFFIC_DIR="/root/network_traffic"
  6. TRAFFIC_FILE="$TRAFFIC_DIR/network_traffic.dat"
  7. LOG_FILE="$TRAFFIC_DIR/network_traffic_monitor.log"
  8. CURRENT_MONTH=$(date +"%Y-%m")
  9. SHUTDOWN_THRESHOLD=$((19 * 1024 * 1024 * 1024))  # 19GB 转换为字节的整数表示
  10. # 要监控的网络接口
  11. INTERFACE="eth0"
  12. # 创建保存流量数据的目录
  13. if [ ! -d "$TRAFFIC_DIR" ]; then
  14.     mkdir -p "$TRAFFIC_DIR"
  15.     if [ $? -eq 0 ]; then
  16.         echo "目录 $TRAFFIC_DIR 创建成功"
  17.     else
  18.         echo "无法创建目录 $TRAFFIC_DIR"
  19.         exit 1
  20.     fi
  21. fi
  22. # 定义日志记录函数
  23. log_message() {
  24.     local message="$1"
  25.     echo "$(date +"%Y-%m-%d %H:%M:%S") – $message" >> $LOG_FILE
  26. }
  27. # 初始化日志文件
  28. if [ ! -f $LOG_FILE ]; then
  29.     touch $LOG_FILE
  30.     log_message "日志文件创建成功"
  31. fi
  32. # 如果流量文件不存在或者月份不同,则创建并初始化
  33. if [ ! -f $TRAFFIC_FILE ]; then
  34.     echo "$CURRENT_MONTH 0 0" > $TRAFFIC_FILE
  35.     log_message "流量文件创建成功"
  36. else
  37.     saved_month=$(awk ‘{print $1}’ $TRAFFIC_FILE)
  38.     if [ "$saved_month" != "$CURRENT_MONTH" ]; then
  39.         echo "$CURRENT_MONTH 0 0" > $TRAFFIC_FILE
  40. # 月初先删除日志,再重建日志文件
  41.         rm $LOG_FILE
  42.         touch $LOG_FILE
  43.         log_message "流量文件月份更新"
  44.     fi
  45. fi
  46. # 读取之前的流量记录
  47. read saved_month  last_total_out last_check_out < $TRAFFIC_FILE
  48. # 获取当前接收和发送的字节数
  49. if ! out_bytes=$(cat /sys/class/net/$INTERFACE/statistics/tx_bytes); then
  50.     log_message "无法读取发送字节数"
  51.     exit 1
  52. fi
  53. # 如果上次检查值大于当前值,说明流量可能已被重置
  54. if [ "$out_bytes" -lt "$last_check_out" ]; then
  55.     log_message "检测到网卡流量重置,恢复累积流量"
  56.     last_check_out=0
  57. fi
  58. # 计算增量流量
  59. delta_out=$((out_bytes – last_check_out))
  60. # 计算启动前后的累计流量
  61. total_out=$((last_total_out + delta_out))
  62. total_bytes=$((total_out))
  63. # 检查是否达到阈值
  64. if [ "$total_bytes" -ge "$SHUTDOWN_THRESHOLD" ]; then
  65.     log_message "总流量已达到19GB,五分钟后关机"
  66.     sleep 5m
  67.     poweroff
  68. fi
  69. # 自适应单位输出
  70. if [ $total_bytes -lt 1024 ]; then
  71.     total="$total_bytes bytes"
  72. elif [ $total_bytes -lt $((1024 * 1024)) ]; then
  73.     total=$(echo "scale=2; $total_bytes / 1024" | bc)
  74.     total="$total KB"
  75. elif [ $total_bytes -lt $((1024 * 1024 * 1024)) ]; then
  76.     total=$(echo "scale=2; $total_bytes / 1024 / 1024" | bc)
  77.     total="$total MB"
  78. else
  79.     total=$(echo "scale=2; $total_bytes / 1024 / 1024 / 1024" | bc)
  80.     total="$total GB"
  81. fi
  82. # 输出结果
  83. log_message "$total"
  84. echo "$(date +%R) This Month: $total"
  85. echo "——————————"
  86. # 将当前流量值保存到文件
  87. echo "$CURRENT_MONTH $total_out $out_bytes" > $TRAFFIC_FILE

复制代码

牛逼
牛鼻
mark一下。不知道openwrt能用不