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)