Adding MongoDB to the ISP QoS Monitor

In an earlier post I showed how to create a basic tool for tracking internet connectivity. (My modem at home seems to drop its internet connection as specific times in the day. I want to validate this.)

We’ll use the popular NoSQL document database MongoDB. Even though MongoDB is designed for document structures, it is quite performant for time series data (at least in this particular use case).

Open up your python editor and add in the following line:

import socket # For attempting to connect to a web site.
import datetime # For timestamps.
from time import sleep # Reduce web site request frequency. Play nice.
from pymongo import MongoClient # <--NEWLINE For storing data.

Assuming you have downloaded and installed MongoDB, be sure to start up the process if it isn’t already running: mongod  You can find MongoDB at if you need it.

In our code let’s create a simple method to connect to MongoDB:

def mongo_connect():
     # Connection to Mongo DB
          client = MongoClient('localhost', 27017)
          db = client['isp_qos'] # Select the database.
          collection = db['webcheck']
     except Exception as e:
          print("Could not connect to MongoDB: %s" % e)
     return collection

(The db and collection names are hard-coded. I’ll do another tutorial on how to use a configuration file in order to avoid hard-coding as well as allow for flexibility in deploying code to different environments.)

In the __main__() method add the following

if __name__ == "__main__":
     db = mongo_connect() # <--NEWLINE For connecting to MongoDB.
     url="" # <--Explicitly declared the URL we want to use.
     while True: # Infinite loop.
          ts, status = connect_to_url(url=url) #<-- Added in the url parameter.
          print(ts, status)
          # New block of code to insert the URL connection outcome into MongoDB:
          except Exception as e:
               print("Exception caught:\n", str(e))

Now you can run the script from the command line. Say your script is named, all you need to do from the command line is type: python

Let’s check the data in MongoDB. From the command line, run the mongo shell command. Switch to the isp_qos database with: use isp_qos and run a simple query: db.webcheck.findOne()  You should see a sample record:

MongoDB query for ISP QoS records