def create_test_monitors_manager( config_monitors=None, platform_monitors=None, extra_toplevel_config=None, null_logger=False, fake_clock=False, ): """Create a test MonitorsManager @param config_monitors: config monitors @param platform_monitors: platform monitors @param extra_toplevel_config: dict of extra top-level key value objects @param null_logger: If True, set all monitors to log to Nullhandler @param fake_clock: If non-null, the manager and all it's monitors' _run_state's will use the provided fake_clock """ monitors_json_array = JsonArray() if config_monitors: for entry in config_monitors: monitors_json_array.add(JsonObject(content=entry)) extras = {"monitors": monitors_json_array} if extra_toplevel_config: extras.update(extra_toplevel_config) config = ScalyrTestUtils.create_configuration( extra_toplevel_config=extras) config.parse() if not platform_monitors: platform_monitors = [] # noinspection PyTypeChecker test_manager = MonitorsManager(config, FakePlatform(platform_monitors)) if null_logger: # Override Agent Logger to prevent writing to disk for monitor in test_manager.monitors: monitor._logger = FakeAgentLogger("fake_agent_logger") if fake_clock: for monitor in test_manager.monitors + [test_manager]: monitor._run_state = scalyr_util.RunState( fake_clock=fake_clock) # AGENT-113 set all monitors and the monitors_manager threads to daemon to eliminate occasionally hanging tests test_manager.setDaemon(True) for monitor in test_manager.monitors: if isinstance(monitor, threading.Thread): monitor.setDaemon(True) return test_manager, config
def _test_k8s_event_object_filter_from_environment(self, environment_value): elems = ["CronJob", "DaemonSet", "Deployment"] os.environ["SCALYR_K8S_EVENT_OBJECT_FILTER"] = environment_value self._write_file_with_separator_conversion(""" { api_key: "hi there", logs: [ { path:"/var/log/tomcat6/access.log" }], monitors: [ { module: "scalyr_agent.builtin_monitors.kubernetes_events_monitor", } ] } """) config = self._create_test_configuration_instance() config.parse() test_manager = MonitorsManager(config, FakePlatform([])) k8s_event_monitor = test_manager.monitors[0] event_object_filter = k8s_event_monitor._config.get( "event_object_filter") self.assertNotEquals(elems, event_object_filter) # list != ArrayOfStrings self.assertEquals(type(event_object_filter), ArrayOfStrings) self.assertEquals(elems, list(event_object_filter))
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_test_monitors_manager(config_monitors=None, platform_monitors=None, extra_toplevel_config=None, null_logger=False, fake_clock=False, set_daemon=False): """Create a test MonitorsManager @param config_monitors: config monitors @param platform_monitors: platform monitors @param extra_toplevel_config: dict of extra top-level key value objects @param null_logger: If True, set all monitors to log to Nullhandler @param fake_clock: If non-null, the manager and all it's monitors' _run_state's will use the provided fake_clock """ monitors_json_array = JsonArray() if config_monitors: for entry in config_monitors: monitors_json_array.add(JsonObject(content=entry)) extras = {'monitors': monitors_json_array} if extra_toplevel_config: extras.update(extra_toplevel_config) config = ScalyrTestUtils.create_configuration(extra_toplevel_config=extras) config.parse() if not platform_monitors: platform_monitors = [] # noinspection PyTypeChecker test_manager = MonitorsManager(config, FakePlatform(platform_monitors)) if null_logger: # Override Agent Logger to prevent writing to disk for monitor in test_manager.monitors: monitor._logger = FakeAgentLogger('fake_agent_logger') if fake_clock: for monitor in test_manager.monitors + [test_manager]: monitor._run_state = scalyr_util.RunState(fake_clock=fake_clock) # AGENT-113 set all monitors and the monitors_manager threads to daemon to eliminate occasionally hanging tests test_manager.setDaemon(True) for monitor in test_manager.monitors: if isinstance(monitor, threading.Thread): monitor.setDaemon(True) return test_manager
def test_log_write_rate(self): self._write_file_with_separator_conversion(""" { api_key: "hi there", logs: [ { path:"/var/log/tomcat6/access.log" }], monitors: [ { module: "scalyr_agent.builtin_monitors.linux_system_metrics", "monitor_log_write_rate": -1, "monitor_log_max_write_burst": -1 } ] } """) config = self._create_test_configuration_instance() config.parse() test_manager = MonitorsManager(config, FakePlatform([])) system_metrics_monitor = test_manager.monitors[0] self.assertEquals(system_metrics_monitor._log_write_rate, -1) self.assertEquals(system_metrics_monitor._log_max_write_burst, -1)
def _assert_environment_variable(env_var_name, env_var_value, expected_value): os.environ[env_var_name] = env_var_value self._write_file_with_separator_conversion(""" { api_key: "hi there", logs: [ { path:"/var/log/tomcat6/access.log" }], monitors: [ { module: "scalyr_agent.builtin_monitors.kubernetes_events_monitor", } ] } """) config = self._create_test_configuration_instance() config.parse() test_manager = MonitorsManager(config, FakePlatform([])) k8s_event_monitor = test_manager.monitors[0] self.assertEquals( expected_value, k8s_event_monitor._KubernetesEventsMonitor__disable_monitor)
def test_k8s_event_object_filter_from_config(self): self._write_file_with_separator_conversion(""" { api_key: "hi there", logs: [ { path:"/var/log/tomcat6/access.log" }], monitors: [ { module: "scalyr_agent.builtin_monitors.kubernetes_events_monitor", event_object_filter: ["CronJob", "DaemonSet", "Deployment"] } ] } """) config = self._create_test_configuration_instance() config.parse() test_manager = MonitorsManager(config, FakePlatform([])) k8s_event_monitor = test_manager.monitors[0] event_object_filter = k8s_event_monitor._config.get( "event_object_filter") elems = ["CronJob", "DaemonSet", "Deployment"] self.assertNotEquals(elems, event_object_filter) # list != JsonArray self.assertEquals(elems, [x for x in event_object_filter])
def handle_shutdown_signal(signum, frame): print >>sys.stdout, 'Signal received, stopping monitor...' monitor.stop() for sig in (signal.SIGTERM, signal.SIGINT): signal.signal(sig, handle_shutdown_signal) try: if global_config_path is not None: controller = PlatformController.new_platform() paths = controller.default_paths global_config = Configuration( global_config_path, paths ) global_config.parse(logger=log) else: global_config = None monitor = MonitorsManager.build_monitor(parsed_config, monitor_python_path, float(monitor_sample_interval), global_config ) log.log(scalyr_logging.DEBUG_LEVEL_1, 'Constructed monitor') monitor.open_metric_log() log.log(scalyr_logging.DEBUG_LEVEL_1, 'Starting monitor') monitor.start() while monitor.isAlive(): time.sleep(0.1) except BadMonitorConfiguration, e: print >>sys.stderr, 'Invalid monitor configuration: %s' % str(e) return 0 if __name__ == '__main__': parser = OptionParser(usage='Usage: python -m scalyr_agent.scalyr_monitor [options] monitor_module') parser.add_option("-p", "--monitor-python-path", dest="monitor_python_path",
def run_standalone_monitor( monitor_module, monitor_python_path, monitor_config, monitor_sample_interval, monitor_debug_level, global_config_path, ): """Runs a single plugin monitor instance. @param monitor_module: The name of the python module implementing the monitor. @param monitor_python_path: The python path to search to find the module. @param monitor_config: The monitor configuration object. @param monitor_sample_interval: The default to use for the sample interval. @param monitor_debug_level: The debug level to use for logging. @param global_config_path: The path to the agent.json global configuration file to use, or None if none was supplied. """ scalyr_logging.set_log_destination(use_stdout=True) scalyr_logging.set_log_level(monitor_debug_level) log.log(scalyr_logging.DEBUG_LEVEL_1, "Attempting to run module %s", monitor_module) try: # Needs to be json_lib.parse because it is parsing configuration parsed_config = scalyr_util.json_scalyr_config_decode(monitor_config) log.log(scalyr_logging.DEBUG_LEVEL_1, "Parsed configuration successfully") except JsonParseException as e: print( "Failed to parse the monitor configuration as valid JSON: %s", six.text_type(e), file=sys.stderr, ) return 1 parsed_config["module"] = monitor_module if "id" not in parsed_config: parsed_config["id"] = "" # noinspection PyUnusedLocal def handle_shutdown_signal(signum, frame): print("Signal received, stopping monitor...", file=sys.stdout) monitor.stop() for sig in (signal.SIGTERM, signal.SIGINT): signal.signal(sig, handle_shutdown_signal) try: if global_config_path is not None: controller = PlatformController.new_platform() paths = controller.default_paths global_config = Configuration(global_config_path, paths, log) global_config.parse() else: global_config = None monitor = MonitorsManager.build_monitor( parsed_config, monitor_python_path, float(monitor_sample_interval), global_config, ) log.log(scalyr_logging.DEBUG_LEVEL_1, "Constructed monitor") monitor.open_metric_log() log.log(scalyr_logging.DEBUG_LEVEL_1, "Starting monitor") monitor.start() while monitor.isAlive(): time.sleep(0.1) except BadMonitorConfiguration as e: print("Invalid monitor configuration: %s" % six.text_type(e), file=sys.stderr) return 0
return 1 parsed_config['module'] = monitor_module if 'id' not in parsed_config: parsed_config['id'] = '' # noinspection PyUnusedLocal def handle_shutdown_signal(signum, frame): print >>sys.stdout, 'Signal received, stopping monitor...' monitor.stop() for sig in (signal.SIGTERM, signal.SIGINT): signal.signal(sig, handle_shutdown_signal) try: monitor = MonitorsManager.build_monitor(parsed_config, monitor_python_path) monitor.set_sample_interval(float(monitor_sample_interval)) monitor.open_metric_log() monitor.start() while monitor.is_alive(): time.sleep(0.1) except BadMonitorConfiguration, e: print >>sys.stderr, 'Invalid monitor configuration: %s' % str(e) return 0 if __name__ == '__main__': parser = OptionParser(usage='Usage: python -m scalyr_agent.scalyr_monitor [options] monitor_module') parser.add_option("-p", "--monitor-python-path", dest="monitor_python_path", help="Add PATH to the paths searched to find the python module for the monitor.", metavar="PATH",
def create_manager(): scalyr_monitor.log = mock_logger return MonitorsManager(config, FakePlatform([]))
return 1 parsed_config['module'] = monitor_module if 'id' not in parsed_config: parsed_config['id'] = '' # noinspection PyUnusedLocal def handle_shutdown_signal(signum, frame): print >> sys.stdout, 'Signal received, stopping monitor...' monitor.stop() for sig in (signal.SIGTERM, signal.SIGINT): signal.signal(sig, handle_shutdown_signal) try: monitor = MonitorsManager.build_monitor(parsed_config, monitor_python_path) log.log(scalyr_logging.DEBUG_LEVEL_1, 'Constructed monitor') monitor.set_sample_interval(float(monitor_sample_interval)) monitor.open_metric_log() log.log(scalyr_logging.DEBUG_LEVEL_1, 'Starting monitor') monitor.start() while monitor.is_alive(): time.sleep(0.1) except BadMonitorConfiguration, e: print >> sys.stderr, 'Invalid monitor configuration: %s' % str(e) return 0 if __name__ == '__main__':