def process(self, context, discoverer, dnsresolver, installpath): try: server = discoverer.getHanaDatabaseServer() instances = discoverer.getHanaDatabaseInstances() get_sql_ports = Sfn(discoverer.getHanaDatabaseInstanceSqlPorts) instance_ports_pairs = [(instance, get_sql_ports(instance.hostname)) for instance in instances] resolve_ips = Sfn(dnsresolver.resolve_ips) instance_descriptors = [] for instance, ports in instance_ports_pairs: instance_pdo = buildDatabaseInstancePdo(instance, installpath, server=server) host_pdo = hana_host.parse_from_address( instance.hostname, resolve_ips) # ignore the name as it is could be an alias and not a real hostname host_pdo = host_pdo._replace(name=None) instance_descriptors.append((instance_pdo, host_pdo, ports)) reporter = hana.DatabaseTopologyReporter() _, _, _, oshs = reporter.report_database_with_instances( server, instance_descriptors) context.resultsVector.addAll(oshs) except command.ExecuteException, e: raise Exception(e.result.output)
def get_process_bin_path(shell, process, system_home_folder=None, instance_name=None): ''' Get executable path for Win or Unix like system r@types: shellutils.Shell, process.Process, str, str -> str ''' if shell.isWinOs(): bin_path = process.executablePath or process.getName() else: discoverer = ProcessCwdDiscovererOnUnixByShell(shell) working_dir_path = Sfn(discoverer.getWorkingDirByReadlinkInProc, fallbackFn=Sfn( discoverer.getWorkingDirByCwd, fallbackFn=Sfn( discoverer.getWorkingDirByPwdInProc, fallbackFn=Sfn( discoverer.getWorkingDirByPwdx))))( process.getPid()) logger.debug('Working directory path %s' % working_dir_path) if working_dir_path: bin_path = '%s/%s' % (working_dir_path, process.getName()) else: bin_path = process.getName() if system_home_folder and instance_name: bin_path = '%s/%s/exe/%s' % (system_home_folder, instance_name, process.getName()) return bin_path
def get_fc_hbas(self, shell): result = [] executor = command.cmdlet.executeCommand(shell) lsdev = lsdev_aix.find(executor) vpd_discoverer = vital_product_data.Discoverer.find_impl(executor) exec_ = Sfn(self.__get_produce_result_executor(shell).process) fcadapters = self.list_fc_adapter_names(lsdev, exec_) for fcs in fcadapters: try: descriptor = vpd_discoverer.get_fc_vpd_by_devicename( fcs, executor) id_ = descriptor.hardware_location_code nodewwn = descriptor.device_specific.get('Z8') fw_version = None if 'Z9' in descriptor.device_specific: fw_version = descriptor.device_specific.get('Z9') _, fw_version = fw_version.split('.') nodewwn = wwn.parse_from_str(nodewwn) portwwn = descriptor.network_address serialnum = descriptor.serial_number model = descriptor.platform_specific.model driverid = descriptor.driverid vendor = Sfn(self.parse_vendor_from_driverid)(driverid) driver_version = self.get_driver_version(driverid, exec_) fchba = fc_hba_model.FcHba(id_, fcs, wwn=nodewwn, vendor=vendor, model=model, serial_number=serialnum, driver_version=driver_version, firmware_version=fw_version) ports = [] try: port_wwn = wwn.parse_from_str(portwwn) fcstat = Sfn(fcstat_aix.find)(executor) port_id = None type_ = None if fcstat: fcstat_descriptor = exec_(fcstat(fcs)) if fcstat_descriptor: port_id = Sfn(int)(fcstat_descriptor.port_fc_id, 16) type_ = fcstat_descriptor.port_type port_speed = fcstat_descriptor.running_port_speed ports.append((fc_hba_model.FcPort(port_id, port_wwn, type_, None, port_speed), ())) except (command.ExecuteException, TypeError, ValueError), ex: logger.debugException( 'Failed to create fcport data object') result.append((fchba, ports)) except (command.ExecuteException, TypeError, ValueError), ex: logger.debugException('Failed to create fchba data object')
def is_applicable(self, os_platform, executor=None, protocol_name=None, **kwargs): is_applicable_platform_fn = fc_hba_discoverer.Discoverer.is_applicable is_applicable_platform = is_applicable_platform_fn(self, os_platform) if is_applicable_platform: lsdev = Sfn(lsdev_aix.find)(executor) vpd_impl = Sfn(vital_product_data.Discoverer.find_impl)(executor) return lsdev and vpd_impl
def getHanaDbLogFiles(self, db_instance): r'@types: hana.DatabaseServer -> list[db.LogFile]' descriptors = HdbLogFilesCmd(db_instance.hostname) | self._executor logfiles = starmap(Sfn(db.LogFile), descriptors) return filter(None, logfiles) return HdbLogFilesCmd(db_instance.hostname) | self._executor
def parse(self, configParser): r'@types: ConfigParser.ConfigParser -> list[RfcConfiguration]' if not configParser: raise ValueError("ConfigParser is not specified") parseConnectionSection = Sfn(Fn(self.__parseSection, __, configParser)) sessions = configParser.sections() sessions = ifilter(self.__isTrexRfcConnectionConfigSection, sessions) return keep(parseConnectionSection, sessions)
def _create_target_fchba_details(self, remote_descriptors): result = [] if remote_descriptors: for remote_descriptor in remote_descriptors: try: port_id = Sfn(int)(remote_descriptor.target_n_port_id, 16) wwpn = wwn.normalize(remote_descriptor.target_port_wwn) wwnn = wwn.normalize(remote_descriptor.target_node_wwn) port_type = remote_descriptor.port_type port_name = '' node_name = '' if remote_descriptor.symbolic_port_name and remote_descriptor.symbolic_port_name.lower( ) != 'none': port_name = remote_descriptor.symbolic_port_name if remote_descriptor.symbolic_node_name and remote_descriptor.symbolic_node_name.lower( ) != 'none': node_name = remote_descriptor.symbolic_node_name fchba = fc_hba_model.FcHba('', node_name, wwn=wwnn) fcport = fc_hba_model.FcPort(port_id, wwpn, type=port_type, name=port_name) result.append((fchba, fcport, None)) except (TypeError, ValueError): logger.debugException( 'Failed to create target fchba/fcport data object') return tuple(result)
def find(executor, alternatives=None): '''Finds vmkmgmt_keyval binary and appropriate wrapper implementation @param executor: a command executor instance @type executor: command.Executor @return: vmkmgmt_keyval command implementation @rtype: vmkmgmt_keyval.Cmd @raise command.NotFoundException: in case if `vmkmgmt_keyval` command is not available @raise service_loader.NoImplementationException: in case if no `vmkmgmt_keyval` wrapper available ''' alternatives = alternatives or bin_alternatives try: bin = is_command_available.find_first(alternatives, executor) except service_loader.NoImplementationException: bin = first(alternatives) if not bin: raise command.NotFoundException( 'No vmkmgmt_keyval binary found among provided path alternatives') vmkmgmt_keyval_impls = (Cmd, ) for vmkmgmt_keyval_impl in vmkmgmt_keyval_impls: if Sfn(vmkmgmt_keyval_impl.is_applicable)(bin, executor): return partial(vmkmgmt_keyval_impl, bin=bin) raise service_loader.NoImplementationException( 'No vmkmgmt_keyval impl found')
def is_applicable(lsdev, bin, executor): '''Returns bool value indicating whether current command is applicable for target destination @param bin: path to binary @type bin: basestring @param executor: a command executor instance @type executor: command.Executor @return: True if command is applicable for target destination, False otherwise @rtype: bool ''' expected_output = 'usage: lscfg [-vps] [-l Name ]' raise_on_invalid_return_code = partial(raise_on_return_code_not_in_range, codes=(1, )) handlers = (command.UnixBaseCmd.DEFAULT_HANDLERS + (raise_on_invalid_return_code, command.cmdlet.raiseWhenOutputIsNone, attrgetter('output'), methodcaller('strip'), )) handler = lsdev.compose_handler(handlers) exec_ = Sfn(executor(useCache=1).process) result = exec_(lsdev(bin, options=['usage', ], handler=handler)) if result: result = result.handler(result) return result == expected_output
def is_applicable(fcinfo, bin, executor): '''Returns bool value indicating whether current command is applicable for target destination @param bin: path to binary @type bin: basestring @param executor: a command executor instance @type executor: command.Executor @return: True if command is applicable for target destination, False otherwise @rtype: bool ''' handlers = (command.UnixBaseCmd.DEFAULT_HANDLERS + ( command.cmdlet.raiseWhenOutputIsEmpty, attrgetter('output'), methodcaller('strip'), )) handler = fcinfo.compose_handler(handlers) exec_ = Sfn(executor(useCache=1).process) result = exec_(fcinfo(bin, options=[ '-?', ], handler=handler)) if result: result = result.handler(result) return 'fcinfo hba-port' in result and 'fcinfo remote-port' in result
def find(executor, alternatives=None): '''Finds lsdev binary and appropriate wrapper implementation @param executor: a command executor instance @type executor: command.Executor @return: lsdev command implementation @rtype: lsdev.Cmd @raise command.NotFoundException: in case if `lsdev` command is not available @raise service_loader.NoImplementationException: in case if no `lsdev` wrapper available ''' alternatives = alternatives or bin_alternatives try: bin = is_command_available.find_first(alternatives, executor) except service_loader.NoImplementationException: bin = first(alternatives) if not bin: raise command.NotFoundException( 'No lsdev binary found among provided path alternatives.') lsdev_impls = service_loader.global_lookup[Cmd] for lsdev_impl in lsdev_impls: if Sfn(lsdev_impl.is_applicable)(bin, executor): return partial(lsdev_impl, bin=bin) raise service_loader.NoImplementationException('No lsdev impl found')
class HdbServicesCmd(SqlCmd): TABLE_NAME = r'm_services' FIELDS = ( 'host', 'port', 'service_name', 'process_id', 'sql_port', 'coordinator_type', ) WHERE = "lower(host)=lower('%s')" DEFAULT_HANDLERS = (SqlCmd.DEFAULT_HANDLERS + ( partial(map, operator.attrgetter(*FIELDS)), partial( starmap, Sfn(aggregate_fn(identity, int, identity, identity, int, identity))), )) def __init__(self, hostname, handler=None): self.hostname = hostname SqlCmd.__init__(self, handler=handler) def _get_where_conditions(self): return self.WHERE % (self.hostname)
def find(tools): discoverers = service_loader.global_lookup[Discoverer] for discoverer in discoverers: is_applicable = webseal_wiring.wired()(discoverer.is_applicable) if Sfn(is_applicable)(**tools): return discoverer.create(**tools) raise flow.DiscoveryException('No webseal discoverer found')
def findHdbsqlPathBySid(fs, installationPath, dbSid, is_cmd_exist, shell_executor): binName = _HDBSQL_BINARY_NAME if fs._shell.isWinOs(): binName = '%s.exe' % binName pathTool = file_system.getPath(fs) alternatives = ( file_system.Path(installationPath, pathTool) + _HDBCLIENT_FOLDER_NAME + binName, file_system.Path('/usr/sap', pathTool) + _HDBCLIENT_FOLDER_NAME + binName, file_system.Path('/usr/sap', pathTool) + dbSid + _HDBCLIENT_FOLDER_NAME + binName, file_system.Path('/usr/sap', pathTool) + dbSid + 'exe' + 'linuxx86_64' + 'hdb' + binName, file_system.Path('/usr/sap', pathTool) + dbSid + r'SYS' + r'global' + _HDBCLIENT_FOLDER_NAME + binName, file_system.Path('/sapmnt', pathTool) + dbSid + _HDBCLIENT_FOLDER_NAME + binName, file_system.Path('/sapmnt', pathTool) + dbSid + r'global' + _HDBCLIENT_FOLDER_NAME + binName, ) alternatives = imap(shell_interpreter.normalizePath, alternatives) binpath = findFirst(Sfn(fs.exists), alternatives) if not binpath: bin_in_path = is_cmd_exist(binName) | shell_executor if not bin_in_path: raise NoHdbsqlException('Hdbsql binary not found') binpath = binName return binpath
def _get_pf_doc(self, pf_path, sys_name, shell): '@types: str, str, Shell -> File, File, InitDocument' base_path = sap_discoverer.findSystemBasePath(pf_path, sys_name) get_def_pf_fn = Fn(discover_default_pf, shell, pf_path, sys_name) get_inst_pf_fn = Fn(sap_discoverer_by_shell.read_pf, shell, pf_path) default_pf_result = self._get_or_discover(base_path, Sfn(get_def_pf_fn)) default_pf_file, default_pf_doc = default_pf_result or (None, None) if not default_pf_file: logger.warn("Failed to get content for the DEFAULT.PFL") pf_file, pf_doc = self._get_or_discover(pf_path, Sfn(get_inst_pf_fn)) if not pf_file: logger.warn("Failed to get content for the instance profile") doc = sap_discoverer.createPfsIniDoc(default_pf_doc, None, pf_doc) return default_pf_file, pf_file, doc
def find_discoverer_by_os_platform(platform, executor=None, protocol_name=None): discoverers = service_loader.global_lookup[Discoverer] for discoverer in discoverers: if Sfn(discoverer.is_applicable)(platform, executor=executor, protocol_name=protocol_name): return discoverer raise flow.DiscoveryException('No fc hba discoverer found for %s' % platform)
def find(tools): impls = service_loader.global_lookup[Cmd] for impl in impls: is_applicable = webseal_wiring.wired()(impl.is_applicable) if Sfn(is_applicable)(**tools): return impl.create(**tools) raise service_loader.NoImplementationException('No pdadmin impl found')
def get_fc_hbas(self, shell): result = [] descriptors = fc_hba_descriptors_by_hpux_fcmsutil.get_fc_hba_descriptors( shell) find_name_by_id_in_hex = Sfn(vendors.find_name_by_id_in_hex) for ioscan_dscriptor, fc_descriptor, fc_vpd_descriptor in descriptors: try: hba_id = ioscan_dscriptor.device_filename name = ioscan_dscriptor.device_filename node_wwn = fc_descriptor.n_port_node_world_wide_name node_wwn = wwn.normalize(node_wwn) vendor = find_name_by_id_in_hex(fc_descriptor.vendor_id) model = fc_vpd_descriptor.part_number serial_number = fc_vpd_descriptor.part_serial_number driver_version = fc_descriptor.driver_version firmware_version = fc_vpd_descriptor.rom_firmware_version port_speed = fc_descriptor.link_speed fchba = fc_hba_model.FcHba(hba_id, name, wwn=node_wwn, vendor=vendor, model=model, serial_number=serial_number, driver_version=driver_version, firmware_version=firmware_version) port_id = fc_descriptor.local_n_port_id port_wwn = wwn.normalize( fc_descriptor.n_port_port_world_wide_name) type_ = fc_descriptor.topology ports = [] port_id = Sfn(int)(port_id, 16) speed = _parse_port_speed(port_speed) remote_descriptors = Sfn(fc_hba_descriptors_by_hpux_fcmsutil. get_remote_fc_hba_descriptors)( shell, ioscan_dscriptor.device_filename) target_fchbas = self._create_target_fchba_details( remote_descriptors) ports.append((fc_hba_model.FcPort(port_id, port_wwn, type_, None, speed), target_fchbas)) result.append((fchba, tuple(ports))) except (TypeError, ValueError), ex: logger.debugException('Failed to create fchba data object')
def _resolveEndpoint(resolver, endpoint): r'@types: dns_resolver.DnsResolver, Endpoint -> list[Endpoint]' address = endpoint.getAddress() if netutils.isValidIp(address): return [endpoint] resolveAddressFn = Sfn(resolver.resolveIpsByHostname, fallbackFn=constantly([])) return filter(None, (netutils.updateEndpointAddress(endpoint, ip) for ip in resolveAddressFn(address)))
def _discoverRfcDestinations(sapUtils, systemOsh, config): r'@types: SapUtils, osh, flow.DiscoveryConfigBuilder -> oshv' if not config.discoverRFCConnections: return ObjectStateHolderVector() logger.info('Discover RFC connections') getRfcCmd = sap_abap_discoverer.GetRfcDestinationsRfcCommand() connections = Sfn(getRfcCmd.getAllRfcConnections)(sapUtils) or () logger.info("Found %s possible RFC connections" % len(connections)) connections = filter(comp(sap_abap_discoverer.isEnglishVersion, third), connections) logger.info("Found %s RFC connections with EN language" % len(connections)) connByName = applyMapping(first, connections) destinations = getRfcCmd.getAllRfcDestinations(sapUtils) logger.info("Found %s RFC destinations" % len(destinations)) # get destinations with valid host destinations = [d for d in destinations if _isDestFull(d)] logger.info("Found %s destinations with host available" % len(destinations)) destinationsByHost = groupby(lambda d: d.targetHost, destinations) ips = map(Sfn(_resolve), destinationsByHost.iterkeys()) pairIpToDestinations = zip(ips, destinationsByHost.itervalues()) resolved, notResolved = partition(first, pairIpToDestinations) if notResolved: skippedDestsCount = sum([len(dests) for ip, dests in notResolved]) logger.debug("%s destinations skipped due to not resolved %s hosts" % (skippedDestsCount, len(notResolved))) vector = ObjectStateHolderVector() for ip, destinations in resolved: # TODO: # 1) query RFC connections (to get description) only for these # destinations as it will reduce amount of data fetched from system # One query for connections returns ~8K rows of data, while we are # interested in less than ~50 or even less # 2) another improvement query only records in English language countOfDests = len(destinations) host = first(destinations).targetHost reportDst = Sfn(_reportRfcDestination) logger.debug("%s destinations resolved for %s" % (countOfDests, host)) vectors = (reportDst(dst, ip, connByName, systemOsh) for dst in destinations) each(vector.addAll, ifilter(None, vectors)) return vector
def get_safe_exec_fn(*executors): '''Creates exec function accepting command.Cmd object and returning processed command result which raises no exception. @param executors: sequence of command.Cmdlet instances @type executors: [command.Cmdlet] @return: exec function implementation based on chained execution of each executor passed @rtype: callable[command.Cmd]->object or None ''' return Sfn(get_exec_fn(*executors))
def _discover_paths(dbm_cli, db_name, osh): '''Discover application, program and data pathes for the current DB @types: DbmCli7_6, str, osh -> osh''' attrs = ('application_path', 'data_path', 'program_path') fns = (dbm_cli.get_isntallation_path, dbm_cli.get_indep_data_path, dbm_cli.get_indep_prog_path) paths = (Sfn(fn)(db_name=db_name) for fn in fns) attrname_to_value = ifilter(second, izip(attrs, paths)) tuple(starmap(osh.setAttribute, attrname_to_value)) return osh
def getHanaDatabaseServer(self): r'@types: str -> hana.DatabaseServer' name, hostname, version, startTime = MDatabaseCmd() | self._executor replication_role = Sfn(self.getReplicationRoleByHostname)(hostname) return hana.DatabaseServer(name, hostname, version, startTime, replication_role=replication_role)
def get_fc_hbas(self, shell): result = [] executor = self.get_executor(shell) pairs = get_fc_hba_descriptors(executor) parse_wwn_ = Sfn(parse_wwn) for adapter_descriptor, port_descriptors in pairs: hba_id = adapter_descriptor.UniqueAdapterId name = adapter_descriptor.InstanceName node_wwn = adapter_descriptor.NodeWWN node_wwn = node_wwn and parse_wwn_(node_wwn) vendor = adapter_descriptor.Manufacturer model = adapter_descriptor.Model serial_number = adapter_descriptor.SerialNumber driver_version = adapter_descriptor.DriverVersion firmware_version = adapter_descriptor.FirmwareVersion fchba = fc_hba_model.FcHba(hba_id, name, wwn=node_wwn, vendor=vendor, model=model, serial_number=serial_number, driver_version=driver_version, firmware_version=firmware_version) ports = [] for port_descriptor in port_descriptors: try: port_id = port_descriptor.UniquePortId portname = port_descriptor.InstanceName port_wwn = parse_wwn_(port_descriptor.Attributes.PortWWN) type_ = Sfn(parse_port_type)( port_descriptor.Attributes.PortType) port_speed = float(port_descriptor.Attributes.PortSpeed) ports.append((fc_hba_model.FcPort(port_id, port_wwn, type_, portname, speed=port_speed), ())) except (TypeError, ValueError), ex: logger.debugException('Failed to create fchba data object') result.append((fchba, tuple(ports)))
def get_webseal_servers(self): server_names = self.http_executor(self.pdadmin.server.list) active_partition = Sfn(self.get_active_firmware_partition)() version = active_partition and active_partition.firmware_version result = [] for server_name in server_names: if server_name != 'ivmgrd-master': server = self.http_executor( self.pdadmin.server.show(server_name)) result.append((webseal_model.Server(server.name, version), server.hostname, server.admin_request_port)) return tuple(result)
def is_applicable(esxcli, bin, executor): '''Returns bool value indicating whether current command is applicable for target destination @param bin: path to binary @type bin: basestring @param executor: a command executor instance @type executor: command.Executor @return: True if command is applicable for target destination, False otherwise @rtype: bool ''' exec_ = Sfn(executor(useCache=1).process) result = exec_(esxcli(bin=bin).version()) if result: result = Sfn(result.handler)(result) # Applicable to all versions as of now. When there will be a conflict # introducing new functionality for this esxcli impl, lets distinguish # between impls basing on version return True
def getHanaDbTraceFiles(self, db_instance): r'@types: hana.DatabaseServer -> list[db.TraceFile]' descriptors = HdbTraceFilesCmd(db_instance.hostname) | self._executor traceFiles = starmap(Sfn(db.TraceFile), descriptors) traceFiles = filter(None, traceFiles) path = HdbTraceFilePathCmd(db_instance.hostname) | self._executor if path: path = self._pathTool.join(path, 'trace') return path and map( lambda traceFile: db.TraceFile( self._pathTool.join(path, traceFile.name), traceFile.size), traceFiles) or traceFiles
def discover_fc_hbas(framework, host_osh, protocol, vector): discoverFcHBAs = Boolean.parseBoolean( framework.getParameter('discoverFcHBAs')) if discoverFcHBAs: import wmi from fc_hba_discoverer import discover_fc_hba_oshs_by_shell client = None try: client = wmi.ClientWrapper(partial(__create_client_fn, framework)) shell = wmi.ShellWrapper(client) oshs = discover_fc_hba_oshs_by_shell(shell, host_osh, protocol) vector.addAll(oshs) finally: client and Sfn(client.close)()
class HdbTraceFilesCmd(SqlCmd): TABLE_NAME = r'm_tracefiles' FIELDS = ('file_name', 'file_size') DEFAULT_HANDLERS = (SqlCmd.DEFAULT_HANDLERS + (partial(map, operator.attrgetter(*FIELDS)), partial(starmap, Sfn(aggregate_fn(identity, long))))) WHERE = "lower(host)=lower('%s')" def __init__(self, hostname, handler=None): self.hostname = hostname SqlCmd.__init__(self, handler=handler) def _get_where_conditions(self): return self.WHERE % (self.hostname)
def _discoverServers(solman, hostnameToAddress, sysPairsBySysName, sendVector, reportError, resolveIps): ''' Discover SAP instances related to already discovered systems @type hostnameToAddress: dict[str, sap.Address] @type sysPairsBySysName: dict[str, tuple[System, osh]] ''' try: # get servers by one of the specified queries queries = (GetServersWithNotActiveFlag(), GetServers()) queryExecutor = TableQueryExecutor(solman) result = imap(Sfn(queryExecutor.executeQuery), queries) servers = findFirst(truth, result) or () # group servers by system name pairsBySysName = groupby(GetServers.Server.systemName.fget, servers) inDiscoveredSystems = comp(sysPairsBySysName.get, first) pairs = ifilter(inDiscoveredSystems, pairsBySysName.iteritems()) resolveIps = comp(resolveIps, GetServers.Server.hostname.fget) for sysName, servers in pairs: logger.info("Found %s servers for %s system" % (len(servers), sysName)) # collect parsed names for each server parseServerName = comp(GetServers.parseServerName, GetServers.Server.name.fget) parsedServerNames = imap(parseServerName, servers) # resolve IPs for each server ips = imap(resolveIps, servers) # get information for each server where name and IPs are present infoSeq = ifilter(all, izip(servers, parsedServerNames, ips)) # not interested in server nodes - only instances infoSeq = ifilterfalse(isServerNode, infoSeq) # report each server system, systemOsh = sysPairsBySysName.get(sysName) reportServer = F(_reportServer, fptools._, fptools._, fptools._, system, systemOsh) vector = ObjectStateHolderVector() each(vector.addAll, starmap(reportServer, infoSeq)) sendVector(vector) except (Exception, JException): msg = "Failed to discover servers" logger.warnException(msg) reportError(msg)