geffers
Posts: 382
Joined: Sun Jun 24, 2012 6:25 am
Location: UK
Contact: Website

Network Totals

Tue Jan 09, 2018 4:24 pm

Folks,

I use a Pi Zero for a headless ADS-B project, it is headless and just chugs away in the background.

I am curious as to how much data it sends on a daily basis and wonder which network tool I could use to monitor. I know of some that will give current transfer rates etc but I'd like to see how much is being sent.

I do use ntop on a different machine but not sure if or how it can monitor a different device.

Thanks in advance,

Geffers

W. H. Heydt
Posts: 10760
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Network Totals

Wed Jan 10, 2018 2:36 am

The command "ifconfig" shows accumulated traffic for each network interface.

pcmanbob
Posts: 6646
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Network Totals

Wed Jan 10, 2018 4:48 pm

Here a python2 program I used to collect the amount data sent from one of my pi's, it originally updated every 60 seconds and only output the info in the terminal , but I have changed it to update every hour and output the info to a file ( /home/pi/TX_bytes.txt)

now there is no error trapping and it assumes you only have one network interface active.

posting here in case its of use to you, this is the stretch version if you need it for Jessie let me know.

Code: Select all

#!/usr/bin/python
# for Raspbian Stretch
import subprocess
import time
resultline = ""
value = ""
oldbytes = 0

def getifc():
    global resultline
    p = subprocess.Popen("ifconfig | grep 'TX packets'", stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    status = p.wait()
    resultline = output
    
def getbytes():
    global resultline , value
    data = resultline.split(" ")
    c = 0
    while True:
        # test for bytes value
        if data[c] == "bytes":
            value = float(data[c+1])
            if value > 0:
                break
            
        c = c + 1

getifc()
getbytes()
oldbytes = value        
        
while True:
    getifc()
    getbytes()
    newbytes = value
    sentbytes = newbytes - oldbytes
    dsentbytes = str(int(sentbytes))
    sentkib = sentbytes/1024
    dsentkib = str("%.2f" % sentkib)

    dataoutput = "bytes sent in last hour = " + dsentbytes + " (" + dsentkib + " KiB)"
    file = open("/home/pi/TX_bytes.txt","a")
    file.write(dataoutput + "\n")
    file.close()    
    oldbytes = newbytes
    time.sleep (3600)
    
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

geffers
Posts: 382
Joined: Sun Jun 24, 2012 6:25 am
Location: UK
Contact: Website

Re: Network Totals

Thu Jan 11, 2018 2:01 pm

W. H. Heydt wrote:
Wed Jan 10, 2018 2:36 am
The command "ifconfig" shows accumulated traffic for each network interface.
Thank you, that worked a treat, didn't realise TX bytes were an accumulation.

Geffers

geffers
Posts: 382
Joined: Sun Jun 24, 2012 6:25 am
Location: UK
Contact: Website

Re: Network Totals

Thu Jan 11, 2018 2:04 pm

pcmanbob wrote:
Wed Jan 10, 2018 4:48 pm
Here a python2 program I used to collect the amount data sent from one of my pi's, it originally updated every 60 seconds and only output the info in the terminal , but I have changed it to update every hour and output the info to a file ( /home/pi/TX_bytes.txt)

now there is no error trapping and it assumes you only have one network interface active.

posting here in case its of use to you, this is the stretch version if you need it for Jessie let me know.
That will be very useful.

I currently an now using ifconfig | grep "TX bytes" and just write it to a log file every hour but I am trying to learn terminal and python scripts so will try your's out.

Thank you.

Geffers

User avatar
allfox
Posts: 452
Joined: Sat Jun 22, 2013 1:36 pm
Location: Guang Dong, China

Re: Network Totals

Thu Jan 11, 2018 2:42 pm

I was told ifconfig is the old working stuff. There is a reinvented wheel:

ip -s link

It's NEW

pcmanbob
Posts: 6646
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Network Totals

Thu Jan 11, 2018 2:59 pm

allfox wrote:
Thu Jan 11, 2018 2:42 pm
I was told ifconfig is the old working stuff. There is a reinvented wheel:

ip -s link

It's NEW
Tried the new wheel

Code: Select all

pi@StretchTest:~ $ ip -s link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT               group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    RX: bytes  packets  errors  dropped overrun mcast
    0          0        0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    0          0        0       0       0       0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mo              de DEFAULT group default qlen 1000
    link/ether b8:27:eb:ea:9b:2f brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    2974       22       0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    9099       55       0       0       0       0
think I prefer the old one contains more information and easier to search and pick out data using a python program.

Code: Select all

pi@StretchTest:~ $ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.16  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::bb67:3a18:6c31:133b  prefixlen 64  scopeid 0x20<link>
        ether b8:27:eb:ea:9b:2f  txqueuelen 1000  (Ethernet)
        RX packets 55  bytes 5677 (5.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 77  bytes 12535 (12.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

geffers
Posts: 382
Joined: Sun Jun 24, 2012 6:25 am
Location: UK
Contact: Website

Re: Network Totals

Thu Jan 11, 2018 3:40 pm

pcmanbob wrote:
Thu Jan 11, 2018 2:59 pm
allfox wrote:
Thu Jan 11, 2018 2:42 pm
I was told ifconfig is the old working stuff. There is a reinvented wheel:

ip -s link

It's NEW
Tried the new wheel

think I prefer the old one contains more information and easier to search and pick out data using a python program.
Yep, think I prefer the older wheel.

Geffers

Return to “General discussion”