A potential customer approached us and asked if they could use Python to request from the opCharts API.

You can get yourself up to speed with the API in this community WIKI guide.

The answer was very quick, yes, but then an example was required. Chris Gatlin, an APAC support engineer, wrote a terrific example of how to achieve this that will be outlined below.


For this example, we’ll build an HTML Network Status web page via a CGI script.  This page will be comprised of tables; the first table will summarize group status while the subsequent tables will provide node level details for each group.

This example will utilize the following modules:

We also need to define the following variables:

  • Protocol (HTTP or https)
  • Server Address
  • Username
  • Password
import urllib.request, http.cookiejar, json
PROTOCOL = 'https'
SERVER = 'demo.opmantek.com'
USERNAME = 'nmis'
with open('https://static.firstwave.com/var/www/opChartsApi.conf', 'r') as confFile:
USERPASS = confFile.read().strip()

Note: the password is hidden inside the conFile, which is why we read from it.


The next step involves configuring the URL handling;

CJ = http.cookiejar.CookieJar()
OPENER = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(CJ))
loginUrl = (PROTOCOL + '://' + SERVER + '/omk/opCharts/login')
loginDict = {'username' : USERNAME , 'password' : USERPASS}
DATA = urllib.parse.urlencode(loginDict).encode("utf-8")
REQUEST = urllib.request.Request(loginUrl, DATA)

Once you have access the next step is to query opCharts for the node status;

nodeData = OPENER.open(PROTOCOL + '://' + SERVER + 'https://static.firstwave.com/omk/opCharts/nodes.json')
nodeDecoded = nodeData.read().decode()
nodeList = json.loads(nodeDecoded)

Once we have the data, we need to repurpose it into a logical data structure;

groupStatus = { }
for n in nodeList:
if n['group'] not in groupStatus:
groupStatus[n['group']] = {'up' : 0, 'down' : 0}
groupStatus[n['group']]['nodes'] = {}
if n['nodedown'] == 'true':
groupStatus[n['group']]['down'] += 1
groupStatus[n['group']]['up'] += 1
groupStatus[n['group']]['nodes'][n['name']] =
{'name' : n['name'],
'location' : n['location'],
'type' : n['nodeType'],
'net' : n['netType'],
'role' : n['roleType'],
'status' : n['nodestatus']

Now to build and display the tables;

print('Content-type: text/html\n\n')


print(‘table, th, td { border: 1px solid black; border-collapse: collapse;}</style>’)

print(‘<head><title>Opmantek Network Status</title></head><body><strong>Opmantek Network Status</strong><br><br>’)

print(‘<table><tr><th>Group</th><th>Nodes Up</th><th>Nodes Down</th></tr>’)

for k in sorted(groupStatus):

print(‘<tr><td>’ + k + ‘</td><td>&nbsp’ + str(groupStatus[k][‘up’]) + ‘</td><td>&nbsp’ + str(groupStatus[k][‘down’]) + ‘</td></tr>’)


for k in sorted(groupStatus):

print(‘<strong>Group: ‘ + k + ‘</strong><br>’)


for g in sorted(groupStatus[k][‘nodes’]):


‘<tr><td>’ + groupStatus[k][‘nodes’][g][‘name’] + ‘</td>

<td>&nbsp’ + groupStatus[k][‘nodes’][g][‘location’] + ‘&nbsp</td>

<td>&nbsp’ + groupStatus[k][‘nodes’][g][‘type’] + ‘&nbsp</td>

<td>&nbsp’ + groupStatus[k][‘nodes’][g][‘net’] + ‘&nbsp</td>

<td>&nbsp’ + groupStatus[k][‘nodes’][g][‘role’] + ‘&nbsp</td>

<td>&nbsp’ + groupStatus[k][‘nodes’][g][‘status’] + ‘&nbsp</td>’)



After accomplishing all of that, you will get a page that will look similar to the following;

Python Dashboard - 700

There are a lot of possibilities when you engage with opCharts’ API. What novel ways have you used it?