def default_paths(self): """Returns the default paths to use for various configuration options for this platform. @return: The default paths @rtype: DefaultPaths """ if self._install_type == PACKAGE_INSTALL: return DefaultPaths( "/var/log/scalyr-agent-2", "/etc/scalyr-agent-2/agent.json", "/var/lib/scalyr-agent-2", ) elif self._install_type == TARBALL_INSTALL: install_location = get_install_root() return DefaultPaths( os.path.join(install_location, "log"), os.path.join(install_location, "config", "agent.json"), os.path.join(install_location, "data"), ) else: assert self._install_type == DEV_INSTALL # For developers only. We default to a directory ~/scalyr-agent-dev for storing # all log/data information, and then require a log, config, and data subdirectory in each of those. base_dir = os.path.join(os.path.expanduser("~"), "scalyr-agent-dev") return DefaultPaths( os.path.join(base_dir, "log"), os.path.join(base_dir, "config", "agent.json"), os.path.join(base_dir, "data"), )
def default_paths(self): """Returns the default paths to use for various configuration options for this platform. @return: The default paths @rtype: DefaultPaths """ if self._install_type == PACKAGE_INSTALL: return DefaultPaths('/var/log/scalyr-agent-2', '/etc/scalyr-agent-2/agent.json', '/var/lib/scalyr-agent-2') elif self._install_type == TARBALL_INSTALL: install_location = get_install_root() return DefaultPaths( os.path.join(install_location, 'log'), os.path.join(install_location, 'config', 'agent.json'), os.path.join(install_location, 'data')) else: assert (self._install_type == DEV_INSTALL) # For developers only. We default to a directory ~/scalyr-agent-dev for storing # all log/data information, and then require a log, config, and data subdirectory in each of those. base_dir = os.path.join(os.path.expanduser('~'), 'scalyr-agent-dev') return DefaultPaths(os.path.join(base_dir, 'log'), os.path.join(base_dir, 'config', 'agent.json'), os.path.join(base_dir, 'data'))
def __create_test_instance(self, configuration_logs_entry, monitors_log_configs): config_dir = tempfile.mkdtemp() config_file = os.path.join(config_dir, 'agentConfig.json') config_fragments_dir = os.path.join(config_dir, 'configs.d') os.makedirs(config_fragments_dir) logs_json_array = JsonArray() for entry in configuration_logs_entry: logs_json_array.add(JsonObject(content=entry)) fp = open(config_file, 'w') fp.write( json_lib.serialize(JsonObject(api_key='fake', logs=logs_json_array))) fp.close() default_paths = DefaultPaths('/var/log/scalyr-agent-2', '/etc/scalyr-agent-2/agent.json', '/var/lib/scalyr-agent-2') config = Configuration(config_file, default_paths) config.parse() self.__monitor_fake_instances = [] for monitor_log_config in monitors_log_configs: self.__monitor_fake_instances.append( FakeMonitor(monitor_log_config)) # noinspection PyTypeChecker return CopyingManager(config, self.__monitor_fake_instances)
def create_configuration(extra_toplevel_config=None): """Creates a blank configuration file with default values. Optionally overwrites top-level key/values. Sets api_key to 'fake' unless defined in extra_toplevel_config @param extra_toplevel_config: Dict of top-level key/value objects to overwrite. @return: The configuration object @rtype: Configuration """ config_dir = tempfile.mkdtemp() config_file = os.path.join(config_dir, 'agentConfig.json') config_fragments_dir = os.path.join(config_dir, 'configs.d') os.makedirs(config_fragments_dir) toplevel_config = {'api_key': 'fake'} if extra_toplevel_config: toplevel_config.update(extra_toplevel_config) fp = open(config_file, 'w') fp.write(json_lib.serialize(JsonObject(**toplevel_config))) fp.close() default_paths = DefaultPaths('/var/log/scalyr-agent-2', '/etc/scalyr-agent-2/agent.json', '/var/lib/scalyr-agent-2') config = Configuration(config_file, default_paths, None) config.parse() # we need to delete the config dir when done atexit.register(shutil.rmtree, config_dir) return config
def __create_test_instance(self, config_monitors, platform_monitors): config_dir = tempfile.mkdtemp() config_file = os.path.join(config_dir, 'agentConfig.json') config_fragments_dir = os.path.join(config_dir, 'configs.d') os.makedirs(config_fragments_dir) monitors_json_array = JsonArray() for entry in config_monitors: monitors_json_array.add(JsonObject(content=entry)) fp = open(config_file, 'w') fp.write( json_lib.serialize( JsonObject(api_key='fake', monitors=monitors_json_array))) fp.close() default_paths = DefaultPaths('/var/log/scalyr-agent-2', '/etc/scalyr-agent-2/agent.json', '/var/lib/scalyr-agent-2') config = Configuration(config_file, default_paths) config.parse() # noinspection PyTypeChecker return MonitorsManager(config, FakePlatform(platform_monitors))
def create_configuration(extra_toplevel_config=None): """Creates a blank configuration file with default values. Optionally overwrites top-level key/values. Sets api_key to 'fake' unless defined in extra_toplevel_config @param extra_toplevel_config: Dict of top-level key/value objects to overwrite. @return: The configuration object @rtype: Configuration """ config_dir = tempfile.mkdtemp() config_file = os.path.join(config_dir, "agentConfig.json") config_fragments_dir = os.path.join(config_dir, "configs.d") os.makedirs(config_fragments_dir) toplevel_config = {"api_key": "fake"} if extra_toplevel_config: toplevel_config.update(extra_toplevel_config) fp = open(config_file, "w") fp.write(scalyr_util.json_encode(toplevel_config)) fp.close() default_paths = DefaultPaths( "/var/log/scalyr-agent-2", "/etc/scalyr-agent-2/agent.json", "/var/lib/scalyr-agent-2", ) config = Configuration(config_file, default_paths, None) config.parse() # we need to delete the config dir when done atexit.register(shutil.rmtree, config_dir) return config
def __create_test_configuration_instance(self): default_paths = DefaultPaths( "/var/log/scalyr-agent-2", "/etc/scalyr-agent-2/agent.json", "/var/lib/scalyr-agent-2", ) return Configuration(self.__config_file, default_paths, None)
def __create_test_configuration_instance(self): """Creates an instance of a Configuration file for testing. @return: The test instance @rtype: Configuration """ default_paths = DefaultPaths(self.convert_path('/var/log/scalyr-agent-2'), self.convert_path('/etc/scalyr-agent-2/agent.json'), self.convert_path('/var/lib/scalyr-agent-2')) return Configuration(self.__config_file, default_paths)
def default_paths(self): """Returns the default paths to use for various configuration options for this platform. @return: The default paths @rtype: DefaultPaths """ # NOTE: For this module, it is assumed that the 'install_type' is always PACKAGE_INSTALL root = get_install_root() logdir = os.path.join(root, "log") libdir = os.path.join(root, "data") config = os.path.join(root, "config", "agent.json") return DefaultPaths(logdir, config, libdir)
def create_copying_manager(self, config): if 'api_key' not in config: config['api_key'] = 'fake' f = open(self._config_file, "w") if f: f.write(scalyr_util.json_encode(config)) f.close() default_paths = DefaultPaths(self._log_dir, self._config_file, self._data_dir) configuration = Configuration(self._config_file, default_paths, None) configuration.parse() self._manager = TestableCopyingManager(configuration, []) self._controller = self._manager.controller
def __create_test_instance(self, use_pipelining=False): tmp_dir = tempfile.mkdtemp() config_dir = os.path.join(tmp_dir, "config") data_dir = os.path.join(tmp_dir, "data") log_dir = os.path.join(tmp_dir, "log") os.mkdir(data_dir) os.mkdir(config_dir) os.mkdir(log_dir) self.__test_log_file = os.path.join(tmp_dir, "test.log") fp = open(self.__test_log_file, "w") fp.close() config_file = os.path.join(config_dir, "agentConfig.json") config_fragments_dir = os.path.join(config_dir, "configs.d") os.makedirs(config_fragments_dir) logs_json_array = JsonArray() logs_json_array.add(JsonObject(path=self.__test_log_file)) pipeline_threshold = 1.1 if use_pipelining: pipeline_threshold = 0.0 fp = open(config_file, "w") fp.write( json_lib.serialize( JsonObject( api_key="fake", logs=logs_json_array, pipeline_threshold=pipeline_threshold, ))) fp.close() default_paths = DefaultPaths(log_dir, config_file, data_dir) config = Configuration(config_file, default_paths, None) config.parse() # noinspection PyTypeChecker self._controller = TestableCopyingManager(config, []).controller return self._controller
def init_config(self, config_data=None): # type: (Optional[Dict]) -> None """ Create config object and apply all additions. :param config_data: :return: """ default_paths = DefaultPaths( six.text_type(self.agent_logs_path), six.text_type(self.agent_config_path), six.text_type(self.agent_data_path), ) if config_data is None: config_data = dict() if "api_key" not in config_data: config_data["api_key"] = "fake" if "debug_level" not in config_data: config_data["debug_level"] = 5 logs = config_data.get("logs", list()) logs.append({"path": six.text_type(self.glob_logs_dir / "log_*.log")}) for i in range(self.MAX_NON_GLOB_TEST_LOGS): path = self.non_glob_logs_dir / ("log_%s.log" % i) logs.append({"path": six.text_type(path)}) config_data["logs"] = logs self.agent_config_path.write_text( six.text_type(json.dumps(config_data))) config = TestingConfiguration(six.text_type(self.agent_config_path), default_paths, None) config.parse() self._config = config
def create_copying_manager(self, config, monitor_agent_log=False): if "api_key" not in config: config["api_key"] = "fake" if not monitor_agent_log: config["implicit_agent_log_collection"] = False f = open(self._config_file, "w") if f: f.write(scalyr_util.json_encode(config)) f.close() default_paths = DefaultPaths(self._log_dir, self._config_file, self._data_dir) configuration = Configuration(self._config_file, default_paths, None) configuration.parse() self._manager = TestableCopyingManager(configuration, []) self._controller = self._manager.controller
def __create_test_configuration_instance(self): default_paths = DefaultPaths('/var/log/scalyr-agent-2', '/etc/scalyr-agent-2/agent.json', '/var/lib/scalyr-agent-2') def log_factory(config): return CopyingParamsTest.LogObject(config) def monitor_factory(config, _): return CopyingParamsTest.MonitorObject(config) monitors = [ JsonObject( module='scalyr_agent.builtin_monitors.linux_system_metrics'), JsonObject( module='scalyr_agent.builtin_monitors.linux_process_metrics', pid='$$', id='agent') ] return Configuration(self.__config_file, default_paths, monitors, log_factory, monitor_factory)
def test_config_parse_memory_leak(self): # There was a memory leak with config.parse() and underlying __perform_substitutions method config_path = os.path.join(BASE_DIR, "fixtures/configs/agent2.json") default_paths = DefaultPaths( "/var/log/scalyr-agent-2", "/etc/scalyr-agent-2/agent.json", "/var/lib/scalyr-agent-2", ) # New config object is created for index in range(0, 50): config = Configuration(file_path=config_path, default_paths=default_paths, logger=None) config.parse() self.assertNoNewGarbage() # Existing config object is reused config = Configuration(file_path=config_path, default_paths=default_paths, logger=None) for index in range(0, 50): config.parse() self.assertNoNewGarbage()
def __create_test_configuration_instance(self): default_paths = DefaultPaths('/var/log/scalyr-agent-2', '/etc/scalyr-agent-2/agent.json', '/var/lib/scalyr-agent-2') return Configuration(self.__config_file, default_paths)