def DiscoveryMain(Framework):
    vector = ObjectStateHolderVector()
    protocol = Framework.getDestinationAttribute('Protocol')
    client = None
    shell = None
    try:
        try:
            # connect to destination
            client = Framework.createClient()
            # wrap client with Shell
            shell = shellutils.ShellFactory().createShell(client)
        except AuthenticationException, ae:
            errormessages.resolveAndReport(ae.getMessage(), protocol,
                                           Framework)
        except:
            exInfo = logger.prepareJythonStackTrace('')
            errormessages.resolveAndReport(exInfo, protocol, Framework)
        else:

            language = Framework.getDestinationAttribute('language')
            hostCmdbId = Framework.getDestinationAttribute('hostId')

            # configure internationalization support
            if language and language != 'NA':
                langBund = Framework.getEnvironmentInformation().getBundle(
                    'langHost_Resources_By_TTY', language)
            else:
                langBund = Framework.getEnvironmentInformation().getBundle(
                    'langHost_Resources_By_TTY')

            # discovery
            discoverCPUs = Boolean.parseBoolean(
                Framework.getParameter('discoverCPUs'))
            discoverDisks = Boolean.parseBoolean(
                Framework.getParameter('discoverDisks'))
            discoveriSCSIInfo = Boolean.parseBoolean(
                Framework.getParameter('discoveriSCSIInfo'))
            discoverDrivers = Boolean.parseBoolean(
                Framework.getParameter('discoverDrivers'))
            discoverMemory = Boolean.parseBoolean(
                Framework.getParameter('discoverMemory'))
            discoverSoftware = Boolean.parseBoolean(
                Framework.getParameter('discoverInstalledSoftware'))
            discoverUsers = Boolean.parseBoolean(
                Framework.getParameter('discoverUsers'))
            discoverServices = Boolean.parseBoolean(
                Framework.getParameter('discoverServices'))
            discoverShares = Boolean.parseBoolean(
                Framework.getParameter('discoverShares'))
            discoverP2P = Boolean.parseBoolean(
                Framework.getParameter('discoverP2P'))

            try:

                hostOsh = modeling.createOshByCmdbIdString('host', hostCmdbId)

                if discoverShares:
                    try:
                        vector.addAll(_discoverSharedResources(shell, hostOsh))
                    except:
                        errorMessage = 'Failed to discover shared resources'
                        _logWarn(
                            errorcodes.
                            FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                            ['share', protocol], errorMessage)

                if discoverCPUs:
                    try:
                        vector.addAll(
                            TTY_HR_CPU_Lib.disWinOS(hostCmdbId, shell,
                                                    Framework, langBund))
                    except:
                        errorMessage = 'Failed to discover CPUs'
                        _logWarn(
                            errorcodes.
                            FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                            ['cpus', protocol], errorMessage)

                if discoverDisks:
                    try:
                        vector.addAll(TTY_HR_Disk_Lib.disWinOS(hostOsh, shell))
                    except:
                        errorMessage = 'Failed to discover disks'
                        _logWarn(
                            errorcodes.
                            FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                            ['disks', protocol], errorMessage)

                if discoverDrivers and shell.isWinOs():
                    try:
                        vector.addAll(
                            _discoverWindowsDeviceDriver(shell, hostOsh))
                    except:
                        errorMessage = 'Failed to discover windows device driver by powershell'
                        _logWarn(
                            errorcodes.
                            FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                            ['windows device driver', protocol], errorMessage)

                if discoveriSCSIInfo:
                    try:
                        vector.addAll(
                            TTY_HR_Disk_Lib.disWinOSiSCSIInfo(hostOsh, shell))
                    except:
                        errorMessage = 'Failed to discover iSCSI info'
                        _logWarn(
                            errorcodes.
                            FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                            ['iSCSI', protocol], errorMessage)

                if discoverMemory:
                    try:
                        vector.addAll(
                            TTY_HR_Memory_Lib.disWinOS(hostCmdbId, shell,
                                                       Framework, langBund))
                    except:
                        errorMessage = 'Failed to discover memory'
                        _logWarn(
                            errorcodes.
                            FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                            ['memory', protocol], errorMessage)

                processes = []
                try:
                    processesDiscoverer = process_discoverer.DiscovererByShellOnWindows(
                        shell)
                    processes = processesDiscoverer.discoverAllProcesses()
                    if not processes:
                        raise ValueError()
                except:
                    errorMessage = 'Failed to discover processes'
                    _logWarn(
                        errorcodes.
                        FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                        ['processes', protocol], errorMessage)

                if processes:
                    # save processes to DB
                    process_discoverer.saveProcessesToProbeDb(
                        processes, hostCmdbId, Framework)

                    # report processes
                    discoverProcesses = Boolean.parseBoolean(
                        Framework.getParameter('discoverProcesses'))
                    if discoverProcesses:
                        processReporter = process.Reporter()
                        for processObject in processes:
                            processVector = processReporter.reportProcess(
                                hostOsh, processObject)
                            vector.addAll(processVector)

                if discoverUsers:
                    try:
                        vector.addAll(_discoverUsers(hostOsh, shell))
                    except:
                        errorMessage = 'Failed to discover users'
                        _logWarn(
                            errorcodes.
                            FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                            ['users', protocol], errorMessage)

                cmdLineToInstalledSoftware = {}
                softNameToInstSoftOSH = {}
                if discoverSoftware:
                    try:
                        softwareOSHs = TTY_HR_Software_Lib.disWinOS(
                            hostCmdbId, shell, Framework, langBund,
                            softNameToInstSoftOSH)
                        if softwareOSHs:
                            vector.addAll(softwareOSHs)
                    except:
                        errorMessage = 'Failed to discover installed software'
                        _logWarn(errorcodes.FAILED_DISCOVERING_RESOURCE,
                                 ['installed software', protocol],
                                 errorMessage)

                servicesByCmd = Hashtable()
                if discoverServices:
                    try:
                        srvcOSHs = NTCMD_HR_REG_Service_Lib.doService(
                            shell, hostOsh, servicesByCmd, langBund, Framework)
                        vector.addAll(srvcOSHs)
                    except:
                        errorMessage = 'Failed to discover services'
                        _logWarn(errorcodes.FAILED_DISCOVERING_RESOURCE,
                                 ['services', protocol], errorMessage)

                connectivityEndPoints = []
                try:
                    tcpDiscoverer = Dis_TCP.getDiscovererByShell(
                        client, Framework, shell)
                    if tcpDiscoverer is not None:
                        tcpDiscoverer.discoverTCP()
                        connectivityEndPoints = tcpDiscoverer.getProcessEndPoints(
                        )
                except:
                    errorMessage = 'Failed to run TCP discovery'
                    _logWarn(
                        errorcodes.FAILED_RUNNING_DISCOVERY_WITH_CLIENT_TYPE,
                        ['tcp', protocol], errorMessage)

                appSign = applications.createApplicationSignature(
                    Framework, client, shell)

                if processes:
                    appSign.setProcessesManager(
                        applications.ProcessesManager(processes,
                                                      connectivityEndPoints))

                servicesInfo = applications.ServicesInfo(servicesByCmd)
                appSign.setServicesInfo(servicesInfo)

                softwareInfo = applications.InstalledSoftwareInfo(
                    cmdLineToInstalledSoftware, softNameToInstSoftOSH)
                appSign.setInstalledSoftwareInfo(softwareInfo)

                appSign.getApplicationsTopology(hostCmdbId)

                if discoverP2P:
                    try:
                        p2p = process_to_process.ProcessToProcess(Framework)
                        p2p.getProcessesToProcess()
                    except:
                        errorMessage = 'Failed to run p2p discovery'
                        _logWarn(errorcodes.FAILED_RUNNING_DISCOVERY,
                                 ['p2p', protocol], errorMessage)

            except JException, ex:
                exInfo = ex.getMessage()
                errormessages.resolveAndReport(exInfo, protocol, Framework)
            except:
Exemplo n.º 2
0
def processContainerInfo(shell, skipDockerVolume, filesystemDict,
                         containerInspectOutput, containerInfo, imageDict,
                         containerDict, containerLinks, dockerDaemonOSH,
                         nodeOSH, client, Framework, OSHVResult):
    json = _JSONs()
    jsonOutput = json.loads(containerInspectOutput)
    inspectJsonObj = jsonOutput[0]

    imageName = containerInfo[1]
    containerName = containerInfo[2]
    if len(containerInfo) == 4:
        containerPorts = containerInfo[3]
    else:
        portsArray = []
        ports = inspectJsonObj['NetworkSettings']['Ports']
        port_keys = ports.keys()
        port_keys.sort()
        for port in port_keys:
            if ports[port]:
                if ports[port][0]['HostIp'] and ports[port][0]['HostPort']:
                    portsArray.append(ports[port][0]['HostIp'] + ':' +
                                      ports[port][0]['HostPort'] + ' -> ' +
                                      port)
            else:
                portsArray.append(port)
        containerPorts = ', '.join(portsArray)

    # get container related image
    containerId = inspectJsonObj['Id']
    imageId = inspectJsonObj['Image']
    containerOSH = ObjectStateHolder('docker_container')
    containerOSH.setAttribute('name', containerName)
    containerOSH.setAttribute('docker_container_id', containerId)
    containerOSH.setAttribute('docker_image_id', imageId)
    containerOSH.setAttribute('docker_image', imageName)
    containerDict[containerId] = containerOSH

    # set container ports
    containerOSH.setAttribute('docker_container_ports', containerPorts)

    OSHVResult.add(containerOSH)
    containerOSH.setContainer(dockerDaemonOSH)
    daemonContainerLink = modeling.createLinkOSH('manage', dockerDaemonOSH,
                                                 containerOSH)
    OSHVResult.add(daemonContainerLink)

    # get container links
    if inspectJsonObj['HostConfig']['Links'] is not None:
        for link in inspectJsonObj['HostConfig']['Links']:
            linkedContainer = link.split(':')[0].split('/')[1]
            containerInspectCMD = 'docker inspect -f {{.Id}} ' + linkedContainer
            linkedContainerId = shell.execCmd(containerInspectCMD)
            if shell.getLastCmdReturnCode() == 0:
                linkedContainerId = linkedContainerId.strip()
                containerLinks[containerId] = linkedContainerId
            else:
                Framework.reportError(
                    ('Failed in command: docker inspect linked container <%s>.'
                     % linkedContainer))

    # link image and container
    logger.debug('imageOSH: ', imageDict[imageId])
    logger.debug('containerOSH: ', containerOSH)
    imageContainerLink = modeling.createLinkOSH('realization',
                                                imageDict[imageId],
                                                containerOSH)
    OSHVResult.add(imageContainerLink)

    # get running software in container
    discoverRSinDockerContainer = Framework.getParameter('discoverRunningSW')
    if discoverRSinDockerContainer == 'true':
        topCmd = 'docker top ' + containerId
        topOutput = shell.execCmd(topCmd)
        logger.debug('docker top container: ', imageName)
        processList = []

        topLines = None
        if shell.getLastCmdReturnCode() == 0:
            topLines = checkLastCmd(topOutput)
        else:
            Framework.reportWarning(
                ('Failed in command: docker top container <%s>.' %
                 containerId))
        if topLines:
            topcount = 0
            for topLine in topLines:
                topcount += 1
                if topcount == 1:
                    continue
                topLine = topLine.strip()

                matcher = re.match(
                    r'\s*(\w+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(.+)\s+(.+)\s+(\d+):(\d+):(\d+)\s+(.+)',
                    topLine)
                if matcher:
                    owner = matcher.group(1)
                    pid = matcher.group(2)
                    commandLine = matcher.group(10)
                    fullCommand = None
                    argumentsLine = None

                    if commandLine:
                        tokens = re.split(r"\s+", commandLine, 1)
                        fullCommand = tokens[0]
                        if len(tokens) > 1:
                            argumentsLine = tokens[1]

                    commandName = fullCommand
                    commandPath = None
                    matcher = re.match(r"(.*/)([^/]+)$", fullCommand)
                    if matcher:
                        commandName = matcher.group(2)
                        commandPath = fullCommand

                    process = process_module.Process(commandName, pid,
                                                     commandLine)
                    logger.debug('process generated: ', process)
                    process.argumentLine = argumentsLine
                    process.owner = owner
                    process.executablePath = commandPath
                    processList.append(process)

        if len(processList) > 0:
            logger.debug('start apply to: ', containerOSH)
            appSign = applications.createApplicationSignature(
                Framework, client, shell)
            logger.debug('created ApplicationSignature: ', containerOSH)
            appSign.setProcessesManager(
                applications.ProcessesManager(processList, None))
            logger.debug('ProcessesManager: ', containerOSH)
            appSign.getApplicationsTopology(containerOSH)
            logger.debug('finish apply to: ', containerOSH)

    # get container volumes
    if not skipDockerVolume:
        if inspectJsonObj.has_key('Mounts') and inspectJsonObj['Mounts']:
            mountResults = inspectJsonObj['Mounts']
            for mountStr in mountResults:
                mount = mountStr
                dockerVolumeOSH = ObjectStateHolder('docker_volume')
                dockerVolumeOSH.setAttribute('name', 'Docker Volume')
                dockerVolumeOSH.setAttribute('dockervolume_source',
                                             mount['Source'])
                dockerVolumeOSH.setAttribute('dockervolume_destination',
                                             mount['Destination'])
                if mount['RW'] == 'true':
                    dockerVolumeOSH.setAttribute('logicalvolume_accesstype',
                                                 'RW')
                else:
                    dockerVolumeOSH.setAttribute('logicalvolume_accesstype',
                                                 'R')
                OSHVResult.add(dockerVolumeOSH)
                volumeContainerLink = modeling.createLinkOSH(
                    'usage', containerOSH, dockerVolumeOSH)
                OSHVResult.add(volumeContainerLink)
                linkDockerVolumeToLv(mount['Source'], filesystemDict, nodeOSH,
                                     dockerVolumeOSH, OSHVResult)

        elif inspectJsonObj.has_key('Volumes') and inspectJsonObj.has_key(
                'VolumesRW') and inspectJsonObj['Volumes']:
            dockerVolumeOSH = ObjectStateHolder('docker_volume')
            dockerVolumeOSH.setAttribute('name', 'Docker Volume')
            OSHVResult.add(dockerVolumeOSH)
            volumeContainerLink = modeling.createLinkOSH(
                'usage', containerOSH, dockerVolumeOSH)
            OSHVResult.add(volumeContainerLink)
            volumResults = inspectJsonObj['Volumes']
            for (dst, src) in volumResults.items():
                dockerVolumeOSH.setAttribute('dockervolume_source', src)
                dockerVolumeOSH.setAttribute('dockervolume_destination', dst)
                if inspectJsonObj['VolumesRW'][dst] == 'true':
                    dockerVolumeOSH.setAttribute('logicalvolume_accesstype',
                                                 'RW')
                else:
                    dockerVolumeOSH.setAttribute('logicalvolume_accesstype',
                                                 'R')
                linkDockerVolumeToLv(src, filesystemDict, nodeOSH,
                                     dockerVolumeOSH, OSHVResult)
    else:
        logger.debug('Skip Docker Volume since filesystem is not find!')
Exemplo n.º 3
0
def DiscoveryMain(Framework):
    OSHVResult = ObjectStateHolderVector()
    client = None
    try:
        client = createWmiClient(Framework)

        if client:
            protocol = Framework.getDestinationAttribute('Protocol')
            hostId = Framework.getDestinationAttribute('hostId')
            hostOsh = modeling.createOshByCmdbIdString('nt', hostId)

            _, warning = discover_or_warn('fibre channel HBAs',
                                discover_fc_hbas, Framework, hostOsh,
                                protocol, OSHVResult, protocol_name=protocol)
            if warning:
                logger.reportWarningObject(warning)

            discoverUsers = Boolean.parseBoolean(Framework.getParameter('discoverUsers'))
            if discoverUsers:
                try:
                    wmi_dis_user_lib.executeWmiQuery(client, Framework, OSHVResult, hostOsh)
                except:
                    errobj = errorobject.createError(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['users', 'wmi'], 'Failed to discover users by wmi')
                    logger.reportErrorObject(errobj)
                    logger.errorException('Failed to discover users by wmi')

            discoverShares = Boolean.parseBoolean(Framework.getParameter('discoverShares'))
            if discoverShares:
                try:
                    wmi_dis_share_lib.executeWmiQuery(client, OSHVResult, hostOsh)
                except:
                    errobj = errorobject.createError(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['shares', 'wmi'], 'Failed to discover shares by wmi')
                    logger.reportErrorObject(errobj)
                    logger.errorException('Failed to discover shares by wmi')

            discoverProcesses = Boolean.parseBoolean(Framework.getParameter('discoverProcesses'))
            processes = []

            try:
                processDiscoverer = process_discoverer.getDiscovererByWmi(client)
                processes = processDiscoverer.discoverAllProcesses()
                if not processes:
                    raise ValueError()
            except:
                errobj = errorobject.createError(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['processes', 'wmi'], 'Failed to discover processes by wmi')
                logger.reportErrorObject(errobj)
                logger.errorException('Failed to discover processes by wmi')

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

                # report processes
                if discoverProcesses:
                    processReporter = process.Reporter()
                    for processObject in processes:
                        processesVector = processReporter.reportProcess(hostOsh, processObject)
                        OSHVResult.addAll(processesVector)

            discoverMemory = Boolean.parseBoolean(Framework.getParameter('discoverMemory'))
            if discoverMemory:
                try:
                    wmi_dis_memory_lib.executeWmiQuery(client, OSHVResult, hostOsh)
                except:
                    errobj = errorobject.createError(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['memory', 'wmi'], 'Failed to discover memory by wmi')
                    logger.reportErrorObject(errobj)
                    logger.errorException('Failed to discover memory by wmi')

            discoverDisks = Boolean.parseBoolean(Framework.getParameter('discoverDisks'))
            if discoverDisks:
                try:
                    containerOsh = hostOsh or modeling.createHostOSH(client.getIpAddress())
                    NTCMD_HR_Dis_Disk_Lib.discoverDiskByWmic(client, OSHVResult, containerOsh)
                    NTCMD_HR_Dis_Disk_Lib.discoverPhysicalDiskByWmi(client, OSHVResult, containerOsh)

                except:
                    errobj = errorobject.createError(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['disks', 'wmi'], 'Failed to discover disks by wmi')
                    logger.reportErrorObject(errobj)
                    logger.errorException('Failed to discover disks by wmi')

            discoverDrivers = Boolean.parseBoolean(Framework.getParameter('discoverDrivers'))
            if discoverDrivers:
                try:
                    containerOsh = hostOsh or modeling.createHostOSH(client.getIpAddress())
                    HR_Dis_Driver_Lib.discoverDriverByWmi(client, OSHVResult, containerOsh)

                except:
                    errobj = errorobject.createError(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['drivers', 'wmi'], 'Failed to discover drivers by wmi')
                    logger.reportErrorObject(errobj)
                    logger.errorException('Failed to discover drivers by wmi')

            discoveriSCSIInfo = Boolean.parseBoolean(Framework.getParameter('discoveriSCSIInfo'))
            if discoveriSCSIInfo:
                try:
                    NTCMD_HR_Dis_Disk_Lib.discoveriSCSIInfo(client,OSHVResult, containerOsh)
                except:
                    logger.warn('Failed to connect with namespace Root\Microsoft\Windows\Storage')

            discoverCPUs = Boolean.parseBoolean(Framework.getParameter('discoverCPUs'))
            if discoverCPUs:
                try:
                    wmi_dis_cpu_lib.executeWmiQuery(client, OSHVResult, hostOsh)
                except:
                    errobj = errorobject.createError(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['cpus', 'wmi'], 'Failed to discover cpus by wmi')
                    logger.reportErrorObject(errobj)
                    logger.errorException('Failed to discover cpus by wmi')

            discoverServices = Boolean.parseBoolean(Framework.getParameter('discoverServices'))
            servicesByCmd = Hashtable()
            if discoverServices:
                try:
                    servOSHV = wmi_dis_service_lib.executeWmiQuery(client, OSHVResult, servicesByCmd, hostOsh)
                    OSHVResult.addAll(servOSHV)
                except:
                    errobj = errorobject.createError(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['services', 'wmi'], 'Failed to discover services by wmi')
                    logger.reportErrorObject(errobj)
                    logger.errorException('Failed to discover services by wmi')

            #NOTE: software discovery had to be the last in discovery chain
            discoverSoftware = Boolean.parseBoolean(Framework.getParameter('discoverInstalledSoftware'))
            softNameToInstSoftOSH = {}
            if discoverSoftware:
                (softNameToInstSoftOSH, client) = __discoverInstalledSoftware(Framework, OSHVResult, client)

            if not client:
                logger.warn("Application Signature will not be run since the client is not initialized")

            if client:

                appSign = applications.createApplicationSignature(Framework, client)
                if processes:
                    appSign.setProcessesManager(applications.ProcessesManager(processes, []))
                servicesInfo = applications.ServicesInfo(servicesByCmd)
                appSign.setServicesInfo(servicesInfo)

                softwareInfo = applications.InstalledSoftwareInfo(None, softNameToInstSoftOSH)
                appSign.setInstalledSoftwareInfo(softwareInfo)

                appSign.getApplicationsTopology(hostId)

    finally:
        if client != None:
            client.close()
    return OSHVResult
def DiscoveryMain(Framework):
    vector = ObjectStateHolderVector()
    protocol = Framework.getDestinationAttribute('Protocol')
    client = None
    shell = None
    try:
        try:
            # connect to destination
            client = Framework.createClient()
            # wrap client with Shell
            shell = shellutils.ShellFactory().createShell(client)
        except AuthenticationException, ae:
            errormessages.resolveAndReport(ae.getMessage(), protocol, Framework)
        except:
            exInfo = logger.prepareJythonStackTrace('')
            errormessages.resolveAndReport(exInfo, protocol, Framework)
        else:

            language = Framework.getDestinationAttribute('language')
            hostCmdbId = Framework.getDestinationAttribute('hostId')

            # configure internationalization support
            if language and language != 'NA':
                langBund = Framework.getEnvironmentInformation().getBundle('langHost_Resources_By_TTY', language)
            else:
                langBund = Framework.getEnvironmentInformation().getBundle('langHost_Resources_By_TTY')

            # discovery
            discoverCPUs = Boolean.parseBoolean(Framework.getParameter('discoverCPUs'))
            discoverDisks = Boolean.parseBoolean(Framework.getParameter('discoverDisks'))
            discoveriSCSIInfo = Boolean.parseBoolean(Framework.getParameter('discoveriSCSIInfo'))
            discoverDrivers = Boolean.parseBoolean(Framework.getParameter('discoverDrivers'))
            discoverMemory = Boolean.parseBoolean(Framework.getParameter('discoverMemory'))
            discoverSoftware = Boolean.parseBoolean(Framework.getParameter('discoverInstalledSoftware'))
            discoverUsers = Boolean.parseBoolean(Framework.getParameter('discoverUsers'))
            discoverServices = Boolean.parseBoolean(Framework.getParameter('discoverServices'))
            discoverShares = Boolean.parseBoolean(Framework.getParameter('discoverShares'))
            discoverP2P = Boolean.parseBoolean(Framework.getParameter('discoverP2P'))

            try:

                hostOsh = modeling.createOshByCmdbIdString('host', hostCmdbId)

                if discoverShares:
                    try:
                        vector.addAll(_discoverSharedResources(shell, hostOsh))
                    except:
                        errorMessage = 'Failed to discover shared resources'
                        _logWarn(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['share', protocol], errorMessage)

                if discoverCPUs:
                    try:
                        vector.addAll(TTY_HR_CPU_Lib.disWinOS(hostCmdbId, shell, Framework, langBund))
                    except:
                        errorMessage = 'Failed to discover CPUs'
                        _logWarn(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['cpus', protocol], errorMessage)

                if discoverDisks:
                    try:
                        vector.addAll(TTY_HR_Disk_Lib.disWinOS(hostOsh, shell))
                    except:
                        errorMessage = 'Failed to discover disks'
                        _logWarn(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['disks', protocol], errorMessage)

                if discoverDrivers and shell.isWinOs():
                    try:
                        vector.addAll(_discoverWindowsDeviceDriver(shell, hostOsh))
                    except:
                        errorMessage = 'Failed to discover windows device driver by powershell'
                        _logWarn(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['windows device driver', protocol], errorMessage)

                if discoveriSCSIInfo:
                    try:
                        vector.addAll(TTY_HR_Disk_Lib.disWinOSiSCSIInfo(hostOsh, shell))
                    except:
                        errorMessage = 'Failed to discover iSCSI info'
                        _logWarn(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['iSCSI', protocol], errorMessage)

                if discoverMemory:
                    try:
                        vector.addAll(TTY_HR_Memory_Lib.disWinOS(hostCmdbId, shell, Framework, langBund))
                    except:
                        errorMessage = 'Failed to discover memory'
                        _logWarn(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['memory', protocol], errorMessage)

                processes = []
                try:
                    processesDiscoverer = process_discoverer.DiscovererByShellOnWindows(shell)
                    processes = processesDiscoverer.discoverAllProcesses()
                    if not processes:
                        raise ValueError()
                except:
                    errorMessage = 'Failed to discover processes'
                    _logWarn(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['processes', protocol], errorMessage)

                if processes:
                    # save processes to DB
                    process_discoverer.saveProcessesToProbeDb(processes, hostCmdbId, Framework)

                    # report processes
                    discoverProcesses = Boolean.parseBoolean(Framework.getParameter('discoverProcesses'))
                    if discoverProcesses:
                        processReporter = process.Reporter()
                        for processObject in processes:
                            processVector = processReporter.reportProcess(hostOsh, processObject)
                            vector.addAll(processVector)

                if discoverUsers:
                    try:
                        vector.addAll(_discoverUsers(hostOsh, shell))
                    except:
                        errorMessage = 'Failed to discover users'
                        _logWarn(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['users', protocol], errorMessage)

                cmdLineToInstalledSoftware = {}
                softNameToInstSoftOSH = {}
                if discoverSoftware:
                    try:
                        softwareOSHs = TTY_HR_Software_Lib.disWinOS(hostCmdbId, shell, Framework, langBund, softNameToInstSoftOSH)
                        if softwareOSHs:
                            vector.addAll(softwareOSHs)
                    except:
                        errorMessage = 'Failed to discover installed software'
                        _logWarn(errorcodes.FAILED_DISCOVERING_RESOURCE, ['installed software', protocol], errorMessage)

                servicesByCmd = Hashtable()
                if discoverServices:
                    try:
                        srvcOSHs = NTCMD_HR_REG_Service_Lib.doService(shell, hostOsh, servicesByCmd, langBund, Framework)
                        vector.addAll(srvcOSHs)
                    except:
                        errorMessage = 'Failed to discover services'
                        _logWarn(errorcodes.FAILED_DISCOVERING_RESOURCE, ['services', protocol], errorMessage)

                connectivityEndPoints = []
                try:
                    tcpDiscoverer = Dis_TCP.getDiscovererByShell(client, Framework, shell)
                    if tcpDiscoverer is not None:
                        tcpDiscoverer.discoverTCP()
                        connectivityEndPoints = tcpDiscoverer.getProcessEndPoints()
                except:
                    errorMessage = 'Failed to run TCP discovery'
                    _logWarn(errorcodes.FAILED_RUNNING_DISCOVERY_WITH_CLIENT_TYPE, ['tcp', protocol], errorMessage)

                appSign = applications.createApplicationSignature(Framework, client, shell)

                if processes:
                    appSign.setProcessesManager(applications.ProcessesManager(processes, connectivityEndPoints))

                servicesInfo = applications.ServicesInfo(servicesByCmd)
                appSign.setServicesInfo(servicesInfo)

                softwareInfo = applications.InstalledSoftwareInfo(cmdLineToInstalledSoftware, softNameToInstSoftOSH)
                appSign.setInstalledSoftwareInfo(softwareInfo)

                appSign.getApplicationsTopology(hostCmdbId)

                if discoverP2P:
                    try:
                        p2p = process_to_process.ProcessToProcess(Framework)
                        p2p.getProcessesToProcess()
                    except:
                        errorMessage = 'Failed to run p2p discovery'
                        _logWarn(errorcodes.FAILED_RUNNING_DISCOVERY, ['p2p', protocol], errorMessage)

            except JException, ex:
                exInfo = ex.getMessage()
                errormessages.resolveAndReport(exInfo, protocol, Framework)
            except:
Exemplo n.º 5
0
def DiscoveryMain(Framework):
    OSHVResult = ObjectStateHolderVector()
    client = None
    framework = StatisticsFramework(Framework)
    try:
        try:
            client = framework.createClient()
        except:
#            No need to report twice, exception msg is sufficient
#            errobj = errorobject.createError(errorcodes.CONNECTION_FAILED_NO_PROTOCOL, None, 'Connection failed')
#            logger.reportErrorObject(errobj)
            errMsg ='Exception while creating %s client: %s' % (ClientsConsts.SNMP_PROTOCOL_NAME, sys.exc_info()[1])
            errormessages.resolveAndReport(str(sys.exc_info()[1]), ClientsConsts.SNMP_PROTOCOL_NAME, framework)
            logger.debugException(errMsg)
        else:

            config = (flow.DiscoveryConfigBuilder(framework)
                      .dest_data_required_params_as_str('ip_address')
                      .dest_data_params_as_list('host_ips')
                      ).build()
            ipaddress = config.ip_address
            host_ips = filter(None, config.host_ips)

            ips = set(host_ips)
            ips.add(ipaddress)

            hostId = framework.getDestinationAttribute('hostId')
            hostOsh = modeling.createOshByCmdbIdString('host', hostId)

            discoverUsers = Boolean.parseBoolean(framework.getParameter('discoverUsers'))
            if discoverUsers:
                logger.debug('Starting to discover users')
                try:
                    snmp_dis_user_lib.doQueryOSUsers(client, OSHVResult)
                except:
                    errobj = errorobject.createError(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['users', 'snmp'], 'Failed to discover users by snmp')
                    logger.reportWarningObject(errobj)
                    logger.errorException('Failed to discover users by snmp')


            discoverDisks = Boolean.parseBoolean(framework.getParameter('discoverDisks'))
            if discoverDisks:
                logger.debug('Starting to discover disks')
                try:
                    snmp_dis_disk_lib.doQueryDisks(client, OSHVResult)
                except:
                    errobj = errorobject.createError(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['disks', 'snmp'], 'Failed to discover disks by snmp')
                    logger.reportWarningObject(errobj)
                    logger.errorException('Failed to discover disks by snmp')


            discoverProcesses = Boolean.parseBoolean(framework.getParameter('discoverProcesses'))
            processes = []

            logger.debug('Starting to discover processes')
            try:

                processDiscoverer = process_discoverer.getDiscovererBySnmp(client)
                processes = processDiscoverer.discoverAllProcesses()
                if not processes:
                    raise ValueError()
            except:
                errobj = errorobject.createError(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['processes', 'snmp'], 'Failed to discover processes by snmp')
                logger.reportWarningObject(errobj)
                logger.errorException('Failed to discover processes by snmp')

            if processes:

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

                # report processes
                if discoverProcesses:
                    processReporter = process.Reporter()
                    for processObject in processes:
                        processesVector = processReporter.reportProcess(hostOsh, processObject)
                        OSHVResult.addAll(processesVector)


            discoverServices = Boolean.parseBoolean(framework.getParameter('discoverServices'))
            if discoverServices:
                logger.debug('Starting to discover services')
                try:
                    snmp_dis_service_lib.doQuerySNMPService(client, OSHVResult)
                except:
                    errobj = errorobject.createError(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['services', 'snmp'], 'Failed to discover services by snmp')
                    logger.reportWarningObject(errobj)
                    logger.errorException('Failed to discover services by snmp')


            discoverSoftware = Boolean.parseBoolean(framework.getParameter('discoverInstalledSoftware'))
            if discoverSoftware:
                logger.debug('Starting to discover software')
                try:
                    snmp_dis_software_lib.doQuerySoftware(client, OSHVResult)
                except:
                    errobj = errorobject.createError(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['software', 'snmp'], 'Failed to discover software by snmp')
                    logger.reportWarningObject(errobj)
                    logger.errorException('Failed to discover software by snmp')

            connectivityEndPoints = []
            try:
                tcpDiscoverer = Dis_TCP.TCPDisBySNMP(client, framework, ips=tuple(ips))
                if tcpDiscoverer is not None:
                    tcpDiscoverer.discoverTCP()
                    connectivityEndPoints = tcpDiscoverer.getProcessEndPoints()
            except:
                errorMessage = 'Failed to run tcp discovery by snmp'
                logger.debugException(errorMessage)
                errobj = errorobject.createError(errorcodes.FAILED_RUNNING_DISCOVERY_WITH_CLIENT_TYPE, ['tcp', 'snmp'], errorMessage)
                logger.reportWarningObject(errobj)

            if processes:
                appSign = applications.createApplicationSignature(framework, client)
                appSign.setProcessesManager(applications.ProcessesManager(processes, connectivityEndPoints))
                appSign.getApplicationsTopology(hostId)

            discoverModules = Boolean.parseBoolean(framework.getParameter('discoverModules'))
            if discoverModules:
                logger.debug('Begin discover snmp modules...')
                try:
                    from snmp_model_finder import SnmpStateHolder
                    from snmp_model_finder import SnmpQueryHelper
                    from snmp_model_finder import ModelTypeMatcher
                    import snmp_model_discovery

                    snmpStateHolder = SnmpStateHolder()
                    cacheClient = None
                    # from discovery.client.snmp import SnmpClientCacheProxy

                    # cacheClient = SnmpClientCacheProxy(client, 'cache/'+ipaddress+'.cache')
                    snmpQueryHelper = SnmpQueryHelper(client)
                    mtm = ModelTypeMatcher(snmpStateHolder, snmpQueryHelper)
                    logger.debug('The target is matched:', mtm.match())
                    logger.debug('The type of the target is:', snmpStateHolder.getTypes())
                    vector = snmp_model_discovery.discoverAll(hostOsh, snmpStateHolder, snmpQueryHelper)
                    logger.debug('Discovered CI count:', vector.size())
                    OSHVResult.addAll(vector)
                    if cacheClient:
                        cacheClient.writeCache()
                except:
                    errobj = errorobject.createError(errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['modules', 'snmp'],
                                                     'Failed to discover modules by snmp')
                    logger.reportWarningObject(errobj)
                    logger.errorException('Failed to discover modules by snmp')
                logger.debug('End discover snmp modules')

    finally:
        if client != None:
            client.close()
    if OSHVResult.size() == 0 and framework.getSentObjectsCount() == 0:
        logger.reportWarning('SNMP: No data collected')
    return OSHVResult
Exemplo n.º 6
0
def DiscoveryMain(Framework):
    OSHVResult = ObjectStateHolderVector()
    client = None
    try:
        client = createWmiClient(Framework)

        if client:
            protocol = Framework.getDestinationAttribute('Protocol')
            hostId = Framework.getDestinationAttribute('hostId')
            hostOsh = modeling.createOshByCmdbIdString('nt', hostId)

            _, warning = discover_or_warn('fibre channel HBAs',
                                          discover_fc_hbas,
                                          Framework,
                                          hostOsh,
                                          protocol,
                                          OSHVResult,
                                          protocol_name=protocol)
            if warning:
                logger.reportWarningObject(warning)

            discoverUsers = Boolean.parseBoolean(
                Framework.getParameter('discoverUsers'))
            if discoverUsers:
                try:
                    wmi_dis_user_lib.executeWmiQuery(client, Framework,
                                                     OSHVResult, hostOsh)
                except:
                    errobj = errorobject.createError(
                        errorcodes.
                        FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                        ['users', 'wmi'], 'Failed to discover users by wmi')
                    logger.reportErrorObject(errobj)
                    logger.errorException('Failed to discover users by wmi')

            discoverShares = Boolean.parseBoolean(
                Framework.getParameter('discoverShares'))
            if discoverShares:
                try:
                    wmi_dis_share_lib.executeWmiQuery(client, OSHVResult,
                                                      hostOsh)
                except:
                    errobj = errorobject.createError(
                        errorcodes.
                        FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                        ['shares', 'wmi'], 'Failed to discover shares by wmi')
                    logger.reportErrorObject(errobj)
                    logger.errorException('Failed to discover shares by wmi')

            discoverProcesses = Boolean.parseBoolean(
                Framework.getParameter('discoverProcesses'))
            processes = []

            try:
                processDiscoverer = process_discoverer.getDiscovererByWmi(
                    client)
                processes = processDiscoverer.discoverAllProcesses()
                if not processes:
                    raise ValueError()
            except:
                errobj = errorobject.createError(
                    errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                    ['processes', 'wmi'],
                    'Failed to discover processes by wmi')
                logger.reportErrorObject(errobj)
                logger.errorException('Failed to discover processes by wmi')

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

                # report processes
                if discoverProcesses:
                    processReporter = process.Reporter()
                    for processObject in processes:
                        processesVector = processReporter.reportProcess(
                            hostOsh, processObject)
                        OSHVResult.addAll(processesVector)

            discoverMemory = Boolean.parseBoolean(
                Framework.getParameter('discoverMemory'))
            if discoverMemory:
                try:
                    wmi_dis_memory_lib.executeWmiQuery(client, OSHVResult,
                                                       hostOsh)
                except:
                    errobj = errorobject.createError(
                        errorcodes.
                        FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                        ['memory', 'wmi'], 'Failed to discover memory by wmi')
                    logger.reportErrorObject(errobj)
                    logger.errorException('Failed to discover memory by wmi')

            discoverDisks = Boolean.parseBoolean(
                Framework.getParameter('discoverDisks'))
            if discoverDisks:
                try:
                    containerOsh = hostOsh or modeling.createHostOSH(
                        client.getIpAddress())
                    NTCMD_HR_Dis_Disk_Lib.discoverDiskByWmic(
                        client, OSHVResult, containerOsh)
                    NTCMD_HR_Dis_Disk_Lib.discoverPhysicalDiskByWmi(
                        client, OSHVResult, containerOsh)

                except:
                    errobj = errorobject.createError(
                        errorcodes.
                        FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                        ['disks', 'wmi'], 'Failed to discover disks by wmi')
                    logger.reportErrorObject(errobj)
                    logger.errorException('Failed to discover disks by wmi')

            discoverDrivers = Boolean.parseBoolean(
                Framework.getParameter('discoverDrivers'))
            if discoverDrivers:
                try:
                    containerOsh = hostOsh or modeling.createHostOSH(
                        client.getIpAddress())
                    HR_Dis_Driver_Lib.discoverDriverByWmi(
                        client, OSHVResult, containerOsh)

                except:
                    errobj = errorobject.createError(
                        errorcodes.
                        FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                        ['drivers', 'wmi'],
                        'Failed to discover drivers by wmi')
                    logger.reportErrorObject(errobj)
                    logger.errorException('Failed to discover drivers by wmi')

            discoveriSCSIInfo = Boolean.parseBoolean(
                Framework.getParameter('discoveriSCSIInfo'))
            if discoveriSCSIInfo:
                try:
                    NTCMD_HR_Dis_Disk_Lib.discoveriSCSIInfo(
                        client, OSHVResult, containerOsh)
                except:
                    logger.warn(
                        'Failed to connect with namespace Root\Microsoft\Windows\Storage'
                    )

            discoverCPUs = Boolean.parseBoolean(
                Framework.getParameter('discoverCPUs'))
            if discoverCPUs:
                try:
                    wmi_dis_cpu_lib.executeWmiQuery(client, OSHVResult,
                                                    hostOsh)
                except:
                    errobj = errorobject.createError(
                        errorcodes.
                        FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                        ['cpus', 'wmi'], 'Failed to discover cpus by wmi')
                    logger.reportErrorObject(errobj)
                    logger.errorException('Failed to discover cpus by wmi')

            discoverServices = Boolean.parseBoolean(
                Framework.getParameter('discoverServices'))
            servicesByCmd = Hashtable()
            if discoverServices:
                try:
                    servOSHV = wmi_dis_service_lib.executeWmiQuery(
                        client, OSHVResult, servicesByCmd, hostOsh)
                    OSHVResult.addAll(servOSHV)
                except:
                    errobj = errorobject.createError(
                        errorcodes.
                        FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                        ['services', 'wmi'],
                        'Failed to discover services by wmi')
                    logger.reportErrorObject(errobj)
                    logger.errorException('Failed to discover services by wmi')

            #NOTE: software discovery had to be the last in discovery chain
            discoverSoftware = Boolean.parseBoolean(
                Framework.getParameter('discoverInstalledSoftware'))
            softNameToInstSoftOSH = {}
            if discoverSoftware:
                (softNameToInstSoftOSH,
                 client) = __discoverInstalledSoftware(Framework, OSHVResult,
                                                       client)

            if not client:
                logger.warn(
                    "Application Signature will not be run since the client is not initialized"
                )

            if client:

                appSign = applications.createApplicationSignature(
                    Framework, client)
                if processes:
                    appSign.setProcessesManager(
                        applications.ProcessesManager(processes, []))
                servicesInfo = applications.ServicesInfo(servicesByCmd)
                appSign.setServicesInfo(servicesInfo)

                softwareInfo = applications.InstalledSoftwareInfo(
                    None, softNameToInstSoftOSH)
                appSign.setInstalledSoftwareInfo(softwareInfo)

                appSign.getApplicationsTopology(hostId)

    finally:
        if client != None:
            client.close()
    return OSHVResult
Exemplo n.º 7
0
                        for connectivityEndPoint in connectivityEndPoints:
                            processid = connectivityEndPoint.getKey()
                            endpoints = connectivityEndPoint.getEndpoints()
                            for processObject in processes:
                                if 4 < processid == processObject.getPid() and processObject.getName() != "svchost.exe":
                                    processOSH = processReporter.reportProcessOsh(hostOsh, processObject)
                                    for endpoint in endpoints:
                                        builder = netutils.ServiceEndpointBuilder()
                                        reporter = netutils.EndpointReporter(builder)
                                        ipServerOSH = reporter.reportEndpoint(endpoint, hostOsh)
                                        linkOsh = modeling.createLinkOSH("usage", processOSH, ipServerOSH)
                                        linkOshv.add(linkOsh)
                                    break
                        OSHVResult.addAll(linkOshv)

                    appSign = applications.createApplicationSignature(Framework, originClient, shell)
                    if processes:
                        appSign.setProcessesManager(applications.ProcessesManager(processes, connectivityEndPoints))

                    servicesInfo = applications.ServicesInfo(servicesByCmd)
                    appSign.setServicesInfo(servicesInfo)

                    softwareInfo = applications.InstalledSoftwareInfo(cmdLineToInstalledSoftware, softNameToInstSoftOSH)
                    appSign.setInstalledSoftwareInfo(softwareInfo)

                    appSign.getApplicationsTopology(hostId)
                    if discoverP2P:
                        try:
                            p2p = process_to_process.ProcessToProcess(Framework)
                            p2p.getProcessesToProcess()
                        except:
Exemplo n.º 8
0
def DiscoveryMain(Framework):
    OSHVResult = ObjectStateHolderVector()
    client = None
    framework = StatisticsFramework(Framework)
    try:
        try:
            client = framework.createClient()
        except:
            #            No need to report twice, exception msg is sufficient
            #            errobj = errorobject.createError(errorcodes.CONNECTION_FAILED_NO_PROTOCOL, None, 'Connection failed')
            #            logger.reportErrorObject(errobj)
            errMsg = 'Exception while creating %s client: %s' % (
                ClientsConsts.SNMP_PROTOCOL_NAME, sys.exc_info()[1])
            errormessages.resolveAndReport(str(sys.exc_info()[1]),
                                           ClientsConsts.SNMP_PROTOCOL_NAME,
                                           framework)
            logger.debugException(errMsg)
        else:

            config = (flow.DiscoveryConfigBuilder(
                framework).dest_data_required_params_as_str('ip_address').
                      dest_data_params_as_list('host_ips')).build()
            ipaddress = config.ip_address
            host_ips = filter(None, config.host_ips)

            ips = set(host_ips)
            ips.add(ipaddress)

            hostId = framework.getDestinationAttribute('hostId')
            hostOsh = modeling.createOshByCmdbIdString('host', hostId)

            discoverUsers = Boolean.parseBoolean(
                framework.getParameter('discoverUsers'))
            if discoverUsers:
                logger.debug('Starting to discover users')
                try:
                    snmp_dis_user_lib.doQueryOSUsers(client, OSHVResult)
                except:
                    errobj = errorobject.createError(
                        errorcodes.
                        FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                        ['users', 'snmp'], 'Failed to discover users by snmp')
                    logger.reportWarningObject(errobj)
                    logger.errorException('Failed to discover users by snmp')

            discoverDisks = Boolean.parseBoolean(
                framework.getParameter('discoverDisks'))
            if discoverDisks:
                logger.debug('Starting to discover disks')
                try:
                    snmp_dis_disk_lib.doQueryDisks(client, OSHVResult)
                except:
                    errobj = errorobject.createError(
                        errorcodes.
                        FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                        ['disks', 'snmp'], 'Failed to discover disks by snmp')
                    logger.reportWarningObject(errobj)
                    logger.errorException('Failed to discover disks by snmp')

            discoverProcesses = Boolean.parseBoolean(
                framework.getParameter('discoverProcesses'))
            processes = []

            logger.debug('Starting to discover processes')
            try:

                processDiscoverer = process_discoverer.getDiscovererBySnmp(
                    client)
                processes = processDiscoverer.discoverAllProcesses()
                if not processes:
                    raise ValueError()
            except:
                errobj = errorobject.createError(
                    errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                    ['processes', 'snmp'],
                    'Failed to discover processes by snmp')
                logger.reportWarningObject(errobj)
                logger.errorException('Failed to discover processes by snmp')

            if processes:

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

                # report processes
                if discoverProcesses:
                    processReporter = process.Reporter()
                    for processObject in processes:
                        processesVector = processReporter.reportProcess(
                            hostOsh, processObject)
                        OSHVResult.addAll(processesVector)

            discoverServices = Boolean.parseBoolean(
                framework.getParameter('discoverServices'))
            if discoverServices:
                logger.debug('Starting to discover services')
                try:
                    snmp_dis_service_lib.doQuerySNMPService(client, OSHVResult)
                except:
                    errobj = errorobject.createError(
                        errorcodes.
                        FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                        ['services', 'snmp'],
                        'Failed to discover services by snmp')
                    logger.reportWarningObject(errobj)
                    logger.errorException(
                        'Failed to discover services by snmp')

            discoverSoftware = Boolean.parseBoolean(
                framework.getParameter('discoverInstalledSoftware'))
            if discoverSoftware:
                logger.debug('Starting to discover software')
                try:
                    snmp_dis_software_lib.doQuerySoftware(client, OSHVResult)
                except:
                    errobj = errorobject.createError(
                        errorcodes.
                        FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                        ['software', 'snmp'],
                        'Failed to discover software by snmp')
                    logger.reportWarningObject(errobj)
                    logger.errorException(
                        'Failed to discover software by snmp')

            connectivityEndPoints = []
            try:
                tcpDiscoverer = Dis_TCP.TCPDisBySNMP(client,
                                                     framework,
                                                     ips=tuple(ips))
                if tcpDiscoverer is not None:
                    tcpDiscoverer.discoverTCP()
                    connectivityEndPoints = tcpDiscoverer.getProcessEndPoints()
            except:
                errorMessage = 'Failed to run tcp discovery by snmp'
                logger.debugException(errorMessage)
                errobj = errorobject.createError(
                    errorcodes.FAILED_RUNNING_DISCOVERY_WITH_CLIENT_TYPE,
                    ['tcp', 'snmp'], errorMessage)
                logger.reportWarningObject(errobj)

            if processes:
                appSign = applications.createApplicationSignature(
                    framework, client)
                appSign.setProcessesManager(
                    applications.ProcessesManager(processes,
                                                  connectivityEndPoints))
                appSign.getApplicationsTopology(hostId)

            discoverModules = Boolean.parseBoolean(
                framework.getParameter('discoverModules'))
            if discoverModules:
                logger.debug('Begin discover snmp modules...')
                try:
                    from snmp_model_finder import SnmpStateHolder
                    from snmp_model_finder import SnmpQueryHelper
                    from snmp_model_finder import ModelTypeMatcher
                    import snmp_model_discovery

                    snmpStateHolder = SnmpStateHolder()
                    cacheClient = None
                    # from discovery.client.snmp import SnmpClientCacheProxy

                    # cacheClient = SnmpClientCacheProxy(client, 'cache/'+ipaddress+'.cache')
                    snmpQueryHelper = SnmpQueryHelper(client)
                    mtm = ModelTypeMatcher(snmpStateHolder, snmpQueryHelper)
                    logger.debug('The target is matched:', mtm.match())
                    logger.debug('The type of the target is:',
                                 snmpStateHolder.getTypes())
                    vector = snmp_model_discovery.discoverAll(
                        hostOsh, snmpStateHolder, snmpQueryHelper)
                    logger.debug('Discovered CI count:', vector.size())
                    OSHVResult.addAll(vector)
                    if cacheClient:
                        cacheClient.writeCache()
                except:
                    errobj = errorobject.createError(
                        errorcodes.
                        FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE,
                        ['modules', 'snmp'],
                        'Failed to discover modules by snmp')
                    logger.reportWarningObject(errobj)
                    logger.errorException('Failed to discover modules by snmp')
                logger.debug('End discover snmp modules')

    finally:
        if client != None:
            client.close()
    if OSHVResult.size() == 0 and framework.getSentObjectsCount() == 0:
        logger.reportWarning('SNMP: No data collected')
    return OSHVResult
def processContainerInfo(shell, skipDockerVolume, filesystemDict, containerInspectOutput, containerInfo, imageDict, containerDict, containerLinks, dockerDaemonOSH, nodeOSH, client, Framework, OSHVResult):
    json = _JSONs()
    jsonOutput = json.loads(containerInspectOutput)
    inspectJsonObj = jsonOutput[0]

    imageName = containerInfo[1]
    containerName = containerInfo[2]
    if len(containerInfo) == 4:
        containerPorts = containerInfo[3]
    else:
        portsArray = []
        ports = inspectJsonObj['NetworkSettings']['Ports']
        port_keys = ports.keys()
        port_keys.sort()
        for port in port_keys:
            if ports[port]:
                if ports[port][0]['HostIp'] and ports[port][0]['HostPort']:
                    portsArray.append(ports[port][0]['HostIp'] + ':' + ports[port][0]['HostPort'] + ' -> ' + port)
            else:
                portsArray.append(port)
        containerPorts = ', '.join(portsArray)

    # get container related image
    containerId = inspectJsonObj['Id']
    imageId = inspectJsonObj['Image']
    containerOSH = ObjectStateHolder('docker_container')
    containerOSH.setAttribute('name', containerName)
    containerOSH.setAttribute('docker_container_id', containerId)
    containerOSH.setAttribute('docker_image_id', imageId)
    containerOSH.setAttribute('docker_image', imageName)
    containerDict[containerId] = containerOSH

    # set container ports
    containerOSH.setAttribute('docker_container_ports', containerPorts)

    OSHVResult.add(containerOSH)
    containerOSH.setContainer(dockerDaemonOSH)
    daemonContainerLink = modeling.createLinkOSH('manage', dockerDaemonOSH, containerOSH)
    OSHVResult.add(daemonContainerLink)

    # get container links
    if inspectJsonObj['HostConfig']['Links'] is not None:
        for link in inspectJsonObj['HostConfig']['Links']:
            linkedContainer = link.split(':')[0].split('/')[1]
            containerInspectCMD = 'docker inspect -f {{.Id}} ' + linkedContainer
            linkedContainerId = shell.execCmd(containerInspectCMD)
            if shell.getLastCmdReturnCode() == 0:
                linkedContainerId = linkedContainerId.strip()
                containerLinks[containerId] = linkedContainerId
            else:
                Framework.reportError(('Failed in command: docker inspect linked container <%s>.' % linkedContainer))

    # link image and container
    logger.debug('imageOSH: ', imageDict[imageId])
    logger.debug('containerOSH: ', containerOSH)
    imageContainerLink = modeling.createLinkOSH('realization', imageDict[imageId], containerOSH)
    OSHVResult.add(imageContainerLink)

    # get running software in container
    discoverRSinDockerContainer = Framework.getParameter('discoverRunningSW')
    if discoverRSinDockerContainer == 'true':
        topCmd = 'docker top ' + containerId
        topOutput = shell.execCmd(topCmd)
        logger.debug('docker top container: ', imageName)
        processList = []

        topLines = None
        if shell.getLastCmdReturnCode() == 0:
            topLines = checkLastCmd(topOutput)
        else:
            Framework.reportWarning(('Failed in command: docker top container <%s>.' % containerId))
        if topLines:
            topcount = 0
            for topLine in topLines:
                topcount += 1
                if topcount == 1:
                    continue
                topLine = topLine.strip()

                matcher = re.match(r'\s*(\w+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(.+)\s+(.+)\s+(\d+):(\d+):(\d+)\s+(.+)', topLine)
                if matcher:
                    owner = matcher.group(1)
                    pid = matcher.group(2)
                    commandLine = matcher.group(10)
                    fullCommand = None
                    argumentsLine = None

                    if commandLine:
                        tokens = re.split(r"\s+", commandLine, 1)
                        fullCommand = tokens[0]
                        if len(tokens) > 1:
                            argumentsLine = tokens[1]

                    commandName = fullCommand
                    commandPath = None
                    matcher = re.match(r"(.*/)([^/]+)$", fullCommand)
                    if matcher:
                        commandName = matcher.group(2)
                        commandPath = fullCommand

                    process = process_module.Process(commandName, pid, commandLine)
                    logger.debug('process generated: ', process)
                    process.argumentLine = argumentsLine
                    process.owner = owner
                    process.executablePath = commandPath
                    processList.append(process)

        if len(processList) > 0:
            logger.debug('start apply to: ', containerOSH)
            appSign = applications.createApplicationSignature(Framework, client, shell)
            logger.debug('created ApplicationSignature: ', containerOSH)
            appSign.setProcessesManager(applications.ProcessesManager(processList, None))
            logger.debug('ProcessesManager: ', containerOSH)
            appSign.getApplicationsTopology(containerOSH)
            logger.debug('finish apply to: ', containerOSH)

    # get container volumes
    if not skipDockerVolume:
        if inspectJsonObj.has_key('Mounts') and inspectJsonObj['Mounts']:
            mountResults = inspectJsonObj['Mounts']
            for mountStr in mountResults:
                mount = mountStr
                dockerVolumeOSH = ObjectStateHolder('docker_volume')
                dockerVolumeOSH.setAttribute('name', 'Docker Volume')
                dockerVolumeOSH.setAttribute('dockervolume_source', mount['Source'])
                dockerVolumeOSH.setAttribute('dockervolume_destination', mount['Destination'])
                if mount['RW'] == 'true':
                    dockerVolumeOSH.setAttribute('logicalvolume_accesstype', 'RW')
                else:
                    dockerVolumeOSH.setAttribute('logicalvolume_accesstype', 'R')
                OSHVResult.add(dockerVolumeOSH)
                volumeContainerLink = modeling.createLinkOSH('usage', containerOSH, dockerVolumeOSH)
                OSHVResult.add(volumeContainerLink)
                linkDockerVolumeToLv(mount['Source'], filesystemDict, nodeOSH, dockerVolumeOSH, OSHVResult)

        elif inspectJsonObj.has_key('Volumes') and inspectJsonObj.has_key('VolumesRW') and inspectJsonObj['Volumes']:
            dockerVolumeOSH = ObjectStateHolder('docker_volume')
            dockerVolumeOSH.setAttribute('name', 'Docker Volume')
            OSHVResult.add(dockerVolumeOSH)
            volumeContainerLink = modeling.createLinkOSH('usage', containerOSH, dockerVolumeOSH)
            OSHVResult.add(volumeContainerLink)
            volumResults = inspectJsonObj['Volumes']
            for (dst, src) in volumResults.items():
                dockerVolumeOSH.setAttribute('dockervolume_source', src)
                dockerVolumeOSH.setAttribute('dockervolume_destination', dst)
                if inspectJsonObj['VolumesRW'][dst] == 'true':
                    dockerVolumeOSH.setAttribute('logicalvolume_accesstype', 'RW')
                else:
                    dockerVolumeOSH.setAttribute('logicalvolume_accesstype', 'R')
                linkDockerVolumeToLv(src, filesystemDict, nodeOSH, dockerVolumeOSH, OSHVResult)
    else:
        logger.debug('Skip Docker Volume since filesystem is not find!')