Skip to content

Instantly share code, notes, and snippets.

@imerr
Created July 10, 2014 10:48
Show Gist options
  • Save imerr/ca7da1fcf64c1667aeb2 to your computer and use it in GitHub Desktop.
Save imerr/ca7da1fcf64c1667aeb2 to your computer and use it in GitHub Desktop.
network monitor
#!/usr/bin/python
import time, threading
import requests
TIMER_CYCLE = 30
last = []
def update():
global last_total, device_speed, requestPool
threading.Timer(1, tick).start()
with open("/sys/class/net/eth0/statistics/rx_bytes", "r") as frx, open("/sys/class/net/eth0/statistics/tx_bytes", "r") as ftx:
rx = int(frx.read())
tx = int(ftx.read())
if len(last) == 2:
r = requests.post('http://reporturl', data = {'rx':(rx-last[0])/TIMER_CYCLE, 'tx':(tx-last[1])/TIMER_CYCLE})
last = [rx, tx]
threading.Timer(TIMER_CYCLE, update).start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment