2

When doing ifconfig from hour to hour I notice that the counters for RX/TX bytes transfers resets:

RX bytes:921640934 (921.6 MB) TX bytes:4001470884 (4.0 GB)

How come? I would like to keep track how much data i transfer from day to day but they keep resetting.

3 Answers 3

4

I believe that MrShunz's answer is correct. However, not all hope is lost. If you are interested in keeping statistics on how much you transfer each day, you might consider vnstat.

0
3

Seems like counters are 32bit integers so they "wrap around" at ~4GB.

1

The issue is definitely the 32-bit integer answer provided above. I had the same problem. It's related to the operating system installed on the processor that can run it.

If you're running a 32-bit O/S on a 64-bit processor, you'll also be limited to the 4Gb cut-off.
2^32 = 4294967296
4294967296/1024/1024/1024 = 4

I was really frustrated that my (32-bit) file server couldn't keep track of the many Terabytes that it was transferring, so I built a little script to keep track of the data usage for it (both eth0 and wlan0):

getstats.sh is located in /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

This will create a number of files in the system directory, however the Actual totals will be located in the following files:

/usr/local/bin/system/rx_running is the total bytes downloaded on eth0
/usr/local/bin/system/tx_running is the total bytes uploaded on eth0
/usr/local/bin/system/wlan_rx_running is the total bytes downloaded on wlan0
/usr/local/bin/system/wlan_tx_running is the total bytes uploaded on wlan0

Naturally, you'll want to update these values automatically. So I'd recommend running the script periodically (I do mine once a minute using cron with * * * * * chronic /usr/local/bin/system/getStats.sh

I went on after this to get other stats including load averages, CPU temperatures, Memory usage etc and output them to graphs using RRDTool.

This graph demonstrates the fact that a 32-bit OS (raspberry Pi2 running Jessie) can still give you usable transferred data that's greater the 4Gb:

enter image description here

P.S. chronic is a part of the moreutils package that allows you to run a command that will not output anything unless there's an error.

1
  • If you want the full script that I use, you can get a copy here
    – Jim
    Sep 1, 2016 at 10:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .