Monitoring router and internet uptime

So recently I was playing my hardcore character in Diablo 3 (hardcore means that death is permanent and irreversible) when my router lost internet connection and my character promptly died. Understandably I was a little irritated by this. Blizzard take a bunch of the blame for making the game online only even for single player (seriously, what the hell?). But I was annoyed at my internet connection as well. Was this an isolated incident or was my connection dropping consistently without my knowledge? I’ve had a few other drops in the past but not enough to really think there was a problem. Anyway the Diablo experience enraged me enough that I decided to get some hard data about the stability of my internet connection.

The task to achieve this seemed immediately straightforward to me. My router has a web management interface that reports the current uptime of the connection to the internet so my thinking was to write a python script to log in to the router, extract the uptime data and from that detect when I had lost internet connection. This approach is sound and is exactly the implementation I ended up with.

Necessary Tools

To make the script work there are two main python libraries that are necessary: urllib2 and BeautifulSoup.

urllib2 allows you to retrieve files, pages etc from the web from within a python script. I used this to log in to my router and then read the contents of the page containing the uptime information.

The boisterously named BeautifulSoup is a html parser that allows direct navigation of the underlying html. I used this to parse the page with the uptime information and extract the uptime string.

urllib2

The usage of urllib2 was almost the simplest use case except that my router requires a username/password combination to log in. This complicates things slightly, but not a great deal. See below for how to open a page that requires authentication:


passwordManager = urllib2.HTTPPasswordMgrWithDefaultRealm()

topLevelUrl = "http://192.168.X.X"
passwordManager.add_password(None, topLevelUrl, "username", "password")
handler = urllib2.HTTPBasicAuthHandler(passwordManager)
opener = urllib2.build_opener(handler)
page = opener.open(topLevelUrl + "/welcome.html") # page with uptime info
opener.open(topLevelUrl + "/logout.html") # logout or nobody else can login

BeautifulSoup

My usage of BeautifulSoup was very basic, all I needed to do was navigate to the desired element and then extract the uptime string:


soup = BeautifulSoup(page)
uptimeString = ""
for elem in soup('td', text="Connected time so far"):
		uptimeString = elem.parent.nextSibling.nextSibling.next

Conclusion

The final script contains more than just urllib2 and BeautifulSoup: regex for parsing the time string and the logic for periodically checking the router and validating uptime, this stuff is just the usual python glue. Check out the script for yourself if you’re interested in seeing the whole process put together.

As for my problems; with the script I found that my router was indeed losing internet connection on a regular basis so I changed the ADSL2 profile and things have improved a great deal, with the cost being a 15% drop in overall throughput. See below for what my router was getting up to before I made the change (spoiler, not the best):


Router lost internet connection!!
Uptime before failure was: 2 days, 14:00:00

Router lost internet connection!!
Uptime before failure was: 4 days, 14:00:00

Router lost internet connection!!
Uptime before failure was: 18:02:11

Router lost internet connection!!
Uptime before failure was: 0:01:18

Router lost internet connection!!
Uptime before failure was: 0:05:33

Router lost internet connection!!
Uptime before failure was: 0:51:42

Router lost internet connection!!
Uptime before failure was: 0:16:08

Router lost internet connection!!
Uptime before failure was: 2:26:52

Router lost internet connection!!
Uptime before failure was: 0:21:57

Router lost internet connection!!
Uptime before failure was: 10:36:35

Router lost internet connection!!
Uptime before failure was: 0:53:00

Router lost internet connection!!
Uptime before failure was: 0:58:23

Router lost internet connection!!
Uptime before failure was: 2 days, 7:00:00

Leave a Comment