To help users use our API, we have put together the following Python examples.


see also: API Programming Guide


Example 1: Add_Nodes.py

import requests
import time

url = 'https://<...>'
auth = ('<user@domain>', '<password>')

newNodes = [
  {'address': '192.168.1.1', 'label': 'Node 1'},
  {'address': '192.168.1.2', 'label': 'Node 2'},
  {'address': '192.168.1.3', 'label': 'Node 3'}
]

def run_task(type, data):
  task = {
    'type': type
  }
  if data:
    task['data'] = data

  r = requests.post(url + '/api/tasks/', auth=auth, json=task)
  if (r.status_code != 201) :
    print('Could not start task: ' + r.text)
    exit(1)

  taskLocation = r.headers['Location']
  # TODO: Implement timeout.
  while True :
    time.sleep(1)
    r = requests.get(url + taskLocation, auth=auth)
    taskOutput = r.json()
    if (taskOutput['finished']) :
      return taskOutput


data = {
  'discoveryType': 'fullSNMP',
  'protoNodes': newNodes
}

print('Starting discovery for ' + str(len(newNodes)) + ' addresses.')
taskOutput = run_task('discovery', data)
print('Discovery completed: ' + taskOutput['stateLabel'])
if 'addedNodes' in taskOutput:
  print('New Node IDs: ' + str(taskOutput['addedNodes']))

### Alternative version that adds addresses sequentially ###
# for newNode in newNodes:
#   data = {
#     'discoveryType': 'fullSNMP',
#     'addresses': [newNode['address']],
#     'label': newNode['label']    
#   }
#
#   print('\nStarting discovery for ' + newNode['address'] + ' / ' + newNode['label'])
#   taskOutput = run_task('discovery', data)
#   print('Discovery completed: ' + taskOutput['stateLabel'])
#   if 'addedNodes' in taskOutput:
#     print('New Node ID: ' + taskOutput['addedNodes'][0])


Example 2: Get_node_status.py

import requests

url = 'https://<...>'
auth = ('<user@domain>', '<password>')

r = requests.get(url + '/api/nodes/', auth=auth,
  params={'outputFormat': 'extended', 'sort': 'status', 'sortDir': 'desc'}
)

if (r.status_code == 200):
  nodes = r.json()
  for node in nodes:
    print(node['label'] + '\t\t' + node['status']['status'], end='')
    if 'maintenance' in node['status']:
      print(' - In Maintenance', end='')
    if 'acknowledged' in node['status']:
      print(' - Acknowledged', end='')
    print()

else:
  print('Failed: ' + str(r.status_code))


Example 3: reparent_children.py

This call will cause the children of a node to be moved to a new parent by using a traceroute.

import requests
import time
import argparse

url = 'https://<...>'
auth = ('<user@domain>', '<password>')

def run_task(type, data):
  task = {
    'type': type
  }
  if data:
    task['data'] = data

  r = requests.post(url + '/api/tasks/', auth=auth, json=task)
  if (r.status_code != 201) :
    print('Could not start task: ' + r.text)
    exit(1)

  taskLocation = r.headers['Location']
  # TODO: Implement timeout.
  while True :
    time.sleep(1)
    r = requests.get(url + taskLocation, auth=auth)
    taskOutput = r.json()
    if (taskOutput['finished']) :
      return taskOutput


parser = argparse.ArgumentParser(description='', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('parent_id', help='ID of parent Node')
args = parser.parse_args()
config = vars(args)
parentID = config['parent_id']

r = requests.get(url + '/api/nodes/' + parentID + '/children/', auth=auth)

if (r.status_code != 200) :
  print('Could not read children: ' + r.text)
  exit(1)

children = r.json()
if (len(children) == 0) :
  print('Node has no children')
  exit(2)

print('Starting TraceAndMoveTask for ' + str(len(children)) + ' children.')

data = {
  'nodes': children
}

taskOutput = run_task('traceAndMove', data)

print('TraceAndMoveTask completed: ' + taskOutput['stateLabel'])