From ae7e0ba61e2846711a46a3fde15ece4b2932bcbb Mon Sep 17 00:00:00 2001 From: Benjamin Collet Date: Mon, 13 Nov 2017 21:17:35 +0100 Subject: [PATCH] Adding support for multiple instances --- client.py | 63 ++++++++++++++++++++++++++++++------------------- config.yml.dist | 15 ++++++++---- 2 files changed, 50 insertions(+), 28 deletions(-) diff --git a/client.py b/client.py index c295adb..5608f84 100755 --- a/client.py +++ b/client.py @@ -7,9 +7,13 @@ import argparse with open("config.yml", "r") as ymlfile: cfg = yaml.load(ymlfile) -def call_api(path): - url = '/'.join((cfg['base_url'],cfg['api_path'],path)) - response = requests.get(url, auth=(cfg['username'], cfg['password'])) +def call_api(params, path): + url = '/'.join((params['base_url'],params['api_path'],path)) + try: + response = requests.get(url, auth=(params['username'], params['password'])) + except: + return + return response.json() def print_data(key, value, suffix = None): @@ -18,30 +22,41 @@ def print_data(key, value, suffix = None): else: print "%-20s %s" % (key, value) +# Test instances +for instance in cfg['instances'].keys(): + test = call_api(cfg['instances'][instance], 'devices') + if test is None: + print "Instance %s is not working, disabling" % instance + cfg['instances'].pop(instance) + else: + print "Instance %s is working" % instance + def search_ports(args): - data = call_api('ports/?ifAlias=%s' % args.string) + for instance, params in cfg['instances'].iteritems(): + data = call_api(params, 'ports/?ifAlias=%s' % args.string) - if data['count'] < 1: - print "No port found matching description %s" % args.string - return + if data['count'] < 1: + print "No port found for description %s on instance %s" \ + % (args.string, instance) + return - for port in data['ports'].itervalues(): - print "=" * 80 - device = call_api('devices/%s' % port['device_id']) - print_data("Device", device['device']['hostname']) - print_data("", "%s/device/device=%s" % (cfg['base_url'], port['device_id'])) - print_data("Port", port['port_label_short']) - print_data("", "%s/device/device=%s/tab=port/port=%s/" % - (cfg['base_url'], port['device_id'], port['port_id'])) - print_data("Description", port['ifAlias']) - print_data("Port status", "%s (Admin) / %s (Oper)" % (port['ifAdminStatus'], port['ifOperStatus'])) - print_data("Last flap", port['ifLastChange']) - print_data("Speed", port['ifHighSpeed'], "Mbps") - print_data("Duplex", port['ifDuplex']) - print_data("Input rate", port['ifInOctets_rate'], "octets") - print_data("Output rate", port['ifOutOctets_rate'], "octets") - print_data("Input errors rate", port['ifInErrors_rate']) - print_data("Output errors rate", port['ifOutErrors_rate']) + for port in data['ports'].itervalues(): + print "=" * 80 + device = call_api(params, 'devices/%s' % port['device_id']) + print_data("Device", device['device']['hostname']) + print_data("", "%s/device/device=%s" % (params['base_url'], port['device_id'])) + print_data("Port", port['port_label_short']) + print_data("", "%s/device/device=%s/tab=port/port=%s/" % + (params['base_url'], port['device_id'], port['port_id'])) + print_data("Description", port['ifAlias']) + print_data("Port status", "%s (Admin) / %s (Oper)" % (port['ifAdminStatus'], port['ifOperStatus'])) + print_data("Last flap", port['ifLastChange']) + print_data("Speed", port['ifHighSpeed'], "Mbps") + print_data("Duplex", port['ifDuplex']) + print_data("Input rate", port['ifInOctets_rate'], "octets") + print_data("Output rate", port['ifOutOctets_rate'], "octets") + print_data("Input errors rate", port['ifInErrors_rate']) + print_data("Output errors rate", port['ifOutErrors_rate']) # Argument parsing parser = argparse.ArgumentParser() diff --git a/config.yml.dist b/config.yml.dist index bc7f369..8a83ea8 100644 --- a/config.yml.dist +++ b/config.yml.dist @@ -1,4 +1,11 @@ -base_url: 'https://noc1.luxnetwork.eu' -api_path: 'api/v0' -username: '' -password: '' +instances: + luxnetwork: + base_url: 'https://noc1.luxnetwork.eu' + api_path: 'api/v0' + username: '' + password: '' + tlpo: + base_url: 'http://observium2.tl-noc.com' + api_path: 'api/v0' + username: '' + password: ''