def discoverInterfaceData(client, sysTable):
    snmpAgent = snmputils.SnmpAgent(None, client)
    queryBuilder = snmputils.SnmpQueryBuilder(BASE_INTERFACE_OID)
    queryBuilder.addQueryElement(1, 'ifIndex')
    queryBuilder.addQueryElement(2, 'ifDescr')
    queryBuilder.addQueryElement(3, 'ifType')
    queryBuilder.addQueryElement(5, 'ifSpeed')
    queryBuilder.addQueryElement(6, 'ifMac', 'hexa')
    queryBuilder.addQueryElement(7, 'ifAdminStatus')
    queryBuilder.addQueryElement(8, 'ifOperStatus')

    ifList = snmpAgent.getSnmpData(queryBuilder)

    queryBuilderHigh = snmputils.SnmpQueryBuilder(BASE_HIGH_INTERFACE_OID)
    queryBuilderHigh.addQueryElement(1, 'ifName')
    queryBuilderHigh.addQueryElement(15, 'ifHighSpeed')
    ifHighList = snmpAgent.getSnmpData(queryBuilderHigh)

    refifHigh = {}
    for iface in ifHighList:
        if iface.ifName:
            refifHigh[iface.ifName] = iface

    indx2if = {}
    refIfList = []
    for interface in ifList:
        if interface.ifType == '':
            interface.ifType = -1
        if interface.ifMac == None:
            interface.ifMac = ''
        ifValid = SnmpQueries.checkValidMacByInterfaceTypeAndDescription(int(interface.ifType), interface.ifMac, str(interface.ifDescr).lower())
        if (ifValid == 1)  and (not netutils.isValidMac(interface.ifMac)):
            logger.debug('Mac is invalid:' + interface.ifMac+', using mac index instead - '+interface.ifIndex)
            interface.ifMac = interface.ifIndex
        setattr(interface, 'ifName', None)
        setattr(interface, 'ifAlias', None)
        indx2if[interface.ifIndex] = interface
        refIfList.append(interface)
    try:
        getInterfaceNameAndAlias(client, indx2if)
    except:
        logger.debugException('Failed to get Name and Alias')

    #removing interfaces with invalid mac address and no name or description
    for refIf in refIfList:
        if not modeling.isValidInterface(refIf.ifMac, refIf.ifDescr, refIf.ifName):
            logger.warn('Skipped invalid interface [', str(refIf.ifMac), '], name[', str(refIf.ifName), '], description[', str(refIf.ifDescr), ']')
            ifList.remove(refIf)

    for iface in ifList:
        if iface.ifSpeed and long(iface.ifSpeed) == 4294967295L:#magic number in case speed is higher than 10Gb
            hiIface = refifHigh.get(iface.ifName)
            if hiIface and hiIface.ifHighSpeed:
                iface.ifSpeed = long(hiIface.ifHighSpeed) * 1000000

    return ifList
示例#2
0
def createLayer2Links(ipMap, ndMap, ifMap, l2Map, hostIpMap, nodeIfMap, macHostMap):
	_vector = ObjectStateHolderVector()
	ifsLayer2NameMap = {}

	for (k, v) in l2Map.items():
		nnmL2Name = v.name
		nnmL2Name = nnmL2Name.replace('],', '];')
		tempList = nnmL2Name.split(';')
		(concentrator, retVal) = getL2Interfaces(tempList, nodeIfMap)
		if concentrator == 1:
			macList = []
			for ifidx in retVal:
				if ifMap.has_key(ifidx):
					ifObj = ifMap[ifidx]
					interface = NetworkInterface(ifObj.ifName, ifObj.physicalAddress)
					macList.append(interface)

			## create concentrator host...
			try:
				conOSH = modeling.createCompleteHostOSHByInterfaceList('concentrator', macList)
				conOSH.setAttribute('data_name', 'concentrator')
				conOSH.setAttribute('data_note', 'NNM Data')
				_vector.add(conOSH)
			except:
				continue

			## create other hosts that will connect to the concentrator host...
			for ifidx in retVal:
				if ifMap.has_key(ifidx):
					ifObj = ifMap[ifidx]
					hostedOnId = ifObj.hostedOnId
					if ndMap.has_key(hostedOnId) and hostIpMap.has_key(hostedOnId):
						iplist = hostIpMap[hostedOnId]
						hostOSH = None
						hostOSH = createHost(0, 1, hostedOnId, ndMap, ipMap[iplist[0]].ipValue, None)
						if hostOSH != None:
							_vector.add(hostOSH)

							## create IPs and contained links for the rest of the IPs of that host
							for ip in iplist:
								if ipMap.has_key(ip):
									## create IPs and contained links for the rest of the IPs of that host
									ipOSH = modeling.createIpOSH(ipMap[ip].ipValue);
									ipOSH.setAttribute('data_note', 'NNM data')
									containedLink = modeling.createLinkOSH('contained', hostOSH, ipOSH)
									_vector.add(ipOSH)
									_vector.add(containedLink)

							## create interfaces
							physicalAddress = ifObj.physicalAddress
							description = None
							name = None
							if notNull(ifObj.ifName):
								name = ifObj.ifName
							if notNull(ifObj.ifDescr):
								description = ifObj.ifDescr
							if notNull(physicalAddress) and modeling.isValidInterface(physicalAddress, description):
								ifOSH = createIF_OSH(physicalAddress, hostOSH, description, macHostMap, hostedOnId)
								if notNull(name):
									ifOSH.setAttribute('data_name', name)
								ifOSH.setAttribute('data_note', "NNM data")
								_vector.add(ifOSH)

								## create concentrator's ifOSH
								conIfOSH = createIF_OSH(physicalAddress, conOSH, description, macHostMap, hostedOnId, concentrator)
								if notNull(name):
									conIfOSH.setAttribute('data_name', name)
								ifOSH.setAttribute('data_note', "NNM data")
								_vector.add(conIfOSH)

								## create layer2 link between ifOSH and concentrator's ifOSH
								layer2Link = modeling.createLinkOSH('layertwo', conIfOSH, ifOSH)
								if notNull(name):
									layer2Link.setAttribute('data_name', '%s:%s' % (name, name))
								_vector.add(layer2Link)
								#print 'Layer 2 Link: %s -> %s' % (ifOSH.getAttribute('interface_macaddr'), conIfOSH.getAttribute('interface_macaddr'))
		elif concentrator == 0:
			vals = retVal.split("|")
			if1 = vals[0]
			if2 = vals[1]
			## get the hosts for these ifs
			ifObj1 = ifMap[if1]
			ifObj2 = ifMap[if2]
			hostedOnId1 = ifObj1.hostedOnId
			hostedOnId2 = ifObj2.hostedOnId

			try:
				if ndMap.has_key(hostedOnId1) and hostIpMap.has_key(hostedOnId1) and ndMap.has_key(hostedOnId2) and hostIpMap.has_key(hostedOnId2):
					hostOSH1 = createHost(0, 1, hostedOnId1, ndMap, ipMap[hostIpMap[hostedOnId1][0]].ipValue, None)
					hostOSH2 = createHost(0, 1, hostedOnId2, ndMap, ipMap[hostIpMap[hostedOnId2][0]].ipValue, None)
					_vector.add(hostOSH1)
					_vector.add(hostOSH2)

					if ifMap.has_key(if1) and ifMap.has_key(if2):
						physicalAddress1 = ifObj1.physicalAddress
						description = None
						name1 = None
						if notNull(ifObj1.ifName):
							name1 = ifObj1.ifName
						if notNull(ifObj1.ifDescr):
							description = ifObj1.ifDescr
						if notNull(physicalAddress1) and modeling.isValidInterface(physicalAddress1, description):
							ifOSH1 = createIF_OSH(physicalAddress1, hostOSH1, description, macHostMap, hostedOnId1)
							if notNull(name1):
								ifOSH1.setAttribute('data_name', name1)
							ifOSH1.setAttribute('data_note', "NNM data")
							_vector.add(ifOSH1)

						physicalAddress2 = ifObj2.physicalAddress
						description = None
						name2 = None
						if notNull(ifObj2.ifName):
							name2 = ifObj2.ifName
						if notNull(ifObj2.ifDescr):
							description = ifObj2.ifDescr
						if notNull(physicalAddress2) and modeling.isValidInterface(physicalAddress2, description):
							ifOSH2 = createIF_OSH(physicalAddress2, hostOSH2, description, macHostMap, hostedOnId2)
							if notNull(name2):
								ifOSH2.setAttribute('data_name', name2)
							ifOSH2.setAttribute('data_note', "NNM data")
							_vector.add(ifOSH2)

						## create layer2 links
						# check to see if a similar link already exists
						if ifsLayer2NameMap.has_key('%s:%s' % (physicalAddress2, physicalAddress1)):
							logger.debug('Found duplicate layer2 link. Ignoring...')
							continue
						else:
							layer2Link = modeling.createLinkOSH('layertwo', ifOSH2, ifOSH1)
							if notNull(name1) and notNull(name2):
								layer2Link.setAttribute('data_name', '%s:%s' % (name2, name1))
							_vector.add(layer2Link)
							ifsLayer2NameMap['%s:%s' % (physicalAddress2, physicalAddress1)] = '%s:%s' % (name2, name1)
						#print 'Layer 2 Link: %s -> %s' % (ifOSH1.getAttribute('interface_macaddr'), ifOSH2.getAttribute('interface_macaddr'))
			except:
				continue
	return _vector
示例#3
0
def create_Hosts_Ips_Interfaces(ipMap, ndMap, ifMap, nwMap, ptMap, vlMap, cdMap, hostIpMap, hostIfMap, complete, discoverNonL2Devices, macHostMap):
	_vector = ObjectStateHolderVector()

	ifOshMap = {}
	portOshMap = {}
	cardOshMap = {}
	hostOSH = None
	for (k, v) in hostIpMap.items():
		hostedOnId = k
		iplist = v
		if complete == 0:   ## create incomplete host
			hostOSH = createHost(0, discoverNonL2Devices, hostedOnId, ndMap, ipMap[iplist[0]].ipValue, None)
		elif complete == 1: ## create complete host
			macList = []
			if hostIfMap.has_key(hostedOnId):
				ifList = hostIfMap[hostedOnId]
				for ifidx in ifList:
					if ifMap.has_key(ifidx):
						ifObj = ifMap[ifidx]
						interface = NetworkInterface(ifObj.ifName, ifObj.physicalAddress)
						macList.append(interface)

			hostOSH = createHost(1, discoverNonL2Devices, hostedOnId, ndMap, ipMap[iplist[0]].ipValue, macList)
		else:
			hostOSH = None
			logger.error('ERROR: No hosts (complete/incomplete) created. Only hosts with L2 connections will be created.')

		if hostOSH != None:
			_vector.add(hostOSH)

			## create IPs and contained links for the rest of the IPs of that host
			for ip in iplist:
				if ipMap.has_key(ip):
					ipObj = ipMap[ip]
					## create IPs and contained links for the rest of the IPs of that host
					ipOSH = modeling.createIpOSH(ipObj.ipValue);
					ipOSH.setAttribute('data_note', 'NNM data')
					containedLink = modeling.createLinkOSH('contained', hostOSH, ipOSH)
					_vector.add(ipOSH)
					_vector.add(containedLink)

					## create the network for each IP
					ipSubnetId = ipObj.ipSubnetId
					if nwMap.has_key(ipSubnetId):
						netmaskPrefixLength = nwMap[ipSubnetId].prefixLength;
						if notNull(netmaskPrefixLength):
							netOSH = modeling.createNetworkOSH(ipObj.ipValue, computeStringNetmaskFromPrefix(netmaskPrefixLength))
							netOSH.setAttribute('data_note', "NNM data")
							_vector.add(netOSH)

							## create member link between ip and network
							memberLink = modeling.createLinkOSH('member', netOSH, ipOSH)
							_vector.add(memberLink)

							## create member link between host and network
							memberLink = modeling.createLinkOSH('member', netOSH, hostOSH)
							_vector.add(memberLink)


			## create interfaces
			if hostIfMap.has_key(hostedOnId):
				ifList = hostIfMap[hostedOnId]
				for ifs in ifList:
					if ifMap.has_key(ifs):
						physicalAddress = ifMap[ifs].physicalAddress
						description = None
						name = None
						if notNull(ifMap[ifs].ifName):
							name = ifMap[ifs].ifName
						if notNull(ifMap[ifs].ifDescr):
							description = ifMap[ifs].ifDescr
						if notNull(physicalAddress) and modeling.isValidInterface(physicalAddress, description):
							ifOSH = createIF_OSH(physicalAddress, hostOSH, description, macHostMap, hostedOnId)
							if notNull(name):
								ifOSH.setAttribute('data_name', name)
							ifOSH.setAttribute('data_note', "NNM data")
							_vector.add(ifOSH)
							ifOshMap[ifs] = ifOSH ## Add interface OSH to map for later use to create L2 Connection objects

			# process cards
			# TODO: This is OOB in UCMDB 9.0, but placed here in case someone needs HardwareBoards in 8.x
			#(_vector, cardOshMap) = processCards(_vector, cardOshMap, cdMap, hostOSH, hostedOnId)

			## create ports
			(_vector, portOshMap) = processPorts(_vector, portOshMap, ptMap, ifOshMap, cardOshMap, hostOSH, hostedOnId)

	# add vlan objects
	_vector.addAll(createVlanOshv(vlMap, portOshMap))

	return _vector
示例#4
0
def createInterfaceObjects(ifList, host_id, ucmdbversion = None):
    result = ObjectStateHolderVector()
    
    discoveredHostOSH = modeling.createOshByCmdbId('host', host_id)
    
    for interface in ifList:
        if interface.ifType and int(interface.ifType) == 24:
            continue
        
        if not modeling.isValidInterface(interface.ifMac, interface.ifDescr, interface.ifName):
            continue
        
        interface.ifDescr = _stripVirtualMiniportInfo(interface.ifDescr)
        
        interfaceOSH = modeling.createInterfaceOSH(interface.ifMac, discoveredHostOSH, interface.ifDescr, interface.ifIndex, interface.ifType, interface.ifAdminStatus, interface.ifOperStatus, interface.ifSpeed, interface.ifName, interface.ifAlias)
        if not interfaceOSH:
            continue

        result.add(interfaceOSH)
        if ucmdbversion and ucmdbversion < 9:
            interfaceIndexOSH = ObjectStateHolder("interfaceindex")
            interfaceIndexOSH.setIntegerAttribute("interfaceindex_index", interface.ifIndex)
            if interface.ifAdminStatus:
                intValue = None
                try:
                    intValue = int(interface.ifAdminStatus)
                except:
                    logger.warn("Failed to convert the interface admin status '%s'" % interface.ifAdminStatus)
                else:
                    if intValue > 0:
                        interfaceIndexOSH.setEnumAttribute("interfaceindex_adminstatus", intValue)
            if interface.ifDescr != None and interface.ifDescr != '':
                interfaceIndexOSH.setStringAttribute("interfaceindex_description", interface.ifDescr)
            if interface.ifIndex != None and interface.ifIndex != '':
                interfaceIndexOSH.setIntegerAttribute("interfaceindex_index", interface.ifIndex)
            if interface.ifSpeed != None and interface.ifSpeed != '':
                interfaceIndexOSH.setDoubleAttribute("interfaceindex_speed", interface.ifSpeed)
            if interface.ifType:
                intValue = None
                try:
                    intValue = int(interface.ifType)
                except:
                    logger.warn("Failed to convert the interface type '%s'" % interface.ifType)
                else:
                    if intValue > 0:
                        interfaceIndexOSH.setEnumAttribute("interfaceindex_type", intValue)
            interfaceIndexOSH.setContainer(discoveredHostOSH)
            result.add(interfaceIndexOSH)
            
    
            parentLinkOSH = modeling.createLinkOSH('parent', interfaceIndexOSH, interfaceOSH)
            result.add(parentLinkOSH)

        for ip in interface.ipList:
            if str(ip.ipAddr).startswith('0.') or str(ip.ipAddr).startswith('127.'):
                continue
            ipOSH = modeling.createIpOSH(ip.ipAddr)
            parentLinkOSH = modeling.createLinkOSH('containment', interfaceOSH, ipOSH)
            result.add(parentLinkOSH)
        
    return result
示例#5
0
def discoverInterfaceData(client, sysTable):
    snmpAgent = snmputils.SnmpAgent(None, client)    
    queryBuilder = snmputils.SnmpQueryBuilder(BASE_INTERFACE_OID)
    queryBuilder.addQueryElement(1, 'ifIndex')
    queryBuilder.addQueryElement(2, 'ifDescr')
    queryBuilder.addQueryElement(3, 'ifType')
    queryBuilder.addQueryElement(5, 'ifSpeed')
    queryBuilder.addQueryElement(6, 'ifMac', 'hexa')
    queryBuilder.addQueryElement(7, 'ifAdminStatus')
    queryBuilder.addQueryElement(8, 'ifOperStatus')
    
    ifList = snmpAgent.getSnmpData(queryBuilder)
    
    queryBuilderHigh = snmputils.SnmpQueryBuilder(BASE_HIGH_INTERFACE_OID)
    queryBuilderHigh.addQueryElement(1, 'ifName')
    queryBuilderHigh.addQueryElement(15, 'ifHighSpeed')
    ifHighList = snmpAgent.getSnmpData(queryBuilderHigh)
    
    refifHigh = {}
    for iface in ifHighList:
        if iface.ifName:
            refifHigh[iface.ifName] = iface
            
    indx2if = {}
    refIfList = []
    for interface in ifList:
        if interface.ifType == '':
            interface.ifType = -1
        if interface.ifMac == None:
            interface.ifMac = ''
        if interface.ifMac and len(interface.ifMac) >= 34:
            #most likely we're dealing with the string to hex encoded value
            #try to decode it
            try:
                mac = interface.ifMac.decode('hex')
                #the fetched MAC might have missing 0 character at the end
                if mac:
                    if len(mac) in [16, 11]:
                        mac = mac + '0'
                    interface.ifMac = mac
                
            except:
                logger.debugException('')
        ifValid = SnmpQueries.checkValidMacByInterfaceTypeAndDescription(int(interface.ifType), interface.ifMac, interface.ifDescr and interface.ifDescr.lower())
        if (ifValid == 1)  and (not netutils.isValidMac(interface.ifMac)):
            logger.debug('Mac is invalid:' + interface.ifMac+', using mac index instead - '+interface.ifIndex)
            interface.ifMac = interface.ifIndex
        setattr(interface, 'ifName', None)
        setattr(interface, 'ifAlias', None)
        indx2if[interface.ifIndex] = interface
        refIfList.append(interface)
    try:
        getInterfaceNameAndAlias(client, indx2if)
    except:
        logger.debugException('Failed to get Name and Alias')

    #removing interfaces with invalid mac address and no name or description
    for refIf in refIfList:
        if not modeling.isValidInterface(refIf.ifMac, refIf.ifDescr, refIf.ifName):
            logger.warn('Skipped invalid interface [', str(refIf.ifMac), '], name[', str(refIf.ifName), '], description[', str(refIf.ifDescr), ']')
            ifList.remove(refIf)
            
    for iface in ifList:
        if iface.ifSpeed and long(iface.ifSpeed) == 4294967295L:#magic number in case speed is higher than 10Gb
            hiIface = refifHigh.get(iface.ifName)
            if hiIface and hiIface.ifHighSpeed:
                iface.ifSpeed = long(hiIface.ifHighSpeed) * 1000000
                
    return ifList