def setOids(cls, oidDb, bdsData, bdsIds, uptime): """Populates OID DB with BDS information. Takes known objects from JSON document, puts them into the OID DB as specific MIB managed objects. Args: oidDb (OidDb): OID DB instance to work on bdsData (dict): BDS information to put into OID DB bdsIds (list): list of last known BDS record sequence IDs uptime (int): system uptime in hundreds of seconds Raises: BdsError: on OID DB population error """ newBdsIds = [obj['sequence'] for obj in bdsData['objects']] if newBdsIds == bdsIds: return add = oidDb.add for i, bdsJsonObject in enumerate(bdsData['objects']): ifName = bdsJsonObject['attribute']['interface_name'] index = if_tools.ifIndexFromIfName(ifName) add('IF-MIB', 'ifIndex', index, value=index) add('IF-MIB', 'ifDescr', index, value=bdsJsonObject['attribute']['interface_name']) add('IF-MIB', 'ifType', index, value=6) if i < len(bdsIds): # possible table entry change ifLastChange = None if newBdsIds[i] == bdsIds[i] else uptime else: # initial run or table size change ifLastChange = uptime if bdsIds else 0 add('IF-MIB', 'ifLastChange', index, value=ifLastChange) # count *all* IF-MIB interfaces we currently have - some # may be contributed by other modules ifNumber = len(oidDb.getObjectsByName('IF-MIB', 'ifIndex')) add('IF-MIB', 'ifNumber', 0, value=ifNumber) add('IF-MIB', 'ifStackLastChange', 0, value=uptime if bdsIds else 0) add('IF-MIB', 'ifTableLastChange', 0, value=uptime if bdsIds else 0) bdsIds[:] = newBdsIds
def setOids(cls, oidDb, bdsData, bdsIds, uptime): """Populates OID DB with BDS information. Takes known objects from JSON document, puts them into the OID DB as specific MIB managed objects. Args: oidDb (OidDb): OID DB instance to work on bdsData (dict): BDS information to put into OID DB bdsIds (list): list of last known BDS record sequence IDs uptime (int): system uptime in hundreds of seconds Raises: BdsError: on OID DB population error """ newBdsIds = [obj['sequence'] for obj in bdsData['objects']] if newBdsIds == bdsIds: return add = oidDb.add for i, bdsJsonObject in enumerate(bdsData['objects']): ifName = bdsJsonObject['attribute']['interface_name'] if not ifName.startswith('if'): #fix for lo0 in table continue index = if_tools.ifIndexFromIfName(ifName) #ifPhysicalLocation = if_tools.stripIfPrefixFromIfName(ifName) add('IF-MIB', 'ifIndex', index, value=index) add('IF-MIB', 'ifDescr', index, value=ifName) if 'interface_type' in bdsJsonObject['attribute']: add('IF-MIB', 'ifType', index, value=IFTYPEMAP[int(bdsJsonObject['attribute']['interface_type'])]) if 'layer2_mtu' in bdsJsonObject['attribute']: add('IF-MIB', 'ifMtu', index, value=IFMTU_LAMBDA(bdsJsonObject['attribute']['layer2_mtu'])) if 'mac_address' in bdsJsonObject['attribute']: add('IF-MIB', 'ifPhysAddress', index, valueFormat='hexValue', value=bdsJsonObject['attribute']['mac_address'].replace(':', '')) if 'admin_status' in bdsJsonObject['attribute']: add('IF-MIB', 'ifAdminStatus', index, value=IFOPERSTATUSMAP[int(bdsJsonObject['attribute']['admin_status'])]) if 'link_status' in bdsJsonObject['attribute']: add('IF-MIB', 'ifOperStatus', index, value=IFOPERSTATUSMAP[int(bdsJsonObject['attribute']['link_status'])]) if 'bandwidth' in bdsJsonObject['attribute']: add('IF-MIB', 'ifSpeed', index, value=IFSPEED_LAMBDA(bdsJsonObject['attribute']['bandwidth'])) if i < len(bdsIds): # possible table entry change ifLastChange = None if newBdsIds[i] == bdsIds[i] else uptime else: # initial run or table size change ifLastChange = uptime if bdsIds else 0 add('IF-MIB', 'ifLastChange', index, value=ifLastChange) # count *all* IF-MIB interfaces we currently have - some # may be contributed by other modules ifNumber = len(oidDb.getObjectsByName('IF-MIB', 'ifIndex')) add('IF-MIB', 'ifNumber', 0, value=ifNumber) add('IF-MIB', 'ifStackLastChange', 0, value=uptime if bdsIds else 0) add('IF-MIB', 'ifTableLastChange', 0, value=uptime if bdsIds else 0) bdsIds[:] = newBdsIds
def setOids(cls, oidDb, bdsData, bdsIds, birthday): """Populates OID DB with BDS information. Takes known objects from JSON document, puts them into the OID DB as specific MIB managed objects. Args: oidDb (OidDb): OID DB instance to work on bdsData (dict): BDS information to put into OID DB bdsIds (list): list of last known BDS record sequence IDs birthday (float): timestamp of system initialization Raises: BdsError: on OID DB population error """ newBdsIds = [obj['sequence'] for obj in bdsData['objects']] if newBdsIds == bdsIds: return currentSysTime = int((time.time() - birthday) * 100) add = oidDb.add for i, bdsJsonObject in enumerate(bdsData['objects']): attribute = bdsJsonObject['attribute'] ifName = attribute['interface_name'] index = if_tools.ifIndexFromIfName(ifName) add('IF-MIB', 'ifInOctets', index, value=LELL_LAMBDA(attribute['port_stat_if_in_octets'])) add('IF-MIB', 'ifInUcastPkts', index, value=LELL_LAMBDA(attribute['port_stat_if_in_ucast_pkts'])) add('IF-MIB', 'ifInNUcastPkts', index, value=LELL_LAMBDA(attribute['port_stat_if_in_non_ucast_pkts'])) add('IF-MIB', 'ifInDiscards', index, value=LELL_LAMBDA(attribute['port_stat_if_in_discards'])) add('IF-MIB', 'ifInErrors', index, value=LELL_LAMBDA(attribute['port_stat_if_in_errors'])) add('IF-MIB', 'ifInUnknownProtos', index, value=LELL_LAMBDA(attribute['port_stat_if_in_unknown_protos'])) add('IF-MIB', 'ifOutOctets', index, value=LELL_LAMBDA(attribute['port_stat_if_out_octets'])) add('IF-MIB', 'ifOutUcastPkts', index, value=LELL_LAMBDA(attribute['port_stat_if_out_ucast_pkts'])) add('IF-MIB', 'ifOutNUcastPkts', index, value=LELL_LAMBDA(attribute['port_stat_if_out_non_ucast_pkts'])) add('IF-MIB', 'ifOutDiscards', index, value=LELL_LAMBDA(attribute['port_stat_if_out_discards'])) add('IF-MIB', 'ifOutErrors', index, value=LELL_LAMBDA(attribute['port_stat_if_out_errors'])) add('IF-MIB', 'ifOutQLen', index, value=LELL_LAMBDA(attribute['port_stat_if_out_qlen'])) if i < len(bdsIds): # possible table entry change ifLastChange = None if newBdsIds[i] == bdsIds[i] else currentSysTime else: # initial run or table size change ifLastChange = currentSysTime if bdsIds else 0 add('IF-MIB', 'ifLastChange', index, value=ifLastChange) # count *all* IF-MIB interfaces we currently have - some # may be contributed by other modules ifNumber = len(oidDb.getObjectsByName('IF-MIB', 'ifIndex')) add('IF-MIB', 'ifNumber', 0, value=ifNumber) add('IF-MIB', 'ifStackLastChange', 0, value=currentSysTime if bdsIds else 0) add('IF-MIB', 'ifTableLastChange', 0, value=currentSysTime if bdsIds else 0) bdsIds[:] = newBdsIds
def test_ifIndexFromIfName_unknown(self): ifName = "unk-1/0/1" ifIndex = if_tools.ifIndexFromIfName(ifName) self.assertIsNone(ifIndex)
def test_ifIndexFromIfName_lo(self): ifName = "lo-0/0/1" ifIndex = if_tools.ifIndexFromIfName(ifName) expected = 4294971392 self.assertEqual(expected, ifIndex)
def test_ifIndexFromIfName_ifl(self): ifName = "ifl-0/0/1/1/0" ifIndex = if_tools.ifIndexFromIfName(ifName) expected = 528385 self.assertEqual(expected, ifIndex)
def test_ifIndexFromIfName_ifp(self): ifName = "ifp-0/0/1" ifIndex = if_tools.ifIndexFromIfName(ifName) expected = 4096 self.assertEqual(expected, ifIndex)