def test_telemetry(subscribe1: dict = test_telemetry_dict): load_dotenv() username_str = os.getenv("PYGNMI_USER") password_str = os.getenv("PYGNMI_PASS") hostname_str = os.getenv("PYGNMI_HOST") port_str = os.getenv("PYGNMI_PORT") path_cert_str = os.getenv("PYGNMI_CERT") with gNMIclient(target=(hostname_str, port_str), username=username_str, password=password_str, path_cert=path_cert_str) as gc: gc.capabilities() telemetry_iterator = gc.subscribe(subscribe=subscribe1) telemetry_entry_item = telemetryParser(telemetry_iterator.__next__()) assert "update" in telemetry_entry_item or "sync_response" in telemetry_entry_item
def sendGnmiRequest(self, origCommandDataStructure, timeout=-1): if not self.connected: raise Exception("not connected ... cannot send commands") self.logger.debug( 'sendGnmiRequest {}'.format(origCommandDataStructure)) command = copy.deepcopy(origCommandDataStructure) if isinstance(command, str): self.logger.info("gnmi command string: %s", command) splitList = shlex.split(command) if len(splitList) == 1: if command == "capabilities": response = self.gc.capabilities() return json.dumps(response, indent=2) elif len(splitList) == 2: command = splitList[0] if command == "get": path = splitList[1] paths = [path] response = self.gc.get(path=paths, encoding='json') return json.dumps(response, indent=2) elif isinstance(command, dict): if list(command.keys())[0] == "get": get_dict = command["get"] paths = get_dict.get("paths") _encoding = get_dict.get("encoding", "json_ietf") try: response = self.gc.get(path=paths, encoding=_encoding) except Exception as exc: self.logger.error("error: %s\n %s", exc, traceback.format_exc()) self.logger.error('Error on line %s', sys.exc_info()[-1].tb_lineno) raise Exception(exc) self.logger.info('gnmi response %s', response) try: return json.dumps(response, indent=2) except Exception as exc: self.logger.error("error: %s\n %s", exc, traceback.format_exc()) self.logger.error('Error on line %s', sys.exc_info()[-1].tb_lineno) raise Exception("cannot convert %s to json: %s", response, exc) if list(command.keys())[0] == "set": set_dict = command["set"] _json = set_dict.get("json") # fixme Yaml _update = [(set_dict["path"], _json)] response = self.gc.set(update=_update, encoding='json') self.logger.info('gnmi response %s', response) try: return json.dumps(response, indent=2) except Exception: return str(response) if list(command.keys())[0] == "subscribe": subscribe_dict = command["subscribe"] self.ts = self.gc.subscribe(subscribe=subscribe_dict) self.logger.info('gnmi telemetry_stream type %s', type(self.ts)) self.nb_ts = Nonblocking_iterator(self.ts, self.timeout, self.logger, stop_if_timeout=True) self.logger.info('noneblocking type %s timeout %s', type(self.nb_ts), self.nb_ts.timeout) return json.dumps("telemetry data receiver intialized") if list(command.keys())[0] == "display_received_telemetry_data": _dict = command["display_received_telemetry_data"] timeout = int(_dict.get("timeout", 60)) * 1000 end_ts = int(round(time.time() * 1000)) + timeout t_data = [] while int(round(time.time() * 1000)) < end_ts: try: telemetry_entry = next(self.nb_ts) except Exception as exc: self.logger.warning('next exception: %s', exc) break if telemetry_entry: t_data.append(telemetryParser(telemetry_entry)) try: return json.dumps(t_data, indent=2) except Exception as exc: self.logger.error("error: %s\n %s", exc, traceback.format_exc()) self.logger.error('Error on line %s', sys.exc_info()[-1].tb_lineno) return json.dumps(aggr_t_data, indent=2) return "unknown gnmiConnect command"
from pygnmi.client import gNMIclient, telemetryParser subscribe = { 'subscription': [ { 'path': 'interfaces/interface[name=Ethernet1]/state/counters', 'mode': 'sample', 'sample_interval': 10000000000 }, { 'path': 'network-instances/network-instance/protocols/protocol/bgp/neighbors/neighbor/state', 'mode': 'sample', 'sample_interval': 10000000000 } ], 'mode': 'stream', 'encoding': 'json' } host = ('10.73.1.105', '6030') with gNMIclient(target=host, username='******', password='******', insecure=True) as gc: telemetry_stream = gc.subscribe(subscribe=subscribe) for telemetry_entry in telemetry_stream: print(telemetryParser(telemetry_entry))
## Subscribe request subscribe = { 'subscription': [{ 'path': 'openconfig-interfaces:interfaces/interface/state', 'mode': 'sample', 'sample_interval': 10000000000 }], 'use_aliases': False, 'mode': 'once', 'encoding': 'proto' } # Body if __name__ == '__main__': with gNMIclient( target=host, username='******', password='******', path_cert='/ws/avpathak-bgl/BossHogg/openconfig/ems_BH_P2A4.pem', override='ems.cisco.com') as gc: response = gc.subscribe(subscribe=subscribe) for telemetry_entry in response: raw_data = telemetryParser(telemetry_entry) #print(json.dumps(raw_data, sort_keys=True, indent=4)) pp = pprint.PrettyPrinter(indent=2) pp.pprint(raw_data)
'stream', 'encoding': 'proto' } subscribe2 = { 'subscription': [{ 'path': 'openconfig-interfaces:interfaces/interface[name=1/1/c1/1]', 'mode': 'sample', 'sample_interval': 10000000000 }], 'use_aliases': False, 'mode': 'stream', 'encoding': 'json' } result = GC.subscribe(subscribe=subscribe1) for ent in result: print(telemetryParser(ent)) if result: print(result) # except: # logging.critical(f'The connectivity towards {DD.targets} cannot be established. The execution is terminated.') # sys.exit(1)