Adding -s flag for short display

This commit is contained in:
Benjamin Collet 2019-07-24 11:07:02 +02:00
parent fddf989b5b
commit 59e8237886
2 changed files with 92 additions and 50 deletions

141
client.py
View file

@ -40,58 +40,79 @@ def print_data(term_cols, key, value, suffix = None):
for instance in list(cfg['instances']): for instance in list(cfg['instances']):
test = call_api(cfg['instances'][instance], 'devices') test = call_api(cfg['instances'][instance], 'devices')
if test is None: if test is None:
print("Instance %s is not working, disabling" % instance) if cfg['debug']: print("Instance %s is not working, disabling" % instance)
cfg['instances'].pop(instance) cfg['instances'].pop(instance)
else: else:
print("Instance %s is working" % instance) if cfg['debug']: print("Instance %s is working" % instance)
def search_ports(args): def search_ports(args):
for instance, params in cfg['instances'].items(): for instance, params in cfg['instances'].items():
data = call_api(params, 'ports/?ifAlias=%s' % args.string) data = call_api(params, 'ports/?ifAlias=%s' % args.string)
data_devices = call_api(params, 'devices/')
devices = data_devices['devices']
if data['count'] < 1: if data['count'] < 1:
print("No port found for description %s on instance %s" % print("No port found for description %s on instance %s" %
(args.string, instance)) (args.string, instance))
return return
for port in data['ports'].values(): if args.short:
device = call_api(params, 'devices/%s' % port['device_id'])
address = call_api(params, 'address/?device_id=%s&interface=%s' % (port['device_id'], port['port_label_short']))
address6 = call_api(params, 'address/?af=ipv6&device_id=%s&interface=%s' % (port['device_id'], port['port_label_short']))
term_cols = getattr(shutil.get_terminal_size((80, 20)), 'columns') term_cols = getattr(shutil.get_terminal_size((80, 20)), 'columns')
value_cols = term_cols - 51
if device['device']['disabled'] == "1": continue print("(0l" + "q" * 27 + "w" + "q" * 18 + "w" + "q" * (term_cols - 49) + "k(B")
print("(0x(B %-25.25s (0x(B %-16.16s (0x(B %-*.*s (0x(B" %
("Hostname", "Interface", value_cols, value_cols, "Description"))
print("(0t" + "q" * 27 + "n" + "q" * 18 + "n" + "q" * (term_cols - 49) + "u(B")
for port in data['ports'].values():
if port['disabled'] == "1": continue if port['disabled'] == "1": continue
if port['deleted'] == "1": continue if port['deleted'] == "1": continue
device = devices[port['device_id']]
if device['disabled'] == "1": continue
print("(0l" + "q" * 20 + "w" + "q" * (term_cols - 23) + "k(B") if args.short:
short_hostname = device['hostname'].split(".",1)[0]
print("(0x(B %-25.25s (0x(B %-16.16s (0x(B %-*.*s (0x(B" %
(short_hostname, port['port_label_short'], value_cols,
value_cols, port['port_descr_descr'] or port['ifAlias']))
print_data(term_cols, "Device", device['device']['hostname']) else:
print_data(term_cols, "", "%s/device/device=%s" % address = call_api(params, 'address/?device_id=%s&interface=%s' % (port['device_id'], port['port_label_short']))
(params['base_url'], port['device_id'])) address6 = call_api(params, 'address/?af=ipv6&device_id=%s&interface=%s' % (port['device_id'], port['port_label_short']))
print_data(term_cols, "Hardware", device['device']['hardware'])
print_data(term_cols, "Port", port['port_label_short'])
print_data(term_cols, "", "%s/device/device=%s/tab=port/port=%s/" %
(params['base_url'], port['device_id'], port['port_id']))
print_data(term_cols, "Description", port['ifAlias'])
print_data(term_cols, "Port status", "%s (Admin) / %s (Oper)" %
(port['ifAdminStatus'], port['ifOperStatus']))
print_data(term_cols, "Last change", port['ifLastChange'])
print_data(term_cols, "Speed", port['ifHighSpeed'], "Mbps")
print_data(term_cols, "Duplex", port['ifDuplex'])
print_data(term_cols, "MTU", port['ifMtu'])
for ip in address['addresses']:
print_data(term_cols, "IP address", "%s/%s" % (ip['ipv4_address'],ip['ipv4_prefixlen']))
for ip6 in address6['addresses']: term_cols = getattr(shutil.get_terminal_size((80, 20)), 'columns')
print_data(term_cols, "IPv6 address", "%s/%s" % (ip6['ipv6_compressed'],ip6['ipv6_prefixlen']))
print_data(term_cols, "Input rate", port['ifInOctets_rate'], "octets")
print_data(term_cols, "Output rate", port['ifOutOctets_rate'], "octets")
print_data(term_cols, "Input errors rate", port['ifInErrors_rate'])
print_data(term_cols, "Output errors rate", port['ifOutErrors_rate'])
print("(0m" + "q" * 20 + "v" + "q" * (term_cols - 23) + "j(B") print("(0l" + "q" * 20 + "w" + "q" * (term_cols - 23) + "k(B")
print_data(term_cols, "Device", device['hostname'])
print_data(term_cols, "", "%s/device/device=%s" %
(params['base_url'], port['device_id']))
print_data(term_cols, "Hardware", device['hardware'])
print_data(term_cols, "Port", port['port_label_short'])
print_data(term_cols, "", "%s/device/device=%s/tab=port/port=%s/" %
(params['base_url'], port['device_id'], port['port_id']))
print_data(term_cols, "Description", port['ifAlias'])
print_data(term_cols, "Port status", "%s (Admin) / %s (Oper)" %
(port['ifAdminStatus'], port['ifOperStatus']))
print_data(term_cols, "Last change", port['ifLastChange'])
print_data(term_cols, "Speed", port['ifHighSpeed'], "Mbps")
print_data(term_cols, "Duplex", port['ifDuplex'])
print_data(term_cols, "MTU", port['ifMtu'])
for ip in address['addresses']:
print_data(term_cols, "IP address", "%s/%s" % (ip['ipv4_address'],ip['ipv4_prefixlen']))
for ip6 in address6['addresses']:
print_data(term_cols, "IPv6 address", "%s/%s" % (ip6['ipv6_compressed'],ip6['ipv6_prefixlen']))
print_data(term_cols, "Input rate", port['ifInOctets_rate'], "octets")
print_data(term_cols, "Output rate", port['ifOutOctets_rate'], "octets")
print_data(term_cols, "Input errors rate", port['ifInErrors_rate'])
print_data(term_cols, "Output errors rate", port['ifOutErrors_rate'])
print("(0m" + "q" * 20 + "v" + "q" * (term_cols - 23) + "j(B")
if args.short:
print("(0m" + "q" * 27 + "v" + "q" * 18 + "v" + "q" * (term_cols - 49) + "j(B")
def search_devices(args): def search_devices(args):
if args.field == "location": if args.field == "location":
@ -105,24 +126,44 @@ def search_devices(args):
(args.string, instance)) (args.string, instance))
return return
if args.short:
term_cols = getattr(shutil.get_terminal_size((80, 20)), 'columns')
value_cols = term_cols - 61
print("(0l" + "q" * 27 + "w" + "q" * 28 + "w" + "q" * (term_cols - 59) + "k(B")
print("(0x(B %-25.25s (0x(B %-26.26s (0x(B %-*.*s (0x(B" %
("Hostname", "Hardware", value_cols, value_cols, "Serial"))
print("(0t" + "q" * 27 + "n" + "q" * 28 + "n" + "q" * (term_cols - 59) + "u(B")
for device in data['devices'].values(): for device in data['devices'].values():
if device['disabled'] == "1": continue if device['disabled'] == "1": continue
term_cols = getattr(shutil.get_terminal_size((80, 20)), 'columns') if args.short:
print("(0l" + "q" * 20 + "w" + "q" * (term_cols - 23) + "k(B") short_hostname = device['hostname'].split(".",1)[0]
print_data(term_cols, "Device", device['hostname']) print("(0x(B %-25.25s (0x(B %-26.26s (0x(B %-*.*s (0x(B" %
print_data(term_cols, "", "%s/device/device=%s" % (short_hostname,
(params['base_url'], device['device_id'])) " ".join([device['vendor'] or "", device['hardware'] or ""]),
print_data(term_cols, "System name", device['sysName']) value_cols, value_cols, device['serial']))
print_data(term_cols, "Hardware", " ".join([device['vendor'] or "", device['hardware'] or ""])) else:
print_data(term_cols, "Software version", term_cols = getattr(shutil.get_terminal_size((80, 20)), 'columns')
" ".join([device['os_text'], print("(0l" + "q" * 20 + "w" + "q" * (term_cols - 23) + "k(B")
device['version'] or ""])) print_data(term_cols, "Device", device['hostname'])
if device['features']: print_data(term_cols, "", "%s/device/device=%s" %
print_data(term_cols, "Features", device['features']) (params['base_url'], device['device_id']))
print_data(term_cols, "Serial number", device['serial']) print_data(term_cols, "System name", device['sysName'])
print_data(term_cols, "Location", device['location']) print_data(term_cols, "Hardware", " ".join([device['vendor'] or "", device['hardware'] or ""]))
print("(0m" + "q" * 20 + "v" + "q" * (term_cols - 23) + "j(B") print_data(term_cols, "Software version",
" ".join([device['os_text'],
device['version'] or ""]))
if device['features']:
print_data(term_cols, "Features", device['features'])
print_data(term_cols, "Serial number", device['serial'])
print_data(term_cols, "Location", device['location'])
print("(0m" + "q" * 20 + "v" + "q" * (term_cols - 23) + "j(B")
if args.short:
print("(0m" + "q" * 27 + "v" + "q" * 28 + "v" + "q" * (term_cols - 59) + "j(B")
# Argument parsing # Argument parsing
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -130,12 +171,12 @@ subparsers = parser.add_subparsers(help='Action to perform',dest='action')
parser_search_port_by_descr = subparsers.add_parser('search_ports', help="Search ports by description") parser_search_port_by_descr = subparsers.add_parser('search_ports', help="Search ports by description")
parser_search_port_by_descr.add_argument('string', type=str, help="String to search") parser_search_port_by_descr.add_argument('string', type=str, help="String to search")
parser_search_port_by_descr.add_argument('-s', '--short', action='store_true')
parser_search_device = subparsers.add_parser('search_devices', help="Search devices") parser_search_device = subparsers.add_parser('search_devices', help="Search devices")
parser_search_device.add_argument('field', choices=['hostname', 'location', 'hardware', 'serial'], help="Field to use for search")
parser_search_device.add_argument('field', choices=['sysname', 'location', 'hardware', 'serial'], help="Field to use for search")
parser_search_device.add_argument('string', type=str, help="String to search") parser_search_device.add_argument('string', type=str, help="String to search")
parser_search_device.add_argument('-s', '--short', action='store_true')
args = parser.parse_args() args = parser.parse_args()
globals()[args.action](args) globals()[args.action](args)

View file

@ -1,3 +1,4 @@
debug: false
instances: instances:
luxnetwork: luxnetwork:
base_url: 'https://noc1.luxnetwork.eu' base_url: 'https://noc1.luxnetwork.eu'