def processIpsAndSubnets(_vector, hostIpMap, hostId, ipMap, nwMap, hostOSH):
	if hostIpMap.has_key(hostId):
		iplist = hostIpMap[hostId]
		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)
				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))
						_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)
	return _vector
示例#2
0
def setObjectVectorByResStringArray(objectVector,
                                    ipsResult,
                                    virtualMode,
                                    netAddress=None,
                                    netMask=None):
    # create new network for link object
    if netAddress and netMask:
        networkOSHForLink = modeling.createNetworkOSH(netAddress, netMask)
    else:
        networkOSHForLink = None
    # run on the string array (Holding all live pinged ips)
    for ipResult in ipsResult:
        isVirtual = 0
        # pingedIp - the ip we sent the ping to
        # replyIp - the ip replied to the ping

        # Break the curr result by ':' <Reply-IP>:<Pinged-IP>
        token = ipResult.split(':')

        if (len(token) == 2):
            # In case where we ping a virtual ip we get the reply from the real ip
            # If we are pinging a virtual ip
            if virtualMode:
                replyIp, pingedIp = token[0], token[1]
                isVirtual = 1
            else:
                replyIp, pingedIp = token[1], token[1]
        else:
            replyIp, pingedIp = ipResult, ipResult

        # Create Ip OSH and add to vector
        pingedIpOSH = modeling.createIpOSH(ip_addr.IPAddress(pingedIp),
                                           netMask)
        objectVector.add(pingedIpOSH)

        if networkOSHForLink:
            # Create MEMBER link and set end1(discovered network) and end2(host)
            objectVector.add(
                modeling.createLinkOSH('member', networkOSHForLink,
                                       pingedIpOSH))

        if isVirtual:
            # Create Ip OSH
            replyIpOSH = modeling.createIpOSH(replyIp, netMask)

            # Create a depend  link and set end1(pingedIp) and end2(replyIp)
            newDependLink = modeling.createLinkOSH('depend', pingedIpOSH,
                                                   replyIpOSH)

            objectVector.add(replyIpOSH)
            objectVector.add(newDependLink)

            if networkOSHForLink:
                replyIpNetAddress = IPv4(replyIp,
                                         netMask).getFirstIp().toString()
                if replyIpNetAddress == netAddress:
                    # Create MEMBER link and set end1(discovered network) and end2(host)
                    objectVector.add(
                        modeling.createLinkOSH('member', networkOSHForLink,
                                               replyIpOSH))
示例#3
0
def DiscoveryMain(Framework):
    OSHVResult = ObjectStateHolderVector()
    snmpMethod = getFrameworkParameter(Framework, 'snmpMethod', SnmpQueries.defaultSnmpMethod)
    backupSnmpMethod = getFrameworkParameter(Framework, 'backupSnmpMethod', SnmpQueries.defaultBackupSnmpMethod)
    moonWalkBulkSize = int(getFrameworkParameter(Framework, 'moonWalkBulkSize', SnmpQueries.defaultMoonWalkBulkSize))
    moonWalkSleep = long(getFrameworkParameter(Framework, 'moonWalkSleep', SnmpQueries.defaultMoonWalkSleep))
    snmpBulkSize = int(getFrameworkParameter(Framework, 'snmpBulkSize', SnmpQueries.defaultSnmpBulkSize))
    discoverUnknownIPs = Boolean.parseBoolean(Framework.getParameter('discoverUnknownIPs'))
    
    #getting DestinationData from Framework; the method is protected.
    destination = Framework.getCurrentDestination()
    
    discoveredHostIpList = SnmpQueries.getSnmpIpDataOneDestination(snmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, destination)
    logger.debug('Discover ARP by %s returned %s objects' % (snmpMethod, str(discoveredHostIpList.size())))
    if (discoveredHostIpList.size() == 0) and (snmpMethod != backupSnmpMethod):
        discoveredHostIpList = SnmpQueries.getSnmpIpDataOneDestination(backupSnmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, destination)
        logger.debug('Discover ARP by %s returned %s objects' % (backupSnmpMethod, str(discoveredHostIpList.size())))
        if (discoveredHostIpList.size()==0):
            Framework.reportWarning('Failed to discover SNMP IP data')
            return OSHVResult
    
    discoveredHostArpList = SnmpQueries.getSnmpArpDataOneDestination(snmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, destination)
    discoveredHostArpList.addAll(SnmpQueries.getSnmpArpDataOneDestination(snmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, Boolean.TRUE, destination))
    if (discoveredHostArpList.size()==0) and (snmpMethod != backupSnmpMethod):
        discoveredHostArpList = SnmpQueries.getSnmpArpDataOneDestination(backupSnmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, destination)
        discoveredHostArpList.addAll(SnmpQueries.getSnmpArpDataOneDestination(backupSnmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, Boolean.TRUE, destination))
        if (discoveredHostArpList.size()==0):
            Framework.reportWarning('Failed to discover SNMP ARP data')
            return OSHVResult

    networkOSH = None
    for i in range(discoveredHostArpList.size()):
        currArp = discoveredHostArpList.get(i)
        for currIp in discoveredHostIpList:
            if networkOSH is None:
                networkOSH = modeling.createNetworkOSH(currIp.netaddr, currIp.netmask)
                OSHVResult.add(networkOSH)
            if (currIp.domain == 'unknown') and not discoverUnknownIPs:
                continue
            if not netutils.isValidMac(currArp.designatedMacAddress):
                continue
            
            #report start
            designatedIpNetAddress = IPv4(currArp.designatedIpAddress, currIp.netmask).getFirstIp().toString();
            if designatedIpNetAddress == currIp.netaddr:
                hostOSH = modeling.createHostOSH(currArp.designatedIpAddress)
                OSHVResult.add(hostOSH)
                OSHVResult.add(modeling.createLinkOSH('member', networkOSH, hostOSH))
                
                ipOsh = modeling.createIpOSH(currArp.designatedIpAddress, currIp.netmask)
                OSHVResult.add(ipOsh)
                OSHVResult.add(modeling.createLinkOSH('member', networkOSH, ipOsh))
                
                ifOsh = modeling.createInterfaceOSH(netutils.parseMac(currArp.designatedMacAddress), hostOSH)
                OSHVResult.add(ifOsh)
                OSHVResult.add(modeling.createLinkOSH('containment', ifOsh, ipOsh))
    
    return OSHVResult
示例#4
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
示例#5
0
def reportNatedNetworks(networks, container_osh):
    vector = ObjectStateHolderVector()
    if not networks:
        return vector
    for network in networks:
        network_osh = modeling.createNetworkOSH(network.ip , network.mask)
        vector.add(network_osh)
        link_osh = modeling.createLinkOSH('route', container_osh, network_osh)
        vector.add(link_osh)
    return vector
示例#6
0
def reportNatedNetworks(networks, container_osh):
    vector = ObjectStateHolderVector()
    if not networks:
        return vector
    for network in networks:
        network_osh = modeling.createNetworkOSH(network.ip, network.mask)
        vector.add(network_osh)
        link_osh = modeling.createLinkOSH('route', container_osh, network_osh)
        vector.add(link_osh)
    return vector
 def createOsh(self, subnetDto, credentialId=None):
     osh = modeling.createNetworkOSH(subnetDto.ipAddress, subnetDto.netMask)
     if subnetDto.description:
         osh.setAttribute('data_description', subnetDto.description)
     if credentialId:
         osh.setAttribute('credentials_id', credentialId)
     osh.setAttribute('data_name', subnetDto.ipAddress)
     osh.setAttribute('network_netaddr', subnetDto.ipAddress)
     osh.setAttribute('network_netmask', subnetDto.netMask)
     return osh
 def createOsh(self, subnetDto, credentialId=None):
     osh = modeling.createNetworkOSH(subnetDto.ipAddress, subnetDto.netMask)
     if subnetDto.description:
         osh.setAttribute('data_description', subnetDto.description)
     if credentialId:
         osh.setAttribute('credentials_id', credentialId)
     osh.setAttribute('data_name', subnetDto.ipAddress)
     osh.setAttribute('network_netaddr', subnetDto.ipAddress)
     osh.setAttribute('network_netmask', subnetDto.netMask)
     return osh
示例#9
0
def setObjectVectorByResStringArray(objectVector, ipsResult, virtualMode, netAddress=None, netMask=None):
    # create new network for link object
    if netAddress and netMask:
        networkOSHForLink = modeling.createNetworkOSH(netAddress, netMask)
    else:
        networkOSHForLink = None
    # run on the string array (Holding all live pinged ips)
    for ipResult in ipsResult:
        isVirtual = 0
        # pingedIp - the ip we sent the ping to
        # replyIp - the ip replied to the ping

        # Break the curr result by ':' <Reply-IP>:<Pinged-IP>
        token = ipResult.split(':')

        if (len(token) == 2):
            # In case where we ping a virtual ip we get the reply from the real ip
            # If we are pinging a virtual ip
            if virtualMode:
                replyIp, pingedIp = token[0], token[1]
                isVirtual = 1
            else:
                replyIp, pingedIp = token[1], token[1]
        else:
            replyIp, pingedIp = ipResult, ipResult

        # Create Ip OSH and add to vector
        pingedIpOSH = modeling.createIpOSH(ip_addr.IPAddress(pingedIp), netMask)
        objectVector.add(pingedIpOSH)

        if networkOSHForLink:
            # Create MEMBER link and set end1(discovered network) and end2(host)
            objectVector.add(modeling.createLinkOSH('member', networkOSHForLink, pingedIpOSH))

        if isVirtual:
            # Create Ip OSH
            replyIpOSH = modeling.createIpOSH(replyIp, netMask)

            # Create a depend  link and set end1(pingedIp) and end2(replyIp)
            newDependLink = modeling.createLinkOSH('depend', pingedIpOSH, replyIpOSH)

            objectVector.add(replyIpOSH)
            objectVector.add(newDependLink)

            if networkOSHForLink:
                replyIpNetAddress = IPv4(replyIp, netMask).getFirstIp().toString()
                if replyIpNetAddress == netAddress:
                    # Create MEMBER link and set end1(discovered network) and end2(host)
                    objectVector.add(modeling.createLinkOSH('member', networkOSHForLink, replyIpOSH))
示例#10
0
def createIpsNetworksOSHV(ipList, sysTable, hostId, hostIsComplete):
    ipsAndNetworksOSHV = ObjectStateHolderVector()
    isRoute = checkIsRoute(ipList)
    hostOSH = modeling.createOshByCmdbId(sysTable.sysClass, hostId)
    builder = HostBuilder(hostOSH)
    
    builder.setAsRouter(isRoute, __NOT_SET_ROLE)    
    if str(sysTable.sysModel).lower() != 'unknown':
        builder.setStringAttribute("host_model", sysTable.sysModel)
    if str(sysTable.sysOs).lower() != 'unknown':
        builder.setOsName(sysTable.sysOs)
    if str(sysTable.sysVendor).lower() != 'unknown':
        builder.setStringAttribute("host_vendor", sysTable.sysVendor)
    if sysTable.sysName != None and str(sysTable.sysName).lower() != 'unknown':
        builder.setStringAttribute("host_snmpsysname", sysTable.sysName)
    if sysTable.sysNodeName is not None:
        builder.setStringAttribute("host_hostname", sysTable.sysNodeName)
    hostOSH = builder.build()
    ipsAndNetworksOSHV.add(hostOSH)
    
    for ip in ipList:
        if not isValidIP(ip):
            continue
        #create ip object
        ipOSH = modeling.createIpOSH(ip.ipAddr, ip.ipNetMask)
        
        #create network object
        networkOSH = modeling.createNetworkOSH(ip.ipAddr, ip.ipNetMask)
        if ip.nettype != None and int(ip.nettype) > 0:
            networkOSH.setEnumAttribute("network_nettype", int(ip.nettype))
        
        #create member link object ( end1(network) and end2(ip) )
        memberLinkOSHIpNetwork = modeling.createLinkOSH("member", networkOSH, ipOSH)
        
        #create member link object ( end1(network) and end2(host) )
        memberLinkOSHHostNetwork = modeling.createLinkOSH("member", networkOSH, hostOSH)
        
        #create contained link object ( end1(host) and end2(ip) )
        if Boolean.parseBoolean(hostIsComplete):
            containedLinkOSHIpHost = modeling.createLinkOSH("contained", hostOSH, ipOSH)
            ipsAndNetworksOSHV.add(containedLinkOSHIpHost)
        
        ipsAndNetworksOSHV.add(ipOSH)
        ipsAndNetworksOSHV.add(networkOSH)
        ipsAndNetworksOSHV.add(memberLinkOSHIpNetwork)
        ipsAndNetworksOSHV.add(memberLinkOSHHostNetwork)
    
    return ipsAndNetworksOSHV
示例#11
0
def osh_createNetworkOsh(lparOsh, tcpStacks):
    # tcpStacks  [ip, network, mask, interface name, status, type, mac address]
    _vector = ObjectStateHolderVector()
    for mac, tcpentry in tcpStacks.items():
        networkAddress = tcpentry[1].strip()
        ipAddress = tcpentry[0].strip()
        mask = tcpentry[2].strip()
        ipOsh = modeling.createIpOSH(ipAddress)
        netOsh = modeling.createNetworkOSH(networkAddress, mask)
        memberOsh = modeling.createLinkOSH('membership', netOsh, lparOsh)
        _vector.add(lparOsh)
        _vector.add(netOsh)
        _vector.add(memberOsh)
        memberOsh = modeling.createLinkOSH('membership', netOsh, ipOsh)
        _vector.add(memberOsh)
    return _vector
示例#12
0
def osh_createNetworkOsh(lparOsh, tcpStacks):
    # tcpStacks  [ip, network, mask, interface name, status, type, mac address]
    _vector = ObjectStateHolderVector()
    for mac, tcpentry in tcpStacks.items():
        networkAddress = tcpentry[1].strip()
        ipAddress = tcpentry[0].strip()
        mask = tcpentry[2].strip()
        ipOsh = modeling.createIpOSH(ipAddress)
        netOsh = modeling.createNetworkOSH(networkAddress, mask)        
        memberOsh = modeling.createLinkOSH('membership', netOsh, lparOsh)         
        _vector.add(lparOsh)
        _vector.add(netOsh)
        _vector.add(memberOsh)
        memberOsh = modeling.createLinkOSH('membership',  netOsh, ipOsh)
        _vector.add(memberOsh)          
    return _vector
示例#13
0
def getIPNetworkMemebrList(ipList, netmaskList, dnsname='', dhcpEnabled=None, description=None):
    _ipList = []
    _maskList = []
    _ipOshList = []
    vec = ObjectStateHolderVector()
    if ipList:
        _ipList = str(ipList).split(',')
    if netmaskList:
        _maskList = str(netmaskList).split(',')

    for i in range(len(_ipList)):
        try:
            try:
                ip = ip_addr.IPAddress(_ipList[i])
                mask = _maskList[i]
                if ip.is_loopback or ip.is_unspecified:
                    # invalid or local ip
                    raise ValueError()
            except:
                logger.debug('ignoring invalid ip=%s' % ip)
                continue

            # ip is valid and not local (i.e. 0.0.0.0, 127.0.0.1)
            # create an IP
            ipProps = modeling.getIpAddressPropertyValue(str(ip), mask, dhcpEnabled, description)
            ipOSH = modeling.createIpOSH(ip, mask, None, ipProps)
            vec.add(ipOSH)
            if ip.version != 6:
                # create network
                netOSH = modeling.createNetworkOSH(str(ip), mask)
                link = modeling.createLinkOSH('member', netOSH, ipOSH)
                vec.add(netOSH)
                vec.add(link)
        except:
            logger.errorException("Failed parsing IP: ", ip)

    return vec
示例#14
0
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
示例#15
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
示例#16
0
def getIPOSHV(Framework,
              ip,
              netmask=None,
              dnsServers=None,
              failPing=False,
              failDns=False):
    OSHVResult = ObjectStateHolderVector()
    if not ip:
        return OSHVResult

    ip = str(ip)
    localShell = None

    #if Framework.getParameter('dnsServers'):
    #    dnsServers = [dnsServer for dnsServer in dnsServers.split(',') if dnsServer and dnsServer.strip()] or None

    try:
        flag = False
        probeName = getProbeName(ip)
        ipsResult = [ip]
        if Framework:
            client = None
            try:
                localShell = shellutils.ShellUtils(
                    Framework.createClient(
                        ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME))
                client = Framework.createClient(
                    ClientsConsts.ICMP_PROTOCOL_NAME, Properties())

                if probeName:
                    ipsResult = client.executePing([ip])
                    if not ipsResult:
                        raise ValueError("No reply from ping")
                    logger.debug("ping result for %s: %s" % (ip, ipsResult))
            except:
                ipsResult = [ip]
                #logger.reportWarning("No reply from ping")
                logger.warnException("Error pinging address %s: " % (ip))
                flag = True

            if client is not None:
                try:
                    client.close()
                    client = None
                except:
                    pass

        # run on the string array (Holding all live pinged ips)

        if flag and failPing:
            logger.reportWarning("No reply from ping")
        else:
            for ipResult in ipsResult:
                isVirtual = 0
                # pingedIp - the ip we sent the ping to
                # replyIp - the ip replied to the ping

                # Break the curr result by ':' <Reply-IP>:<Pinged-IP>
                token = ipResult.split(':')

                if (len(token) >= 2):
                    # In case where we ping a virtual ip we get the reply from the real ip
                    # If we are pinging a virtual ip

                    replyIp, pingedIp = token[0], token[1]
                    isVirtual = 1
                else:
                    replyIp, pingedIp = ipResult, ipResult

                dnsName, alias = getHostNamesFromShell(pingedIp, localShell,
                                                       dnsServers)

                if not dnsName:
                    #logger.reportWarning("Cannot resolve hostname")
                    if failDns:
                        raise ValueError("Cannot find FQDN for IP")

                pingedIpOSH = modeling.createIpOSH(pingedIp, netmask, dnsName)
                # Create Ip OSH and add to vector

                OSHVResult.add(pingedIpOSH)
                #if probeName:
                #    hostOsh = modeling.createHostOSH(pingedIp, 'node', None, dnsName)
                #    OSHVResult.add(modeling.createLinkOSH('containment', hostOsh, pingedIpOSH))

                networkOSHForLink = None
                try:
                    if netmask:
                        netAddress = str(
                            IPFactory.getNetAddress(pingedIp, netmask))
                        if netmask and netAddress:
                            pingedIpOSH.setAttribute("ip_netaddr", netAddress)
                            networkOSHForLink = modeling.createNetworkOSH(
                                netAddress, netmask)
                            OSHVResult.add(networkOSHForLink)
                            OSHVResult.add(
                                modeling.createLinkOSH('member',
                                                       networkOSHForLink,
                                                       pingedIpOSH))

                except:
                    logger.warnException(
                        "Error getting net address for %s / %s: " %
                        (ip, netmask))

                if isVirtual:
                    # Create Ip OSH
                    dnsName, alias = getHostNamesFromShell(
                        replyIp, localShell, dnsServers)

                    replyIpOSH = modeling.createIpOSH(replyIp, netmask,
                                                      dnsName)

                    replyIpOSH.setBoolAttribute("isvirtual", True)
                    # Create a depend  link and set end1(pingedIp) and end2(replyIp)
                    newDependLink = modeling.createLinkOSH(
                        'depend', pingedIpOSH, replyIpOSH)

                    OSHVResult.add(replyIpOSH)
                    OSHVResult.add(newDependLink)

                    if networkOSHForLink:
                        try:
                            replyIpNetAddress = str(
                                IPFactory.getNetAddress(replyIp, netmask))
                            if replyIpNetAddress == netAddress:
                                # Create MEMBER link and set end1(discovered network) and end2(host)
                                OSHVResult.add(
                                    modeling.createLinkOSH(
                                        'member', networkOSHForLink,
                                        replyIpOSH))
                                replyIpOSH.setAttribute(
                                    "ip_netaddr", netAddress)
                        except:
                            logger.warnException(
                                "Error getting net address for %s / %s: " %
                                (replyIp, netmask))

    except:
        logger.errorException("Error getting DNS info for %s: " % (ip))
    finally:
        if localShell is not None:
            try:
                localShell.close()
                localShell = None
            except:
                pass

    return OSHVResult
示例#17
0
 def build(self):
     if isinstance(self.ip, type("")) or isinstance(self.ip, type(u"")) or (self.ip and self.ip.version == 4):
         self.osh = modeling.createNetworkOSH(str(self.ip), self.netmask)
示例#18
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))
示例#19
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))
示例#20
0
def createRouteObjects(routeList, ifList, ip_address, host_id):
    routeArrayList = []
    routeObjects = ObjectStateHolderVector()
    
    for route in routeList:
        if route.ipRouteType and int(route.ipRouteType) != 4:
            continue
        if str(route.ipRouteNextHop).startswith('0.') or str(route.ipRouteNextHop).startswith('127.'):
            continue  
        if route.ipRouteIfIndex == 0:
            #Host (next hop)
            nextHopHostOSH = __createRouterIncompleteHostByIp(route.ipRouteNextHop)
            #Ip (next hop)
            nextHopIpOSH = modeling.createIpOSH(route.ipRouteNextHop)
            routeObjects.add(nextHopHostOSH)
            routeObjects.add(nextHopIpOSH)
            
        currIf = getInterfaceByIndex(ifList, route.ipRouteIfIndex)
            
        if not currIf:
            continue
        if len(currIf.ipList) == 0 or currIf.ipList[0].netaddr == None:
            #Host (next hop)
            nextHopHostOSH = __createRouterIncompleteHostByIp(route.ipRouteNextHop)
            #Ip (next hop)
            nextHopIpOSH = modeling.createIpOSH(route.ipRouteNextHop)
            discoveredHostOSH = modeling.createOshByCmdbId("host", host_id)
                
            unnumberedLinkOSHHostHost = modeling.createLinkOSH("unnumbered", discoveredHostOSH, nextHopHostOSH)
            #Add the next hop and the link
            routeObjects.add(nextHopHostOSH)
            routeObjects.add(nextHopIpOSH)
            routeObjects.add(unnumberedLinkOSHHostHost)
        else:
            for ip in currIf.ipList:
                nextHopNetAddress = IPv4(route.ipRouteNextHop, ip.ipNetMask).getFirstIp().toString()
                if nextHopNetAddress != ip.netaddr:
                    continue
                    
                nextHopIpDomain =  DomainScopeManager.getDomainByIp(route.ipRouteNextHop, ip.domain)
                routeFound = 0
                for currRoute in routeArrayList:
                    if currRoute['localIpAddress'] == ip.ipAddr and currRoute['localIpDomain'] == ip.domain and currRoute['nextHopIp'] == route.ipRouteNextHop and currRoute['nextHopIpDomain'] == nextHopIpDomain:
                        currRoute['destinationList'].append(route.ipRouteDest)
                        break
                    routeFound += 1
                if routeFound >= len(routeArrayList):
                    currRoute = {}
                    currRoute['destAddress'] = route.ipRouteDest
                    currRoute['destinationList'] = []
                    currRoute['destinationList'].append(route.ipRouteDest)
                    currRoute['ifIndex'] = route.ipRouteIfIndex
                    currRoute['localIpAddress'] = ip.ipAddr
                    currRoute['localIpDomain'] = ip.domain
                    currRoute['localIpMask'] = ip.ipNetMask
                    currRoute['localIpNetClass'] = ip.netclass
                    currRoute['nextHopNetAddr'] = nextHopNetAddress
                    currRoute['nextHopIp'] = route.ipRouteNextHop
                    currRoute['nextHopIpDomain'] = DomainScopeManager.getDomainByIp(currRoute['nextHopIp'], currRoute['localIpDomain'])
                    currRoute['type'] = route.ipRouteType
                    currRoute['ifAdminStatus'] = currIf.ifAdminStatus
                    routeArrayList.append(currRoute)
                        
    for currRouteData in routeArrayList:
        #Ip (next hop)
        nextHopIpOSH = modeling.createIpOSH(currRouteData['nextHopIp'], currRouteData['localIpMask'])
            
        routeObjects.add(nextHopIpOSH)
        # Ip (local for link)
        localIpOSHForLink = modeling.createIpOSH(currRouteData['localIpAddress'])
            
        routeLinkOSHIpIp = modeling.createLinkOSH('route', localIpOSHForLink, nextHopIpOSH) 
            
        for ipDest in currRouteData['destinationList']:
            routeLinkOSHIpIp.addAttributeToList(AttributeStateHolder("route_netaddress", ipDest))
            
        # Network (for link)
        nextHopNetworkOSH = modeling.createNetworkOSH(currRouteData['nextHopNetAddr'], currRouteData['localIpMask'])
            
        nextHopHostOSH = __createRouterIncompleteHostByIp(currRouteData['nextHopIp'])
        #Member (Connecting the next hop host to the next hop network)
        memberLinkOSHHostNetwork = modeling.createLinkOSH('member', nextHopNetworkOSH, nextHopHostOSH)
        #Member (Connecting the next hop ip to the next hop network)
        memberLinkOSHIpNetwork = modeling.createLinkOSH('member', nextHopNetworkOSH, nextHopIpOSH)
            
        routeObjects.add(nextHopHostOSH)
        routeObjects.add(memberLinkOSHHostNetwork)
        routeObjects.add(memberLinkOSHIpNetwork)
        routeObjects.add(routeLinkOSHIpIp)
    
    return routeObjects
示例#21
0
def DiscoveryMain(Framework):
    OSHVResult = ObjectStateHolderVector()
    snmpMethod = getFrameworkParameter(Framework, "snmpMethod", SnmpQueries.defaultSnmpMethod)
    backupSnmpMethod = getFrameworkParameter(Framework, "backupSnmpMethod", SnmpQueries.defaultBackupSnmpMethod)
    moonWalkBulkSize = int(getFrameworkParameter(Framework, "moonWalkBulkSize", SnmpQueries.defaultMoonWalkBulkSize))
    moonWalkSleep = long(getFrameworkParameter(Framework, "moonWalkSleep", SnmpQueries.defaultMoonWalkSleep))
    snmpBulkSize = int(getFrameworkParameter(Framework, "snmpBulkSize", SnmpQueries.defaultSnmpBulkSize))
    discoverUnknownIPs = Boolean.parseBoolean(Framework.getParameter("discoverUnknownIPs"))

    # getting DestinationData from Framework; the method is protected.
    destination = Framework.getCurrentDestination()

    discoveredHostIpList = SnmpQueries.getSnmpIpDataOneDestination(
        snmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, destination
    )
    logger.debug("Discover ARP by %s returned %s objects" % (snmpMethod, str(discoveredHostIpList.size())))
    if (discoveredHostIpList.size() == 0) and (snmpMethod != backupSnmpMethod):
        discoveredHostIpList = SnmpQueries.getSnmpIpDataOneDestination(
            backupSnmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, destination
        )
        logger.debug("Discover ARP by %s returned %s objects" % (backupSnmpMethod, str(discoveredHostIpList.size())))
        if discoveredHostIpList.size() == 0:
            Framework.reportWarning("Failed to discover SNMP IP data")
            return OSHVResult

    discoveredHostArpList = SnmpQueries.getSnmpArpDataOneDestination(
        snmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, destination
    )
    discoveredHostArpList.addAll(
        SnmpQueries.getSnmpArpDataOneDestination(
            snmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, Boolean.TRUE, destination
        )
    )
    if (discoveredHostArpList.size() == 0) and (snmpMethod != backupSnmpMethod):
        discoveredHostArpList = SnmpQueries.getSnmpArpDataOneDestination(
            backupSnmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, destination
        )
        discoveredHostArpList.addAll(
            SnmpQueries.getSnmpArpDataOneDestination(
                backupSnmpMethod,
                snmpBulkSize,
                moonWalkBulkSize,
                moonWalkSleep,
                Boolean.FALSE,
                Boolean.TRUE,
                destination,
            )
        )
        if discoveredHostArpList.size() == 0:
            Framework.reportWarning("Failed to discover SNMP ARP data")
            return OSHVResult

    networkOSH = None
    for i in range(discoveredHostArpList.size()):
        currArp = discoveredHostArpList.get(i)
        for currIp in discoveredHostIpList:
            if networkOSH is None:
                networkOSH = modeling.createNetworkOSH(currIp.netaddr, currIp.netmask)
                OSHVResult.add(networkOSH)
            if (currIp.domain == "unknown") and not discoverUnknownIPs:
                continue
            if not netutils.isValidMac(currArp.designatedMacAddress):
                continue

            # report start
            designatedIpNetAddress = IPv4(currArp.designatedIpAddress, currIp.netmask).getFirstIp().toString()
            if designatedIpNetAddress == currIp.netaddr:
                hostOSH = modeling.createHostOSH(currArp.designatedIpAddress)
                OSHVResult.add(hostOSH)
                OSHVResult.add(modeling.createLinkOSH("member", networkOSH, hostOSH))

                ipOsh = modeling.createIpOSH(currArp.designatedIpAddress, currIp.netmask)
                OSHVResult.add(ipOsh)
                OSHVResult.add(modeling.createLinkOSH("member", networkOSH, ipOsh))

                ifOsh = modeling.createInterfaceOSH(netutils.parseMac(currArp.designatedMacAddress), hostOSH)
                OSHVResult.add(ifOsh)
                OSHVResult.add(modeling.createLinkOSH("containment", ifOsh, ipOsh))

    return OSHVResult
示例#22
0
def getIPOSHV(Framework, ip, netmask = None, dnsServers=None, failPing=False, failDns=False):
    OSHVResult = ObjectStateHolderVector()  
    if not ip:
        return OSHVResult
    
    ip = str(ip)
    localShell = None    
   
    #if Framework.getParameter('dnsServers'):
    #    dnsServers = [dnsServer for dnsServer in dnsServers.split(',') if dnsServer and dnsServer.strip()] or None 
    
    try:
        flag = False
        probeName = getProbeName(ip)
        ipsResult = [ip]
        if Framework:
            client = None
            try:
                localShell = shellutils.ShellUtils(Framework.createClient(ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME))   
                client = Framework.createClient(ClientsConsts.ICMP_PROTOCOL_NAME, Properties())
                
                if probeName:             
                    ipsResult = client.executePing([ip])            
                    if not ipsResult:
                        raise ValueError("No reply from ping")
                    logger.debug("ping result for %s: %s" % (ip, ipsResult))
            except:
                ipsResult = [ip]
                #logger.reportWarning("No reply from ping")
                logger.warnException("Error pinging address %s: " % (ip))                
                flag = True          
            
            if client is not None:
                try:
                    client.close()
                    client = None
                except:
                    pass

        # run on the string array (Holding all live pinged ips)
        
        if flag and failPing:
            logger.reportWarning("No reply from ping")
        else:
            for ipResult in ipsResult:
                isVirtual = 0
                # pingedIp - the ip we sent the ping to
                # replyIp - the ip replied to the ping
                
                # Break the curr result by ':' <Reply-IP>:<Pinged-IP>
                token = ipResult.split(':')
        
                if (len(token) >= 2):
                    # In case where we ping a virtual ip we get the reply from the real ip
                    # If we are pinging a virtual ip
        
                    replyIp, pingedIp = token[0], token[1]
                    isVirtual = 1           
                else:
                    replyIp, pingedIp = ipResult, ipResult            
                
                dnsName, alias = getHostNamesFromShell(pingedIp,localShell,dnsServers)  
                                
                if not dnsName:
                    #logger.reportWarning("Cannot resolve hostname")
                    if failDns:
                        raise ValueError("Cannot find FQDN for IP")
                
               
                pingedIpOSH = modeling.createIpOSH(pingedIp, netmask, dnsName)
                # Create Ip OSH and add to vector                

                OSHVResult.add(pingedIpOSH)
                #if probeName:
                #    hostOsh = modeling.createHostOSH(pingedIp, 'node', None, dnsName)
                #    OSHVResult.add(modeling.createLinkOSH('containment', hostOsh, pingedIpOSH))
                
                networkOSHForLink = None
                try: 
                    if netmask:       
                        netAddress = str(IPFactory.getNetAddress(pingedIp, netmask))
                        if netmask and netAddress:
                            pingedIpOSH.setAttribute("ip_netaddr", netAddress)
                            networkOSHForLink = modeling.createNetworkOSH(netAddress, netmask)
                            OSHVResult.add(networkOSHForLink)
                            OSHVResult.add(modeling.createLinkOSH('member', networkOSHForLink, pingedIpOSH))
            
                except:            
                    logger.warnException("Error getting net address for %s / %s: " % (ip, netmask)) 
        
                if isVirtual:
                    # Create Ip OSH
                    dnsName, alias = getHostNamesFromShell(replyIp,localShell,dnsServers)               
                        
                    replyIpOSH = modeling.createIpOSH(replyIp, netmask, dnsName)

                    replyIpOSH.setBoolAttribute("isvirtual", True)
                    # Create a depend  link and set end1(pingedIp) and end2(replyIp)
                    newDependLink = modeling.createLinkOSH('depend', pingedIpOSH, replyIpOSH)
        
                    OSHVResult.add(replyIpOSH)
                    OSHVResult.add(newDependLink)
        
                    if networkOSHForLink:
                        try:            
                            replyIpNetAddress = str(IPFactory.getNetAddress(replyIp, netmask))
                            if replyIpNetAddress == netAddress:
                                # Create MEMBER link and set end1(discovered network) and end2(host)
                                OSHVResult.add(modeling.createLinkOSH('member', networkOSHForLink, replyIpOSH))
                                replyIpOSH.setAttribute("ip_netaddr", netAddress)
                        except:
                            logger.warnException("Error getting net address for %s / %s: " % (replyIp, netmask))   
    
    except:
        logger.errorException("Error getting DNS info for %s: " % (ip))       
    finally:        
        if localShell is not None:
            try:
                localShell.close()
                localShell = None
            except:
                pass 
            
    return OSHVResult
示例#23
0
 def build(self):
     if (isinstance(self.ip, type("")) or isinstance(self.ip, type(u""))
             or (self.ip and self.ip.version == 4)):
         self.osh = modeling.createNetworkOSH(str(self.ip), self.netmask)