def test_initialize_monitor(self): monitor = ProcessMonitor( self.config_commandline, scalyr_logging.getLogger("syslog_monitor[test]")) self.assertEqual(monitor._ProcessMonitor__metrics_history, defaultdict(dict)) self.assertEqual(monitor._ProcessMonitor__aggregated_metrics, {})
def setUp(self): self.config_commandline = { "module": "scalyr_agent.builtin_monitors.linux_process_metrics", "id": "myapp", "commandline": ".foo.*", } self.monitor = ProcessMonitor(self.config_commandline, scalyr_logging.getLogger("syslog_monitor[test]"))
def test_gather_sample_by_pid_failure_pid_doesnt_exist(self): monitor_config = { "module": "linux_process_metrics", "id": "my-id", "pid": 65555, } mock_logger = mock.Mock() monitor = ProcessMonitor(monitor_config, mock_logger) monitor.gather_sample() self.assertEqual(mock_logger.error.call_count, 3) self.assertEqual(mock_logger.emit_value.call_count, 0)
def test_gather_sample_by_commandline_success(self): monitor_config = { "module": "linux_process_metrics", "id": "my-id", "commandline": ".*%s.*" % (" ".join(sys.argv)), } mock_logger = mock.Mock() monitor = ProcessMonitor(monitor_config, mock_logger) monitor_module = "scalyr_agent.builtin_monitors.linux_process_metrics" monitor_info = load_monitor_class(monitor_module, [])[1] monitor.gather_sample() self.assertEqual(mock_logger.error.call_count, 0) self.assertEqual(mock_logger.emit_value.call_count, len(monitor_info.metrics))
def test_late_process_pid_setting(self): self.config = { "module": "scalyr_agent.builtin_monitors.linux_process_metrics", "id": "myapp", # the process ID is not known yet. "pid": "$$TBD", } self.monitor = ProcessMonitor( self.config, scalyr_logging.getLogger("syslog_monitor[test]")) # it can not select any process until the process id is set. self.assertEqual(self.monitor._select_processes(), []) self.assertEqual(self.monitor._select_processes(), []) # set the process id. self.monitor.set_pid(os.getpid()) # it has to select the current PID after it has been set. self.assertEqual(self.monitor._select_processes(), [os.getpid()])