def load_monitor(monitor_module, additional_python_paths): """Loads the module for the specified monitor. @param monitor_module: The module for the monitor. @param additional_python_paths: A list of paths (separate by os.pathsep) to add to the PYTHONPATH when instantiating the module in case it needs to packages in other directories. @type monitor_module: str @type additional_python_paths: str @return: The class for the ScalyrMonitor in the module. @rtype: class """ # Augment the PYTHONPATH if requested to locate the module. paths_to_pass = [] # Also add in scalyr_agent/../monitors/local and scalyr_agent/../monitors/contrib to the Python path to search # for monitors. (They are always in the parent directory of the scalyr_agent package. path_to_package_parent = os.path.dirname(get_package_root()) paths_to_pass.append( os.path.join(path_to_package_parent, 'monitors', 'local')) paths_to_pass.append( os.path.join(path_to_package_parent, 'monitors', 'contrib')) # Add in the additional paths. if additional_python_paths is not None and len( additional_python_paths) > 0: for x in additional_python_paths.split(os.pathsep): paths_to_pass.append(x) return load_monitor_class(monitor_module, os.pathsep.join(paths_to_pass))[0]
def load_monitor(monitor_module, additional_python_paths): """Loads the module for the specified monitor. @param monitor_module: The module for the monitor. @param additional_python_paths: A list of paths (separate by os.pathsep) to add to the PYTHONPATH when instantiating the module in case it needs to packages in other directories. @type monitor_module: str @type additional_python_paths: str @return: The class for the ScalyrMonitor in the module. @rtype: class """ # Augment the PYTHONPATH if requested to locate the module. paths_to_pass = [] # Also add in scalyr_agent/../monitors/local and scalyr_agent/../monitors/contrib to the Python path to search # for monitors. (They are always in the parent directory of the scalyr_agent package. path_to_package_parent = os.path.dirname(get_package_root()) paths_to_pass.append(os.path.join(path_to_package_parent, 'monitors', 'local')) paths_to_pass.append(os.path.join(path_to_package_parent, 'monitors', 'contrib')) # Add in the additional paths. if additional_python_paths is not None and len(additional_python_paths) > 0: for x in additional_python_paths.split(os.pathsep): paths_to_pass.append(x) return load_monitor_class(monitor_module, os.pathsep.join(paths_to_pass))[0]
def test_gather_sample_success(self): monitor_config = { "module": "linux_system_metrics", } mock_logger = mock.Mock() monitor = SystemMetricsMonitor(monitor_config, mock_logger) monitor_module = "scalyr_agent.builtin_monitors.linux_system_metrics" monitor_info = load_monitor_class(monitor_module, [])[1] monitor.setDaemon(True) monitor.start() # Give it some time to spawn collectors and collect metrics time.sleep(2) monitor.stop() self.assertEqual(mock_logger.error.call_count, 0) self.assertTrue(mock_logger.info.call_count > len(monitor_info.metrics)) # Verify all the metrics have been dispatched seen_metrics = set() for call_args in mock_logger.info.call_args_list: line = call_args[0][0] split = line.split(" ") metric_name = split[0] if "." not in metric_name: # Not a metric continue seen_metrics.add(metric_name) self.assertTrue(len(seen_metrics) >= len(monitor_info.metrics)) for metric in monitor_info.metrics: metric_name = metric.metric_name if ( IS_FEDORA and metric_name.startswith("df.") and metric_name not in seen_metrics ): # On some newer versions of fedora, retrieving disk metrics will fail with # "operation not permitted" error if not running as root so this error should not # be fatal here continue self.assertTrue( metric_name in seen_metrics, "metric %s not seen" % (metric_name) )
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_gather_sample_success(self): monitor_config = { "module": "linux_system_metrics", } mock_logger = mock.Mock() monitor = SystemMetricsMonitor(monitor_config, mock_logger) monitor_module = "scalyr_agent.builtin_monitors.linux_system_metrics" monitor_info = load_monitor_class(monitor_module, [])[1] monitor.setDaemon(True) monitor.start() # Give it some time to spawn collectors and collect metrics time.sleep(2) monitor.stop() self.assertEqual(mock_logger.error.call_count, 0) self.assertTrue( mock_logger.info.call_count > len(monitor_info.metrics)) # Verify all the metrics have been dispatched seen_metrics = set() for call_args in mock_logger.info.call_args_list: line = call_args[0][0] split = line.split(" ") metric_name = split[0] if "." not in metric_name: # Not a metric continue seen_metrics.add(metric_name) self.assertTrue(len(seen_metrics) >= len(monitor_info.metrics)) for metric in monitor_info.metrics: metric_name = metric.metric_name self.assertTrue(metric_name in seen_metrics, "metric %s not seen" % (metric_name))