在太阳下
不管你喜不喜欢,高不高兴,爱与不爱;太阳照旧照耀着你!温暖着你!

bash脚本,流量监控 Plus

bash脚本,流量监控 Plus
TIME 2023-06-19 20:28

创建名为“流量监控”的 bash 脚本

vi 流量监控.sh

粘贴以下内容

#!/bin/bash

# 初始化累计出站和入站数据为0
total_out_bytes=0
total_in_bytes=0
monthly_out_bytes=0
monthly_in_bytes=0

# 获取初始日期
previous_month=$(date "+%m")

while true; do
    # 获取当前时间、出网流量和入网流量
    date=$(date "+%Y-%m-%d")
    month=$(date "+%m")
    day_of_week=$(date "+%u")
    case $day_of_week in
        "1") day_of_week="周一" ;;
        "2") day_of_week="周二" ;;
        "3") day_of_week="周三" ;;
        "4") day_of_week="周四" ;;
        "5") day_of_week="周五" ;;
        "6") day_of_week="周六" ;;
        "7") day_of_week="周日" ;;
    esac
    system_time=$(date "+%H:%M:%S")

    # 获取CPU和内存使用情况
    cpu_usage=$(ps aux | awk 'BEGIN {sum=0} {sum+=$3}; END {printf("%.0f", sum)}')
    memory_usage=$(free | awk '/Mem/{printf("%.0f"), $3/$2*100}')

    out_bytes=$(cat /sys/class/net/eth0/statistics/tx_bytes)
    in_bytes=$(cat /sys/class/net/eth0/statistics/rx_bytes)
    # 等待1秒钟,并获取新的出网流量和入网流量
    sleep 1
    out_bytes_new=$(cat /sys/class/net/eth0/statistics/tx_bytes)
    in_bytes_new=$(cat /sys/class/net/eth0/statistics/rx_bytes)
    # 计算出网速度,单位为M
    out_speed=$(echo "scale=3; ($out_bytes_new - $out_bytes) / 1000000" | bc)
    if (( $(echo "$out_speed < 1" | bc -l) )); then
        out_speed=$(printf '%.3f' $out_speed)
    fi
    # 计算入网速度,单位为M
    in_speed=$(echo "scale=3; ($in_bytes_new - $in_bytes) / 1000000" | bc)
    if (( $(echo "$in_speed < 1" | bc -l) )); then
        in_speed=$(printf '%.3f' $in_speed)
    fi
    # 累加出站和入站数据
    total_out_bytes=$(echo "$total_out_bytes + $out_bytes_new - $out_bytes" | bc)
    total_in_bytes=$(echo "$total_in_bytes + $in_bytes_new - $in_bytes" | bc)
    total_out_MB=$(echo "scale=3; $total_out_bytes / 1000000" | bc)
    total_in_MB=$(echo "scale=3; $total_in_bytes / 1000000" | bc)
    if (( $(echo "$total_out_MB < 1" | bc -l) )); then
        total_out_MB=$(printf '%.3f' $total_out_MB)
    fi
    if (( $(echo "$total_in_MB < 1" | bc -l) )); then
        total_in_MB=$(printf '%.3f' $total_in_MB)
    fi    # 如果月份变更,则重置月度流量
    if [ "$month" != "$previous_month" ]; then
        monthly_out_bytes=0
        monthly_in_bytes=0
        previous_month=$month
    fi

    # 累加月度出站和入站数据
    monthly_out_bytes=$(echo "$monthly_out_bytes + $out_bytes_new - $out_bytes" | bc)
    monthly_in_bytes=$(echo "$monthly_in_bytes + $in_bytes_new - $in_bytes" | bc)
    monthly_out_MB=$(echo "scale=3; $monthly_out_bytes / 1000000" | bc)
    monthly_in_MB=$(echo "scale=3; $monthly_in_bytes / 1000000" | bc)
    if (( $(echo "$monthly_out_MB < 1" | bc -l) )); then
        monthly_out_MB=$(printf '%.3f' $monthly_out_MB)
    fi
    if (( $(echo "$monthly_in_MB < 1" | bc -l) )); then
        monthly_in_MB=$(printf '%.3f' $monthly_in_MB)
    fi

    # 输出结果
    echo "$date $day_of_week $system_time CPU$cpu_usage% 内存$memory_usage% 出站${out_speed}M/s 进站${in_speed}M/s 今日出站${total_out_MB}M 进站${total_in_MB}M 本月出站${monthly_out_MB}M 进站${monthly_in_MB}M"

    # 保存到日志文件
    echo "$date $day_of_week $system_time CPU$cpu_usage% 内存$memory_usage% 出站${out_speed}M/s 进站${in_speed}M/s 今日出站${total_out_MB}M 进站${total_in_MB}M 本月出站${monthly_out_MB}M 进站${monthly_in_MB}M" >> ./net_usage.log
done
上面这个脚本依赖于 bc 二进制计算器
安装命令

apt install -y bc

赋予流量监控脚本“流量监控.sh”可执行权限

chmod +x 流量监控.sh

用 Screen 创建一个名为“流量监控”离线任务

安装命令(Debian 11)

apt install screen -y

使用详见:Screen-用于命令行终端切换的自由软件

screen -S 流量监控

运行流量监控脚本“流量监控.sh”

./流量监控.sh

返回输出以下结果,每秒钟刷新一次

2023-06-19 周一 20:45:05 CPU1% 内存19% 出站0.002M/s 进站0.001M/s 今日出站0.258M 进站0.157M 本月出站0.258M 进站0.157M
2023-06-19 周一 20:45:06 CPU6% 内存19% 出站0.002M/s 进站0.001M/s 今日出站0.261M 进站0.158M 本月出站0.261M 进站0.158M
2023-06-19 周一 20:45:07 CPU102% 内存19% 出站0.001M/s 进站0.001M/s 今日出站0.262M 进站0.159M 本月出站0.262M 进站0.159M
2023-06-19 周一 20:45:08 CPU23% 内存19% 出站0.002M/s 进站0.001M/s 今日出站0.264M 进站0.160M 本月出站0.264M 进站0.160M

 这个脚本有个小bug,那就是CPU项不能数值对其,三位数对齐

#!/bin/bash

# 初始化累计出站和入站数据为0
total_out_bytes=0
total_in_bytes=0
monthly_out_bytes=0
monthly_in_bytes=0

# 获取初始日期
previous_month=$(date "+%m")

while true; do
    # 获取当前时间、出网流量和入网流量
    date=$(date "+%Y-%m-%d")
    month=$(date "+%m")
    day_of_week=$(date "+%u")
    case $day_of_week in
        "1") day_of_week="周一" ;;
        "2") day_of_week="周二" ;;
        "3") day_of_week="周三" ;;
        "4") day_of_week="周四" ;;
        "5") day_of_week="周五" ;;
        "6") day_of_week="周六" ;;
        "7") day_of_week="周日" ;;
    esac
    system_time=$(date "+%H:%M:%S")

    # 获取CPU和内存使用情况
    cpu_usage=$(ps aux | awk 'BEGIN {sum=0} {sum+=$3}; END {printf("%03d", sum)}')
    memory_usage=$(free | awk '/Mem/{printf("%.0f"), $3/$2*100}')

    out_bytes=$(cat /sys/class/net/eth0/statistics/tx_bytes)
    in_bytes=$(cat /sys/class/net/eth0/statistics/rx_bytes)
    # 等待1秒钟,并获取新的出网流量和入网流量
    sleep 1
    out_bytes_new=$(cat /sys/class/net/eth0/statistics/tx_bytes)
    in_bytes_new=$(cat /sys/class/net/eth0/statistics/rx_bytes)

    
    # 计算出网速度,单位为M
    out_speed=$(echo "scale=3; ($out_bytes_new - $out_bytes) / 1000000" | bc)
    if (( $(echo "$out_speed < 1" | bc -l) )); then
        out_speed=$(printf '%.3f' $out_speed)
    fi
    # 计算入网速度,单位为M
    in_speed=$(echo "scale=3; ($in_bytes_new - $in_bytes) / 1000000" | bc)
    if (( $(echo "$in_speed < 1" | bc -l) )); then
        in_speed=$(printf '%.3f' $in_speed)
    fi
    # 累加出站和入站数据
    total_out_bytes=$(echo "$total_out_bytes + $out_bytes_new - $out_bytes" | bc)
    total_in_bytes=$(echo "$total_in_bytes + $in_bytes_new - $in_bytes" | bc)
    total_out_MB=$(echo "scale=3; $total_out_bytes / 1000000" | bc)
    total_in_MB=$(echo "scale=3; $total_in_bytes / 1000000" | bc)
    if (( $(echo "$total_out_MB < 1" | bc -l) )); then
        total_out_MB=$(printf '%.3f' $total_out_MB)
    fi
    if (( $(echo "$total_in_MB < 1" | bc -l) )); then
        total_in_MB=$(printf '%.3f' $total_in_MB)
    fi
    # 如果月份变更,则重置月度流量
    if [ "$month" != "$previous_month" ]; then
        monthly_out_bytes=0
        monthly_in_bytes=0
        previous_month=$month
    fi

    # 累加月度出站和入站数据
    monthly_out_bytes=$(echo "$monthly_out_bytes + $out_bytes_new - $out_bytes" | bc)
    monthly_in_bytes=$(echo "$monthly_in_bytes + $in_bytes_new - $in_bytes" | bc)
    monthly_out_MB=$(echo "scale=3; $monthly_out_bytes / 1000000" | bc)
    monthly_in_MB=$(echo "scale=3; $monthly_in_bytes / 1000000" | bc)
    if (( $(echo "$monthly_out_MB < 1" | bc -l) )); then
        monthly_out_MB=$(printf '%.3f' $monthly_out_MB)
    fi
    if (( $(echo "$monthly_in_MB < 1" | bc -l) )); then
        monthly_in_MB=$(printf '%.3f' $monthly_in_MB)
    fi

    # 输出结果
    echo "$date $day_of_week $system_time CPU${cpu_usage}% 内存$memory_usage% 出站${out_speed}M/s 进站${in_speed}M/s 今日出站${total_out_MB}M 进站${total_in_MB}M 本月出站${monthly_out_MB}M 进站${monthly_in_MB}M"

    # 保存到日志文件
    echo "$date $day_of_week $system_time CPU${cpu_usage}% 内存$memory_usage% 出站${out_speed}M/s 进站${in_speed}M/s 今日出站${total_out_MB}M 进站${total_in_MB}M 本月出站${monthly_out_MB}M 进站${monthly_in_MB}M" >> ./net_usage.log
done

运行效果

2023-06-19 周一 21:56:55 CPU000% 内存44% 出站0.001M/s 进站0.001M/s 今日出站0.119M 进站0.074M 本月出站0.119M 进站0.074M
2023-06-19 周一 21:56:56 CPU000% 内存44% 出站0.002M/s 进站0.001M/s 今日出站0.121M 进站0.076M 本月出站0.121M 进站0.076M
2023-06-19 周一 21:56:57 CPU000% 内存44% 出站0.002M/s 进站0.001M/s 今日出站0.123M 进站0.077M 本月出站0.123M 进站0.077M

后面再把磁盘IO给添加上去

点击数:16

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
Verified by MonsterInsights