Exemple #1
0
    def buildTopology(self):
        '''
        Construct as400 node topology
        @types AS400Client -> ObjectStateHolderVector
        '''
        myvec = ObjectStateHolderVector()
        ip_address = self.__client.getIpAddress()
        # create as400 agent OSH

        interfaceList = self.__interfacesInfo
        as400HostOsh = None
        if interfaceList and len(interfaceList) > 0:
            # Create as400 CIT       
            try: 
                # Get the host_key - lowest mac address of the valid interface list
                as400HostOsh = modeling.createCompleteHostOSHByInterfaceList('as400_node', interfaceList, 'OS400', self.__sysInfo.hostName)
                as400HostOsh.setAttribute('host_model', self.__sysInfo.hostModel)
                as400HostOsh.setAttribute('host_serialnumber', self.__sysInfo.hostSerialNum)
                as400HostOsh.setAttribute('host_osversion', self.__sysInfo.hostOSVersion)
                as400HostOsh.setAttribute('host_vendor', 'IBM')
                myvec.add(as400HostOsh)
                
                as400Osh = self.__createAS400AgentOSH(ip_address)
                as400Osh.setAttribute('credentials_id', self.__client.getCredentialId())
                as400Osh.setContainer(as400HostOsh)
                myvec.add(as400Osh)

            except:
                logger.warn('Could not find a valid MAC addresses for key on ip : ', self.__client.getIpAddress())
       
        # add all interfaces to the host
        if as400HostOsh is not None:
            for interface in interfaceList:
                interfaceOsh = modeling.createInterfaceOSH(interface.macAddress, as400HostOsh, interface.description, interface.interfaceIndex, interface.type, interface.adminStatus, interface.adminStatus, interface.speed, interface.name, interface.alias, interface.className)
                ips = interface.ips
                masks = interface.masks
                if interfaceOsh:
                    myvec.add(interfaceOsh)
                    if ips and masks:
                        for i in xrange(len(ips)):
                            ipAddress = ips[i]
                            ipMask = masks[i]

                            ipProp = modeling.getIpAddressPropertyValue(ipAddress, ipMask, interface.dhcpEnabled, interface.description)
                            ipOSH = modeling.createIpOSH(ipAddress, ipMask, ipProp)
                            myvec.add(ipOSH)
                            
                            myvec.add(modeling.createLinkOSH('containment', as400HostOsh, ipOSH))
                            netOSH = modeling.createNetworkOSH(ipAddress, ipMask)   
                            myvec.add(netOSH)
                            
                            myvec.add(modeling.createLinkOSH('member', netOSH, as400HostOsh))
                            myvec.add(modeling.createLinkOSH('member', netOSH, ipOSH))
                            myvec.add(modeling.createLinkOSH('containment', interfaceOsh, ipOSH))
        else:
            logger.warn('Parent host wasn\'t created. No interfaces will be reported.')
        return myvec
Exemple #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
Exemple #3
0
def createHost(complete, discoverNonL2Devices, hostedOnId, ndMap, ipValue = None, maclist = None):

	hostObj = None
	try:
		hostObj = ndMap[hostedOnId]
	except:
		return
	host_hostname = hostObj.name          ## name -> host_hostname
	## TODO Figure out why NNM stores IP addresses  in Host DNS names... host_dnsname = hostObj.longName       ## longName -> host_dnsname
	host_dnsname = hostObj.name           ## longName -> host_dnsname
	host_model = hostObj.deviceModel      ## deviceModel -> host_model
	host_snmpsysname = hostObj.systemName ## systemName -> host_snmpsysname
	host_os = hostObj.systemDescription   ## systemDescription -> host_os

	host_nnm_uid = "NNM:%s" % hostedOnId         ## external reference ID for NNM node object

	devVendor = hostObj.deviceVendor      ## deviceVendor -> host_vendor
	host_vendor  = None
	if notNull(devVendor):
		host_vendor = devVendor[len('com.hp.ov.nms.devices.'):len(devVendor)]
	else:
		host_vendor = 'nosnmp'

	## figure out the host class if possible...
	devCat = hostObj.deviceCategory       ## deviceCategory -> host class
	host_class = 'host'
	if notNull(devCat):
		nnm_cat = devCat[len('com.hp.ov.nms.devices.'):len(devCat)]
		if nnm_cat == 'atmswitch' or nnm_cat == 'firewall' or nnm_cat == 'switch' or nnm_cat == 'router' or nnm_cat == 'switchrouter':
			host_class = nnm_cat
		elif nnm_cat == 'loadbalancer':
			host_class = 'lb'
		elif nnm_cat == 'printer':
			host_class = 'netprinter'
		else:
			host_class = 'host'

	## isSwitch OR isRouter OR isSwitchRouter -> host class
	isSwitch = hostObj.isLanSwitch
	isRouter = hostObj.isRouter
	isSwitchRouter = 0
	if (isSwitch and isRouter):
		isSwitchRouter = 1

	isL2Dev = 1
	if isSwitchRouter:
		host_class = 'switchrouter'
	elif isSwitch:
		host_class = 'switch'
	elif isRouter:
		host_class = 'router'
	else:
		isL2Dev = 0

	created = 1
	hostOSH = None
	if not isL2Dev and not discoverNonL2Devices:
		created = 0
	else:
		if complete:
			## create complete host...
			if maclist != None and len(maclist) > 0:
				try:
					hostOSH = modeling.createCompleteHostOSHByInterfaceList(host_class, maclist, host_os, host_hostname)
				except:
					## Looks like we have an empty MAC here, so we'll set the isPseudo attribute to true
					complete = 0
					created = 0
			else:
				## no mac list sent. try to create an incomplete host...
				complete = 0
				created = 0

		if not complete:
			if notNull(ipValue):
				hostOSH = modeling.createHostOSH(ipValue, host_class, host_os, host_hostname)
				created = 1
			else:
				## no ip address sent. return None
				created = 0

	if created and hostOSH:
		if host_dnsname:
			hostOSH.setAttribute('host_dnsname', host_dnsname)

		if host_model and host_model != '<No SNMP>':
			hostOSH.setAttribute('host_model', host_model)

		if host_vendor and host_vendor != 'nosnmp':
			hostOSH.setAttribute('host_vendor', host_vendor)

		if host_snmpsysname:
			hostOSH.setAttribute('host_snmpsysname', host_snmpsysname)
			if (host_class in netDeviceClasses) and notNull(host_snmpsysname):
				hostOSH.setAttribute('host_key', host_snmpsysname)
				hostOSH.setBoolAttribute('host_iscomplete',1)

		hostOSH.setAttribute('data_note', "NNM data")
		hostOSH.setAttribute('host_nnm_uid', host_nnm_uid)
		hostOSH = finalizeHostOsh(hostOSH)
	else:
		hostOSH = None

	return hostOSH
def doWMI(client, wmiOSH, ip_address, ip_domain, hostForLinkOSH, host_cmdbid=None, host_key=None, host_macs=None, ucmdb_version=None):
    '''@types: WmiClient, ObjectStateHolder, IPAddress, str, ObjectStateHolder, str, str, list[str], int -> ObjectStateHolderVector
    @param ip_address: Destination IP address
    '''
    wmiProvider = wmiutils.getWmiProvider(client)
    hostDiscoverer = host_win_wmi.WmiHostDiscoverer(wmiProvider)
    hostInfo = hostDiscoverer.discoverHostInfo()
    machineName = hostInfo.hostName

    interfacesDiscover = networking_win_wmi.WmiInterfaceDiscoverer(wmiProvider,
                                                ip_address)

    vector = ObjectStateHolderVector()
    interfaceList = interfacesDiscover.getInterfaces()
    parentLinkList = ObjectStateHolderVector()

    isVirtual = 1
    resultEmpty = 1
    interfacesToUpdateList = []
    for interface in interfaceList:
        ips = interface.ips
        MACAddress = interface.macAddress
        masks = interface.masks
        Description = interface.description
        dhcpEnabled = interface.dhcpEnabled

        resultEmpty = 0
        for ipIndex, ipAddress in enumerate(__iterate_valid_ips(ips)):
            IPSubnet = (masks and masks[ipIndex]) or None
            if str(ip_address) == str(ipAddress):
                isVirtual = 0

            ipOSH = modeling.createIpOSH(ipAddress)

            # Test if the same ip and interface are in the list allready

            logger.debug('Found ip address: ', ipAddress, ', MACAddress: ', MACAddress, ', IPSubnet: ', IPSubnet, ', Description: ', Description)
                # create OSH for IP and add it to the list
                # create OSH for Network and add it to the list
            __vector = getIPNetworkMemebrList(ipAddress, IPSubnet, '', dhcpEnabled, Description)
            if __vector.size() > 0:
                vector.addAll(__vector)

            if netutils.isValidMac(MACAddress):
                # create link interface to its ip only it has an ip defined
                interfaceOSH = modeling.createInterfaceOSH(MACAddress, hostForLinkOSH, Description)
                parentLinkList.add(modeling.createLinkOSH('containment', interfaceOSH, ipOSH))
                interfacesToUpdateList.append(interfaceOSH)

    # Check if the Input IP is virtual, we do not want to return the WMI
    if isVirtual == 1:
        logger.warn('Destination is not among discovered IPs assuming virtual. WMI object will not be reported.')
        vIPOSH = modeling.createIpOSH(ip_address)
        vIPOSH.setBoolAttribute('isvirtual', 1)
        vector.add(vIPOSH)

    if resultEmpty == 1:
        logger.warn('WMI was able to connect, but WMI Query returns no results')
        vector.clear()
        return vector

    # create the host and all the objects
    if len(interfaceList) > 0:
        hostOSH = None
        try:
            hostOSH = modeling.createCompleteHostOSHByInterfaceList('nt',
                            interfaceList, 'Windows', machineName, None,
                            host_cmdbid, host_key, host_macs, ucmdb_version)
        except:
            hostOSH = modeling.createHostOSH(str(ip_address), 'nt')
            logger.debugException('Could not find a valid MAC address for key on ip : %s. Creating incomplete host\n' % ip_address)
            logger.warn('Could not find a valid MAC address for key on ip : %s. Creating incomplete host\n' % ip_address)

        # select from Win32_OperatingSystem
        _wmiQuery = 'select Caption,Version,ServicePackMajorVersion,ServicePackMinorVersion,BuildNumber,Organization,RegisteredUser,TotalVisibleMemorySize,LastBootUpTime,OtherTypeDescription,description from Win32_OperatingSystem'
        resultSet = client.executeQuery(_wmiQuery)  # @@CMD_PERMISION wmi protocol execution
        osinstalltype = None
        if resultSet.next():
            Caption = resultSet.getString(1)
            Version = resultSet.getString(2)
            ServicePackMajorVersion = resultSet.getString(3)
            ServicePackMinorVersion = resultSet.getString(4)
            BuildNumber = resultSet.getString(5)
            Organization = resultSet.getString(6)
            RegisteredUser = resultSet.getString(7)
            TotalVisibleMemorySize = resultSet.getString(8)
            LastBootUpTime = resultSet.getString(9)
            OtherTypeDescription = resultSet.getString(10)
            description = resultSet.getString(11)

            (vendor, osName, osinstalltype) = separateCaption(Caption, OtherTypeDescription)
            hostOSH.setAttribute('host_vendor', vendor)
            modeling.setHostOsName(hostOSH, osName)
            hostOSH.setAttribute('host_osinstalltype', osinstalltype)

            setLastBootUpTime(hostOSH, LastBootUpTime)
            biosUUID = getBIOSUUID(client)
            defaultGateway = getDefaultGateway(client)

            hostOSH.setAttribute('host_osversion', Version)
            sp = ServicePackMajorVersion + '.' + ServicePackMinorVersion
            if checkSpVersion(sp):
                hostOSH.setAttribute('nt_servicepack', sp)
            hostOSH.setAttribute('host_osrelease', str(BuildNumber))
            hostOSH.setAttribute('nt_registrationorg', Organization)
            hostOSH.setAttribute('nt_registeredowner', RegisteredUser)
            hostOSH.setAttribute('nt_physicalmemory', TotalVisibleMemorySize)
            hostOSH.setAttribute('host_hostname', machineName)
            modeling.setHostBiosUuid(hostOSH, biosUUID)
            modeling.setHostDefaultGateway(hostOSH, defaultGateway)
            modeling.setHostOsFamily(hostOSH, 'windows')

            hostOSH = HostBuilder(hostOSH).setDescription(description).build()

        _wmiQuery2 = 'select Manufacturer,NumberOfProcessors,Model,Domain from Win32_ComputerSystem'
        resultSet = client.executeQuery(_wmiQuery2)  # @@CMD_PERMISION wmi protocol execution

        if resultSet.next():
            Manufacturer = resultSet.getString(1)
            if ((Manufacturer != None) and (Manufacturer.find('system manufacturer') == -1)):
                modeling.setHostManufacturerAttribute(hostOSH, Manufacturer.strip())
            NumberOfProcessors = resultSet.getString(2)
            hostOSH.setAttribute('nt_processorsnumber', int(NumberOfProcessors))
            Model = resultSet.getString(3)
            modeling.setHostModelAttribute(hostOSH, Model)
            osDomain = resultSet.getString(4)
            hostOSH.setAttribute('host_osdomain', osDomain.strip())

        biosAssetTag = hostDiscoverer.getBiosAssetTag()
        if biosAssetTag:
            hostOSH.setAttribute('bios_asset_tag', biosAssetTag)

        _wmiQuery4 = 'SELECT SerialNumber FROM Win32_BIOS'
        resultSet = client.executeQuery(_wmiQuery4)  # @@CMD_PERMISSION wmi protocol execution
        if resultSet.next():
            serialNumber = processSerialNumber(resultSet.getString(1))
            if not serialNumber:
                wmiBaseBoardSerialNumber = 'SELECT SerialNumber FROM Win32_BaseBoard'
                resultSet = client.executeQuery(wmiBaseBoardSerialNumber)  # @@CMD_PERMISION wmi protocol execution
                serialNumber = processSerialNumber(str(resultSet))
            modeling.setHostSerialNumberAttribute(hostOSH, serialNumber)

        try:
            paeEnabled = getPAEState(client)
            if paeEnabled and paeEnabled.lower() in ['1', 'true']:
                hostOSH.setBoolAttribute('pae_enabled', 1)
            elif paeEnabled and paeEnabled.lower() in ['0', 'false']:
                hostOSH.setBoolAttribute('pae_enabled', 0)
        except Exception, ex:
            logger.warn('Failed getting PAE state. %s' % ex)

        try:
            osArchitecture = getOsArchitecture(client)
            if osinstalltype and osinstalltype.find('64') != -1:
                osArchitecture = '64-bit'
            if osArchitecture:
                hostOSH.setStringAttribute('os_architecture', osArchitecture)
        except Exception, ex:
            logger.warn('Failed getting OS Architecture value. %s' % ex)
Exemple #5
0
def createVirtHostOsh(config):
    vHostOsh = modeling.createCompleteHostOSHByInterfaceList('host', config.interfaceList)
    return HostBuilder(vHostOsh).setAsVirtual(1).build()
Exemple #6
0
    def buildInterfaces(self):
        """ We create and report such topology for destination ip in following cases:
                            |host is complete     | host is incomplete|
        ip is virtual        |all except NTCMD    |only virtual ip    |
        ip is not virtual    |all topology        |all topology        |
        """
        try:
            self.hostOsh = modeling.createCompleteHostOSHByInterfaceList('nt', self.interfacesList, self.hostDo.hostOsName, self.hostDo.hostName, self.hostDo.lastBootDate, self.hostCmdbId, self.hostKey, self.hostMacs, self.ucmdbVersion)
            if self.hostDo.ipIsVirtual:
                logger.warn('IP: ', self.destinationIp, ' does not appear in the result buffer of the interfaces output - assuming virtual')
                logger.warn('Host topology will be reported without NTCMD CI.')
        except:
            self.hostOsh = modeling.createHostOSH(str(self.destinationIp), 'nt', self.hostDo.hostOsName, self.hostDo.hostName)
            logger.warn('Could not find a valid MAC address for key on ip : ', self.destinationIp)
            if self.hostDo.ipIsVirtual:
                logger.warn('IP: ', self.destinationIp, ' does not appear in the result buffer of the interfaces output - assuming virtual')
                logger.warn('Only IP marked as virtual will be reported.')
                self.resultVector.clear()
                virtualIpOsh = modeling.createIpOSH(str(self.destinationIp))
                virtualIpOsh.setBoolAttribute('isvirtual', 1)
                self.resultVector.add(virtualIpOsh)
                self.hostOsh = None
                return
        #process interfaces looking for nic teaming interfaces.
        #looking up for interfaces with same mac
        macToInterfaceListMap = {}
        for interface in self.interfacesList:
            intList = macToInterfaceListMap.get(interface.macAddress, [])
            intList.append(interface)
            macToInterfaceListMap[interface.macAddress] = intList
        #checking if interface has a Team key word in it's name
        for interfaceList in macToInterfaceListMap.values():
            if interfaceList and len(interfaceList) < 2:
                continue
            for interf in interfaceList:
                if (interf.name and re.search('[Tt]eam', interf.name)) or (interf.description and re.search('[Tt]eam', interf.description)):
                    #picking up interface with max interfaceIndex value and setting it aggregate role
                    try:
                        iface = reduce(lambda x,y: int(x.interfaceIndex) > int(y.interfaceIndex) and x or y, interfaceList)
                        iface.role = 'aggregate_interface'
                    except:
                        logger.debugException('')

        interfacesVector = modeling.createInterfacesOSHV(self.interfacesList, self.hostOsh)
        roleManager = InterfaceRoleManager()
        for interface in self.interfacesList:
            interfaceOsh = interface.getOsh()
            if not interfaceOsh: continue
            roleManager.assignInterfaceRole(interface)
            self.resultVector.add(interfaceOsh)
            if interface.ips:
                for ipIndex in range(len(interface.ips)):
                    ipAddress = interface.ips[ipIndex]
                    ipMask = None
                    try:
                        ipMask = interface.masks[ipIndex]
                    except:
                        pass
                    if ipAddress.is_loopback or ipAddress.is_unspecified:
                        logger.debug('Invalid IP retrieved %s. Skipping.' % ipAddress)
                        continue
                    ipProp = modeling.getIpAddressPropertyValue(str(ipAddress), ipMask, interface.dhcpEnabled, interface.description)

                    ipOsh = modeling.createIpOSH(ipAddress, ipMask, None, ipProp)
                    if self.hostDo.ipIsVirtual and ipAddress == self.destinationIp:
                        ipOsh.setBoolAttribute('isvirtual', 1)
                    self.resultVector.add(ipOsh)
                    self.resultVector.add(modeling.createLinkOSH('containment', self.hostOsh, ipOsh))
                    self.resultVector.add(modeling.createLinkOSH('containment', interfaceOsh, ipOsh))
                    if ipAddress.version != 6:
                        networkOsh = modeling.createNetworkOSH(str(ipAddress), ipMask)
                        self.resultVector.add(networkOsh)
                        self.resultVector.add(modeling.createLinkOSH('member', networkOsh, ipOsh))
                        self.resultVector.add(modeling.createLinkOSH('member', networkOsh, self.hostOsh))
Exemple #7
0
    def buildInterfaces(self):
        """ We create and report such topology for destination ip in following cases:
                            |host is complete     | host is incomplete|
        ip is virtual        |all except NTCMD    |only virtual ip    |
        ip is not virtual    |all topology        |all topology        |
        """
        try:
            self.hostOsh = modeling.createCompleteHostOSHByInterfaceList(
                'nt', self.interfacesList, self.hostDo.hostOsName,
                self.hostDo.hostName, self.hostDo.lastBootDate,
                self.hostCmdbId, self.hostKey, self.hostMacs,
                self.ucmdbVersion)
            if self.hostDo.ipIsVirtual:
                logger.warn(
                    'IP: ', self.destinationIp,
                    ' does not appear in the result buffer of the interfaces output - assuming virtual'
                )
                logger.warn('Host topology will be reported without NTCMD CI.')
        except:
            self.hostOsh = modeling.createHostOSH(str(self.destinationIp),
                                                  'nt', self.hostDo.hostOsName,
                                                  self.hostDo.hostName)
            logger.warn('Could not find a valid MAC address for key on ip : ',
                        self.destinationIp)
            if self.hostDo.ipIsVirtual:
                logger.warn(
                    'IP: ', self.destinationIp,
                    ' does not appear in the result buffer of the interfaces output - assuming virtual'
                )
                logger.warn('Only IP marked as virtual will be reported.')
                self.resultVector.clear()
                virtualIpOsh = modeling.createIpOSH(str(self.destinationIp))
                virtualIpOsh.setBoolAttribute('isvirtual', 1)
                self.resultVector.add(virtualIpOsh)
                self.hostOsh = None
                return
        #process interfaces looking for nic teaming interfaces.
        #looking up for interfaces with same mac
        macToInterfaceListMap = {}
        for interface in self.interfacesList:
            intList = macToInterfaceListMap.get(interface.macAddress, [])
            intList.append(interface)
            macToInterfaceListMap[interface.macAddress] = intList
        #checking if interface has a Team key word in it's name
        for interfaceList in macToInterfaceListMap.values():
            if interfaceList and len(interfaceList) < 2:
                continue
            for interf in interfaceList:
                if (interf.name and re.search('[Tt]eam', interf.name)) or (
                        interf.description
                        and re.search('[Tt]eam', interf.description)):
                    #picking up interface with max interfaceIndex value and setting it aggregate role
                    try:
                        iface = reduce(
                            lambda x, y: int(x.interfaceIndex) > int(
                                y.interfaceIndex) and x or y, interfaceList)
                        iface.role = 'aggregate_interface'
                    except:
                        logger.debugException('')

        interfacesVector = modeling.createInterfacesOSHV(
            self.interfacesList, self.hostOsh)
        roleManager = InterfaceRoleManager()
        for interface in self.interfacesList:
            interfaceOsh = interface.getOsh()
            if not interfaceOsh: continue
            roleManager.assignInterfaceRole(interface)
            self.resultVector.add(interfaceOsh)
            if interface.ips:
                for ipIndex in range(len(interface.ips)):
                    ipAddress = interface.ips[ipIndex]
                    ipMask = None
                    try:
                        ipMask = interface.masks[ipIndex]
                    except:
                        pass
                    if ipAddress.is_loopback or ipAddress.is_unspecified:
                        logger.debug('Invalid IP retrieved %s. Skipping.' %
                                     ipAddress)
                        continue
                    ipProp = modeling.getIpAddressPropertyValue(
                        str(ipAddress), ipMask, interface.dhcpEnabled,
                        interface.description)

                    ipOsh = modeling.createIpOSH(ipAddress, ipMask, None,
                                                 ipProp)
                    if self.hostDo.ipIsVirtual and ipAddress == self.destinationIp:
                        ipOsh.setBoolAttribute('isvirtual', 1)
                    self.resultVector.add(ipOsh)
                    self.resultVector.add(
                        modeling.createLinkOSH('containment', self.hostOsh,
                                               ipOsh))
                    self.resultVector.add(
                        modeling.createLinkOSH('containment', interfaceOsh,
                                               ipOsh))
                    if ipAddress.version != 6:
                        networkOsh = modeling.createNetworkOSH(
                            str(ipAddress), ipMask)
                        self.resultVector.add(networkOsh)
                        self.resultVector.add(
                            modeling.createLinkOSH('member', networkOsh,
                                                   ipOsh))
                        self.resultVector.add(
                            modeling.createLinkOSH('member', networkOsh,
                                                   self.hostOsh))
def doSnmp(client, isClient, snmpOSH, ip_address, ip_domain, Framework, host_cmdbid, host_key, host_macs):
    '''SnmpClient, osh, str, str, Framework, str, str, list(str) -> ObjectStateHolderVector
    @deprecated
    '''
    networkList = []
    vector = ObjectStateHolderVector()
    ucmdb_version = modeling.CmdbClassModel().version()
    # system table
    snmpQuery = '1.3.6.1.2.1.1.1,1.3.6.1.2.1.1.2,string,1.3.6.1.2.1.1.2,string,1.3.6.1.2.1.1.5,string'
    table = client.executeQuery(snmpQuery)#@@CMD_PERMISION snmp protocol execution

    sysname = None
    oid = None
    description = None
    try:
        if table.next():
            description = table.getString(2)
            oid = table.getString(3)
            sysname = table.getString(4)
    except:
        logger.debugException('')

    node_description = description or ''

    sysObjId = oid
    className, vendorName, hostOsName, hostModel, serialNumberOid, serialNumberSNMPMethod = getNodeDetails(description, oid)
    logger.debug("Node details:%s, %s, %s, %s, %s, %s"%(className, vendorName, hostOsName, hostModel, serialNumberOid, serialNumberSNMPMethod))
    if className == 'netprinter':
        sysname = getNetPrinterName(client) or sysname
    hostVersion, hostRelease = getOsVersionAndBuild(hostOsName, description)

# since sysUpTime returns a time since the snmp was re-initialized, this
# time doesn't provide us correct answer, therefore this code is disabled
#    snmpQuery = '1.3.6.1.2.1.1.3,1.3.6.1.2.1.1.4,string'
#    upTime = client.executeQuery(snmpQuery)

    if oid != None:
        snmpOSH.setAttribute('snmp_oid', oid)
    if((sysname != None) and (sysname != '') and (sysname != 'unknown')):
        snmpOSH.setAttribute('snmp_sysname', sysname)
    if description != None:
        snmpOSH.setAttribute('snmp_description', description)

    logger.debug('ip_address: ', ip_address, ', sysname: ' , sysname, ', className: ', className, ', oid: ', oid, ', description: ', description)

    #dicovery arp cache available
    arp_available = SNMP_Networking_Utils.isArpCacheAvailable(client)
    snmpOSH.setBoolAttribute('arp_cache_available', arp_available)

    # indx, description & mac

    interfaceList = []
    interfaceDictionary = {}

    ifList = SNMP_Networking_Utils.discoverInterfaceData(client, None)

    for nic in ifList:
        if nic.ifType and int(nic.ifType) == 24:
            continue

        macAddress = str(nic.ifMac).upper()
        if nic.ifIndex != None and nic.ifIndex != '':
            inrfcindex = nic.ifIndex
        description = _stripVirtualMiniportInfo(nic.ifDescr)

        if interfaceDictionary.has_key(inrfcindex):
            logger.debug('this mac was already reported, skip it ... : inrfcindex: ', inrfcindex, ', descrition: ', description, ', macAddress: ', macAddress)
            continue

        logger.debug('inrfcindex: ', inrfcindex, ', description: ', description, ', macAddress: ', macAddress)

        interfaceDictionary[inrfcindex] = macAddress
        networkinterface = modeling.NetworkInterface(description, macAddress, None, None, inrfcindex)
        networkinterface.type = nic.ifType
        networkinterface.adminStatus = nic.ifAdminStatus
        networkinterface.operStatus = nic.ifOperStatus
        networkinterface.speed = nic.ifSpeed
        networkinterface.name = nic.ifName
        networkinterface.alias = nic.ifAlias
        if not networkinterface.name:
            m = re.match('(lan[\d\:\.]+)', description)
            if m:
                networkinterface.name = m.group(1)
                networkinterface.description = None
        interfaceList.append(networkinterface)

    # create the host and all the objects
    if len(interfaceList) > 0:
        macToInterfaceListMap = {}
        for interface in interfaceList:
            macToInterfaceListMap.setdefault(interface.macAddress, []).append(interface)

        for ifaceList in macToInterfaceListMap.values():
            if ifaceList and len(ifaceList) < 2:
                continue
            try:
                iface = reduce(lambda x,y: x.interfaceIndex > y.interfaceIndex and x or y, ifaceList)
                iface.role = 'aggregate_interface'
            except:
                logger.debugException('')
        hostOSH = None

        try:
            # Get the host_key - lowest mac address of the valid interface list
            hostOSH = modeling.createCompleteHostOSHByInterfaceList(className, interfaceList, None, None, None, host_cmdbid, host_key, host_macs, ucmdb_version)
            if (className in netDeviceClasses) and (sysname != None) and (sysname != '') and ((host_cmdbid in ['NA','',None]) or (host_key and (host_key.lower() == sysname.lower()))):
                # JEO @ Fidelity: use SNMP system name for host key of network devices unless its null
                hostOSH.setAttribute('host_key', sysname)

        except:
            logger.debug('Could not find a valid MAC address for key on ip : ', ip_address)
            if (className in netDeviceClasses) and (sysname != None) and (sysname != '') and (host_cmdbid in ['NA','',None]):
                logger.debug('Network device, hostkey is sysname...')
                hostOSH = modeling.createCompleteHostOSH(className, sysname)
            else:
                logger.debug('Creating incomplete host...')
                hostOSH = modeling.createHostOSH(ip_address, className)

        logger.debug('Created [' + className + '] strong key=[' + hostOSH.getAttributeValue('host_key') + ']')

        if((sysname != None) and (sysname != '') and (sysname != 'unknown')):
            hostOSH.setAttribute('snmp_sys_name', sysname)
            # set hostname to SNMP system name less domain suffix which is typical of other data sources
            hostname = sysname.split('.')[0].lower()
            hostOSH.setAttribute('name', hostname)
            logger.debug("hostname=" + hostname)

        defaultGateway = getDefaultGateway(client)
        modeling.setHostDefaultGateway(hostOSH, defaultGateway)
        modeling.setHostOsFamily(hostOSH, None, className)
        if sysObjId and sysObjId.startswith('.') != -1:
            sysObjId = "." + sysObjId
        modeling.setSNMPSysObjectId(hostOSH, sysObjId)

        if((hostOsName != None) and (hostOsName != '') and (hostOsName != 'unknown')):
            modeling.setHostOsName(hostOSH, hostOsName)
        if((vendorName != None) and (vendorName != '') and (vendorName != 'unknown')):
            hostOSH.setAttribute('discovered_os_vendor', vendorName)
        if((hostModel != None) and (hostModel != '') and (hostModel != 'unknown')):
            hostOSH.setAttribute('discovered_model', hostModel)
        if hostRelease is not None:
            hostOSH.setAttribute('host_osrelease', hostRelease)
        if hostVersion is not None:
            hostOSH.setAttribute('discovered_os_version', hostVersion)

# since sysUpTime returns a time since the snmp was re-initialized, this
# time doesn't provide us correct answer, therefore this code is disabled
#        if upTime:
#            today = Calendar.getInstance().getTime()
#            setLastBootDate(hostOSH, upTime, today, Framework)

        vector.add(modeling.finalizeHostOsh(hostOSH))
        snmpOSH.setContainer(hostOSH)
        if className == 'mainframe':
            modeling.setHostOsFamily(hostOSH, 'mainframe')

        interfaceOshToIndex = {}
        roleManager = networking_win.InterfaceRoleManager()
        try:
            reportInterfaceName = Boolean.parseBoolean(Framework.getParameter('reportInterfaceName'))
        except:
            logger.warn('Failed to parse reportInterfaceName parameter falue. Not a Boolean type.')
            reportInterfaceName = True
        for nic in interfaceList:
            if netutils.isValidMac(nic.macAddress):
                nic.osh = modeling.createInterfaceOSH(nic.macAddress, hostOSH, nic.description,
                            nic.interfaceIndex, nic.type, nic.adminStatus, nic.operStatus,
                            nic.speed, nic.name, nic.alias, reportInterfaceName)
                roleManager.assignInterfaceRole(nic)
                interfaceOshToIndex[nic.interfaceIndex] = nic.osh
                vector.add(nic.osh)
            else:
                logger.debug('MAC %s is invalid (name: %s, description: %s, index: %s)' %
                             (nic.macAddress, nic.name, nic.description, nic.interfaceIndex))

        # create the ip's
        logger.debug("create the ip's")
        snmpQuery = '1.3.6.1.2.1.4.20.1.1, 1.3.6.1.2.1.4.20.1.2, string'
        table = client.executeQuery(snmpQuery)#@@CMD_PERMISION snmp protocol execution
        ips = table.asTable()
        snmpQuery = '1.3.6.1.2.1.4.20.1.2, 1.3.6.1.2.1.4.20.1.3, string'
        table = client.executeQuery(snmpQuery)#@@CMD_PERMISION snmp protocol execution
        ifindexes = table.asTable()
        snmpQuery = '1.3.6.1.2.1.4.20.1.3, 1.3.6.1.2.1.4.20.1.4, string'
        table = client.executeQuery(snmpQuery)#@@CMD_PERMISION snmp protocol execution
        ipmasks = table.asTable()
        try:
            rfcIpV6Query = '1.3.6.1.2.1.55.1.8.1.1,1.3.6.1.2.1.55.1.8.1.5, string'
            rfcIpV6Result = client.executeQuery(rfcIpV6Query)#@@CMD_PERMISION snmp protocol execution
            results = rfcIpV6Result.asTable()
        except:
            logger.warn('Failed to get or process IPv6 MIB results.')

        #try to collect IPv6 addresses from IPv6 address table and connect them to corresponding interface
        logger.debug("Begin to discovery IPv6 related info")
        try:
            ipv6Map = {}
            ipv6AddressTable = SNMP_Networking_Utils.discoverIPv6AddressTable(client)
            if ipv6AddressTable:
                for row in ipv6AddressTable:
                    if row.ipv6AddrStatus != '1':# 1 means valid ip address in arp cache
                        continue
                    ifIndex, ipAddress = _parseIPv6FromIndex(row.ipv6AddrAddress)
                    if ifIndex and ipAddress:
                        try:
                            formatedIP = getValidIP(ipAddress)
                            if formatedIP:
                                ipv6Map[ifIndex] = formatedIP
                        except:
                            pass
            mixedIPAddressTable = SNMP_Networking_Utils.discoverMixedIPAddressTable(client)
            if mixedIPAddressTable:
                for row in mixedIPAddressTable:
                    if row.ipAddressStatus != '1' or row.ipAddressRowStatus != '1':# 1 means valid ip address in arp cache
                        continue
                    ifIndex = row.ipAddressIfIndex
                    ipAddress = _parseMixedIPFromIndex(row.ipAddressAddr)
                    if ifIndex and ipAddress:
                        try:
                            formatedIP = getValidIP(ipAddress)
                            if formatedIP and formatedIP.version == 6:
                                ipv6Map[ifIndex] = formatedIP
                        except:
                            pass

            if ipv6Map and interfaceOshToIndex:
                for ifIndex in ipv6Map.keys():
                    ipv6 = ipv6Map[ifIndex]
                    logger.debug("Discovered IPv6:", ipv6)
                    ipOSH = modeling.createIpOSH(ipv6)
                    vector.add(ipOSH)
                    interfaceOsh = interfaceOshToIndex.get(str(ifIndex))
                    if not interfaceOsh and isClient:
                        logger.info('client ip is not associated with an interface')
                        msg = "Can not find the related interface for client IP: %s" % ip_address
                        logger.reportWarningObject(errormessages.resolveError(msg, 'SNMP'))
                        continue
                    if interfaceOsh:
                        parent = modeling.createLinkOSH('containment', interfaceOsh, ipOSH)
                        vector.add(parent)
                        interfaceMacAddress = interfaceOsh.getAttributeValue('mac_address')
                        if (isClient == TRUE):
                            ipOSH.setAttribute('arp_mac', interfaceMacAddress)
                            if (
                            ipv6 == ip_addr_util.IPAddress(ip_address)):#compare ipv6 by the formatting to avoid same IPv6 with different format
                                snmpOSH.setAttribute('arp_mac', interfaceMacAddress)

                    isCompleteAttribute = hostOSH.getAttribute('host_iscomplete')
                    if isCompleteAttribute is not None and isCompleteAttribute.getBooleanValue() == 1:
                        contained = modeling.createLinkOSH('contained', hostOSH, ipOSH)
                        vector.add(contained)
        except:
            logger.debug(str(sys.exc_info()))

        index = 0
        if len(ips) > 0 and len(ifindexes) > 0:
            for ip in ips:
                try:
                    ip_addr = ip[1]
                    #logger.debug('candidate ip_addr: ', ip_addr)
                    ifIndex = ifindexes[index][1]
                    mask = ipmasks[index][1]
                    index += 1

                    interfaceOsh = interfaceOshToIndex.get(ifIndex)

                    ''' this commented out block should be removed
                    # no such thing as netmask IPs, there are broadcast IPs which have .255 in them
                    # but we would definitely want to discover and nic which was asssigned
                    # such an address -
                    #if(ip_addr.find('255.') == 0 ):
                        #exclude netmask ip
                        #continue
                    '''

                    if netutils.isValidIp(ip_addr) and (not netutils.isLocalIp(ip_addr)):
                        '''
                        netutils.isLocalIp is not finding all local addresses
                        127.0.0.0 through 127.255.255.255 are local
                        additional if clause can be removed when this is fixed
                        see http://www.tcpipguide.com/free/t_IPReservedPrivateandLoopbackAddresses.htm
                        '''
                        # ip is valid and not local (i.e. 0.0.0.0, 127.0.0.1)
                        ipOSH = modeling.createIpOSH(ip_addr, mask)
                    else:
                        # loopbacks are standard; don't complain about them in the logs
                        continue
                    logger.debug('ip_addr: ', ip_addr, ', mask: ', mask)

                    netOSH = modeling.createNetworkOSH(ip_addr, mask)

                    strNetAddr = str(netOSH.getAttribute('network_netaddr'))
                    strNetMask = str(netOSH.getAttribute('network_netMask'))
                    currNet = strNetAddr + strNetMask

                    broadcastIp = netOSH.getAttributeValue('network_broadcastaddress')
                    if ip_address == broadcastIp:
                        raise BroadcastIpDiscoveryException()
                    if not interfaceOsh and isClient:
                        logger.info('client ip is not associated with an interface')
                        msg = "Can not find the related interface for client IP: " + ip_addr
                        logger.reportWarningObject(errormessages.resolveError(msg, 'SNMP'))
                        continue
                    if interfaceOsh:
                        parent = modeling.createLinkOSH('containment', interfaceOsh, ipOSH)
                        vector.add(parent)
                        interfaceMacAddress = interfaceOsh.getAttributeValue('mac_address')
                        if (isClient == TRUE):
                            ipOSH.setAttribute('arp_mac', interfaceMacAddress)
                            if (ip_addr == ip_address):
                                snmpOSH.setAttribute('arp_mac', interfaceMacAddress)

                    member1 = modeling.createLinkOSH('member', netOSH, ipOSH)
                    member2 = modeling.createLinkOSH('member', netOSH, hostOSH)

                    if currNet in networkList:
                        pass
                    else:
                        networkList.append(currNet)
                        vector.add(netOSH)
                        vector.add(member2)

                    vector.add(ipOSH)
                    vector.add(member1)

                    # link IPs to host only in case host is complete
                    # otherwise reconciliation may fail
                    isCompleteAttribute = hostOSH.getAttribute('host_iscomplete')
                    if isCompleteAttribute is not None and isCompleteAttribute.getBooleanValue() == 1:
                        contained = modeling.createLinkOSH('contained', hostOSH, ipOSH)
                        vector.add(contained)

                    if interfaceOsh:
                        parent = modeling.createLinkOSH('containment', interfaceOsh, ipOSH)
                        vector.add(parent)
                except BroadcastIpDiscoveryException, ex:
                    raise ex
                except:
                    pass
def createHost(discoverNonL2Devices, hostId, ndMap, maclist = None, ipValue = None):

	if not ndMap.has_key(hostId):
		return

	hostObj = ndMap[hostId]
	name = hostObj.name

	host_class = 'node'
	## figure out the host class if possible...
	if notNull(hostObj.deviceCategory) and NNM_TO_UCMDB.has_key(hostObj.deviceCategory):
		host_class = NNM_TO_UCMDB[hostObj.deviceCategory]
	else:
		host_class = 'node'

	## figure out the device category if node capabilities are set
	isSwitchRouter = 0
	if hostObj.isLanSwitch and hostObj.isRouter:
		isSwitchRouter = 1

	if isSwitchRouter:
		host_class = NNM_TO_UCMDB['switchrouter']
	elif hostObj.isLanSwitch:
		host_class = NNM_TO_UCMDB['switch']
	elif hostObj.isRouter:
		host_class = NNM_TO_UCMDB['router']

	isL2Dev = 1
	if host_class == 'node':
		isL2Dev = 0

	create = 1
	hostOSH = None
	if not isL2Dev and not discoverNonL2Devices: # not a L2 device and nonL2 devices flag is set
		create = 0
	else:
		if maclist != None and len(maclist) > 0:
			try:
				hostOSH = modeling.createCompleteHostOSHByInterfaceList(host_class, maclist, '', name)
			except:
				create = 0 # unable to create host in modeling
		else:
			## no mac list sent. try to create a host with the IP address
			if notNull(ipValue):
				hostOSH = modeling.createHostOSH(ipValue, host_class, '', name)
			else:
				## no ip address sent. return None
				create = 0

	if create and hostOSH:
		for (nnmVar, hostVar) in NODE_ATT_NNM_TO_UCMDB.items():
			if nnmVar in UNODE_VARS:
				val = getattr(hostObj, nnmVar)
				if not notNull(val):
					val = ''
				hostOSH.setAttribute(hostVar, val)

		## if host_class belongs to a network device, set the systemName as the host_key
		snmp_sys_name = getattr(hostObj, 'systemName')
		if (host_class in netDeviceClasses) and notNull(snmp_sys_name):
			hostOSH.setAttribute('host_key', snmp_sys_name)
			hostOSH.setBoolAttribute('host_iscomplete',1)

		hostOSH = finalizeHostOsh(hostOSH)
	else:
		hostOSH = None

	return hostOSH
Exemple #10
0
def createVirtHostOsh(config):
    vHostOsh = modeling.createCompleteHostOSHByInterfaceList(
        'host', config.interfaceList)
    return HostBuilder(vHostOsh).setAsVirtual(1).build()