public
Created

Arris TM702G counter polling script

  • Download Gist
gistfile1.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#!/usr/bin/env python
 
#
# Script to poll DOCSIS downstream channel counters
# on Arris TM702G cable modems and similar
#
# licensed under creative commons CC0 (public domain)
#
 
import functools
import operator
import time
 
import requests
import BeautifulSoup
 
def fetch():
url = 'http://192.168.100.1/cgi-bin/status_cgi'
body = requests.get(url).content
 
bs = BeautifulSoup.BeautifulSoup(body)
downstream_table = bs.findAll('table')[1].findAll('tr')[1:]
octet_counts = []
for tr in downstream_table:
octet_counts.append(int(tr.findAll('td')[6].text))
 
return octet_counts
 
def unit_convert(time_delta, byte_delta):
# Convert bytes/sec -> kbit/sec
return (byte_delta * 8.0) / (time_delta * 1000.0)
 
counts = fetch()
timestamp = time.time()
 
with file('output.txt', 'a') as f:
while True:
time.sleep(5)
new_counts = fetch()
new_timestamp = time.time()
 
time_delta = new_timestamp - timestamp
byte_delta = map(lambda (a, b): a - b, zip(new_counts, counts))
 
convert = functools.partial(unit_convert, time_delta)
 
individual_delta = map(convert, byte_delta)
total_delta = sum(individual_delta)
 
line = [str(new_timestamp), '%0.2f' % time_delta]
 
for i in xrange(len(individual_delta)):
#line.append('DS %d: %7.1f kBit/sec' % (i + 1, bps_delta[i]))
line.append('%0.1f' % individual_delta[i])
 
line.append('%0.1f' % total_delta)
 
print ','.join(line)
 
f.write(','.join(line))
f.write('\n')
 
counts = new_counts
timestamp = new_timestamp

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.