示例#1
0
def DiscoveryMain(Framework):
    OSHVResult = ObjectStateHolderVector()

    language = Framework.getDestinationAttribute("language")
    protocol = Framework.getDestinationAttribute("Protocol")
    langBund = None

    hostId = Framework.getDestinationAttribute("hostId")
    client = None
    uname = None
    shell = None
    try:
        originClient = Framework.createClient()
        client = originClient
        shell = shellutils.ShellUtils(client)
        if shell.isWinOs():
            uname = "Win"
            client = shell
            if (language != None) and (language != "NA"):
                langBund = Framework.getEnvironmentInformation().getBundle("langHost_Resources_By_TTY", language)
            else:
                langBund = Framework.getEnvironmentInformation().getBundle("langHost_Resources_By_TTY")

            try:
                wmicPath = Framework.getParameter("wmicPath")
                if wmicPath:
                    client.execCmd("set PATH=%PATH%;" + wmicPath)
            except:
                logger.debug("Failed to add default wmic location to the PATH variable.")
        else:
            if shell.getClientType() == "ssh":
                uname = netutils.getOSName(client, "uname -a")
            else:
                uname = netutils.getOSName(client, "uname")

            langBund = Framework.getEnvironmentInformation().getBundle("langHost_Resources_By_TTY", "eng")
    except Exception, ex:
        exInfo = ex.getMessage()
        errormessages.resolveAndReport(exInfo, protocol, Framework)
示例#2
0
def DiscoveryMain(Framework):

    OSHVResult = ObjectStateHolderVector()
    
    discoverOracle = _getBoolJobParameter(Parameters.DISCOVER_ORACLE, Framework)
    discoverMssql = _getBoolJobParameter(Parameters.DISCOVER_MSSQL, Framework)
    useSudo = _getBoolJobParameter(Parameters.USE_SUDO, Framework)
    useLsof = _getBoolJobParameter(Parameters.USE_LSOF, Framework)
    protocol = Framework.getDestinationAttribute('Protocol')
    protocolDisplay = errormessages.protocolNames.get(protocol) or protocol
    
    if not discoverOracle and not discoverMssql:
        parameterNames = ", ".join((Parameters.DISCOVER_MSSQL, Parameters.DISCOVER_ORACLE))
        msg = "None of parameters [%s] are enabled. Set at least one to true to discover corresponding Database." % parameterNames
        errorObject = errorobject.createError(errorcodes.INTERNAL_ERROR_WITH_PROTOCOL_DETAILS, [protocolDisplay, msg], msg)
        logger.reportWarningObject(errorObject)
        logger.warn(msg)
        logger.warn("Note: support for DB2 in this job is deprecated since CP13.")
        return OSHVResult
    

    client = None
    shell = None
    osName = None
    processToPortDict = {} ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]}
    databaseDict = {} ## {instanceName/SID:[dbType, listeningPort, ipAddress, installPath, version, status]}
    database_ip_service_endpoints ={}  ## {instanceName:[ip_address:port]}

    try:
        client = Framework.createClient()
        shell = shellutils.ShellUtils(client)
        isWindows = 'false'
        dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':DiscoveryMain] Got client!')
        if shell.isWinOs():
            osName = 'Windows'
            isWindows = 'true'
            dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':DiscoveryMain] Client is Windows!')
        else:
            if shell.getClientType() == 'ssh':
                osName = netutils.getOSName(client, 'uname -a')
            else:
                osName = netutils.getOSName(client, 'uname')
            dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':DiscoveryMain] Client OS is <%s>' % osName)

        ## We have a shell client
        ## Get processes running on the box and ports they're listening on
        if(client and osName):
            
            osNameLower = osName.lower()
            
            if re.search("windows", osNameLower):
                processToPortDict = dbconnect_win_shellutils.getProcToPortDictOnWindows(client, Framework)
            elif re.search("aix", osNameLower):
                processToPortDict = dbconnect_unix_shellutils.getProcToPortDictOnAIX(client, useSudo, useLsof)
            elif re.search("linux", osNameLower):
                processToPortDict = dbconnect_unix_shellutils.getProcToPortDictOnLinux(client, useSudo, useLsof)
            elif re.search("sun", osNameLower):
                processToPortDict = dbconnect_unix_shellutils.getProcToPortDictOnSolaris(client, useSudo, useLsof)
            elif re.search("hp-ux", osNameLower):
                processToPortDict = dbconnect_unix_shellutils.getProcToPortDictOnHPUX(client, useSudo, useLsof)
            else:
                dbconnect_utils.debugPrint('Unknown operating system')

            ## We have process and port infromation
            ## Find databases, if any
            if processToPortDict != None and len(processToPortDict) > 0:
                for pid in processToPortDict.keys():
                    dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':DiscoveryMain] Got process/service/software <%s> listening on port <%s:%s>' % ((processToPortDict[pid])[dbconnect_utils.PROCESSNAME_INDEX], (processToPortDict[pid])[dbconnect_utils.IP_INDEX], (processToPortDict[pid])[dbconnect_utils.PORT_INDEX]))
                
                if discoverOracle:
                    dbconnect_oracle.findDatabases(client, processToPortDict, databaseDict, isWindows)
                
                if discoverMssql and re.search("windows", osNameLower):
                    dbconnect_mssql.findDatabases(client, processToPortDict, databaseDict, database_ip_service_endpoints)

                if databaseDict != None and len(databaseDict) > 0:
                    for dbName in databaseDict.keys():
                        logger.debug('Found <%s> instance <%s> (%s) with listener port <%s:%s> and installed in <%s>' % ((databaseDict[dbName])[dbconnect_utils.DBTYPE_INDEX], dbName, (databaseDict[dbName])[dbconnect_utils.STATUS_INDEX], (databaseDict[dbName])[dbconnect_utils.IP_INDEX], (databaseDict[dbName])[dbconnect_utils.PORT_INDEX], (databaseDict[dbName])[dbconnect_utils.PATH_INDEX]))
                    if database_ip_service_endpoints and len(database_ip_service_endpoints) > 0:
                        OSHVResult.addAll(dbconnect_utils.makeDbOSHs(databaseDict, database_ip_service_endpoints))
                    else:
                        OSHVResult.addAll(dbconnect_utils.makeDbOSHs(databaseDict))
                else:
                    Framework.reportWarning('No databases found')
            else:
                ## If we're here, we couldn't find any processes, service, or software
                ## and we have no data to search for databases
                Framework.reportWarning('Unable to get a list or processes, services, or installed software using <%s>' % shell.getClientType())
        else:
            logger.debug('Unable to connect using NTCMD, SSH , or Telnet')
    except JException, ex:
        exInfo = ex.getMessage()
        errormessages.resolveAndReport(exInfo, protocolDisplay, Framework)
示例#3
0
def doApplication(Framework, ip, OSHVResult, client, shell, hostOsh):
    clientType = client.getClientType()
    language = Framework.getDestinationAttribute('language')
    portToDiscover = Framework.getDestinationAttribute("PORT")
    scp_id = Framework.getDestinationAttribute("SCP_ID")
    portInNetstat = False
    processMapOnPort = {}
    signatureFound = False

    filterRunningSoftwareByPort = Framework.getParameter('filterRunningSoftwareByPort')
    logger.debug("Start to do host application")
    logger.debug("ip:", ip)
    #hostOsh = modeling.createHostOSH(ip)

    platformTrait = None
    processes = []
    packageToExecutablePath = {}
    CLUSTERLIST = ["Microsoft Cluster SW"]

    if shell.isWinOs():
        uname = 'Win'
        #client = shell
        if (language != None) and (language != 'NA'):
            langBund = Framework.getEnvironmentInformation().getBundle('langHost_Resources_By_TTY', language)
        else:
            langBund = Framework.getEnvironmentInformation().getBundle('langHost_Resources_By_TTY')

        try:
            wmicPath = Framework.getParameter('wmicPath')
            if wmicPath:
                shell.execCmd('set PATH=%PATH%;' + wmicPath)
        except:
            logger.debug('Failed to add default wmic location to the PATH variable.')
    else:
        if shell.getClientType() == 'ssh':
            uname = netutils.getOSName(client, 'uname -a')
        else:
            uname = netutils.getOSName(client, 'uname')

        langBund = Framework.getEnvironmentInformation().getBundle('langHost_Resources_By_TTY', 'eng')

        # get platform details
    try:
        platformTrait = process_discoverer.getPlatformTrait(shell)
        if platformTrait is None:
            raise ValueError()
    except:
        logger.warnException("Failed to determine platform")

    # discover processes
    if platformTrait and not uname == 'VMkernel':
        try:
            discoverer = process_discoverer.getDiscovererByShell(shell, platformTrait)
            processes = discoverer.discoverAllProcesses()
            if not processes:
                raise ValueError()
        except:
            errorMessage = 'Failed to discover processes by shell'
            TTY_HR_Main.logWarn(Framework, errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                                ['processes', clientType], errorMessage)

    if processes:

        # save processes to DB
        #process_discoverer.saveProcessesToProbeDb(processes, hostId, Framework)

        # discover packages info
        try:
            packagesDiscoverer = process_discoverer.getPackagesDiscovererByShell(shell, platformTrait)
            packageToExecutablePath = packagesDiscoverer.getPackagesByProcesses(processes)
        except:
            logger.warn("Failed to get package names by processes path")

            # report processes
            #if discoverProcesses:

    connectivityEndPoints = []
    connections = []
    runningApplications = []
    errorsList = []

    #No tcp and p2p discovery for vmkernel
    if not uname == 'VMkernel':
        try:
            tcpDiscoverer = asm_Disc_TCP.getDiscovererByShell(client, Framework, shell)
            if tcpDiscoverer is not None:
                tcpDiscoverer.pdu = asm_Disc_TCP.TcpStateHolder(None)
                tcpDiscoverer.discoverTCP()
                connections = tcpDiscoverer.pdu.tcp_connections
                connectivityEndPoints = tcpDiscoverer.getProcessEndPoints()
        except:
            errorMessage = 'Failed to run tcp discovery by shell'
            TTY_HR_Main.logWarn(Framework, errorcodes.FAILED_RUNNING_DISCOVERY_WITH_CLIENT_TYPE, ['tcp', clientType],
                                errorMessage)

        #if workInTopDown:
        linkOshv = ObjectStateHolderVector()
        processReporter = process.Reporter()
        processBuilder = process.ProcessBuilder()
        for connectivityEndPoint in connectivityEndPoints:
            processid = connectivityEndPoint.getKey()
            endpoints = connectivityEndPoint.getEndpoints()
            #logger.debug('#processid=', processid)
            #logger.debug('#endpoints=', endpoints)
            for processObject in processes:
                #logger.debug('#processObject.getPid()=', processObject.getPid())
                #logger.debug('#processObject.getName()=', processObject.getName())
                if 4 < processid == processObject.getPid() and processObject.getName() != 'svchost.exe':
                    processOSH, _ = processReporter.report(hostOsh, processObject, processBuilder)
                    for endpoint in endpoints:
                        if str(portToDiscover) == str(endpoint.getPort()):
                            portInNetstat = True
                            processMapOnPort[processid] = processObject
                        builder = netutils.ServiceEndpointBuilder()
                        reporter = netutils.EndpointReporter(builder)
                        ipServerOSH = reporter.reportEndpoint(endpoint, hostOsh)
                        linkOsh = modeling.createLinkOSH('usage', processOSH, ipServerOSH)
                        linkOshv.add(linkOsh)
                    break

        if not portInNetstat:
            errorMessage = errormessages.makeErrorMessage(clientType, message=portToDiscover,
                                                          pattern=errormessages.ERROR_FINDING_PROCESS_BY_PORT)
            logger.debugException("port cannot be found:", portToDiscover)
            errobj = errorobject.createError(errorcodes.FAILED_FINDING_PROCESS_BY_PORT, [portToDiscover], errorMessage)
            errorsList.append(errobj)

        OSHVResult.addAll(linkOshv)

        framework_delegator = Framework_delegator.FrameworkDelegator()

        appSign = asm_applications.createASMApplicationSignature(Framework, framework_delegator, client, shell)
        if processes:
            appSign.setProcessesManager(applications.ProcessesManager(processes, connectivityEndPoints))
        servicesByCmd = Hashtable()
        servicesInfo = applications.ServicesInfo(servicesByCmd)
        appSign.setServicesInfo(servicesInfo)
        cmdLineToInstalledSoftware = {}
        softNameToInstSoftOSH = {}
        softwareInfo = applications.InstalledSoftwareInfo(cmdLineToInstalledSoftware, softNameToInstSoftOSH)
        appSign.setInstalledSoftwareInfo(softwareInfo)

        runningApplications = appSign.getApplicationsTopologyUsingHostOsh(hostOsh)
        logger.debug('runningApplications=%s' % runningApplications)
        logger.debug('adding netstat port results')
        for application in runningApplications:
            pids = [str(x.getPid()) for x in application.getProcesses() if x]
            logger.debug('pids for application:%s %s' % (application.getName(), pids))
            if pids in processMapOnPort.keys():
                signatureFound = True
            for connectivityEndPoint in connectivityEndPoints:
                processid = connectivityEndPoint.getKey()
                endpoints = connectivityEndPoint.getEndpoints()
                if str(processid) in pids:
                    for endpoint in endpoints:
                        logger.debug('adding port:', endpoint.getPort())
                        framework_delegator.addNetstatPortResult(application.getName(), endpoint.getPort())

        if (not signatureFound) and processMapOnPort.values():
            errorMessage = errormessages.makeErrorMessage(
                clientType, message="%s listening on port %s" % (processMapOnPort.values(), portToDiscover),
                pattern=errormessages.ERROR_FINDING_APPSIG_BY_PROCESS)
            logger.debugException("no listening port found for process %s and port %s" % (processMapOnPort.values(), portToDiscover))
            errobj = errorobject.createError(errorcodes.FAILED_FINDING_APPSIG_BY_PROCESS,
                                             [repr(processMapOnPort.values()), portToDiscover], errorMessage)
            errorsList.append(errobj)

        filteredRunningApplications = []
        if filterRunningSoftwareByPort and filterRunningSoftwareByPort == 'true':
            logger.debug('try to filter running applications using port:', portToDiscover)
            logger.debug('application list using port filter',
                         framework_delegator.getApplicationNamesByPort(portToDiscover))

            for runningApplication in runningApplications:
                if runningApplication.getName() in framework_delegator.getApplicationNamesByPort(portToDiscover):
                    logger.debug("adding application: ", runningApplication.getName())
                    filteredRunningApplications.append(runningApplication)
                elif runningApplication.getName() in CLUSTERLIST:
                    logger.debug("include cluster related ci: ", runningApplication.getName())
                    filteredRunningApplications.append(runningApplication)
                    OSHVResult.addAll(
                        framework_delegator.getResultVectorByApplicationName(runningApplication.getName()))
                else:
                    logger.debug("remove application: ", runningApplication.getName())

            logger.debug('filteredRunningApplications=%s' % filteredRunningApplications)

            if not filteredRunningApplications:
                logger.debug("running software for port was not found:", portToDiscover)

            for resultVector in framework_delegator.getResultVectorsByPort(portToDiscover):
                OSHVResult.addAll(resultVector)

            #create ownership between runningsoftware and scp
            logger.debug("create ownership between runningsoftware and scp")
            for runningApplication in filteredRunningApplications:
                logger.debug("APP:", runningApplication.getName())
                OSHVResult.addAll(scp.createOwnerShip(scp_id, runningApplication.getOsh()))

        else:
            logger.debug('filterRunningSoftwareByPort is %s, will report all running software discovered')
            for runningApplication in runningApplications:
                filteredRunningApplications.append(runningApplication)
                OSHVResult.addAll(framework_delegator.getResultVectorByApplicationName(runningApplication.getName()))
                OSHVResult.addAll(scp.createOwnerShip(scp_id, runningApplication.getOsh()))

        logger.debug("crgMap = ", appSign.crgMap)
        vector = ObjectStateHolderVector()
        vector.addAll(OSHVResult)
        if appSign.crgMap:
            for osh in vector:
                oshClass = osh.getObjectClass()
                #weak node
                if oshClass == 'node' and osh.getAttributeValue('host_iscomplete') == 0 and osh.getAttributeValue(
                        'host_key'):
                    ip = osh.getAttributeValue('host_key').split(' ')[0]
                    if ip in appSign.crgMap.keys():
                        logger.debug("replace weak node:", osh.getAttribute("host_key"))
                        OSHVResult.remove(osh)
                        OSHVResult.add(appSign.crgMap[ip])
                #root container
                elif osh.getAttribute('root_container'):
                    obj = osh.getAttribute("root_container").getObjectValue()
                    if obj.getObjectClass() == 'node' and obj.getAttributeValue(
                            'host_iscomplete') == 0 and obj.getAttributeValue('host_key'):
                        logger.debug("replace root_container:", osh)
                        ip = obj.getAttributeValue('host_key').split(' ')[0]
                        if ip in appSign.crgMap.keys():
                            logger.debug("replace root_container:", obj.getAttribute("host_key"))
                            osh.setContainer(appSign.crgMap[ip])


    return filteredRunningApplications, processes, connectivityEndPoints, connections, errorsList
示例#4
0
def DiscoveryMain(Framework):

    OSHVResult = ObjectStateHolderVector()

    discoverOracle = _getBoolJobParameter(Parameters.DISCOVER_ORACLE,
                                          Framework)
    discoverMssql = _getBoolJobParameter(Parameters.DISCOVER_MSSQL, Framework)
    useSudo = _getBoolJobParameter(Parameters.USE_SUDO, Framework)
    useLsof = _getBoolJobParameter(Parameters.USE_LSOF, Framework)
    protocol = Framework.getDestinationAttribute('Protocol')
    protocolDisplay = errormessages.protocolNames.get(protocol) or protocol

    if not discoverOracle and not discoverMssql:
        parameterNames = ", ".join(
            (Parameters.DISCOVER_MSSQL, Parameters.DISCOVER_ORACLE))
        msg = "None of parameters [%s] are enabled. Set at least one to true to discover corresponding Database." % parameterNames
        errorObject = errorobject.createError(
            errorcodes.INTERNAL_ERROR_WITH_PROTOCOL_DETAILS,
            [protocolDisplay, msg], msg)
        logger.reportWarningObject(errorObject)
        logger.warn(msg)
        logger.warn(
            "Note: support for DB2 in this job is deprecated since CP13.")
        return OSHVResult

    client = None
    shell = None
    osName = None
    processToPortDict = {
    }  ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]}
    databaseDict = {
    }  ## {instanceName/SID:[dbType, listeningPort, ipAddress, installPath, version, status]}
    database_ip_service_endpoints = {}  ## {instanceName:[ip_address:port]}

    try:
        client = Framework.createClient()
        shell = shellutils.ShellUtils(client)
        isWindows = 'false'
        dbconnect_utils.debugPrint(
            3, '[' + SCRIPT_NAME + ':DiscoveryMain] Got client!')
        if shell.isWinOs():
            osName = 'Windows'
            isWindows = 'true'
            dbconnect_utils.debugPrint(
                3, '[' + SCRIPT_NAME + ':DiscoveryMain] Client is Windows!')
        else:
            if shell.getClientType() == 'ssh':
                osName = netutils.getOSName(client, 'uname -a')
            else:
                osName = netutils.getOSName(client, 'uname')
            dbconnect_utils.debugPrint(
                3, '[' + SCRIPT_NAME +
                ':DiscoveryMain] Client OS is <%s>' % osName)

        ## We have a shell client
        ## Get processes running on the box and ports they're listening on
        if (client and osName):

            osNameLower = osName.lower()

            if re.search("windows", osNameLower):
                processToPortDict = dbconnect_win_shellutils.getProcToPortDictOnWindows(
                    client, Framework)
            elif re.search("aix", osNameLower):
                processToPortDict = dbconnect_unix_shellutils.getProcToPortDictOnAIX(
                    client, useSudo, useLsof)
            elif re.search("linux", osNameLower):
                processToPortDict = dbconnect_unix_shellutils.getProcToPortDictOnLinux(
                    client, useSudo, useLsof)
            elif re.search("sun", osNameLower):
                processToPortDict = dbconnect_unix_shellutils.getProcToPortDictOnSolaris(
                    client, useSudo, useLsof)
            elif re.search("hp-ux", osNameLower):
                processToPortDict = dbconnect_unix_shellutils.getProcToPortDictOnHPUX(
                    client, useSudo, useLsof)
            else:
                dbconnect_utils.debugPrint('Unknown operating system')

            ## We have process and port infromation
            ## Find databases, if any
            if processToPortDict != None and len(processToPortDict) > 0:
                for pid in processToPortDict.keys():
                    dbconnect_utils.debugPrint(
                        3, '[' + SCRIPT_NAME +
                        ':DiscoveryMain] Got process/service/software <%s> listening on port <%s:%s>'
                        %
                        ((processToPortDict[pid]
                          )[dbconnect_utils.PROCESSNAME_INDEX],
                         (processToPortDict[pid])[dbconnect_utils.IP_INDEX],
                         (processToPortDict[pid])[dbconnect_utils.PORT_INDEX]))

                if discoverOracle:
                    dbconnect_oracle.findDatabases(client, processToPortDict,
                                                   databaseDict, isWindows)

                if discoverMssql and re.search("windows", osNameLower):
                    dbconnect_mssql.findDatabases(
                        client, processToPortDict, databaseDict,
                        database_ip_service_endpoints)

                if databaseDict != None and len(databaseDict) > 0:
                    for dbName in databaseDict.keys():
                        logger.debug(
                            'Found <%s> instance <%s> (%s) with listener port <%s:%s> and installed in <%s>'
                            %
                            ((databaseDict[dbName]
                              )[dbconnect_utils.DBTYPE_INDEX], dbName,
                             (databaseDict[dbName]
                              )[dbconnect_utils.STATUS_INDEX],
                             (databaseDict[dbName])[dbconnect_utils.IP_INDEX],
                             (databaseDict[dbName]
                              )[dbconnect_utils.PORT_INDEX],
                             (databaseDict[dbName]
                              )[dbconnect_utils.PATH_INDEX]))
                    if database_ip_service_endpoints and len(
                            database_ip_service_endpoints) > 0:
                        OSHVResult.addAll(
                            dbconnect_utils.makeDbOSHs(
                                databaseDict, database_ip_service_endpoints))
                    else:
                        OSHVResult.addAll(
                            dbconnect_utils.makeDbOSHs(databaseDict))
                else:
                    Framework.reportWarning('No databases found')
            else:
                ## If we're here, we couldn't find any processes, service, or software
                ## and we have no data to search for databases
                Framework.reportWarning(
                    'Unable to get a list or processes, services, or installed software using <%s>'
                    % shell.getClientType())
        else:
            logger.debug('Unable to connect using NTCMD, SSH , or Telnet')
    except JException, ex:
        exInfo = ex.getMessage()
        errormessages.resolveAndReport(exInfo, protocolDisplay, Framework)