def reinit_data(self): """ Subclass update data routine. """ self.man_addr_list = [] # establish connection to application database. self.db_conn.connect(mibs.APPL_DB) mgmt_ip_bytes = self.db_conn.get(mibs.APPL_DB, mibs.LOC_CHASSIS_TABLE, b'lldp_loc_man_addr') if not mgmt_ip_bytes: self.mgmt_ip_str = '' else: self.mgmt_ip_str = mgmt_ip_bytes.decode() logger.debug("Got mgmt ip from db : {}".format(self.mgmt_ip_str)) try: addr_subtype_sub_oid = 4 mgmt_ip_sub_oid = None for mgmt_ip in self.mgmt_ip_str.split(','): if '.' in mgmt_ip: mgmt_ip_sub_oid = (addr_subtype_sub_oid, *[int(i) for i in mgmt_ip.split('.')]) break except ValueError: logger.error("Invalid local mgmt IP {}".format(self.mgmt_ip_str)) return if mgmt_ip_sub_oid == None: return sub_oid = (ManAddrConst.man_addr_subtype_ipv4, *mgmt_ip_sub_oid) self.man_addr_list.append(sub_oid)
def init_mgmt_interface_tables(db_conn): """ Initializes interface maps for mgmt ports :param db_conn: db connector :return: tuple of mgmt name to oid map and mgmt name to alias map """ db_conn.connect(CONFIG_DB) db_conn.connect(STATE_DB) mgmt_ports_keys = db_conn.keys(CONFIG_DB, mgmt_if_entry_table(b'*')) if not mgmt_ports_keys: logger.warning('No managment ports found in {}'.format(mgmt_if_entry_table(b''))) return {}, {} mgmt_ports = [key.split(mgmt_if_entry_table(b''))[-1] for key in mgmt_ports_keys] oid_name_map = {get_index(mgmt_name): mgmt_name for mgmt_name in mgmt_ports} logger.debug('Managment port map:\n' + pprint.pformat(oid_name_map, indent=2)) if_alias_map = dict() for if_name in oid_name_map.values(): if_entry = db_conn.get_all(CONFIG_DB, mgmt_if_entry_table(if_name), blocking=True) if_alias_map[if_name] = if_entry.get(b'alias', if_name) logger.debug("Management alias map:\n" + pprint.pformat(if_alias_map, indent=2)) return oid_name_map, if_alias_map
def init_sync_d_interface_tables(db_conn): """ Initializes interface maps for SyncD-connected MIB(s). :return: tuple(if_name_map, if_id_map, oid_map, if_alias_map) """ # Make sure we're connected to COUNTERS_DB db_conn.connect(COUNTERS_DB) # { if_name (SONiC) -> sai_id } # ex: { "Ethernet76" : "1000000000023" } if_name_map, if_id_map = port_util.get_interface_oid_map(db_conn) logger.debug("Port name map:\n" + pprint.pformat(if_name_map, indent=2)) logger.debug("Interface name map:\n" + pprint.pformat(if_id_map, indent=2)) # { OID -> sai_id } oid_sai_map = { get_index(if_name): sai_id for if_name, sai_id in if_name_map.items() # only map the interface if it's a style understood to be a SONiC interface. if get_index(if_name) is not None } logger.debug("OID sai map:\n" + pprint.pformat(oid_sai_map, indent=2)) # { OID -> if_name (SONiC) } oid_name_map = { get_index(if_name): if_name for if_name in if_name_map # only map the interface if it's a style understood to be a SONiC interface. if get_index(if_name) is not None } logger.debug("OID name map:\n" + pprint.pformat(oid_name_map, indent=2)) # SyncD consistency checks. if not oid_sai_map: # In the event no interface exists that follows the SONiC pattern, no OIDs are able to be registered. # A RuntimeError here will prevent the 'main' module from loading. (This is desirable.) message = "No interfaces found matching pattern '{}'. SyncD database is incoherent." \ .format(port_util.SONIC_ETHERNET_RE_PATTERN) logger.error(message) raise RuntimeError(message) elif len(if_id_map) < len(if_name_map) or len(oid_sai_map) < len( if_name_map): # a length mismatch indicates a bad interface name logger.warning( "SyncD database contains incoherent interface names. Interfaces must match pattern '{}'" .format(port_util.SONIC_ETHERNET_RE_PATTERN)) logger.warning("Port name map:\n" + pprint.pformat(if_name_map, indent=2)) # { SONiC name -> optional rename } if_alias_map = _if_alias_map logger.debug("Chassis name map:\n" + pprint.pformat(if_alias_map, indent=2)) if if_alias_map is None or len(if_alias_map) == 0: logger.warning("No alias map found--port names will use SONiC names.") if_alias_map = dict(zip(if_name_map.keys(), if_name_map.keys())) return if_name_map, if_alias_map, if_id_map, oid_sai_map, oid_name_map
def reinit_data(self): """ Subclass update data routine. """ self.man_addr_list = [] # establish connection to application database. self.db_conn.connect(mibs.APPL_DB) mgmt_ip_bytes = self.db_conn.get(mibs.APPL_DB, mibs.LOC_CHASSIS_TABLE, b'lldp_loc_man_addr') if not mgmt_ip_bytes: self.mgmt_ip_str = '' else: self.mgmt_ip_str = mgmt_ip_bytes.decode() logger.debug("Got mgmt ip from db : {}".format(self.mgmt_ip_str)) try: addr_subtype_sub_oid = 4 mgmt_ip_sub_oid = None for mgmt_ip in self.mgmt_ip_str.split(','): if '.' in mgmt_ip: mgmt_ip_sub_oid = (addr_subtype_sub_oid, *[int(i) for i in mgmt_ip.split('.')]) break except ValueError: logger.error("Invalid local mgmt IP {}".format(self.mgmt_ip_str)) return if mgmt_ip_sub_oid == None: return sub_oid = (ManAddrConst.man_addr_subtype_ipv4, *mgmt_ip_sub_oid) self.man_addr_list.append(sub_oid)
def init_sync_d_queue_tables(db_conn): """ Initializes queue maps for SyncD-connected MIB(s). :return: tuple(port_queues_map, queue_stat_map) """ # { Port name : Queue index (SONiC) -> sai_id } # ex: { "Ethernet0:2" : "1000000000023" } queue_name_map = db_conn.get_all(COUNTERS_DB, COUNTERS_QUEUE_NAME_MAP, blocking=True) logger.debug("Queue name map:\n" + pprint.pformat(queue_name_map, indent=2)) # Parse the queue_name_map and create the following maps: # port_queues_map -> {"port_index : queue_index" : sai_oid} # queue_stat_map -> {"port_index : queue stat table name" : {counter name : value}} # port_queue_list_map -> {port_index: [sorted queue list]} port_queues_map = {} queue_stat_map = {} port_queue_list_map = {} for queue_name, sai_id in queue_name_map.items(): port_name, queue_index = queue_name.split(':') queue_index = ''.join(i for i in queue_index if i.isdigit()) port_index = get_index_from_str(port_name) key = queue_key(port_index, queue_index) port_queues_map[key] = sai_id queue_stat_name = queue_table(sai_id) queue_stat = db_conn.get_all(COUNTERS_DB, queue_stat_name, blocking=False) if queue_stat is not None: queue_stat_key = queue_key(port_index, queue_stat_name) queue_stat_map[queue_stat_key] = queue_stat if not port_queue_list_map.get(int(port_index)): port_queue_list_map[int(port_index)] = [int(queue_index)] else: port_queue_list_map[int(port_index)].append(int(queue_index)) # SyncD consistency checks. if not port_queues_map: # In the event no queue exists that follows the SONiC pattern, no OIDs are able to be registered. # A RuntimeError here will prevent the 'main' module from loading. (This is desirable.) logger.error( "No queues found in the Counter DB. SyncD database is incoherent.") raise RuntimeError('The port_queues_map is not defined') elif not queue_stat_map: logger.error( "No queue stat counters found in the Counter DB. SyncD database is incoherent." ) raise RuntimeError('The queue_stat_map is not defined') for queues in port_queue_list_map.values(): queues.sort() return port_queues_map, queue_stat_map, port_queue_list_map
def init_sync_d_interface_tables(db_conn): """ Initializes interface maps for SyncD-connected MIB(s). :return: tuple(if_name_map, if_id_map, oid_map, if_alias_map) """ # { if_name (SONiC) -> sai_id } # ex: { "Ethernet76" : "1000000000023" } if_name_map, if_id_map = port_util.get_interface_oid_map(db_conn) if_name_map = {if_name: sai_id for if_name, sai_id in if_name_map.items() if \ (re.match(port_util.SONIC_ETHERNET_RE_PATTERN, if_name.decode()) or \ re.match(port_util.SONIC_ETHERNET_BP_RE_PATTERN, if_name.decode()))} # As sai_id is not unique in multi-asic platform, concatenate it with # namespace to get a unique key. Assuming that ':' is not present in namespace # string or in sai id. # sai_id_key = namespace : sai_id if_id_map = {get_sai_id_key(db_conn.namespace, sai_id): if_name for sai_id, if_name in if_id_map.items() if \ (re.match(port_util.SONIC_ETHERNET_RE_PATTERN, if_name.decode()) or \ re.match(port_util.SONIC_ETHERNET_BP_RE_PATTERN, if_name.decode()))} logger.debug("Port name map:\n" + pprint.pformat(if_name_map, indent=2)) logger.debug("Interface name map:\n" + pprint.pformat(if_id_map, indent=2)) # { OID -> sai_id } oid_sai_map = {get_index(if_name): sai_id for if_name, sai_id in if_name_map.items() # only map the interface if it's a style understood to be a SONiC interface. if get_index(if_name) is not None} logger.debug("OID sai map:\n" + pprint.pformat(oid_sai_map, indent=2)) # { OID -> if_name (SONiC) } oid_name_map = {get_index(if_name): if_name for if_name in if_name_map # only map the interface if it's a style understood to be a SONiC interface. if get_index(if_name) is not None} logger.debug("OID name map:\n" + pprint.pformat(oid_name_map, indent=2)) # SyncD consistency checks. if not oid_sai_map: # In the event no interface exists that follows the SONiC pattern, no OIDs are able to be registered. # A RuntimeError here will prevent the 'main' module from loading. (This is desirable.) message = "No interfaces found matching pattern '{}'. SyncD database is incoherent." \ .format(port_util.SONIC_ETHERNET_RE_PATTERN) logger.error(message) raise RuntimeError(message) elif len(if_id_map) < len(if_name_map) or len(oid_sai_map) < len(if_name_map): # a length mismatch indicates a bad interface name logger.warning("SyncD database contains incoherent interface names. Interfaces must match pattern '{}'" .format(port_util.SONIC_ETHERNET_RE_PATTERN)) logger.warning("Port name map:\n" + pprint.pformat(if_name_map, indent=2)) if_alias_map = dict() for if_name in if_name_map: if_entry = db_conn.get_all(APPL_DB, if_entry_table(if_name), blocking=True) if_alias_map[if_name] = if_entry.get(b'alias', if_name) logger.debug("Chassis name map:\n" + pprint.pformat(if_alias_map, indent=2)) return if_name_map, if_alias_map, if_id_map, oid_sai_map, oid_name_map
def update_data(self): """ Background task to add CPU Utilization sample / refresh memory utilization. """ cpu_util = psutil.cpu_percent() self.cpuutils.append(cpu_util) self.system_virtual_memory = psutil.virtual_memory() logger.debug('Updating CPU/Mem Utilization with: {}% / {}%'.format(cpu_util, self.get_memutil()))
def init_sync_d_interface_tables(): """ DRY helper method. Connects to and initializes interface maps for SyncD-connected MIB(s). :return: tuple(db_conn, if_name_map, if_id_map, oid_map, if_alias_map) """ # SyncD database connector. THIS MUST BE INITIALIZED ON A PER-THREAD BASIS. # Redis PubSub objects (such as those within swsssdk) are NOT thread-safe. db_conn = SonicV2Connector(**redis_kwargs) db_conn.connect(COUNTERS_DB) # { if_name (SONiC) -> sai_id } # ex: { "Ethernet76" : "1000000000023" } if_name_map = db_conn.get_all(COUNTERS_DB, COUNTERS_PORT_NAME_MAP, blocking=True) logger.debug("Port name map:\n" + pprint.pformat(if_name_map, indent=2)) # { sai_id -> if_name (SONiC) } if_id_map = {sai_id: if_name for if_name, sai_id in if_name_map.items() # only map the interface if it's a style understood to be a SONiC interface. if get_index(if_name) is not None} logger.debug("Interface name map:\n" + pprint.pformat(if_id_map, indent=2)) # { OID -> sai_id } oid_sai_map = {get_index(if_name): sai_id for if_name, sai_id in if_name_map.items() # only map the interface if it's a style understood to be a SONiC interface. if get_index(if_name) is not None} logger.debug("OID sai map:\n" + pprint.pformat(oid_sai_map, indent=2)) # { OID -> if_name (SONiC) } oid_name_map = {get_index(if_name): if_name for if_name in if_name_map # only map the interface if it's a style understood to be a SONiC interface. if get_index(if_name) is not None} logger.debug("OID name map:\n" + pprint.pformat(oid_name_map, indent=2)) # SyncD consistency checks. if not oid_sai_map: # In the event no interface exists that follows the SONiC pattern, no OIDs are able to be registered. # A RuntimeError here will prevent the 'main' module from loading. (This is desirable.) message = "No interfaces found matching pattern '{}'. SyncD database is incoherent." \ .format(SONIC_ETHERNET_RE_PATTERN) logger.error(message) raise RuntimeError(message) elif len(if_id_map) < len(if_name_map) or len(oid_sai_map) < len(if_name_map): # a length mismatch indicates a bad interface name logger.warning("SyncD database contains incoherent interface names. Interfaces must match pattern '{}'" .format(SONIC_ETHERNET_RE_PATTERN)) logger.warning("Port name map:\n" + pprint.pformat(if_name_map, indent=2)) # { SONiC name -> optional rename } if_alias_map = _if_alias_map logger.debug("Chassis name map:\n" + pprint.pformat(if_alias_map, indent=2)) if if_alias_map is None or len(if_alias_map) == 0: logger.warning("No alias map found--port names will use SONiC names.") if_alias_map = dict(zip(if_name_map.keys(), if_name_map.keys())) return db_conn, if_name_map, if_alias_map, if_id_map, oid_sai_map, oid_name_map
def init_sync_d_queue_tables(db_conn): """ Initializes queue maps for SyncD-connected MIB(s). :return: tuple(port_queues_map, queue_stat_map) """ # Make sure we're connected to COUNTERS_DB db_conn.connect(COUNTERS_DB) # { Port index : Queue index (SONiC) -> sai_id } # ex: { "1:2" : "1000000000023" } queue_name_map = db_conn.get_all(COUNTERS_DB, COUNTERS_QUEUE_NAME_MAP, blocking=True) logger.debug("Queue name map:\n" + pprint.pformat(queue_name_map, indent=2)) # Parse the queue_name_map and create the following maps: # port_queues_map -> {"if_index : queue_index" : sai_oid} # queue_stat_map -> {queue stat table name : {counter name : value}} # port_queue_list_map -> {if_index: [sorted queue list]} port_queues_map = {} queue_stat_map = {} port_queue_list_map = {} for queue_name, sai_id in queue_name_map.items(): port_name, queue_index = queue_name.decode().split(':') queue_index = ''.join(i for i in queue_index if i.isdigit()) port_index = get_index_from_str(port_name) key = queue_key(port_index, queue_index) port_queues_map[key] = sai_id queue_stat_name = queue_table(sai_id) queue_stat = db_conn.get_all(COUNTERS_DB, queue_stat_name, blocking=False) if queue_stat is not None: queue_stat_map[queue_stat_name] = queue_stat if not port_queue_list_map.get(int(port_index)): port_queue_list_map[int(port_index)] = [int(queue_index)] else: port_queue_list_map[int(port_index)].append(int(queue_index)) # SyncD consistency checks. if not port_queues_map: # In the event no queue exists that follows the SONiC pattern, no OIDs are able to be registered. # A RuntimeError here will prevent the 'main' module from loading. (This is desirable.) logger.error("No queues found in the Counter DB. SyncD database is incoherent.") raise RuntimeError('The port_queues_map is not defined') elif not queue_stat_map: logger.error("No queue stat counters found in the Counter DB. SyncD database is incoherent.") raise RuntimeError('The queue_stat_map is not defined') for queues in port_queue_list_map.values(): queues.sort() return port_queues_map, queue_stat_map, port_queue_list_map
def init_sync_d_interface_tables(db_conn): """ Initializes interface maps for SyncD-connected MIB(s). :return: tuple(if_name_map, if_id_map, oid_map, if_alias_map) """ # Make sure we're connected to COUNTERS_DB db_conn.connect(COUNTERS_DB) # { if_name (SONiC) -> sai_id } # ex: { "Ethernet76" : "1000000000023" } if_name_map, if_id_map = port_util.get_interface_oid_map(db_conn) logger.debug("Port name map:\n" + pprint.pformat(if_name_map, indent=2)) logger.debug("Interface name map:\n" + pprint.pformat(if_id_map, indent=2)) # { OID -> sai_id } oid_sai_map = {get_index(if_name): sai_id for if_name, sai_id in if_name_map.items() # only map the interface if it's a style understood to be a SONiC interface. if get_index(if_name) is not None} logger.debug("OID sai map:\n" + pprint.pformat(oid_sai_map, indent=2)) # { OID -> if_name (SONiC) } oid_name_map = {get_index(if_name): if_name for if_name in if_name_map # only map the interface if it's a style understood to be a SONiC interface. if get_index(if_name) is not None} logger.debug("OID name map:\n" + pprint.pformat(oid_name_map, indent=2)) # SyncD consistency checks. if not oid_sai_map: # In the event no interface exists that follows the SONiC pattern, no OIDs are able to be registered. # A RuntimeError here will prevent the 'main' module from loading. (This is desirable.) message = "No interfaces found matching pattern '{}'. SyncD database is incoherent." \ .format(port_util.SONIC_ETHERNET_RE_PATTERN) logger.error(message) raise RuntimeError(message) elif len(if_id_map) < len(if_name_map) or len(oid_sai_map) < len(if_name_map): # a length mismatch indicates a bad interface name logger.warning("SyncD database contains incoherent interface names. Interfaces must match pattern '{}'" .format(port_util.SONIC_ETHERNET_RE_PATTERN)) logger.warning("Port name map:\n" + pprint.pformat(if_name_map, indent=2)) db_conn.connect(APPL_DB) if_alias_map = dict() for if_name in if_name_map: if_entry = db_conn.get_all(APPL_DB, if_entry_table(if_name), blocking=True) if_alias_map[if_name] = if_entry.get(b'alias', if_name) logger.debug("Chassis name map:\n" + pprint.pformat(if_alias_map, indent=2)) return if_name_map, if_alias_map, if_id_map, oid_sai_map, oid_name_map
def init_sync_d_rif_tables(db_conn): """ Initializes map of RIF SAI oids to port SAI oid. :return: dict """ rif_port_map = port_util.get_rif_port_map(db_conn) if not rif_port_map: return {}, {} port_rif_map = {port: rif for rif, port in rif_port_map.items()} logger.debug("Rif port map:\n" + pprint.pformat(rif_port_map, indent=2)) return rif_port_map, port_rif_map
def init_sync_d_rif_tables(db_conn): """ Initializes map of RIF SAI oids to port SAI oid. :return: dict """ rif_port_map = { get_sai_id_key(db_conn.namespace, rif): get_sai_id_key(db_conn.namespace, port) for rif, port in port_util.get_rif_port_map(db_conn).items() } port_rif_map = {port: rif for rif, port in rif_port_map.items()} logger.debug("Rif port map:\n" + pprint.pformat(rif_port_map, indent=2)) return rif_port_map, port_rif_map
def __init__(self): super().__init__() # From the psutil documentation https://pythonhosted.org/psutil/#psutil.cpu_percent: # # Warning the first time this function is called # with interval = 0.0 or None it will return a # meaningless 0.0 value which you are supposed # to ignore. psutil.cpu_percent() # '...is recommended for accuracy that this function be called with at least 0.1 seconds between calls.' time.sleep(0.1) # a sliding window of 60 contiguous 5 sec utilization (up to five minutes) self.cpuutils = collections.deque([psutil.cpu_percent()], maxlen=60) self.system_virtual_memory = psutil.virtual_memory() logger.debug('System Utilization handler initialized.')
def init_mgmt_interface_tables(db_conn): """ Initializes interface maps for mgmt ports :param db_conn: db connector :return: tuple of mgmt name to oid map and mgmt name to alias map """ db_conn.connect(CONFIG_DB) db_conn.connect(STATE_DB) mgmt_ports_keys = db_conn.keys(CONFIG_DB, mgmt_if_entry_table(b'*')) if not mgmt_ports_keys: logger.debug('No managment ports found in {}'.format(mgmt_if_entry_table(b''))) return {}, {} mgmt_ports = [key.split(mgmt_if_entry_table(b''))[-1] for key in mgmt_ports_keys] oid_name_map = {get_index(mgmt_name): mgmt_name for mgmt_name in mgmt_ports} logger.debug('Managment port map:\n' + pprint.pformat(oid_name_map, indent=2)) if_alias_map = dict() for if_name in oid_name_map.values(): if_entry = db_conn.get_all(CONFIG_DB, mgmt_if_entry_table(if_name), blocking=True) if_alias_map[if_name] = if_entry.get(b'alias', if_name) logger.debug("Management alias map:\n" + pprint.pformat(if_alias_map, indent=2)) return oid_name_map, if_alias_map
def init_sync_d_vlan_tables(db_conn): """ Initializes vlan interface maps for SyncD-connected MIB(s). :return: tuple(vlan_name_map, oid_sai_map, oid_name_map) """ vlan_name_map = port_util.get_vlan_interface_oid_map(db_conn) logger.debug("Vlan oid map:\n" + pprint.pformat(vlan_name_map, indent=2)) # { OID -> sai_id } oid_sai_map = { get_index_from_str(if_name): sai_id for sai_id, if_name in vlan_name_map.items() # only map the interface if it's a style understood to be a SONiC interface. if get_index_from_str(if_name) is not None } logger.debug("OID sai map:\n" + pprint.pformat(oid_sai_map, indent=2)) # { OID -> if_name (SONiC) } oid_name_map = { get_index_from_str(if_name): if_name for sai_id, if_name in vlan_name_map.items() # only map the interface if it's a style understood to be a SONiC interface. if get_index_from_str(if_name) is not None } logger.debug("OID name map:\n" + pprint.pformat(oid_name_map, indent=2)) return vlan_name_map, oid_sai_map, oid_name_map
def init_sync_d_vlan_tables(db_conn): """ Initializes vlan interface maps for SyncD-connected MIB(s). :return: tuple(vlan_name_map, oid_sai_map, oid_name_map) """ vlan_name_map = port_util.get_vlan_interface_oid_map(db_conn) logger.debug("Vlan oid map:\n" + pprint.pformat(vlan_name_map, indent=2)) oid_sai_map = {} oid_name_map = {} for sai_id, if_name in vlan_name_map.items(): port_index = get_index_from_str(if_name) if not port_index: continue # { OID -> sai_id } oid_sai_map[port_index] = sai_id # { OID -> if_name (SONiC) } oid_name_map[port_index] = if_name logger.debug("OID sai map:\n" + pprint.pformat(oid_sai_map, indent=2)) logger.debug("OID name map:\n" + pprint.pformat(oid_name_map, indent=2)) return vlan_name_map, oid_sai_map, oid_name_map
def reinit_data(self): """ Subclass update data routine. """ self.man_addr_list = [] self.mgmt_ip_str = None # establish connection to application database. self.db_conn.connect(mibs.APPL_DB) mgmt_ip_bytes = self.db_conn.get(mibs.APPL_DB, mibs.LOC_CHASSIS_TABLE, 'lldp_loc_man_addr') if not mgmt_ip_bytes: logger.warning("Missing lldp_loc_man_addr from APPL DB") return self.mgmt_ip_str = mgmt_ip_bytes logger.debug("Got mgmt ip from db : {}".format(self.mgmt_ip_str)) try: addr_subtype_sub_oid = 4 mgmt_ip_sub_oid = None for mgmt_ip in self.mgmt_ip_str.split(','): if '.' in mgmt_ip: mgmt_ip_tuple = ip2byte_tuple(mgmt_ip) mgmt_ip_sub_oid = (addr_subtype_sub_oid, *mgmt_ip_tuple) break else: logger.error( "Could not find IPv4 address in lldp_loc_man_addr") return except ValueError: logger.error("Invalid local mgmt IP {}".format(self.mgmt_ip_str)) return sub_oid = (ManAddrConst.man_addr_subtype_ipv4, *mgmt_ip_sub_oid) self.man_addr_list.append(sub_oid)