Example #1
0
def discoverSysTableData(client):
    snmpAgent = snmputils.SnmpAgent(None, client)
    queryBuilder = snmputils.SnmpQueryBuilder(BASE_SYSTABLE_OID)
    queryBuilder.addQueryElement(1, 'sysDescr')
    queryBuilder.addQueryElement(2, 'sysObjectID')
    queryBuilder.addQueryElement(4, 'sysContact')
    queryBuilder.addQueryElement(5, 'sysName')
    queryBuilder.addQueryElement(6, 'sysLocation')
    queryBuilder.addQueryElement(7, 'sysServices')
    
    sysTable = snmpAgent.getSnmpData(queryBuilder)
    
    for data in sysTable:
        classVendorOsAndModel = SnmpQueries.getClassVendorOsAndModelByOid(data.sysObjectID, data.sysDescr)
        setattr(data, 'sysClass', classVendorOsAndModel[0])
        setattr(data, 'sysVendor', classVendorOsAndModel[1])
        setattr(data, 'sysOs', classVendorOsAndModel[2])
        setattr(data, 'sysModel', classVendorOsAndModel[3])
        
        match = re.search("Nodename:\s*(\w+)", data.sysDescr)
        setattr(data, 'sysNodeName', None)
        if match:
            setattr(data, 'sysNodeName', match.group(1))
        
        if data.sysServices and (int(data.sysServices) & 2) > 0:
            setattr(data, 'isBridge', 1)
        else:
            setattr(data, 'isBridge', 0)
    if sysTable:
        return sysTable[0]
    else:
        raise ValueError, "Query for System Table returned empty result"
def discoverInterfaceData(client, sysTable):
    snmpAgent = snmputils.SnmpAgent(None, client)
    queryBuilder = snmputils.SnmpQueryBuilder(BASE_INTERFACE_OID)
    queryBuilder.addQueryElement(1, 'ifIndex')
    queryBuilder.addQueryElement(2, 'ifDescr')
    queryBuilder.addQueryElement(3, 'ifType')
    queryBuilder.addQueryElement(5, 'ifSpeed')
    queryBuilder.addQueryElement(6, 'ifMac', 'hexa')
    queryBuilder.addQueryElement(7, 'ifAdminStatus')
    queryBuilder.addQueryElement(8, 'ifOperStatus')

    ifList = snmpAgent.getSnmpData(queryBuilder)

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

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

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

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

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

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

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