diff --git a/examples/resources/connectivitymonitor/get_connectivity_monitor.py b/examples/resources/connectivitymonitor/get_connectivity_monitor.py index 1034467f..72a2703f 100644 --- a/examples/resources/connectivitymonitor/get_connectivity_monitor.py +++ b/examples/resources/connectivitymonitor/get_connectivity_monitor.py @@ -14,14 +14,30 @@ ProbeStreamRequest ) from arista.connectivitymonitor.v1 import models -from cloudvision.Connector.grpc_client import GRPCClient, create_query +from cloudvision.Connector.grpc_client import create_query +from arista.inventory.v1 import services +RPC_TIMEOUT = 30 # in seconds debug = False parser = argparse.ArgumentParser(add_help=True) +def getSerialNumberToHostnameDict(channel): + """ + Return a json dict with the mapping S/N --> hostname of all devices known to CVP. + """ + device_dict = {} + stub = services.DeviceServiceStub(channel) + # create a stream request + get_all_req = services.DeviceStreamRequest() + # make the GetAll request and loop over the streamed responses + for resp in stub.GetAll(get_all_req, timeout=RPC_TIMEOUT): + device_dict[resp.value.key.device_id.value] = resp.value.hostname.value + return device_dict + + def getConnMon(channel, device=None): connMonGetAll = ProbeStatsStreamRequest() @@ -41,12 +57,13 @@ def getConnMon(channel, device=None): result = {} for resp in connStub.GetAll(connMonGetAll): + device_id = resp.value.key.device_id.value vrfName = resp.value.key.vrf.value hostName = resp.value.key.host.value intf = resp.value.key.source_intf.value connMonKeyVals = ( - connMonKey.device_id.value, + device_id, hostName, vrfName, intf @@ -85,11 +102,12 @@ def getConnMonCfg(channel, device=None): result = {} for resp in configStub.GetAll(configGetAll): + device_id = resp.value.key.device_id.value vrfName = resp.value.key.vrf.value hostName = resp.value.key.host.value probeKeyVals = ( - probeKey.device_id.value, + device_id, hostName, vrfName, ) @@ -107,29 +125,11 @@ def getConnMonCfg(channel, device=None): return result -def get(client, dataset, pathElts): - """Returns a query on a path element""" - result = {} - query = [create_query([(pathElts, [])], dataset)] - - for batch in client.get(query): - for notif in batch["notifications"]: - if debug: - print(notif["updates"]) - result.update(notif["updates"]) - return result - - -def getSwitchInfo(client, device): - pathElts = ["DatasetInfo", "Devices"] - dataset = "analytics" - return get(client, dataset, pathElts) - - -def report(client, data, configData, device): - switchInfo = getSwitchInfo(client, device) +def report(serialNumberToHostnameDict, data, configData): + connectivity_reports = [] for k, v in data.items(): - hostname = switchInfo[k[0]]["hostname"] + serial_number = k[0] + hostname = serialNumberToHostnameDict[serial_number] host = k[1] vrf = k[2] intf = k[3] @@ -137,14 +137,31 @@ def report(client, data, configData, device): jitter = v["jitter"] latency = v["latency"] pktloss = v["packet_loss"] - if "ip_addr" in configData[(device, host, vrf)]: - ipaddr = configData[(device, host, vrf)]["ip_addr"] + if "ip_addr" in configData[(serial_number, host, vrf)]: + ipaddr = configData[(serial_number, host, vrf)]["ip_addr"] else: ipaddr = "" - hdr_part1 = f"{hostname + ' (' + vrf + '/' + intf + ') to ' + host:<50}" - hdr_part2 = f"{ipaddr:<30}{str(httpResp) + 'ms':<30}{str(jitter) + 'ms':<30}" - hdr_part3 = f"{str(latency) + 'ms':<30}{str(pktloss) + '%':<30}" - print(hdr_part1 + hdr_part2 + hdr_part3) + if "description" in configData[(serial_number, host, vrf)]: + description = configData[(serial_number, host, vrf)]["description"] + else: + description = "" + + connectivity_reports.append({ + "hostname": hostname, + "serial_number": serial_number, + "vrf": vrf, + "interface": intf, + "host": host, + "ip_addr": ipaddr, + "description": description, + "http_response": httpResp, + "jitter": jitter, + "latency": latency, + "packet_loss": pktloss + }) + + print(connectivity_reports) + return connectivity_reports def main(apiserverAddr, token=None, certs=None, key=None, ca=None): @@ -161,8 +178,8 @@ def main(apiserverAddr, token=None, certs=None, key=None, ca=None): with grpc.secure_channel(apiserverAddr, connCreds) as channel: data = getConnMon(channel, args.device) configData = getConnMonCfg(channel, args.device) - with GRPCClient(apiserverAddr, token=token, key=key, ca=ca, certs=certs) as client: - report(client, data, configData, args.device) + serialNumberToHostnameDict = getSerialNumberToHostnameDict(channel) + report(serialNumberToHostnameDict, data, configData) if __name__ == "__main__":