Skip to content

Instantly share code, notes, and snippets.

@5310
Created May 18, 2011 17:14
Show Gist options
  • Save 5310/979040 to your computer and use it in GitHub Desktop.
Save 5310/979040 to your computer and use it in GitHub Desktop.
Cellog - The quick opportunistic hack that scrapes DSL usage stats from my router logs. #script
import urllib2, base64
import time
#THIS PARTICULAR SCRAPER WORKS ONLY ON MY BSNL TYPE-I TERACOM ROUTER
#variables here
delay = 5
gateway = "192.168.1.1"
url = "http://%s/webconfig/ports/ports_advance.html?117" % gateway
username = "admin"
password = "admin"
celltobit = 2048000 / 4830 #TX cell rate / TX cell count, according to the log itself
totalcells = 0 #this resets to zero every router reboot, save it somewhere
def celltobytes(cell, bitprefix=1):
return cell * celltobit / 8 / 2 ** (10 * bitprefix)
#opening the log file here
request = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
#main loop here
flag = 3
while flag:
try:
file = urllib2.urlopen(request).readlines()
#scrape stuff here
# 587 <td class="data_table_data_adv">7536371</td>
# 601 <td class="data_table_data_adv">7536371</td>
print "^", file[586][40:-7], "cells"
print "v", file[600][40:-7], "cells"
totalcells = float(file[586][40:-7]) + float(file[600][40:-7])
#replace above with a function that also saves the scrape
print "total cell count:\t%.0f MB" % celltobytes(totalcells, 2)
except:
print "Couldn't get the log. Maybe the gateway isn't up yet?\nTrying again in 5 seconds..."
time.sleep(delay)
flag -= 1
@5310
Copy link
Author

5310 commented May 18, 2011

Currently the usage resets at every router reboot. Obviously, some sort of saving is needed.

Also, zero confidence regarding cell to byte conversion: I just couldn't/didn't test it out with all the other 'net noise about the place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment