Skip to content

Instantly share code, notes, and snippets.

@berg
Created May 10, 2012 07:05
Show Gist options
  • Select an option

  • Save berg/2651577 to your computer and use it in GitHub Desktop.

Select an option

Save berg/2651577 to your computer and use it in GitHub Desktop.

Revisions

  1. berg created this gist May 10, 2012.
    64 changes: 64 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,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