Skip to content

Commit

Permalink
examples: Have get_connectivity_monitor example use inventory rAPI (#28)
Browse files Browse the repository at this point in the history
Uses the inventory API to get the device info rather than the GRPC client, which can error out
Also;
- removes unused functions
- allows for usage of multiple devices
  • Loading branch information
guillaumeVilar authored Nov 5, 2024
1 parent 725dedc commit 413584f
Showing 1 changed file with 50 additions and 33 deletions.
83 changes: 50 additions & 33 deletions examples/resources/connectivitymonitor/get_connectivity_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand Down Expand Up @@ -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,
)
Expand All @@ -107,44 +125,43 @@ 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]
httpResp = v["http_response"]
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):
Expand All @@ -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__":
Expand Down

0 comments on commit 413584f

Please sign in to comment.