def _getCandidateCredentials(Framework, remoteUDAUserName, ip):
    candidates = []

    connectedUserName = str(remoteUDAUserName)

    if connectedUserName == 'root':
        logger.debug('Connected credential id is irrelevant for this host - connected to root user, no need for sudo')
    else:

        allCredIds = []

        # Getting all ssh and telnet credentials defined for the ip
        allCredIds.extend(netutils.getAvailableProtocols(Framework, ClientsConsts.SSH_PROTOCOL_NAME, ip))
        allCredIds.extend(netutils.getAvailableProtocols(Framework, ClientsConsts.TELNET_PROTOCOL_NAME, ip))

        for credentialId in allCredIds:
            credential = ProtocolManager.getProtocolById(credentialId)

            # Get connected protocol details
            userName = credential.getProtocolAttribute(Protocol.PROTOCOL_ATTRIBUTE_USERNAME, '')

            sudoCommands = credential.getProtocolAttribute(Protocol.SSH_PROTOCOL_ATTRIBUTE_SUDO_COMMANDS, '') or \
                           credential.getProtocolAttribute(Protocol.TELNET_PROTOCOL_ATTRIBUTE_SUDO_COMMANDS, '')

            # Filter out those that don't have sudo defined or do not share same username
            if connectedUserName == userName and sudoCommands and len(sudoCommands) > 0:
                candidates.append(credentialId)

    return candidates
Esempio n. 2
0
def DiscoveryMain(Framework): 
    warningsList = []
    errorsList = []
    oshvector = ObjectStateHolderVector()
    errobj = errorobject.INTERNAL_ERROR
    client = None

    ip_address = Framework.getDestinationAttribute('ip_address')
    ip_domain = Framework.getDestinationAttribute('ip_domain')
    credentials = netutils.getAvailableProtocols(Framework, protocolName, ip_address, ip_domain)  
  
    if len(credentials) == 0:
        msg = errormessages.makeErrorMessage(protocolName, pattern=errormessages.ERROR_NO_CREDENTIALS)
        errobj = errorobject.createError(errorcodes.NO_CREDENTIALS_FOR_TRIGGERED_IP, [protocolName], msg)
        warningsList.append(errobj)
        logger.debug(msg)
    else: 
        try:
            logger.info('Starting AS400 Connection.')
            for credential in credentials:
                client = Framework.createClient(credential)
                dicoverer = AS400Dicoverer(client)
                dicoverer.discover()
                oshvector.addAll(dicoverer.buildTopology())
        except NoClassDefFoundError, error:
            # Trying to catch if as400 java package is not found
            msg = error.getMessage()
            if re.search("as400", msg, re.I):
                processException(errorsList,warningsList,"Third party library is not found. Please read the documentation about prerequisites for this job.")
            else:
                processException(errorsList,warningsList, msg)
        except:
Esempio n. 3
0
def detectUDAAvailable(Framework, ip, credentialId=None):

    logger.debug("Start to detect UDA availability")
    if credentialId:
        credentialIds = [credentialId]
    else:
        import netutils

        credentialIds = netutils.getAvailableProtocols(
            Framework, ClientsConsts.DDM_AGENT_PROTOCOL_NAME, ip)

    props = Properties()
    props.setProperty('permitDDMi', 'true')
    props.setProperty(CollectorsConstants.DESTINATION_DATA_IP_ADDRESS, ip)
    for credentialId in credentialIds:
        props.setProperty(CollectorsConstants.ATTR_CREDENTIALS_ID,
                          credentialId)
        try:
            logger.debug("Start to connect UDA")
            client = Framework.createClient(props)
            if client:
                return client
        except:
            pass
    return None
Esempio n. 4
0
def getClient(Framework):
    macOnAgent = Framework.getTriggerCIDataAsList('mac_on_agent') or []
    applicationIp = Framework.getTriggerCIDataAsList("application_ip") or []
    ipAddressList = Framework.getTriggerCIDataAsList('ip_address') or []
    arpMacList = Framework.getTriggerCIDataAsList('mac_on_ip') or []
    credentialId = Framework.getTriggerCIData("credentials_id")
    possibleProtocol = ClientsConsts.SNMP_PROTOCOL_NAME

    candidateIPs = clientdiscoveryutils.getIPAddressListByApplicationMac(macOnAgent, applicationIp, arpMacList, ipAddressList)
    logger.debug("Try one by one on candidate ips:", candidateIPs)
    try:
        Framework.saveState(credentialId) #save the preferred credential id to framework.
        for candidateIP in candidateIPs:
            if candidateIP == 'NA':
                continue
            possibleCredentials = netutils.getAvailableProtocols(Framework, possibleProtocol, candidateIP, None) or []
            protocolList = []
            if possibleCredentials:
                for i in range(len(possibleCredentials)):
                    protocolList.append(possibleProtocol)

            [newIPList, newProtocolList, newCredentialsIdList, newCodepageList] = clientdiscoveryutils.buildConnectionList([candidateIP],
                protocolList, possibleCredentials)
            for cre in newCredentialsIdList:
                client = _createSnmpClient(Framework, candidateIP, cre)
                if client:
                    return client
    finally:
        Framework.clearState()  #clear the preferred credential id from framework

    return None
Esempio n. 5
0
def StepMain(Framework):
    # if we have shell credentials and we are able to connect with them then connect otherwise we should connect with agent

    ip = Framework.getDestinationAttribute('ip_address')
    domain = Framework.getDestinationAttribute('ip_domain')
    codepage = Framework.getCodePage()

    allShellProtocols = []
    allShellCredentials = []
    allShellIps = []
    allShellCodePages = []

    protocols = netutils.getAvailableProtocols(Framework, ClientsConsts.DDM_AGENT_PROTOCOL_NAME, ip, domain)

    for protocol in protocols:

        allShellProtocols.append(ClientsConsts.DDM_AGENT_PROTOCOL_NAME)
        allShellCredentials.append(protocol)
        allShellIps.append(ip)
        allShellCodePages.append(codepage)

    logger.debug('Will going to attempt to connect in this order: ', allShellCredentials)
    Framework.setProperty(InventoryUtils.STATE_PROPERTY_CONNECTION_PROTOCOLS, allShellProtocols)
    Framework.setProperty(InventoryUtils.STATE_PROPERTY_CONNECTION_CREDENIALS, allShellCredentials)
    Framework.setProperty(InventoryUtils.STATE_PROPERTY_CONNECTION_IPS, allShellIps)
    Framework.setProperty(InventoryUtils.STATE_PROPERTY_CONNECTION_CODEPAGES, allShellCodePages)

    InventoryUtils.executeStep(Framework, connectToRemoteNode, InventoryUtils.STEP_REQUIRES_CONNECTION, InventoryUtils.STEP_DOESNOT_REQUIRES_LOCK)
Esempio n. 6
0
def DiscoveryMain(Framework):
    ip = Framework.getTriggerCIData('ip_address')
    credentialIds = netutils.getAvailableProtocols(Framework, 'ucs', ip)
    if not credentialIds:
        logger.warn('No generic credential for UCS')
        Framework.reportWarning('No generic credential for UCS')
        return

    ucs_id = Framework.getTriggerCIData('ucs_id')

    originFramework = Framework
    connectionManager = None
    connectedCredentialId = None
    for credentialId in credentialIds:
        logger.debug('Begin trying credential id:', credentialId)
        params = {'credentialsId': credentialId}
        tmpFramework = MyFramework(originFramework, parameters=params)
        manager = FrameworkBasedConnectionDataManager(tmpFramework, ip)
        try:
            client = manager.getClient()
            if client:
                logger.debug("Connected")
                connectionManager = manager
                connectedCredentialId = credentialId
                break
        except:
            logger.debugException('')
            logger.debug('Can not connection by credential:', credentialId)
        finally:
            if connectionManager:
                connectionManager.closeClient()

    if connectionManager:
        logger.debug('Connected by credential Id:', connectedCredentialId)
        vec = ObjectStateHolderVector()
        hostOsh = modeling.createHostOSH(ip)
        appOsh = modeling.createApplicationOSH('running_software',
                                               'UCS',
                                               hostOsh,
                                               vendor='Cisco')
        appOsh.setAttribute('application_ip', ip)
        appOsh.setAttribute('credentials_id', connectedCredentialId)
        vec.add(hostOsh)
        vec.add(appOsh)
        return vec
    else:
        if ucs_id:
            logger.debug('Delete the ucs since it can not be connected:%s' %
                         ucs_id)
            softwareOsh = modeling.createOshByCmdbId('running_software',
                                                     ucs_id)
            Framework.deleteObject(softwareOsh)

        logger.warn(
            'All credentials have been tried. No credential can connect to UCS by ip %s'
            % ip)
        Framework.reportWarning(
            'All credentials have been tried. No credential can connect to UCS by ip %s'
            % ip)
Esempio n. 7
0
def DiscoveryMain(Framework):

    ip = Framework.getDestinationAttribute('ip_address')
    domain = Framework.getDestinationAttribute('ip_domain')
    codepage = Framework.getCodePage()

    shell = None
    credentialId = None
    OSHVResult = None
    warningsList = []
    errorsList = []

    protocolType = PowerShell.PROTOCOL_TYPE
    credentials = []
    # use the latest used credentials if any
    lastConnectedCredential = Framework.loadState()
    lastConnectedCredential and credentials.append(lastConnectedCredential)
    # and other defined for triggered IP
    credentials.extend(
        netutils.getAvailableProtocols(Framework, protocolType, ip, domain))
    if credentials:
        shell, credentialId = getShellUtils(Framework, protocolType,
                                            credentials, ip, codepage,
                                            warningsList, errorsList)
    else:
        msg = errormessages.makeErrorMessage(
            protocolType, pattern=errormessages.ERROR_NO_CREDENTIALS)
        errobj = errorobject.createError(
            errorcodes.NO_CREDENTIALS_FOR_TRIGGERED_IP, [protocolType], msg)
        errorsList.append(errobj)

    if shell:
        # successfully connected, do discovery
        errorsList = []
        warningsList = []
        Framework.saveState(credentialId)
        OSHVResult = doDiscovery(Framework, shell, ip, credentialId, codepage,
                                 protocolType, warningsList, errorsList)
    else:
        if not len(errorsList):
            msg = errormessages.makeErrorMessage(
                protocolType, pattern=errormessages.ERROR_CONNECTION_FAILED)
            errobj = errorobject.createError(errorcodes.CONNECTION_FAILED,
                                             [protocolType], msg)
            errorsList.append(errobj)
        Framework.clearState()

    for errobj in warningsList:
        logger.reportWarningObject(errobj)
    for errobj in errorsList:
        logger.reportErrorObject(errobj)

    return OSHVResult
Esempio n. 8
0
def mainFunction(Framework):
    warningsList = []
    errorsList = []
    _vector = ObjectStateHolderVector()
    ip_address = Framework.getDestinationAttribute('ip_address')
    macAddress = Framework.getDestinationAttribute('ip_mac_address')
    ip_domain = Framework.getDestinationAttribute('ip_domain')
    host_cmdbid = Framework.getDestinationAttribute('host_cmdbid')
    host_key = Framework.getDestinationAttribute('host_key')
    host_macs = Framework.getTriggerCIDataAsList('mac_addrs')
    ucmdb_version = modeling.CmdbClassModel().version()

    ip_address = __getIpObjectFromDestinationData(ip_address, macAddress)

    credentials = netutils.getAvailableProtocols(Framework, ClientsConsts.WMI_PROTOCOL_NAME, str(ip_address), ip_domain)
    if len(credentials) == 0:
        msg = errormessages.makeErrorMessage(protocolName, pattern=errormessages.ERROR_NO_CREDENTIALS)
        errobj = errorobject.createError(errorcodes.NO_CREDENTIALS_FOR_TRIGGERED_IP, [protocolName], msg)
        warningsList.append(errobj)
        logger.debug(msg)
        return (_vector, warningsList, errorsList)

    for credential in credentials:
        client = None
        try:
            debug_string = ip_domain + "\\" + str(ip_address)
            props = Properties()
            props.setProperty(CollectorsConstants.DESTINATION_DATA_IP_ADDRESS, str(ip_address))
            logger.debug('try to get wmi agent for: ', debug_string)
            client = Framework.createClient(credential, props)
            logger.debug('got wmi agent for: ', debug_string)

            hostForLinkOSH = modeling.createHostOSH(str(ip_address))

            # create wmi OSH
            wmiOSH = modeling.createWmiOSH(str(ip_address))
            wmiOSH.setAttribute('credentials_id', client.getCredentialId())
            wmiOSH.setContainer(hostForLinkOSH)

            _vector = doWMI(client, wmiOSH, ip_address, ip_domain, hostForLinkOSH, host_cmdbid, host_key, host_macs, ucmdb_version)

            if _vector.size() > 0:
                Framework.saveState(credential)
                del warningsList[:]
                del errorsList[:]
                break
        except Exception, ex:
            strException = str(ex.getMessage())
            shouldStop = errormessages.resolveAndAddToObjectsCollections(strException, protocolName, warningsList, errorsList)
            if shouldStop:
                break
        except:
Esempio n. 9
0
def _getCandidateCredentials(Framework, remoteUDAUserName, ip):
    candidates = []

    connectedUserName = str(remoteUDAUserName)

    if connectedUserName == 'root':
        logger.debug(
            'Connected credential id is irrelevant for this host - connected to root user, no need for sudo'
        )
    else:

        allCredIds = []

        # Getting all ssh and telnet credentials defined for the ip
        allCredIds.extend(
            netutils.getAvailableProtocols(Framework,
                                           ClientsConsts.SSH_PROTOCOL_NAME,
                                           ip))
        allCredIds.extend(
            netutils.getAvailableProtocols(Framework,
                                           ClientsConsts.TELNET_PROTOCOL_NAME,
                                           ip))

        for credentialId in allCredIds:
            credential = ProtocolManager.getProtocolById(credentialId)

            # Get connected protocol details
            userName = credential.getProtocolAttribute(
                Protocol.PROTOCOL_ATTRIBUTE_USERNAME, '')

            sudoCommands = credential.getProtocolAttribute(Protocol.SSH_PROTOCOL_ATTRIBUTE_SUDO_COMMANDS, '') or \
                           credential.getProtocolAttribute(Protocol.TELNET_PROTOCOL_ATTRIBUTE_SUDO_COMMANDS, '')

            # Filter out those that don't have sudo defined or do not share same username
            if connectedUserName == userName and sudoCommands and len(
                    sudoCommands) > 0:
                candidates.append(credentialId)

    return candidates
Esempio n. 10
0
def DiscoveryMain(Framework):
    ip = Framework.getTriggerCIData('ip_address')
    credentialIds = netutils.getAvailableProtocols(Framework, 'ucs', ip)
    if not credentialIds:
        logger.warn('No generic credential for UCS')
        Framework.reportWarning('No generic credential for UCS')
        return

    ucs_id = Framework.getTriggerCIData('ucs_id')

    originFramework = Framework
    connectionManager = None
    connectedCredentialId = None
    for credentialId in credentialIds:
        logger.debug('Begin trying credential id:', credentialId)
        params = {'credentialsId': credentialId}
        tmpFramework = MyFramework(originFramework, parameters=params)
        manager = FrameworkBasedConnectionDataManager(tmpFramework, ip)
        try:
            client = manager.getClient()
            if client:
                logger.debug("Connected")
                connectionManager = manager
                connectedCredentialId = credentialId
                break
        except:
            logger.debugException('')
            logger.debug('Can not connection by credential:', credentialId)
        finally:
            if connectionManager:
                connectionManager.closeClient()

    if connectionManager:
        logger.debug('Connected by credential Id:', connectedCredentialId)
        vec = ObjectStateHolderVector()
        hostOsh = modeling.createHostOSH(ip)
        appOsh = modeling.createApplicationOSH('running_software', 'UCS', hostOsh, vendor='Cisco')
        appOsh.setAttribute('application_ip', ip)
        appOsh.setAttribute('credentials_id', connectedCredentialId)
        vec.add(hostOsh)
        vec.add(appOsh)
        return vec
    else:
        if ucs_id:
            logger.debug('Delete the ucs since it can not be connected:%s' % ucs_id)
            softwareOsh = modeling.createOshByCmdbId('running_software', ucs_id)
            Framework.deleteObject(softwareOsh)

        logger.warn('All credentials have been tried. No credential can connect to UCS by ip %s' % ip)
        Framework.reportWarning('All credentials have been tried. No credential can connect to UCS by ip %s' % ip)
def DiscoveryMain(Framework):

    ip = Framework.getDestinationAttribute('ip_address')
    domain = Framework.getDestinationAttribute('ip_domain')
    codepage = Framework.getCodePage()

    shell = None
    credentialId = None
    OSHVResult = None
    warningsList = []
    errorsList = []

    protocolType = PowerShell.PROTOCOL_TYPE
    credentials = []
    # use the latest used credentials if any
    lastConnectedCredential = Framework.loadState()
    lastConnectedCredential and credentials.append(lastConnectedCredential)
    # and other defined for triggered IP
    credentials.extend(netutils.getAvailableProtocols(Framework, protocolType,
                                                      ip, domain))
    if credentials:
        shell, credentialId = getShellUtils(Framework, protocolType,
                                            credentials, ip, codepage,
                                            warningsList, errorsList)
    else:
        msg = errormessages.makeErrorMessage(protocolType, pattern=errormessages.ERROR_NO_CREDENTIALS)
        errobj = errorobject.createError(errorcodes.NO_CREDENTIALS_FOR_TRIGGERED_IP, [protocolType], msg)
        errorsList.append(errobj)

    if shell:
        # successfully connected, do discovery
        errorsList = []
        warningsList = []
        Framework.saveState(credentialId)
        OSHVResult = doDiscovery(Framework, shell, ip, credentialId, codepage, protocolType, warningsList, errorsList)
    else:
        if not len(errorsList):
            msg = errormessages.makeErrorMessage(protocolType, pattern=errormessages.ERROR_CONNECTION_FAILED)
            errobj = errorobject.createError(errorcodes.CONNECTION_FAILED, [protocolType], msg)
            errorsList.append(errobj)
        Framework.clearState()

    for errobj in warningsList:
        logger.reportWarningObject(errobj)
    for errobj in errorsList:
        logger.reportErrorObject(errobj)

    return OSHVResult
Esempio n. 12
0
def DiscoveryMain(Framework):
    OSHVResult = ObjectStateHolderVector()

    ip = Framework.getDestinationAttribute('ip')
    port = Framework.getDestinationAttribute('port')
    scp_id = Framework.getDestinationAttribute('scp_id')
    apache_id = Framework.getDestinationAttribute('apache_id')
    apache_ip = Framework.getDestinationAttribute('apache_ip')
    contexts = Framework.getTriggerCIDataAsList('contexts')
    context_ids = Framework.getTriggerCIDataAsList('context_ids')

    localShell = shellutils.ShellUtils(Framework.createClient(ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME))
    protocolName = ClientsConsts.HTTP_PROTOCOL_NAME
    connectionFailedMsgs = []
    protocolIds = findProperProtocolIds(ip, netutils.getAvailableProtocols(Framework, protocolName, ip) or [])

    if not protocolIds:
        msg = errormessages.makeErrorMessage(protocolName, pattern=errormessages.ERROR_NO_CREDENTIALS)
        errobj = errorobject.createError(errorcodes.NO_CREDENTIALS_FOR_TRIGGERED_IP, [protocolName], msg)
        logger.reportErrorObject(errobj)
        return OSHVResult

    httpClient, protocolId, version = findHttpProtocol(Framework, protocolName, protocolIds, ip, connectionFailedMsgs)
    if httpClient:
        apacheOsh = modeling.createOshByCmdbIdString('apache', apache_id)
        # create oam running software
        createOAMRunningSoftwareOsh(scp_id, ip, port, protocolId, version, apacheOsh, OSHVResult)
        # get oam policy content
        policy_content = getPolicyContent(httpClient, ip, protocolId, protocolName, version)
        # parse oam policy, get redirect urls
        authorization_policies = oam_policy_parser.parse_oam_policy(policy_content)
        redirect_policies = oam_policy_parser.get_redirect_policies(authorization_policies)
        # create oam dependency scp
        for index in range(0, len(contexts)):
            context, context_id = contexts[index], context_ids[index]
            if context_id:
                matched_policies = findMatchedRedirectPolicies(context, redirect_policies)
                apacheScpOsh = modeling.createOshByCmdbIdString('scp', context_id)
                for policy in matched_policies:
                    createOAMDependenyScp(localShell, apacheOsh, apacheScpOsh, policy.redirect_url, apache_ip, OSHVResult)

    if not OSHVResult.size():
        for msg in connectionFailedMsgs:
            errobj = errorobject.createError(errorcodes.CONNECTION_FAILED, [protocolName], msg)
            logger.reportErrorObject(errobj)

    return OSHVResult
Esempio n. 13
0
def getUDAAvailablePorts(Framework):
    '''
    Get all available ports of UD protocol
    :param Framework:
    :return: array of available ports, if it's not configured, just return [2738, 7738]
    '''

    logger.debug("Start to get all ports of UD protocol")
    credentialIds = netutils.getAvailableProtocols(Framework, ClientsConsts.DDM_AGENT_PROTOCOL_NAME, None)
    ports = set([2738, 7738])
    for credentialId in credentialIds:
        protocolPort = Framework.getProtocolProperty(credentialId, CollectorsConstants.PROTOCOL_ATTRIBUTE_PORT, 'NA')
        if protocolPort is not None:
            ports.add( int(protocolPort) )

    logger.debug("All ports of UD protocol: ", ports)

    return ports
Esempio n. 14
0
def DiscoveryMain(Framework):
    credentialIds = netutils.getAvailableProtocols(Framework, 'ucs', None)
    if not credentialIds:
        logger.warn('No generic credential for UCS')
        Framework.reportWarning('No generic credential for UCS')
        return

    ucsURLList = getUCSURLs()
    if not ucsURLList:
        logger.error('UCS URL list file not found or Empty')
        Framework.reportError('UCS URL list file not found or Empty')
    else:
        for url in ucsURLList:
            logger.info('==========Begin working on:', url)
            resultVec = discoverySingleUCS(Framework, url, credentialIds)
            if resultVec:
                Framework.sendObjects(resultVec)
                Framework.flushObjects()
Esempio n. 15
0
def DiscoveryMain(Framework):
    credentialIds = netutils.getAvailableProtocols(Framework, 'ucs', None)
    if not credentialIds:
        logger.warn('No generic credential for UCS')
        Framework.reportWarning('No generic credential for UCS')
        return

    ucsURLList = getUCSURLs()
    if not ucsURLList:
        logger.error('UCS URL list file not found or Empty')
        Framework.reportError('UCS URL list file not found or Empty')
    else:
        for url in ucsURLList:
            logger.info('==========Begin working on:', url)
            resultVec = discoverySingleUCS(Framework, url, credentialIds)
            if resultVec:
                Framework.sendObjects(resultVec)
                Framework.flushObjects()
def DiscoveryMain(Framework):
    OSHVResult = ObjectStateHolderVector()

    ip = Framework.getDestinationAttribute("ip_address")
    ip_domain = Framework.getDestinationAttribute("ip_domain")
    cmdb_id = Framework.getDestinationAttribute("cmdb_id")
    protocolName = ClientsConsts.HTTP_PROTOCOL_NAME
    connectionFailedMsgs = []
    protocolIds = findProperProtocolIds(
        ip, netutils.getAvailableProtocols(Framework, protocolName, ip, ip_domain) or []
    )

    if not protocolIds:
        msg = errormessages.makeErrorMessage(protocolName, pattern=errormessages.ERROR_NO_CREDENTIALS)
        errobj = errorobject.createError(errorcodes.NO_CREDENTIALS_FOR_TRIGGERED_IP, [protocolName], msg)
        logger.reportErrorObject(errobj)
    else:
        for protocolId in protocolIds:
            protocol = ProtocolManager.getProtocolById(protocolId)
            port = protocol.getProtocolAttribute("protocol_port")

            for version in SUPPORTED_OAM_VERSION:
                props = Properties()
                props.setProperty(CollectorsConstants.ATTR_CREDENTIALS_ID, protocolId)
                props.setProperty("autoAcceptCerts", "true")
                props.setProperty("host", ip)
                try:
                    httpClient = Framework.createClient(props)
                    httpClient.getAsString(
                        "http://%s:%s/oam/services/rest/%s/ssa/policyadmin/appdomain" % (ip, port, version)
                    )

                    oamOsh = modeling.createOshByCmdbId("running_software", cmdb_id)
                    oamOsh.setStringAttribute("credentials_id", protocolId)
                    oamOsh.setStringAttribute("version", version)
                    OSHVResult.add(oamOsh)
                except SocketTimeoutException, e:
                    msg = errormessages.makeErrorMessage(protocolName, pattern=errormessages.ERROR_TIMEOUT)
                    connectionFailedMsgs.append(msg)
                except JException, e:
                    msg = "URL is not accessable: " + e.getMessage()
                    # logger.debugException(msg)
                    connectionFailedMsgs.append(msg)
                finally:
Esempio n. 17
0
def DiscoveryMain(Framework):
    warningsList = []
    errorsList = []
    vector = ObjectStateHolderVector()
    ip_address = Framework.getDestinationAttribute('ip_address')
    ip_domain = Framework.getDestinationAttribute('ip_domain')
    protocolName = cim.Protocol.DISPLAY

    credentials = netutils.getAvailableProtocols(Framework, cim.Protocol.FULL,
                                                 ip_address, ip_domain)
    credentials = smis_discoverer.getSmisCredentials(credentials, Framework)
    if len(credentials) == 0:
        msg = errormessages.makeErrorMessage(
            protocolName, pattern=errormessages.ERROR_NO_CREDENTIALS)
        errobj = errorobject.createError(
            errorcodes.NO_CREDENTIALS_FOR_TRIGGERED_IP, [protocolName], msg)
        warningsList.append(errobj)
        logger.debug(msg)

    smisNamespaces = smis_discoverer.getSmisNamespaces(Framework)
    if not smisNamespaces:
        msg = errormessages.makeErrorMessage(protocolName,
                                             "No SMI-S namespaces found")
        errobj = errorobject.createError(
            errorcodes.INTERNAL_ERROR_WITH_PROTOCOL_DETAILS,
            [cim.Protocol.DISPLAY, msg], msg)
        errorsList.append(errobj)
        logger.reportErrorObject(errobj)
        return vector

    for credential in credentials:
        testedNamespace = None
        for namespaceObject in smisNamespaces:
            try:

                testedNamespace = cim_discover.testConnectionWithNamespace(
                    Framework, ip_address, credential, namespaceObject)
                break
            except JException, ex:
                msg = ex.getMessage()
                msg = cim_discover.translateErrorMessage(msg)
                errormessages.resolveAndAddToObjectsCollections(
                    msg, protocolName, warningsList, errorsList)
            except:
Esempio n. 18
0
def getUDAAvailablePorts(Framework):
    '''
    Get all available ports of UD protocol
    :param Framework:
    :return: array of available ports, if it's not configured, just return [2738, 7738]
    '''

    logger.debug("Start to get all ports of UD protocol")
    credentialIds = netutils.getAvailableProtocols(
        Framework, ClientsConsts.DDM_AGENT_PROTOCOL_NAME, None)
    ports = set([2738, 7738])
    for credentialId in credentialIds:
        protocolPort = Framework.getProtocolProperty(
            credentialId, CollectorsConstants.PROTOCOL_ATTRIBUTE_PORT, 'NA')
        if protocolPort is not None:
            ports.add(int(protocolPort))

    logger.debug("All ports of UD protocol: ", ports)

    return ports
Esempio n. 19
0
def doConnection(Framework, ip, OSHVResult):
    SHELL_CLIENT_PROTOCOLS = _getSupportedShellProtocols(Framework)

    #powershell support
    POWERSHELL_PROTOCOL = shellutils.PowerShell.PROTOCOL_TYPE
    SHELL_CLIENT_PROTOCOLS.append(POWERSHELL_PROTOCOL)

    #todo:add other protocol if needed(wmi, etc.)

    codepage = Framework.getCodePage()

    warningsList = []
    errorsList = []

    credentialsByType = {}

    hostOsh = None

    # Gather credentials for protocols
    for clientType in SHELL_CLIENT_PROTOCOLS:
        # getting an ordered list of credentials for the given client type and storing them in the credentials dictionary
        protocols = netutils.getAvailableProtocols(Framework, clientType, ip)
        if protocols:
            credentialsByType[clientType] = protocols

    client = None
    shell = None
    udaNotAlive = 0

    credentialId = Framework.getDestinationAttribute('credentialsId')

    if credentialId:
        try:
            client = Framework.createClient()
        except (Exception, JException), jex:
            msg = str(jex)
            logger.debugException(msg)
            logger.debug(
                'Fail to create client by existed credential id, try all credentials again'
            )
Esempio n. 20
0
def DiscoveryMain(Framework):
    OSHVResult = ObjectStateHolderVector()

    ip = Framework.getDestinationAttribute('ip_address')
    ip_domain = Framework.getDestinationAttribute('ip_domain')
    cmdb_id = Framework.getDestinationAttribute('cmdb_id')
    protocolName = ClientsConsts.HTTP_PROTOCOL_NAME
    connectionFailedMsgs = []
    protocolIds = findProperProtocolIds(ip, netutils.getAvailableProtocols(Framework, protocolName, ip, ip_domain) or [])

    if not protocolIds:
        msg = errormessages.makeErrorMessage(protocolName, pattern=errormessages.ERROR_NO_CREDENTIALS)
        errobj = errorobject.createError(errorcodes.NO_CREDENTIALS_FOR_TRIGGERED_IP, [protocolName], msg)
        logger.reportErrorObject(errobj)
    else:
        for protocolId in protocolIds:
            protocol = ProtocolManager.getProtocolById(protocolId)
            port = protocol.getProtocolAttribute('protocol_port')

            for version in SUPPORTED_OAM_VERSION:
                props = Properties()
                props.setProperty(CollectorsConstants.ATTR_CREDENTIALS_ID, protocolId)
                props.setProperty('autoAcceptCerts', 'true')
                props.setProperty('host', ip)
                try:
                    httpClient = Framework.createClient(props)
                    httpClient.getAsString('http://%s:%s/oam/services/rest/%s/ssa/policyadmin/appdomain' % (ip, port, version))

                    oamOsh = modeling.createOshByCmdbId('running_software', cmdb_id)
                    oamOsh.setStringAttribute('credentials_id', protocolId)
                    oamOsh.setStringAttribute('version', version)
                    OSHVResult.add(oamOsh)
                except SocketTimeoutException, e:
                    msg = errormessages.makeErrorMessage(protocolName, pattern=errormessages.ERROR_TIMEOUT)
                    connectionFailedMsgs.append(msg)
                except JException, e:
                    msg = 'URL is not accessable: ' + e.getMessage()
                    # logger.debugException(msg)
                    connectionFailedMsgs.append(msg)
                finally:
Esempio n. 21
0
def doConnection(Framework, ip, OSHVResult):
    SHELL_CLIENT_PROTOCOLS = _getSupportedShellProtocols(Framework)

    #powershell support
    POWERSHELL_PROTOCOL = shellutils.PowerShell.PROTOCOL_TYPE
    SHELL_CLIENT_PROTOCOLS.append(POWERSHELL_PROTOCOL)

    #todo:add other protocol if needed(wmi, etc.)

    codepage = Framework.getCodePage()

    warningsList = []
    errorsList = []

    credentialsByType = {}

    hostOsh = None

    # Gather credentials for protocols
    for clientType in SHELL_CLIENT_PROTOCOLS:
        # getting an ordered list of credentials for the given client type and storing them in the credentials dictionary
        protocols = netutils.getAvailableProtocols(Framework, clientType, ip)
        if protocols:
            credentialsByType[clientType] = protocols

    client = None
    shell = None
    udaNotAlive = 0

    credentialId = Framework.getDestinationAttribute('credentialsId')

    if credentialId:
        try:
            client = Framework.createClient()
        except (Exception, JException), jex:
            msg = str(jex)
            logger.debugException(msg)
            logger.debug('Fail to create client by existed credential id, try all credentials again')
Esempio n. 22
0
def detectUDAAvailable(Framework, ip, credentialId=None):

    logger.debug("Start to detect UDA availability")
    if credentialId:
        credentialIds = [credentialId]
    else:
        import netutils

        credentialIds = netutils.getAvailableProtocols(Framework, ClientsConsts.DDM_AGENT_PROTOCOL_NAME, ip)

    props = Properties()
    props.setProperty('permitDDMi', 'true')
    props.setProperty(CollectorsConstants.DESTINATION_DATA_IP_ADDRESS, ip)
    for credentialId in credentialIds:
        props.setProperty(CollectorsConstants.ATTR_CREDENTIALS_ID, credentialId)
        try:
            logger.debug("Start to connect UDA")
            client = Framework.createClient(props)
            if client:
                return client
        except:
            pass
    return None
Esempio n. 23
0
def DiscoveryMain(Framework):
    warningsList = []
    errorsList = []
    vector = ObjectStateHolderVector()
    ip_address = Framework.getDestinationAttribute('ip_address')
    ip_domain = Framework.getDestinationAttribute('ip_domain')
    protocolName = cim.Protocol.DISPLAY
    
    credentials = netutils.getAvailableProtocols(Framework, cim.Protocol.FULL, ip_address, ip_domain)
    credentials = smis_discoverer.getSmisCredentials(credentials, Framework)
    if len(credentials) == 0:
        msg = errormessages.makeErrorMessage(protocolName, pattern=errormessages.ERROR_NO_CREDENTIALS)
        errobj = errorobject.createError(errorcodes.NO_CREDENTIALS_FOR_TRIGGERED_IP, [protocolName], msg)
        warningsList.append(errobj)
        logger.debug(msg)
    
    smisNamespaces = smis_discoverer.getSmisNamespaces(Framework)
    if not smisNamespaces:
        msg = errormessages.makeErrorMessage(protocolName, "No SMI-S namespaces found")
        errobj = errorobject.createError(errorcodes.INTERNAL_ERROR_WITH_PROTOCOL_DETAILS, [cim.Protocol.DISPLAY, msg], msg)
        errorsList.append(errobj)
        logger.reportErrorObject(errobj)
        return vector
    
    for credential in credentials:
        testedNamespace = None
        for namespaceObject in smisNamespaces:
            try:
    
                testedNamespace = cim_discover.testConnectionWithNamespace(Framework, ip_address, credential, namespaceObject)
                break
            except JException, ex:
                msg = ex.getMessage()
                msg = cim_discover.translateErrorMessage(msg)
                errormessages.resolveAndAddToObjectsCollections(msg, protocolName, warningsList, errorsList)
            except:
Esempio n. 24
0
def mainFunction(Framework, isClient, ip_address = None):
    _vector = ObjectStateHolderVector()
    errStr = ''
    ip_domain  = Framework.getDestinationAttribute('ip_domain')
    host_cmdbid = Framework.getDestinationAttribute('host_cmdbid')
    host_key = Framework.getDestinationAttribute('host_key')
    host_macs = Framework.getTriggerCIDataAsList('mac_addrs')
    ip_arp_mac = Framework.getDestinationAttribute('ip_mac')

    # try to get ip address by mac address from ARP Cache
    foundIp = clientdiscoveryutils.getIPAddressOnlyFromMacAddress(ip_arp_mac)
    if foundIp:
        ip_address = foundIp

    if (ip_address == None):
        ip_address = Framework.getDestinationAttribute('ip_address')
    if (ip_domain == None):
        ip_domain = DomainScopeManager.getDomainByIp(ip_address, None)

    protocols = netutils.getAvailableProtocols(Framework, ClientsConsts.SNMP_PROTOCOL_NAME, ip_address, ip_domain)
    if len(protocols) == 0:
        errStr = 'No credentials defined for the triggered ip'
        logger.debug(errStr)
        errObj = errorobject.createError(errorcodes.NO_CREDENTIALS_FOR_TRIGGERED_IP, [ClientsConsts.SNMP_PROTOCOL_NAME], errStr)
        return (_vector, errObj)

    connected = 0
    for protocol in protocols:
        client = None
        try:
            try:
                logger.debug('try to get snmp agent for: %s:%s' % (ip_address, ip_domain))
                if (isClient == TRUE):
                    properties = Properties()
                    properties.setProperty(CollectorsConstants.DESTINATION_DATA_IP_ADDRESS, ip_address)
                    properties.setProperty(CollectorsConstants.DESTINATION_DATA_IP_DOMAIN, ip_domain)
                    client = Framework.createClient(protocol, properties)
                else:
                    properties = Properties()
                    properties.setProperty(CollectorsConstants.DESTINATION_DATA_IP_ADDRESS, ip_address)
                    client = Framework.createClient(protocol, properties)
                logger.debug('Running test connection queries')
                testConnection(client)
                Framework.saveState(protocol)
                logger.debug('got snmp agent for: %s:%s' % (ip_address, ip_domain))
                isMultiOid = client.supportMultiOid()
                logger.debug('snmp server isMultiOid state=%s' %isMultiOid)
                # create snmp OSH
                snmpOSH = modeling.createSnmpOSH(ip_address, client.getPort())
                snmpOSH.setAttribute('application_timeout', client.getTimeout())
                snmpOSH.setAttribute('snmp_port', client.getPort())
                snmpOSH.setAttribute('credentials_id', client.getCredentialId())
                snmpOSH.setAttribute('snmp_retry', client.getRetries())
                snmpOSH.setAttribute('snmp_timeout', client.getTimeout())
                #obtain SNMP protocol version
                snmpVersion = definedSnmpProtocolVersion(protocol, Framework)
                snmpOSH.setAttribute('application_version_number', snmpVersion)
                if ip_arp_mac and ip_arp_mac != 'NA':
                    snmpOSH.setAttribute('arp_mac', ip_arp_mac)

                if isMultiOid == 1:
                    snmpOSH.setBoolAttribute('snmp_supportmultioid', 1)
                else:
                    snmpOSH.setBoolAttribute('snmp_supportmultioid', 0)

                _vector = doSnmp(client, isClient, snmpOSH, ip_address, ip_domain, Framework, host_cmdbid, host_key, host_macs)
                client.close()
                client = None

                if _vector.size() > 0:
                    connected = 1

                    break
            except BroadcastIpDiscoveryException:
                msg = "Job has been triggered on broadcast IP, no results will be reported"
                errObj = errorobject.createError(errorcodes.NO_RESULTS_WILL_BE_REPORTED, ["Job has been triggered on broadcast IP"], msg)
                if client != None:
                    client.close()
                    client = None
                return (_vector, errObj)
            except:
                if client != None:
                    client.close()
                    client = None
                logger.debugException('Unexpected SNMP_AGENT Exception:')
                lastExceptionStr = str(sys.exc_info()[1]).strip()
        finally:
            if client != None:
                client.close()
                client = None

    error = errorobject.INTERNAL_ERROR
    if (not connected):
        errStr = errormessages.makeErrorMessage('SNMP', pattern=errormessages.ERROR_CONNECTION_FAILED)
        error = errorobject.createError(errorcodes.CONNECTION_FAILED, ['SNMP'], errStr)
        logger.debug(errStr)
        Framework.clearState()
    elif (_vector.size() == 0):
        error = errormessages.resolveError(lastExceptionStr, 'SNMP')
    return (_vector, error)
Esempio n. 25
0
def DiscoveryMain(Framework):
    SHELL_CLIENT_PROTOCOLS = _getSupportedShellProtocols(Framework)

    ip = Framework.getDestinationAttribute('ip_address')
    domain = Framework.getDestinationAttribute('ip_domain')
    codepage = Framework.getCodePage()
    useLastState = Framework.getParameter('useLastSuccessConnection')

    vector = ObjectStateHolderVector()
    warningsList = []
    errorsList = []

    # preparing empty dictionary for storing credentials later
    credentialsByType = {}

    # take the latest used credentials if any
    lastState = None
    if useLastState and useLastState.lower() == 'true':
        lastState = Framework.loadState()

    if lastState:
        credentialsByType[None] = [lastState]

    # try to get ip address by mac address from ARP Cache
    macAddress = Framework.getDestinationAttribute('ip_mac_address')
    foundIp = clientdiscoveryutils.getIPAddressOnlyFromMacAddress(macAddress)
    if foundIp:
        ip = foundIp

    # Gather credentials for protocols
    for clientType in SHELL_CLIENT_PROTOCOLS:
        # getting an ordered list of credentials for the given client type and storing them in the credentials dictionary
        protocols = netutils.getAvailableProtocols(Framework, clientType, ip,
                                                   domain)
        if protocols:
            credentialsByType[clientType] = protocols

    ##########################################################################################################
    ##################################Start Special processing for Universal Discovery Agent##################
    # take Universal Discovery Agent credentials if new Universal Discovery Agent installed on that IP

    connectedDDMAgentCredentials = None
    if useLastState and useLastState.lower() == 'true':
        connectedDDMAgentCredentials = Framework.loadGlobalState(ip)

    client = None
    udaNotAlive = 0

    if connectedDDMAgentCredentials:
        logger.debug('Found global state credentials ',
                     connectedDDMAgentCredentials,
                     ' of installed agent on ip:', ip)
        client = createClient(Framework, ClientsConsts.DDM_AGENT_PROTOCOL_NAME,
                              [connectedDDMAgentCredentials], ip, codepage,
                              warningsList, errorsList)
        # If we are successfully connected
        if client:
            logger.debug('Succeeded to connect with global state credentials ',
                         client.getCredentialId(), ' of installed agent')
            Framework.saveState(client.getCredentialId())
        else:
            logger.debug('Failed to connect with global state credentials ',
                         connectedDDMAgentCredentials, ' on ip:', ip)
            udaNotAlive = 1
            #AgentUtils.clearGlobalState(Framework)
    # only for case where no connection established before
    if not client:
        # checks whether there are credential for specified protocol
        if credentialsByType:
            if lastState:
                client = createClientFromLastState(Framework, lastState,
                                                   warningsList, errorsList)
                if not client:
                    logger.debug(
                        'Failed to create client using last state properties. Will try to connect using other credentials.'
                    )
            if not client:
                for clientType in SHELL_CLIENT_PROTOCOLS:
                    credentials = credentialsByType.get(clientType)
                    if credentials:
                        client = createClient(Framework, clientType,
                                              credentials, ip, codepage,
                                              warningsList, errorsList)
                        if client:
                            warningsList = []
                            errorsList = []
                            # save credentials id for further reuse
                            Framework.saveState(client.getCredentialId())
                            break
        else:
            for shellType in SHELL_CLIENT_PROTOCOLS:
                msg = errormessages.makeErrorMessage(
                    shellType, pattern=errormessages.ERROR_NO_CREDENTIALS)
                errobj = errorobject.createError(
                    errorcodes.NO_CREDENTIALS_FOR_TRIGGERED_IP, [shellType],
                    msg)
                warningsList.append(errobj)

    if not client:
        Framework.clearState()
    else:
        # successfully connected, do discovery
        shell = None
        clientType = client.getClientType()

        connectedOSCredentialID = None
        try:
            try:
                shellFactory = shellutils.ShellFactory()
                shell = shellFactory.createShell(client, clientType)

                connectedOSCredentialID = ConnectedOSCredentialFinder.findCredential(
                    Framework, shell, client, errorsList, warningsList)

                # If we got a default value, we just pass None later
                # Else - we need to signal the existing client which can be only UDA by now that it has a credential
                # to take sudo password from, if it needs it
                if (not connectedOSCredentialID or connectedOSCredentialID
                        == ConnectedOSCredentialFinder.NO_CONNECTED_CRED_ID):
                    connectedOSCredentialID = None
                else:
                    try:
                        client.setConnectedShellCredentialID(
                            connectedOSCredentialID)
                    except:
                        logger.warn(
                            'Failed to setConnectedShellCredentialID, sudo commands may not work in this run'
                        )

                vector.addAll(
                    doDiscovery(Framework, shell, client, ip, codepage,
                                connectedOSCredentialID))
            except (Exception, JException), jex:
                msg = str(jex)
                logger.debugException(msg)
                errormessages.resolveAndAddToObjectsCollections(
                    msg, clientType, warningsList, errorsList)
        finally:
            if udaNotAlive and client:
                logger.debug('find another shell can be connected. ', shell)
                logger.debug(
                    'Removing the connected uda shell because it failed to connect'
                )

                agentOsh = ObjectStateHolder(
                    ClientsConsts.DDM_AGENT_PROTOCOL_NAME)

                agentOsh.setAttribute('application_ip', ip)
                agentOsh.setAttribute('data_name',
                                      ClientsConsts.DDM_AGENT_PROTOCOL_NAME)

                #agentOsh.setAttribute('application_port', shell.getPort())
                agentOsh.setContainer(modeling.createHostOSH(ip))
                Framework.deleteObject(agentOsh)
                Framework.flushObjects()

                Framework.clearGlobalState(ip)
            if shell:
                try:
                    shell.closeClient()
                except:
                    errobj = errorobject.createError(
                        errorcodes.CLIENT_NOT_CLOSED_PROPERLY, None,
                        "Client was not closed properly")
                    warningsList.append(errobj)
                    logger.warnException('')
            # close client anyway
            if client and client.close(): pass
            # create shell OSH if connection established but discovery failed
            if not vector.size():
                logger.warn(
                    'Discovery failed, though shell object will be created')
                hostOsh = modeling.createHostOSH(ip, filter_client_ip=True)
                if hostOsh:
                    languageName = None
                    langBund = Framework.getEnvironmentInformation().getBundle(
                        'langNetwork', languageName)
                    shellOsh = createShellObj(
                        client,
                        client,
                        ip,
                        langBund,
                        languageName,
                        codepage,
                        connectedShellCredId=connectedOSCredentialID)
                    shellOsh.setContainer(hostOsh)

                    vector.add(shellOsh)
                else:
                    logger.warn(
                        'Failed to create node and shell since IP is of a Client range type, not enough data for reconciliation.'
                    )

    for errobj in warningsList:
        logger.reportWarningObject(errobj)
    for errobj in errorsList:
        logger.reportErrorObject(errobj)

    return vector
Esempio n. 26
0
def DiscoveryMain(Framework):
    SHELL_CLIENT_PROTOCOLS = _getSupportedShellProtocols(Framework)

    ip = Framework.getDestinationAttribute('ip_address')
    domain = Framework.getDestinationAttribute('ip_domain')
    codepage = Framework.getCodePage()
    useLastState = Framework.getParameter('useLastSuccessConnection')

    vector = ObjectStateHolderVector()
    warningsList = []
    errorsList = []

    # preparing empty dictionary for storing credentials later
    credentialsByType = {}

    # take the latest used credentials if any
    lastState = None
    if useLastState and useLastState.lower() == 'true':
        lastState = Framework.loadState()

    if lastState:
        credentialsByType[None] = [lastState]

    # try to get ip address by mac address from ARP Cache
    macAddress = Framework.getDestinationAttribute('ip_mac_address')
    foundIp = clientdiscoveryutils.getIPAddressOnlyFromMacAddress(macAddress)
    if foundIp:
        ip = foundIp

    # Gather credentials for protocols
    for clientType in SHELL_CLIENT_PROTOCOLS:
        # getting an ordered list of credentials for the given client type and storing them in the credentials dictionary
        protocols = netutils.getAvailableProtocols(Framework, clientType, ip, domain)
        if protocols:
            credentialsByType[clientType] = protocols

    ##########################################################################################################
    ##################################Start Special processing for Universal Discovery Agent##################
    # take Universal Discovery Agent credentials if new Universal Discovery Agent installed on that IP

    connectedDDMAgentCredentials = None
    if useLastState and useLastState.lower() == 'true':
        connectedDDMAgentCredentials = Framework.loadGlobalState(ip)

    client = None
    udaNotAlive = 0

    if connectedDDMAgentCredentials:
        logger.debug('Found global state credentials ', connectedDDMAgentCredentials, ' of installed agent on ip:', ip)
        client = createClient(Framework, ClientsConsts.DDM_AGENT_PROTOCOL_NAME, [connectedDDMAgentCredentials], ip, codepage, warningsList, errorsList)
        # If we are successfully connected
        if client:
            logger.debug('Succeeded to connect with global state credentials ', client.getCredentialId(), ' of installed agent')
            Framework.saveState(client.getCredentialId())
        else:
            logger.debug('Failed to connect with global state credentials ', connectedDDMAgentCredentials, ' on ip:', ip)
            udaNotAlive = 1
            #AgentUtils.clearGlobalState(Framework)
    # only for case where no connection established before
    if not client:
        # checks whether there are credential for specified protocol
        if credentialsByType:
            if lastState:
                client = createClientFromLastState(Framework, lastState, warningsList, errorsList)
                if not client:
                    logger.debug('Failed to create client using last state properties. Will try to connect using other credentials.')
            if not client:
                for clientType in SHELL_CLIENT_PROTOCOLS:
                    credentials = credentialsByType.get(clientType)
                    if credentials:
                        client = createClient(Framework, clientType, credentials, ip, codepage, warningsList, errorsList)
                        if client:
                            warningsList = []
                            errorsList = []
                            # save credentials id for further reuse
                            Framework.saveState(client.getCredentialId())
                            break
        else:
            for shellType in SHELL_CLIENT_PROTOCOLS:
                msg = errormessages.makeErrorMessage(shellType, pattern=errormessages.ERROR_NO_CREDENTIALS)
                errobj = errorobject.createError(errorcodes.NO_CREDENTIALS_FOR_TRIGGERED_IP, [shellType], msg)
                warningsList.append(errobj)

    if not client:
        Framework.clearState()
    else:
        # successfully connected, do discovery
        shell = None
        clientType = client.getClientType()

        connectedOSCredentialID = None
        try:
            try:
                shellFactory = shellutils.ShellFactory()
                shell = shellFactory.createShell(client, clientType)

                connectedOSCredentialID = ConnectedOSCredentialFinder.findCredential(Framework, shell, client,
                    errorsList, warningsList)

                # If we got a default value, we just pass None later
                # Else - we need to signal the existing client which can be only UDA by now that it has a credential
                # to take sudo password from, if it needs it
                if (not connectedOSCredentialID
                    or connectedOSCredentialID == ConnectedOSCredentialFinder.NO_CONNECTED_CRED_ID):
                    connectedOSCredentialID = None
                else:
                    try:
                        client.setConnectedShellCredentialID(connectedOSCredentialID)
                    except:
                        logger.warn('Failed to setConnectedShellCredentialID, sudo commands may not work in this run')

                vector.addAll(doDiscovery(Framework, shell, client, ip, codepage, connectedOSCredentialID))
            except (Exception, JException), jex:
                msg = str(jex)
                logger.debugException(msg)
                errormessages.resolveAndAddToObjectsCollections(msg,
                                        clientType, warningsList, errorsList)
        finally:
            if udaNotAlive and client:
                logger.debug('find another shell can be connected. ', shell)
                logger.debug('Removing the connected uda shell because it failed to connect')

                agentOsh = ObjectStateHolder(ClientsConsts.DDM_AGENT_PROTOCOL_NAME)

                agentOsh.setAttribute('application_ip', ip)
                agentOsh.setAttribute('data_name', ClientsConsts.DDM_AGENT_PROTOCOL_NAME)

                #agentOsh.setAttribute('application_port', shell.getPort())
                agentOsh.setContainer(modeling.createHostOSH(ip))
                Framework.deleteObject(agentOsh)
                Framework.flushObjects()

                Framework.clearGlobalState(ip)
            if shell:
                try:
                    shell.closeClient()
                except:
                    errobj = errorobject.createError(errorcodes.CLIENT_NOT_CLOSED_PROPERLY, None, "Client was not closed properly")
                    warningsList.append(errobj)
                    logger.warnException('')
            # close client anyway
            if client and client.close(): pass
            # create shell OSH if connection established but discovery failed
            if not vector.size():
                logger.warn('Discovery failed, though shell object will be created')
                hostOsh = modeling.createHostOSH(ip, filter_client_ip=True)
                if hostOsh:
                    languageName = None
                    langBund = Framework.getEnvironmentInformation().getBundle('langNetwork', languageName)
                    shellOsh = createShellObj(client, client, ip, langBund, languageName, codepage, connectedShellCredId=connectedOSCredentialID)
                    shellOsh.setContainer(hostOsh)

                    vector.add(shellOsh)
                else:
                    logger.warn('Failed to create node and shell since IP is of a Client range type, not enough data for reconciliation.')

    for errobj in warningsList:
        logger.reportWarningObject(errobj)
    for errobj in errorsList:
        logger.reportErrorObject(errobj)

    return vector
Esempio n. 27
0
def DiscoveryMain(Framework):
    """
    @param Framework:
    @type Framework: com.hp.ucmdb.discovery.probe.services.dynamic.core.DynamicServiceFrameworkImpl
    @return:
    """
    vector = ObjectStateHolderVector()
    ip = Framework.getDestinationAttribute('ip_address')
    credentialId = Framework.getDestinationAttribute('credentialId')
    final_success = False
    try:
        credentials = netutils.getAvailableProtocols(Framework, 'http', ip)
        logger.debug('All http credentials to try:', credentials)
        if credentials:
            # if existing credential id, move it to the first one
            if credentialId and credentialId in credentials:
                credentials.remove(credentialId)
                credentials.insert(0, credentialId)
                logger.debug('Move existing credential to first one:', credentials)
            for creId in credentials:
                session = None
                try:
                    logger.debug('Try credential:', creId)
                    conn = ConnectionManager(Framework, ip, creId)
                    conn.validate()
                    logger.debug('Try login ...')
                    session = conn.getSession()
                    logger.debug('Login done, begin reporting related CIs')
                    host = Host.getAll(session)[0]
                    hostname = host.getHostName()
                    xen_server_osh = modeling.createHostOSH(ip, 'unix', None, hostname)
                    xen_server_osh.setStringAttribute('host_osinstalltype', 'XenServer')  # mark the unix as a XenServer
                    serialNumber = host.getSerialNumber()
                    if serialNumber:
                        xen_server_osh.setStringAttribute('serial_number', serialNumber)
                    virtual_osh = ObjectStateHolder('virtualization_layer')
                    virtual_osh.setStringAttribute('name', 'Citrix Xen Hypervisor')
                    virtual_osh.setStringAttribute('product_name', 'xen_hypervisor')
                    virtual_osh.setStringAttribute('application_ip', ip)
                    virtual_osh.setStringAttribute('credentials_id', conn.credentialsId)
                    virtual_osh.setContainer(xen_server_osh)
                    vector.add(virtual_osh)
                    vector.add(xen_server_osh)
                    final_success = True
                    break
                except:
                    logger.debugException('')
                finally:
                    if session:
                        try:
                            conn.closeSession()
                        except:
                            pass
        else:
            logger.warn('No http credential found for ip:', ip)
    except:
        logger.debugException('')
    if final_success:
        logger.debug('Success finally.')
    else:
        logger.debug('No Citrix Xen Server detected')
        Framework.reportWarning('No Citrix Xen Server detected')

    return vector