Пример #1
0
def DiscoveryMain(Framework):
    # General variables
    OSHVResult = ObjectStateHolderVector()
    protocolName = 'SQL'
    dbClient = None

    ## OSH dictionaries to prevent recreation of the same OSHs in different parts of the script
#    netDeviceOshDict = {}
    ipAddrList = []
    portVlanIdMap = {} ## {'vlanName:;:vlanID':[portOSH]}

    ## Destination properties
    ipAddress = Framework.getDestinationAttribute('ip_address')
    dbPort = Framework.getDestinationAttribute('db_port')
    ## Job parameters
    rmeDbName = Framework.getParameter('rmeDbName')
    if not rmeDbName:
        excInfo = ('Discovery job parameter <rmeDbName> not populated correctly')
        Framework.reportError(excInfo)
        logger.error(excInfo)
        return None

    ignoreNodesWithoutIP = 1
    if Framework.getParameter("ignoreNodesWithoutIP").strip().lower() not in ['true', 'yes', 'y', '1']:
        ignoreNodesWithoutIP = 0
    allowDnsLookup = 0
    if Framework.getParameter("allowDnsLookup").strip().lower() in ['true', 'yes', 'y', '1']:
        allowDnsLookup = 1
    queryChunkSize = eval(Framework.getParameter("queryChunkSize"))
    if not (queryChunkSize and type(queryChunkSize) == type(1)):
        queryChunkSize = 250

    try:
        dbClient = ciscoworks_utils.connectToDb(Framework, ipAddress, dbPort)

        if dbClient:
            logger.debug('[' + SCRIPT_NAME + ':DiscoveryMain] Connected to CiscoWorks LMS Resource Manager Essentials database at port <%s>...' % dbPort)
            ## Check database state
            dbState = ciscoworks_utils.verifyDB(dbClient, rmeDbName)
            ciscoworks_utils.debugPrint(4, '[' + SCRIPT_NAME + ':DiscoveryMain] Got DB state <%s>...' % dbState)
            ## Discover...
            if dbState and dbState == 1:
                OSHVResult.addAll(getNetworkDevices(dbClient, queryChunkSize, ipAddrList, portVlanIdMap, ignoreNodesWithoutIP, allowDnsLookup, Framework))
                OSHVResult.addAll(processVlanPortMap(portVlanIdMap))
                #OSHVResult.addAll(buildNodePortLinks(dbClient, netDeviceOshDict, portOshDict))
            else:
                errorMessage = 'This is probably not a CiscoWorks LMS Resource Manager Essentials database'
                errormessages.resolveAndReport(errorMessage, protocolName, Framework)
        else:
            excInfo = ('Unable to connect to the CiscoWorks LMS Resource Manager Essentials database')
            Framework.reportError(excInfo)
            logger.error(excInfo)
            return None
    except:
        excInfo = logger.prepareJythonStackTrace('')
        logger.warn('[' + SCRIPT_NAME + ':DiscoveryMain] Exception: <%s>' % excInfo)
        errormessages.resolveAndReport(excInfo, protocolName, Framework)
        logger.debug('Closing JDBC connections...')
        if dbClient:
            dbClient.close()

    # Close JDBC stuff
    logger.debug('Closing JDBC connections...')
    if dbClient:
        dbClient.close()

    # Write OSHV to file - only useful for debugging
    #===========================================================================
    # from java.io import FileWriter, BufferedWriter
    # fileName = 'c:/' + SCRIPT_NAME + '.OSHV.xml'
    # theFile = FileWriter(fileName)
    # fileBuffer = BufferedWriter(theFile)
    # fileBuffer.write(OSHVResult.toXmlString())
    # fileBuffer.flush()
    # fileBuffer.close()
    # warningMessage = 'Discovery results not sent to server; Writing them to file <%s> on the Data Flow Probe system' % fileName
    # Framework.reportWarning(warningMessage)
    # logger.warn(warningMessage)
    #===========================================================================
    ## Print CIT counts from OSHV
    ciTypeCounts = {} # {'CI Type':Count}
    for ciTypeIndex in range(OSHVResult.size()):
        ciType = OSHVResult.get(ciTypeIndex).getObjectClass()
        if ciType in ciTypeCounts.keys():
            ciTypeCounts[ciType] = ciTypeCounts[ciType] + 1
        else:
            ciTypeCounts[ciType] = 1
    print ciTypeCounts

    return OSHVResult
Пример #2
0
def DiscoveryMain(Framework):
    # General variables
    OSHVResult = ObjectStateHolderVector()
    protocolName = 'SQL'
    dbClient = None

    ## Destination properties
    ipAddress = Framework.getDestinationAttribute('ip_address')
    dbPort = Framework.getDestinationAttribute('db_port')

    ## Job parameters
    campusDbName = Framework.getParameter('campusDbName')
    if not campusDbName:
        excInfo = ('Discovery job parameter <campusDbName> not populated correctly')
        Framework.reportError(excInfo)
        logger.error(excInfo)
        return None
    allowDnsLookup = 0
    if Framework.getParameter("allowDnsLookup").strip().lower() in ['true', 'yes', 'y', '1']:
        allowDnsLookup = 1
    queryChunkSize = eval(Framework.getParameter("queryChunkSize"))
    if not (queryChunkSize and type(queryChunkSize) == type(1)):
        queryChunkSize = 1000

    try:
        (netDeviceOSHV, portOSHV) = getNetDeviceAndPortOSHVs(Framework)
        dbClient = ciscoworks_utils.connectToDb(Framework, ipAddress, dbPort)

        if dbClient:
            logger.debug('Connected to CiscoWorks LMS Campus database at port <%s>...' % dbPort)
            ## Check database state
            dbState = ciscoworks_utils.verifyDB(dbClient, campusDbName)
            ciscoworks_utils.debugPrint(4, '[' + SCRIPT_NAME + ':DiscoveryMain] Got DB state <%s>...' % dbState)
            ## Discover...
            if dbState and dbState == 1:
                OSHVResult.addAll(getNodes(dbClient, netDeviceOSHV, portOSHV, allowDnsLookup, queryChunkSize, Framework))
            else:
                errorMessage = 'This is probably not a CiscoWorks LMS Campus database'
                errormessages.resolveAndReport(errorMessage, protocolName, Framework)
        else:
            excInfo = ('Unable to connect to the CiscoWorks LMS Campus database')
            Framework.reportError(excInfo)
            logger.error(excInfo)
            return None
    except:
        excInfo = logger.prepareJythonStackTrace('')
        logger.warn('[' + SCRIPT_NAME + ':DiscoveryMain] Exception: <%s>' % excInfo)
        errormessages.resolveAndReport(excInfo, protocolName, Framework)
        logger.debug('Closing JDBC connections...')
        if dbClient:
            dbClient.close()

    # Close JDBC stuff
    logger.debug('Closing JDBC connections...')
    if dbClient:
        dbClient.close()

    ## Write OSHV to file - only useful for debugging 
    #===========================================================================
    # from java.io import FileWriter, BufferedWriter
    # fileName = 'c:/' + SCRIPT_NAME + '.OSHV.xml'
    # theFile = FileWriter(fileName)
    # fileBuffer = BufferedWriter(theFile)
    # fileBuffer.write(OSHVResult.toXmlString())
    # fileBuffer.flush()
    # fileBuffer.close()
    # warningMessage = 'Discovery results not sent to server; Writing them to file <%s> on the Data Flow Probe system' % fileName
    # Framework.reportWarning(warningMessage)
    # logger.warn(warningMessage)
    #===========================================================================
    ## Print CIT counts from OSHV
    ciTypeCounts = {} # {'CI Type':Count}
    for ciTypeIndex in range(OSHVResult.size()):
        ciType = OSHVResult.get(ciTypeIndex).getObjectClass()
        if ciType in ciTypeCounts.keys():
            ciTypeCounts[ciType] = ciTypeCounts[ciType] + 1
        else:
            ciTypeCounts[ciType] = 1
    print ciTypeCounts

    return OSHVResult
Пример #3
0
def DiscoveryMain(Framework):
    # General variables
    OSHVResult = ObjectStateHolderVector()
    protocolName = 'SQL'
    dbClient = None

    ## OSH dictionaries to prevent recreation of the same OSHs in different parts of the script
    #    netDeviceOshDict = {}
    ipAddrList = []
    portVlanIdMap = {}  ## {'vlanName:;:vlanID':[portOSH]}

    ## Destination properties
    ipAddress = Framework.getDestinationAttribute('ip_address')
    dbPort = Framework.getDestinationAttribute('db_port')
    ## Job parameters
    rmeDbName = Framework.getParameter('rmeDbName')
    if not rmeDbName:
        excInfo = (
            'Discovery job parameter <rmeDbName> not populated correctly')
        Framework.reportError(excInfo)
        logger.error(excInfo)
        return None

    ignoreNodesWithoutIP = 1
    if Framework.getParameter("ignoreNodesWithoutIP").strip().lower() not in [
            'true', 'yes', 'y', '1'
    ]:
        ignoreNodesWithoutIP = 0
    allowDnsLookup = 0
    if Framework.getParameter("allowDnsLookup").strip().lower() in [
            'true', 'yes', 'y', '1'
    ]:
        allowDnsLookup = 1
    queryChunkSize = eval(Framework.getParameter("queryChunkSize"))
    if not (queryChunkSize and type(queryChunkSize) == type(1)):
        queryChunkSize = 250

    try:
        dbClient = ciscoworks_utils.connectToDb(Framework, ipAddress, dbPort)

        if dbClient:
            logger.debug(
                '[' + SCRIPT_NAME +
                ':DiscoveryMain] Connected to CiscoWorks LMS Resource Manager Essentials database at port <%s>...'
                % dbPort)
            ## Check database state
            dbState = ciscoworks_utils.verifyDB(dbClient, rmeDbName)
            ciscoworks_utils.debugPrint(
                4, '[' + SCRIPT_NAME +
                ':DiscoveryMain] Got DB state <%s>...' % dbState)
            ## Discover...
            if dbState and dbState == 1:
                OSHVResult.addAll(
                    getNetworkDevices(dbClient, queryChunkSize, ipAddrList,
                                      portVlanIdMap, ignoreNodesWithoutIP,
                                      allowDnsLookup, Framework))
                OSHVResult.addAll(processVlanPortMap(portVlanIdMap))
                #OSHVResult.addAll(buildNodePortLinks(dbClient, netDeviceOshDict, portOshDict))
            else:
                errorMessage = 'This is probably not a CiscoWorks LMS Resource Manager Essentials database'
                errormessages.resolveAndReport(errorMessage, protocolName,
                                               Framework)
        else:
            excInfo = (
                'Unable to connect to the CiscoWorks LMS Resource Manager Essentials database'
            )
            Framework.reportError(excInfo)
            logger.error(excInfo)
            return None
    except:
        excInfo = logger.prepareJythonStackTrace('')
        logger.warn('[' + SCRIPT_NAME +
                    ':DiscoveryMain] Exception: <%s>' % excInfo)
        errormessages.resolveAndReport(excInfo, protocolName, Framework)
        logger.debug('Closing JDBC connections...')
        if dbClient:
            dbClient.close()

    # Close JDBC stuff
    logger.debug('Closing JDBC connections...')
    if dbClient:
        dbClient.close()

    # Write OSHV to file - only useful for debugging
    #===========================================================================
    # from java.io import FileWriter, BufferedWriter
    # fileName = 'c:/' + SCRIPT_NAME + '.OSHV.xml'
    # theFile = FileWriter(fileName)
    # fileBuffer = BufferedWriter(theFile)
    # fileBuffer.write(OSHVResult.toXmlString())
    # fileBuffer.flush()
    # fileBuffer.close()
    # warningMessage = 'Discovery results not sent to server; Writing them to file <%s> on the Data Flow Probe system' % fileName
    # Framework.reportWarning(warningMessage)
    # logger.warn(warningMessage)
    #===========================================================================
    ## Print CIT counts from OSHV
    ciTypeCounts = {}  # {'CI Type':Count}
    for ciTypeIndex in range(OSHVResult.size()):
        ciType = OSHVResult.get(ciTypeIndex).getObjectClass()
        if ciType in ciTypeCounts.keys():
            ciTypeCounts[ciType] = ciTypeCounts[ciType] + 1
        else:
            ciTypeCounts[ciType] = 1
    print ciTypeCounts

    return OSHVResult