You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.2 KiB

import click
import requests
import json
from xml.etree import ElementTree
def search_services(base_url, username, password, service):
ifservices = requests.get(base_url + '/api/v2/ifservices',
params={'limit': 0,
'_s': '' + service},
headers={'Accept': 'application/xml'},
auth=(username, password))
ifservices = ElementTree.fromstring(ifservices.content)
for service in ifservices.iter('service'):
ipiface_id = service.find('ipInterfaceId').text
node = requests.get(base_url + '/api/v2/nodes',
params={'limit': 0,
'_s': '' + ipiface_id},
headers={'Accept': 'application/xml'},
auth=(username, password))
node = ElementTree.fromstring(node.content).find('node')
node_id = node.get('id')
node_label = node.get('label')
ipiface = requests.get(base_url + '/api/v2/nodes/' + node_id + '/ipinterfaces',
params={'limit': 0,
'_s': 'id==' + ipiface_id},
headers={'Accept': 'application/xml'},
auth=(username, password))
ipiface = ElementTree.fromstring(ipiface.content).find('ipInterface')
ipiface_address = ipiface.find('ipAddress').text
yield node_label, node_id, ipiface_address
@click.option('--base-url', default='http://localhost:8980/opennms')
@click.option('--username', default='admin')
@click.option('--password', default='admin')
@click.option('--service', default='HTTP')
@click.option('--bs-id', default=0)
def main(base_url, username, password, service, bs_id):
# Strip trailing slash from base URL
if base_url.endswith('/'):
base_url = base_url[:-1]
rkeys = ((label, '' % (node, addr, service))
for (label, node, addr)
in search_services(base_url, username, password, service))
bs = {'name': service,
'reduce-function': {'type': 'HighestSeverity'},
'reduction-key-edges': [{
'weight': 1,
'map-function': {'type': 'Identity'},
'reduction-key': rkey,
} for (label, rkey) in rkeys]}
bs = json.dumps(bs)
r = requests.put(base_url + '/api/v2/business-services/' + str(bs_id),
headers={'Accept': 'application/json',
'Content-Type': 'application/json'},
auth=(username, password),
r = + '/api/v2/business-services/daemon/reload',
headers={'Accept': 'application/json',
'Content-Type': 'application/json'},
auth=(username, password))
if __name__ == '__main__':