Esempio n. 1
0
 def resolveNamesToIPs(self, hostNode, parsedData):
     if parsedData:
         for internalName in parsedData.keys():
             nodeData = parsedData[internalName]
             values = nodeData.get(hostNode)
             if values:
                 listenedIPs = {}
                 for ipaddr in values:
                     stripedIpAddr = ipaddr.strip()
                     if netutils.isValidIp(stripedIpAddr):
                         listenedIPs[stripedIpAddr] = ''
                     else:
                         resolver = DNSResolver(self.shell, stripedIpAddr)
                         resolvedIp = resolver.getIPAddress()
                         if resolvedIp:
                             listenedIPs[resolvedIp.strip()] = ''
                         else:
                             aliasBasedIps = resolver.resolveNSLookupAliasBased(
                             )
                             if aliasBasedIps:
                                 for ip in aliasBasedIps:
                                     listenedIPs[ip.strip()] = ''
                 nodeData[hostNode] = listenedIPs.keys()
             parsedData[internalName] = nodeData
     return parsedData
 def resolveNamesToIPs(self, hostNode, parsedData):
     if parsedData:
         for internalName in parsedData.keys():
             nodeData = parsedData[internalName]
             values = nodeData.get(hostNode)
             if values:
                 listenedIPs = {}
                 for ipaddr in values:
                     stripedIpAddr = ipaddr.strip()
                     if netutils.isValidIp(stripedIpAddr):
                         listenedIPs[stripedIpAddr] = ""
                     else:
                         resolver = DNSResolver(self.shell, stripedIpAddr)
                         resolvedIp = resolver.getIPAddress()
                         if resolvedIp:
                             listenedIPs[resolvedIp.strip()] = ""
                         else:
                             aliasBasedIps = resolver.resolveNSLookupAliasBased()
                             if aliasBasedIps:
                                 for ip in aliasBasedIps:
                                     listenedIPs[ip.strip()] = ""
                 nodeData[hostNode] = listenedIPs.keys()
             parsedData[internalName] = nodeData
     return parsedData
def DiscoveryMain(Framework):
    OSHVResult = ObjectStateHolderVector()
    shell = None
    protocol = Framework.getDestinationAttribute("Protocol")
    listenerName = Framework.getDestinationAttribute("listenerName")
    listenerPath = Framework.getDestinationAttribute("listener_process_path")
    defOraHomes = Framework.getParameter("OracleHomes")
    listenerIp = Framework.getDestinationAttribute("listener_ip")
    listenedIPs = Framework.getTriggerCIDataAsList("listened_ips")

    try:
        try:
            client = Framework.createClient()
            shell = shellutils.ShellUtils(client)

            if listenerPath:
                envConf = UnixOracleEnvConfig(shell)
                if shell.isWinOs():
                    envConf = WindowsOracleEnvConfig(shell)
                envConf.setOracleHomeEnvVar(listenerPath)
            else:
                envConf = EnvConfigurator(shell, defOraHomes)

            if not listenedIPs:
                Framework.reportError("No listened_ips attribute values found.")
                return OSHVResult
            lookuper = LookupManager(listenedIPs)

            serviceToNodesMap = {}
            srvDiscoverer = SrvctlBasedDiscoverer(shell, envConf)
            databases = srvDiscoverer.getDatabases()
            for database in databases:
                instanceAndNodes = srvDiscoverer.getInstancesWithNodes(database)
                if instanceAndNodes:
                    serviceToNodesMap[database] = instanceAndNodes
                    for elem in instanceAndNodes:
                        resolver = DNSResolver(shell, elem.get("Node"))
                        ipAddr = resolver.resolveNSLookup()
                        if not ipAddr:
                            ipAddr = resolver.resolveNSLookupAliasBased()
                            ipAddr = ipAddr and ipAddr[0]
                            try:
                                if not ipAddr:
                                    ipAddr = resolver.resolveHostsFile()
                            except:
                                pass
                        elem["ip"] = ipAddr

            for (serviceName, params) in serviceToNodesMap.items():
                try:
                    listeners = []
                    oracles = []
                    for elem in params:
                        ipAddr = elem.get("ip")
                        if not ipAddr:
                            raise ValueError("One of the Node Ip is not discovered. Can not create full topology.")
                        hostOSH = modeling.createHostOSH(ipAddr)
                        OSHVResult.add(hostOSH)
                        listenerOSH = createWeakListener(hostOSH)
                        if listenerIp == ipAddr:
                            listenerOSH.setStringAttribute("name", listenerName)
                        listeners.append(listenerOSH)
                        oracleOsh = modeling.createDatabaseOSH("oracle", elem["Instance"], None, ipAddr, hostOSH)
                        listeners.append(oracleOsh)
                        oracles.append(oracleOsh)
                    racName = ""
                    nodes = [x["Node"] for x in params if x["Node"]]
                    nodes.sort()
                    racName = ":".join(nodes)
                    racOsh = createRacOSH(racName, len(params), serviceName, None)
                    OSHVResult.add(racOsh)
                    for listener in listeners:
                        OSHVResult.add(listener)
                        OSHVResult.add(modeling.createLinkOSH("member", racOsh, listener))
                    for oracle in oracles:
                        OSHVResult.add(oracle)
                        OSHVResult.add(modeling.createLinkOSH("member", racOsh, oracle))
                except:
                    Framework.reportWarning(
                        'Failed to lookup host name of the node. Probably not all nodes were discovered by "Oracle Listener by Shell" Job. No RAC CI will be created.'
                    )
                    logger.warn("Failed to lookup host name for node with ip. No RAC CI will be created.")
            if not serviceToNodesMap:
                logger.warn("Failed to get information via srvctl. Will use old approach.")
            else:
                return OSHVResult
            # old flow
            tnsConfig = {}
            try:
                tnsConfig = TNSNamesConfig(shell, envConf)
            except:
                logger.debug("Failed to get tnsnames.ora. Trying different home.")
                envConf = EnvConfigurator(shell, defOraHomes)
                oraHome = envConf.findMatchingDefaultOracleHome()
                envConf = UnixOracleEnvConfig(shell)
                envConf.setOracleHomeEnvVar(oraHome)
                tnsConfig = TNSNamesConfig(shell, envConf)

            racParams = tnsConfig.getRacParams()
            servChec = ServiceChecker(shell, envConf, listenerName=listenerName)
            for racServiceName in racParams.keys():
                parametersDict = racParams[racServiceName]
                racNodeNameList = []
                racInstCount = len(parametersDict.keys())

                if (
                    not servChec.isServiceRunning(racServiceName.upper())
                    or racInstCount == 0
                    or racInstCount != int(servChec.getServiceInstancesNumber(racServiceName))
                ):
                    Framework.reportWarning("Oracle RAC is not running or not all Instances were detected")
                    continue

                racVersion = servChec.getVersion()
                shortVersion = servChec.getShortVersion()
                listeners = []
                oracles = []
                for ip in parametersDict.keys():
                    hostName = lookuper.lookupByIp(ip) or " "
                    hostPrimIp = lookuper.getPrimaryIp(ip)
                    actIp = ip

                    if not hostName:
                        Framework.reportError(
                            'Failed to lookup host name of the node. Probably not all nodes were discovered by "Oracle Listener by Shell" Job. No RAC CI will be created.'
                        )
                        logger.error("Failed to lookup host name for node with ip %s . No RAC CI will be created." % ip)
                        return ObjectStateHolderVector()
                    racNodeNameList.append(hostName)
                    dbSid = parametersDict[actIp]
                    if hostPrimIp:
                        actIp = hostPrimIp
                    hostOSH = modeling.createHostOSH(actIp)
                    OSHVResult.add(hostOSH)
                    listenerOSH = createWeakListener(hostOSH)
                    listeners.append(listenerOSH)
                    oracleOSH = modeling.createDatabaseOSH(
                        "oracle", dbSid, None, actIp, hostOSH, None, None, None, shortVersion, racVersion, shortVersion
                    )
                    instanceNumber = servChec.getDbInstanceNumber(dbSid, racServiceName)
                    if instanceNumber:
                        oracleOSH.setAttribute("oracle_instancenumber", instanceNumber)
                    oracles.append(oracleOSH)
                racNodeNameList.sort()
                racName = ""
                for nodeName in racNodeNameList:
                    if racName == "":
                        racName = nodeName
                    else:
                        racName += ":" + nodeName
                racOSH = createRacOSH(racName, racInstCount, racServiceName, racVersion)
                OSHVResult.add(racOSH)
                for listener in listeners:
                    OSHVResult.add(listener)
                    OSHVResult.add(modeling.createLinkOSH("member", racOSH, listener))
                for oracle in oracles:
                    OSHVResult.add(oracle)
                    OSHVResult.add(modeling.createLinkOSH("member", racOSH, oracle))
        finally:
            try:
                shell and shell.closeClient()
            except:
                logger.debugException("")
                logger.error("Unable to close shell")
    except:
        msg = logger.prepareFullStackTrace("")
        errormessages.resolveAndReport(msg, protocol, Framework)
    return OSHVResult
Esempio n. 4
0
def DiscoveryMain(Framework):
    OSHVResult = ObjectStateHolderVector()
    shell = None
    protocol = Framework.getDestinationAttribute('Protocol')
    listenerName = Framework.getDestinationAttribute('listenerName')
    listenerPath = Framework.getDestinationAttribute('listener_process_path')
    defOraHomes = Framework.getParameter('OracleHomes')
    listenerIp = Framework.getDestinationAttribute('listener_ip')
    listenedIPs = Framework.getTriggerCIDataAsList('listened_ips')

    try:
        try:
            client = Framework.createClient()
            shell = shellutils.ShellUtils(client)

            if listenerPath:
                envConf = UnixOracleEnvConfig(shell)
                if shell.isWinOs():
                    envConf = WindowsOracleEnvConfig(shell)
                envConf.setOracleHomeEnvVar(listenerPath)
            else:
                envConf = EnvConfigurator(shell, defOraHomes)

            if not listenedIPs:
                Framework.reportError(
                    'No listened_ips attribute values found.')
                return OSHVResult
            lookuper = LookupManager(listenedIPs)

            serviceToNodesMap = {}
            srvDiscoverer = SrvctlBasedDiscoverer(shell, envConf)
            databases = srvDiscoverer.getDatabases()
            for database in databases:
                instanceAndNodes = srvDiscoverer.getInstancesWithNodes(
                    database)
                if instanceAndNodes:
                    serviceToNodesMap[database] = instanceAndNodes
                    for elem in instanceAndNodes:
                        resolver = DNSResolver(shell, elem.get('Node'))
                        ipAddr = resolver.resolveNSLookup()
                        if not ipAddr:
                            ipAddr = resolver.resolveNSLookupAliasBased()
                            ipAddr = ipAddr and ipAddr[0]
                            try:
                                if not ipAddr:
                                    ipAddr = resolver.resolveHostsFile()
                            except:
                                pass
                        elem['ip'] = ipAddr

            for (serviceName, params) in serviceToNodesMap.items():
                try:
                    listeners = []
                    oracles = []
                    for elem in params:
                        ipAddr = elem.get('ip')
                        if not ipAddr:
                            raise ValueError(
                                'One of the Node Ip is not discovered. Can not create full topology.'
                            )
                        hostOSH = modeling.createHostOSH(ipAddr)
                        OSHVResult.add(hostOSH)
                        listenerOSH = createWeakListener(hostOSH)
                        if listenerIp == ipAddr:
                            listenerOSH.setStringAttribute(
                                'name', listenerName)
                        listeners.append(listenerOSH)
                        oracleOsh = modeling.createDatabaseOSH(
                            'oracle', elem['Instance'], None, ipAddr, hostOSH)
                        listeners.append(oracleOsh)
                        oracles.append(oracleOsh)
                    racName = ''
                    nodes = [x['Node'] for x in params if x['Node']]
                    nodes.sort()
                    racName = ':'.join(nodes)
                    racOsh = createRacOSH(racName, len(params), serviceName,
                                          None)
                    OSHVResult.add(racOsh)
                    for listener in listeners:
                        OSHVResult.add(listener)
                        OSHVResult.add(
                            modeling.createLinkOSH('member', racOsh, listener))
                    for oracle in oracles:
                        OSHVResult.add(oracle)
                        OSHVResult.add(
                            modeling.createLinkOSH('member', racOsh, oracle))
                except:
                    Framework.reportWarning(
                        'Failed to lookup host name of the node. Probably not all nodes were discovered by \"Oracle Listener by Shell\" Job. No RAC CI will be created.'
                    )
                    logger.warn(
                        'Failed to lookup host name for node with ip. No RAC CI will be created.'
                    )
            if not serviceToNodesMap:
                logger.warn(
                    'Failed to get information via srvctl. Will use old approach.'
                )
            else:
                return OSHVResult
            #old flow
            tnsConfig = {}
            try:
                tnsConfig = TNSNamesConfig(shell, envConf)
            except:
                logger.debug(
                    'Failed to get tnsnames.ora. Trying different home.')
                envConf = EnvConfigurator(shell, defOraHomes)
                oraHome = envConf.findMatchingDefaultOracleHome()
                envConf = UnixOracleEnvConfig(shell)
                envConf.setOracleHomeEnvVar(oraHome)
                tnsConfig = TNSNamesConfig(shell, envConf)

            racParams = tnsConfig.getRacParams()
            servChec = ServiceChecker(shell,
                                      envConf,
                                      listenerName=listenerName)
            for racServiceName in racParams.keys():
                parametersDict = racParams[racServiceName]
                racNodeNameList = []
                racInstCount = len(parametersDict.keys())

                if not servChec.isServiceRunning(racServiceName.upper(
                )) or racInstCount == 0 or racInstCount != int(
                        servChec.getServiceInstancesNumber(racServiceName)):
                    Framework.reportWarning(
                        'Oracle RAC is not running or not all Instances were detected'
                    )
                    continue

                racVersion = servChec.getVersion()
                shortVersion = servChec.getShortVersion()
                listeners = []
                oracles = []
                for ip in parametersDict.keys():
                    hostName = lookuper.lookupByIp(ip) or ' '
                    hostPrimIp = lookuper.getPrimaryIp(ip)
                    actIp = ip

                    if not hostName:
                        Framework.reportError(
                            'Failed to lookup host name of the node. Probably not all nodes were discovered by \"Oracle Listener by Shell\" Job. No RAC CI will be created.'
                        )
                        logger.error(
                            'Failed to lookup host name for node with ip %s . No RAC CI will be created.'
                            % ip)
                        return ObjectStateHolderVector()
                    racNodeNameList.append(hostName)
                    dbSid = parametersDict[actIp]
                    if hostPrimIp:
                        actIp = hostPrimIp
                    hostOSH = modeling.createHostOSH(actIp)
                    OSHVResult.add(hostOSH)
                    listenerOSH = createWeakListener(hostOSH)
                    listeners.append(listenerOSH)
                    oracleOSH = modeling.createDatabaseOSH(
                        'oracle', dbSid, None, actIp, hostOSH, None, None,
                        None, shortVersion, racVersion, shortVersion)
                    instanceNumber = servChec.getDbInstanceNumber(
                        dbSid, racServiceName)
                    if instanceNumber:
                        oracleOSH.setAttribute('oracle_instancenumber',
                                               instanceNumber)
                    oracles.append(oracleOSH)
                racNodeNameList.sort()
                racName = ''
                for nodeName in racNodeNameList:
                    if racName == '':
                        racName = nodeName
                    else:
                        racName += ':' + nodeName
                racOSH = createRacOSH(racName, racInstCount, racServiceName,
                                      racVersion)
                OSHVResult.add(racOSH)
                for listener in listeners:
                    OSHVResult.add(listener)
                    OSHVResult.add(
                        modeling.createLinkOSH('member', racOSH, listener))
                for oracle in oracles:
                    OSHVResult.add(oracle)
                    OSHVResult.add(
                        modeling.createLinkOSH('member', racOSH, oracle))
        finally:
            try:
                shell and shell.closeClient()
            except:
                logger.debugException('')
                logger.error('Unable to close shell')
    except:
        msg = logger.prepareFullStackTrace('')
        errormessages.resolveAndReport(msg, protocol, Framework)
    return OSHVResult