Created
May 10, 2012 07:05
-
-
Save berg/2651577 to your computer and use it in GitHub Desktop.
Arris TM702G counter polling script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment