def discover(self): '''Main discovery method. @return: instance of Cluster DO. ''' outputBuffer = self.getCmviewclOutput() clusterDo = self.parseCluster(outputBuffer) clusterDo.version = self.getVersion() if not clusterDo.version: logger.warn('Failed getting cluster version') outputSections = outputBuffer.split('MULTI_NODE_PACKAGES') nodesBufList = outputSections[0].split('\n NODE ') if len(nodesBufList) > 1: # we remove first part of buffer which contains cluster information nodesBufList = nodesBufList[1:] for nodeBuf in nodesBufList: try: nodeAndPackagesBufferList = nodeBuf.split('\n PACKAGE ') if nodeAndPackagesBufferList: # first element contains Node information nodeDescription = nodeAndPackagesBufferList[0] nodeDo = self.discoverNode(nodeDescription) clusterDo.nodes.append(nodeDo) # package is quite independent from the node... # need to consider the exception handling if len(nodeAndPackagesBufferList) > 1: # all other elements contain package information for packageBuffer in nodeAndPackagesBufferList[1:]: try: packagesList = self.parsePackageInformation(packageBuffer) if packagesList: for package in packagesList: package.packageIp = netutils.resolveIP(self._shell, package.name) clusterDo.packages.extend(packagesList) except: logger.error('Failed parsing package information from buffer: %s' % packageBuffer) logger.debugException('') except: logger.error('Failed parsing node info from buffer: %s' % nodeBuf) logger.debugException('') if len(outputSections) > 1: for packageBuffer in outputSections[1].split(' PACKAGE'): try: package = self.parseMultinodePackage(packageBuffer) if package: package.packageIp = netutils.resolveIP(self._shell, package.name) clusterDo.packages.append(package) except: logger.error('Failed parsing package information from buffer: %s' % packageBuffer) logger.debugException('') return clusterDo
def resolveIpFromDns(Framework, ipOrDnsOrAlias, localShell, dnsServers = None): normalizedIp = str(ipOrDnsOrAlias).strip() if not normalizedIp or normalizedIp == "localhost" or (ip_addr.isValidIpAddress(normalizedIp) and (ip_addr.IPAddress(normalizedIp).is_loopback or ip_addr.IPAddress(normalizedIp).is_multicast)): logger.debug('Skipped ip [', normalizedIp, '] for next hop, because it is empty or loopback or not a valid ip address') return None if dnsServers is not None: logger.debug('Trying to resolve ip using provided dnsServers names') dnsResolver = netutils.DNSResolver(localShell) for dnsServer in dnsServers: logger.debug('Trying to resolve ip using DNS Server [', dnsServer, ']') try: resolvedIp = dnsResolver.resolveHostIp(normalizedIp, dnsServer) if resolvedIp is not None: logger.debug('Resolved ip [', resolvedIp, '] from [', normalizedIp, '] using DNS Server [', dnsServer, ']') return resolvedIp except: Framework.reportWarning(logger.prepareJythonStackTrace('')) logger.debug('Failed to resolve [', normalizedIp, ']') try: logger.debug('Trying to resolve ip using local DNS server') resolvedIp = netutils.resolveIP(localShell, normalizedIp) if resolvedIp is not None: logger.debug('Resolved ip [', resolvedIp, '] from [', normalizedIp, '] using configured local DNS Server or hosts file') return resolvedIp else: errorMessage = 'Failed to resolve ip from [' + normalizedIp + '] using configured local DNS Server or hosts file' Framework.reportWarning(errorMessage) logger.warn(errorMessage) except: Framework.reportWarning(logger.prepareJythonStackTrace('')) logger.warn(errorMessage) return resolvedIp
def mapProcessToPortsInSpecialWay(self): invalidIpsMessage = '' errMsg = '' status = TCPDiscovery.OK try: socket_descriptors = get_socket_descriptors_by_pfiles(self.shUtils) for pid, socket_descriptor in socket_descriptors: local_ip, local_port, is_listen, protocol_type, _, _ = socket_descriptor local_ip = local_ip and sanitizeIps(local_ip) if local_ip and (not ip_addr.isValidIpAddress(local_ip)): local_ip = netutils.resolveIP(self.shUtils, local_ip) if local_ip and (ip_addr.isValidIpAddress(local_ip)): self._addTcpData(local_ip, local_port, pid, is_listen, protocol_type) else: invalidIpsMessage = invalidIpsMessage + 'On parsing ports by pfiles invalid ip:<%s>\n' % local_ip except TimeoutException: errMsg = 'Failed to map processes to ports by pfiles - timeout, try to increase command timeout parameter' logger.debugException(errMsg) self.Framework.reportWarning(errMsg) except: errMsg = 'Failed to map processes to ports by pfiles:see communication log' logger.debugException(errMsg) self.Framework.reportError(errMsg) status = TCPDiscovery.ERROR if len(invalidIpsMessage) > 0: wrnMsg = 'There are invalid ips found while paring port by pfiles, check communication log' self.Framework.reportWarning(wrnMsg) logger.debug(wrnMsg + '\n' + invalidIpsMessage) return status
def _resolveNames(self, ips_dict): result_dict = {} if ips_dict: for (lpar_id, ip) in ips_dict.items(): result_dict[lpar_id] = ip if ip and not netutils.isValidIp(ip): #ip might be a dns name, will try to resolve it ipAddress = netutils.resolveIP(self._shell, ip) if ipAddress: result_dict[lpar_id] = ipAddress return result_dict
def discoverNode(self, nodeBuffer): '''Discovers generic SG Cluster Node attributes @param nodeBuffer: string @raises ValueError: in case failed to parse out the node name or resolve name to ip. @returns: instance of Node DO ''' node = self._parseNode(nodeBuffer) node.ipAddress = netutils.resolveIP(self._shell, node.name) if not node.ipAddress: raise ValueError('Can not resolve node ip address by name, skipping cluster node: ', node.name) logger.debug('Resolved node ip is: %s' % node.ipAddress) return node
def process(self, context): client = context.client BsmBasePlugin.process(self, context) applicationOsh = context.application.getOsh() processes = context.application.getProcesses() vector = None for process in processes: if process.getName() in ["MercuryAS.exe", "MercuryAS"]: fullPath = process.executablePath pathToExecutableMatch = re.match("(.*)MercuryAS", fullPath) if pathToExecutableMatch: configFileContent = "" try: configFilePath = pathToExecutableMatch.group(1) + "../../odb/conf/cmdb.conf" configFileContent = self.getConfigFile(client, configFilePath) except: logger.debugException('') logger.debug('Failed getting configuration file.') else: parsedData = self.parseConfigFile(configFileContent) if parsedData["host"]: if netutils.isValidIp(parsedData["host"]): parsedData["serverIp"] = parsedData["serverIp"] else: parsedData["serverIp"] = netutils.resolveIP(client, parsedData["host"]) if parsedData["dbtype"] == "Oracle": vector = self.createOracleTopology(parsedData, applicationOsh) else: vector = self.createMsSqlTopology(parsedData, applicationOsh) versionFileContent = '' for fileLocation in BsmTopologyByShell.VERSION_FILE_LOCATIONS: try: configFilePath = pathToExecutableMatch.group(1) + fileLocation versionFileContent = self.getConfigFile(client, configFilePath) except: logger.debug('Failed getting version file content') continue (longVersion, shortVersion) = self.parseVersion(versionFileContent) if shortVersion and int(shortVersion) < 9: #Not a BSM but a BAC -> ignoring application raise applications.IgnoreApplicationException('Not a BSM installation') if longVersion: applicationOsh.setStringAttribute('application_version', longVersion) if shortVersion: applicationOsh.setStringAttribute('application_version_number' , shortVersion) vector and context.resultsVector.addAll(vector)
def discoverNode(self, nodeBuffer): '''Discovers generic SG Cluster Node attributes @param nodeBuffer: string @raises ValueError: in case failed to parse out the node name or resolve name to ip. @returns: instance of Node DO ''' node = self._parseNode(nodeBuffer) node.ipAddress = netutils.resolveIP(self._shell, node.name) if not node.ipAddress: raise ValueError( 'Can not resolve node ip address by name, skipping cluster node: ', node.name) logger.debug('Resolved node ip is: %s' % node.ipAddress) return node
def _parsePFilesOutput(self, output): results = [] if output: matcher = re.compile('localaddr/port\s+=\s+([\w\.]+)\s*/\s*(\d+).*listening') for block in re.split('S_ISSOCK', output): match = matcher.search(block) ip = None if match: if not netutils.isValidIp(match.group(1)): ip = netutils.resolveIP(self._shell, match.group(1)) else: ip = match.group(1) if not netutils.isLocalIp(ip): results.append(netutils.createTcpEndpoint(ip, match.group(2))) return results
def createCMDBConnection(i__LocalShell): logger.info('************* START createCMDBConnection *************') l__HostName = CollectorsParameters.getValue( CollectorsParameters.KEY_SERVER_NAME) l__HTTPPort = int( CollectorsParameters.getValue( CollectorsParameters.KEY_SERVER_PORT_HTTP)) l__HTTPSPort = int( CollectorsParameters.getValue( CollectorsParameters.KEY_SERVER_PORT_HTTPS)) l__ProtocolParameters = ProtocolDictionaryManager.getProtocolParameters( 'genericprotocol', netutils.resolveIP(i__LocalShell, l__HostName)) l__UserName = '' for l__Protocol in l__ProtocolParameters: if l__Protocol.getProtocolAttribute( 'protocol_username') == C__INTEGRATION_USER: l__UserName = l__Protocol.getProtocolAttribute('protocol_username') l__UserPassword = l__Protocol.getProtocolAttribute( 'protocol_password') break if not l__UserName: logger.error('Error Username Protocol not initialized') return None #logger.debug('Accessing uCMDB = ',(l__HostName, l__HTTPPort, l__UserName)) # try http first try: logger.debug('Attempting HTTP connection') l__Provider = UcmdbServiceFactory.getServiceProvider( 'http', l__HostName, l__HTTPPort) except: logger.debug('HTTP connection failed, trying HTTPS') UcmdbServiceFactory.initSSL() l__Provider = UcmdbServiceFactory.getServiceProvider( 'https', l__HostName, l__HTTPSPort) l__Credentials = l__Provider.createCredentials(l__UserName, l__UserPassword) l__ClientContext = l__Provider.createClientContext("UD") o__UcmdbService = l__Provider.connect(l__Credentials, l__ClientContext) logger.info('************* END createCMDBConnection *************') return o__UcmdbService
def _parsePFilesOutput(self, output): results = [] if output: matcher = re.compile( 'localaddr/port\s+=\s+([\w\.]+)\s*/\s*(\d+).*listening') for block in re.split('S_ISSOCK', output): match = matcher.search(block) ip = None if match: if not netutils.isValidIp(match.group(1)): ip = netutils.resolveIP(self._shell, match.group(1)) else: ip = match.group(1) if not netutils.isLocalIp(ip): results.append( netutils.createTcpEndpoint(ip, match.group(2))) return results
def parseCluster(self, clusterBuffer): '''Parses generic Cluster attributes @param clusterBuffer: string @raise ValueError: Failed to discover Service Guard Cluster @return: Cluster with link to Quorum Server if used ''' m = re.search('CLUSTER\s+STATUS\s*\n([\w\-\.]+)\s+(\w+)', clusterBuffer) if m: clusterName = m.group(1) packageIpAddress = netutils.resolveIP(self._shell, clusterName) # Discover Quorum server # all nodes have reference to the same server matchObj = self.QUORUM_SERVER_REGEXP.search(clusterBuffer) quorumServer = None if matchObj: # parse Quorum server information createIp = fptools.safeFunc(ip_addr.IPAddress) serverAddress = createIp(matchObj.group(3)) if not serverAddress: logger.warn("Quorum Server IP address is unknown") else: endpoint = netutils.createTcpEndpoint( serverAddress, service_guard.QuorumServer.DEFAULT_PORT) status = matchObj.group(2).lower() try: quorumServer = service_guard.QuorumServer( endpoint, status) logger.info("Discovered %s" % quorumServer) except ValueError, ve: logger.warn("Failed to create Quorum Server. %s" % ve) return service_guard.Cluster(clusterName, None, packageIpAddress, clusterBuffer, quorumServer=quorumServer)
def parseCluster(self, clusterBuffer): '''Parses generic Cluster attributes @param clusterBuffer: string @raise ValueError: Failed to discover Service Guard Cluster @return: Cluster with link to Quorum Server if used ''' m = re.search('CLUSTER\s+STATUS\s*\n([\w\-\.]+)\s+(\w+)', clusterBuffer) if m: clusterName = m.group(1) packageIpAddress = netutils.resolveIP(self._shell, clusterName) # Discover Quorum server # all nodes have reference to the same server matchObj = self.QUORUM_SERVER_REGEXP.search(clusterBuffer) quorumServer = None if matchObj: # parse Quorum server information createIp = fptools.safeFunc(ip_addr.IPAddress) serverAddress = createIp(matchObj.group(3)) if not serverAddress: logger.warn("Quorum Server IP address is unknown") else: endpoint = netutils.createTcpEndpoint(serverAddress, service_guard.QuorumServer.DEFAULT_PORT) status = matchObj.group(2).lower() try: quorumServer = service_guard.QuorumServer(endpoint, status) logger.info("Discovered %s" % quorumServer) except ValueError, ve: logger.warn("Failed to create Quorum Server. %s" % ve) return service_guard.Cluster(clusterName, None, packageIpAddress, clusterBuffer, quorumServer=quorumServer)
def discover(self): '''Main discovery method. @return: instance of Cluster DO. ''' outputBuffer = self.getCmviewclOutput() clusterDo = self.parseCluster(outputBuffer) clusterDo.version = self.getVersion() if not clusterDo.version: logger.warn('Failed getting cluster version') outputSections = outputBuffer.split('MULTI_NODE_PACKAGES') nodesBufList = outputSections[0].split('\n NODE ') if len(nodesBufList) > 1: # we remove first part of buffer which contains cluster information nodesBufList = nodesBufList[1:] for nodeBuf in nodesBufList: try: nodeAndPackagesBufferList = nodeBuf.split('\n PACKAGE ') if nodeAndPackagesBufferList: # first element contains Node information nodeDescription = nodeAndPackagesBufferList[0] nodeDo = self.discoverNode(nodeDescription) clusterDo.nodes.append(nodeDo) # package is quite independent from the node... # need to consider the exception handling if len(nodeAndPackagesBufferList) > 1: # all other elements contain package information for packageBuffer in nodeAndPackagesBufferList[1:]: try: packagesList = self.parsePackageInformation( packageBuffer) if packagesList: for package in packagesList: package.packageIp = netutils.resolveIP( self._shell, package.name) clusterDo.packages.extend(packagesList) except: logger.error( 'Failed parsing package information from buffer: %s' % packageBuffer) logger.debugException('') except: logger.error('Failed parsing node info from buffer: %s' % nodeBuf) logger.debugException('') if len(outputSections) > 1: for packageBuffer in outputSections[1].split(' PACKAGE'): try: package = self.parseMultinodePackage(packageBuffer) if package: package.packageIp = netutils.resolveIP( self._shell, package.name) clusterDo.packages.append(package) except: logger.error( 'Failed parsing package information from buffer: %s' % packageBuffer) logger.debugException('') return clusterDo
def process(self, context): client = context.client BsmBasePlugin.process(self, context) applicationOsh = context.application.getOsh() processes = context.application.getProcesses() vector = None for process in processes: if process.getName() in ["MercuryAS.exe", "MercuryAS"]: fullPath = process.executablePath pathToExecutableMatch = re.match("(.*)MercuryAS", fullPath) if pathToExecutableMatch: configFileContent = "" try: configFilePath = pathToExecutableMatch.group( 1) + "../../odb/conf/cmdb.conf" configFileContent = self.getConfigFile( client, configFilePath) except: logger.debugException('') logger.debug('Failed getting configuration file.') else: parsedData = self.parseConfigFile(configFileContent) if parsedData["host"]: if netutils.isValidIp(parsedData["host"]): parsedData["serverIp"] = parsedData["serverIp"] else: parsedData["serverIp"] = netutils.resolveIP( client, parsedData["host"]) if parsedData["dbtype"] == "Oracle": vector = self.createOracleTopology( parsedData, applicationOsh) else: vector = self.createMsSqlTopology( parsedData, applicationOsh) versionFileContent = '' for fileLocation in BsmTopologyByShell.VERSION_FILE_LOCATIONS: try: configFilePath = pathToExecutableMatch.group( 1) + fileLocation versionFileContent = self.getConfigFile( client, configFilePath) except: logger.debug('Failed getting version file content') continue (longVersion, shortVersion) = self.parseVersion(versionFileContent) if shortVersion and int(shortVersion) < 9: #Not a BSM but a BAC -> ignoring application raise applications.IgnoreApplicationException( 'Not a BSM installation') if longVersion: applicationOsh.setStringAttribute( 'application_version', longVersion) if shortVersion: applicationOsh.setStringAttribute( 'application_version_number', shortVersion) vector and context.resultsVector.addAll(vector)