演示接口上的 rx/tx 计数器如何重置

演示接口上的 rx/tx 计数器如何重置

当每小时执行 ifconfig 时,我注意到 RX/TX 字节传输的计数器会重置:

RX 字节:921640934 (921.6 MB) TX 字节:4001470884 (4.0 GB)

怎么会?我想跟踪我每天传输的数据量,但它们不断重置。

答案1

我认为 Shunz 先生的回答是正确的。不过,希望并非全部破灭。如果您有兴趣统计每天转账的金额,您可以考虑统计数据

答案2

看起来计数器是 32 位整数,因此它们“环绕”约 4GB。

答案3

问题肯定是上面提供的 32 位整数答案。我有同样的问题。它与可以运行它的处理器上安装的操作系统有关。

如果您在 64 位处理器上运行 32 位操作系统,您也将受到 4Gb 限制。
2^32 = 4294967296
4294967296/1024/1024/1024 = 4

我真的很沮丧,我的(32 位)文件服务器无法跟踪它正在传输的许多 TB,因此我构建了一个小脚本来跟踪它的数据使用情况(eth0 和 wlan0):

getstats.sh位于/usr/local/bin/system/

#! /bin/bash

# CHECK FOR FILE IN /USR/LOCAL/BIN/SYSTEM
#       COPY ACTUAL FILES TO MAINTAIN DATA CONSISTENCY DURING CALCULATIONS
        cp /sys/class/net/eth0/statistics/rx_bytes /usr/local/bin/system/rx_actual
        cp /sys/class/net/eth0/statistics/tx_bytes /usr/local/bin/system/tx_actual
        cp /sys/class/net/wlan0/statistics/rx_bytes /usr/local/bin/system/wlan_rx_actual
        cp /sys/class/net/wlan0/statistics/tx_bytes /usr/local/bin/system/wlan_tx_actual

#       BYTES FILES
if [ ! -e /usr/local/bin/system/rx_bytes ]; then                            # IF RX_BYTES DOESN'T EXIST
        cp /usr/local/bin/system/rx_actual /usr/local/bin/system/rx_bytes   # MAKE A NEW COPY FROM THE ACTUAL FILE
else
    mv /usr/local/bin/system/rx_bytes /usr/local/bin/system/rx_bytes_old    # IF IT DOES EXIST, RENAME IT
        cp /usr/local/bin/system/rx_actual /usr/local/bin/system/rx_bytes   # AND COPY IN A NEW ONE
fi
if [ ! -e /usr/local/bin/system/tx_bytes ]; then                            # REPEAT THIS FOR TX_BYTES
        cp /usr/local/bin/system/tx_actual /usr/local/bin/system/tx_bytes
else
    mv /usr/local/bin/system/tx_bytes /usr/local/bin/system/tx_bytes_old
        cp /usr/local/bin/system/tx_actual /usr/local/bin/system/tx_bytes
fi

#       BYTES FILES (WLAN0)
if [ ! -e /usr/local/bin/system/wlan_rx_bytes ]; then                                     # IF RX_BYTES DOESN'T EXIST
        cp /usr/local/bin/system/wlan_rx_actual /usr/local/bin/system/wlan_rx_bytes       # MAKE A NEW COPY FROM THE ACTUAL FILE
else
        mv /usr/local/bin/system/wlan_rx_bytes /usr/local/bin/system/wlan_rx_bytes_old    # IF IT DOES EXIST, RENAME IT
        cp /usr/local/bin/system/wlan_rx_actual /usr/local/bin/system/wlan_rx_bytes       # AND COPY IN A NEW ONE
fi
if [ ! -e /usr/local/bin/system/wlan_tx_bytes ]; then                                     # REPEAT THIS FOR TX_BYTES
        cp /usr/local/bin/system/wlan_tx_actual /usr/local/bin/system/wlan_tx_bytes
else
        mv /usr/local/bin/system/wlan_tx_bytes /usr/local/bin/system/wlan_tx_bytes_old
        cp /usr/local/bin/system/wlan_tx_actual /usr/local/bin/system/wlan_tx_bytes
fi

#       RUNNING TOTAL FILES
if [ ! -e /usr/local/bin/system/rx_running ]; then
    cp /usr/local/bin/system/rx_bytes /usr/local/bin/system/rx_running
fi
if [ ! -e /usr/local/bin/system/tx_running ]; then
        cp /usr/local/bin/system/tx_bytes /usr/local/bin/system/tx_running
fi

#       RUNNING TOTAL FILES (WLAN0)
if [ ! -e /usr/local/bin/system/wlan_rx_running ]; then
        cp /usr/local/bin/system/wlan_rx_bytes /usr/local/bin/system/wlan_rx_running
fi
if [ ! -e /usr/local/bin/system/wlan_tx_running ]; then
        cp /usr/local/bin/system/wlan_tx_bytes /usr/local/bin/system/wlan_tx_running
fi

#       OLD FILES
if [ ! -e /usr/local/bin/system/rx_bytes_old ]; then
        cp /usr/local/bin/system/rx_bytes /usr/local/bin/system/rx_bytes_old
fi
if [ ! -e /usr/local/bin/system/tx_bytes_old ]; then
        cp /usr/local/bin/system/tx_bytes /usr/local/bin/system/tx_bytes_old
fi

#       OLD FILES (WLAN0)
if [ ! -e /usr/local/bin/system/wlan_rx_bytes_old ]; then
        cp /usr/local/bin/system/wlan_rx_bytes /usr/local/bin/system/wlan_rx_bytes_old
fi
if [ ! -e /usr/local/bin/system/wlan_tx_bytes_old ]; then
        cp /usr/local/bin/system/wlan_tx_bytes /usr/local/bin/system/wlan_tx_bytes_old
fi


# SET VARIABLES FOR CALCULATION
OLDRX=`cat /usr/local/bin/system/rx_bytes_old`
NEWRX=`cat /usr/local/bin/system/rx_bytes`
RUNRX=`cat /usr/local/bin/system/rx_running`
OLDTX=`cat /usr/local/bin/system/tx_bytes_old`
NEWTX=`cat /usr/local/bin/system/tx_bytes`
RUNTX=`cat /usr/local/bin/system/tx_running`
OLDWRX=`cat /usr/local/bin/system/wlan_rx_bytes_old`
NEWWRX=`cat /usr/local/bin/system/wlan_rx_bytes`
RUNWRX=`cat /usr/local/bin/system/wlan_rx_running`
OLDWTX=`cat /usr/local/bin/system/wlan_tx_bytes_old`
NEWWTX=`cat /usr/local/bin/system/wlan_tx_bytes`
RUNWTX=`cat /usr/local/bin/system/wlan_tx_running`
MAX=4294967296

# COMPARE AND DO MATH
if [ $NEWRX -lt $OLDRX ]; then                         # IF NEW VALUE IS LESS THAN OLD VALUE (max reached and LOOPED AROUND)
        TOPRX=`expr $MAX - $OLDRX`                             # SUBTRACT THE OLD VALUE FROM THE MAX VALUE
        USERX=`expr $TOPRX + $RUNRX`                           # ADD IT TO RUNNING TOTAL
        $USERX=`expr $USERX + $NEWRX`                          # ADD THAT TO THE NEW VALUE
        echo $USERX > /usr/local/bin/system/rx_running         # OUTPUT THAT TO THE NEW RUNNING TOTAL
else                                                   # OTHERWISE (it hasn't looped around)
        TOPRX=`expr $NEWRX - $OLDRX`                           # SUBTRACT THE OLD VALUE FROM THE NEW
        USERX=`expr $RUNRX + $TOPRX`                           # ADD IT TO THE RUNNING VALUE
        echo $USERX > /usr/local/bin/system/rx_running         # OUTPUT THAT TO THE NEW RUNNING TOTAL
fi
if [ $NEWTX -lt $OLDTX ]; then                         # REPEAT ABOVE FOR UPLOADS
        TOPTX=`expr $MAX - $OLDTX`
        USETX=`expr $TOPTX + $RUNTX`
        $USETX=`expr $USETX + $NEWTX`
        echo $USETX > /usr/local/bin/system/tx_running
else
        TOPTX=`expr $NEWTX - $OLDTX`
        USETX=`expr $RUNTX + $TOPTX`
        echo $USETX > /usr/local/bin/system/tx_running
fi
 #COMPARE AND DO MATH (WLAN0)
if [ $NEWWRX -lt $OLDWRX ]; then                         # IF NEW VALUE IS LESS THAN OLD VALUE (max reached and LOOPED AROUND)
        TOPWRX=`expr $MAX - $OLDWRX`                             # SUBTRACT THE OLD VALUE FROM THE MAX VALUE
        USEWRX=`expr $TOPWRX + $RUNWRX`                           # ADD IT TO RUNNING TOTAL
        $USEWRX=`expr $USEWRX + $NEWWRX`                          # ADD THAT TO THE NEW VALUE
        echo $USEWRX > /usr/local/bin/system/wlan_rx_running         # OUTPUT THAT TO THE NEW RUNNING TOTAL
else                                                     # OTHERWISE (it hasn't looped around)
        TOPWRX=`expr $NEWWRX - $OLDWRX`                           # SUBTRACT THE OLD VALUE FROM THE NEW
        USEWRX=`expr $RUNWRX + $TOPWRX`                           # ADD IT TO THE RUNNING VALUE
        echo $USEWRX > /usr/local/bin/system/wlan_rx_running         # OUTPUT THAT TO THE NEW RUNNING TOTAL
fi
if [ $NEWWTX -lt $OLDWTX ]; then                         # REPEAT ABOVE FOR UPLOADS
        TOPWTX=`expr $MAX - $OLDWTX`
        USEWTX=`expr $TOPWTX + $RUNWTX`
        $USEWTX=`expr $USEWTX + $NEWWTX`
        echo $USEWTX > /usr/local/bin/system/wlan_tx_running
else
        TOPWTX=`expr $NEWWTX - $OLDWTX`
        USEWTX=`expr $RUNWTX + $TOPWTX`
        echo $USEWTX > /usr/local/bin/system/wlan_tx_running
fi

这将在系统目录中创建许多文件,但实际总数将位于以下文件中:

/usr/local/bin/system/rx_running是 eth0 上下载的总字节数
/usr/local/bin/system/tx_running是 eth0 上上传的总字节数
/usr/local/bin/system/wlan_rx_running是 wlan0 上下载的总字节数
/usr/local/bin/system/wlan_tx_running是 wlan0 上上传的总字节数

当然,您会希望自动更新这些值。所以我建议定期运行脚本(我每分钟运行一次,使用cronwith* * * * * chronic /usr/local/bin/system/getStats.sh

之后我继续获取其他统计数据,包括平均负载、CPU 温度、内存使用情况等,并使用RRDTool.

该图表明,32 位操作系统(运行 Jessie 的树莓派)仍然可以为您提供大于 4Gb 的可用传输数据:

在此输入图像描述

PSchronic是软件包的一部分moreutils,它允许您运行一个命令,除非出现错误,否则不会输出任何内容。

相关内容