def test_get_registered_check_plugins(monkeypatch): test_plugin = CheckPlugin( CheckPluginName("check_unit_test"), [], "Unit Test", None, # type: ignore # irrelevant for test None, # type: ignore # irrelevant for test None, # type: ignore # irrelevant for test None, # type: ignore # irrelevant for test None, # type: ignore # irrelevant for test None, # type: ignore # irrelevant for test None, # type: ignore # irrelevant for test None, # type: ignore # irrelevant for test None, # type: ignore # irrelevant for test ) monkeypatch.setattr(agent_based_register._config, "registered_check_plugins", {test_plugin.name: test_plugin}) assert agent_based_register.is_registered_check_plugin(test_plugin.name) assert agent_based_register.get_check_plugin( test_plugin.name) is test_plugin assert agent_based_register.get_check_plugin( CheckPluginName("mgmt_this_should_not_exists")) is None mgmt_plugin = agent_based_register.get_check_plugin( CheckPluginName("mgmt_%s" % test_plugin.name)) assert mgmt_plugin is not None assert str(mgmt_plugin.name).startswith("mgmt_") assert mgmt_plugin.service_name.startswith("Management Interface: ")
def test_discover_datapower_fan(datapower_fan_plugin: CheckPlugin) -> None: assert list(datapower_fan_plugin.discovery_function(_SECTION)) == [ Service(item="Tray 1 Fan 1"), Service(item="Tray 1 Fan 2"), Service(item="Tray 1 Fan 3"), Service(item="Tray 1 Fan 4"), ]
def test_check_table__get_static_check_entries(monkeypatch, check_group_parameters): hostname = "hostname" static_parameters = { 'levels': (1, 2, 3, 4), } static_checks: Dict[str, List] = { "ps": [(('ps', 'item', static_parameters), [], [hostname], {})], } ts = Scenario().add_host(hostname) ts.set_option("static_checks", static_checks) ts.set_ruleset("checkgroup_parameters", { 'ps': [(check_group_parameters, [hostname], [], {})], }) config_cache = ts.apply(monkeypatch) monkeypatch.setattr( agent_based_register, "get_check_plugin", lambda cpn: CheckPlugin( CheckPluginName("ps"), [], "Process item", None, # type: ignore None, # type: ignore None, # type: ignore None, # type: ignore None, # type: ignore {}, "ps", # type: ignore None, # type: ignore None, # type: ignore )) host_config = config_cache.get_host_config(hostname) static_check_parameters = [ service.parameters for service in check_table._get_static_check_entries(host_config) ] entries = config._get_checkgroup_parameters( config_cache, hostname, "ps", "item", "Process item", ) assert len(entries) == 1 assert entries[0] == check_group_parameters assert len(static_check_parameters) == 1 static_check_parameter = static_check_parameters[0] assert static_check_parameter == static_parameters
def test_check_datapower_fan( datapower_fan_plugin: CheckPlugin, item: str, expected_result: Result, ) -> None: assert (list( datapower_fan_plugin.check_function( item=item, params={}, section=_SECTION, )) == [expected_result])
def test_check_scaleio_devices( scaleio_devices: CheckPlugin, item: str, expected_result: list[Result], ) -> None: assert (list( scaleio_devices.check_function( item=item, params={}, section=_SECTION, )) == expected_result)
def management_plugin_factory(original_plugin: CheckPlugin) -> CheckPlugin: return CheckPlugin( original_plugin.name.create_management_name(), original_plugin.sections, "%s%s" % (MANAGEMENT_DESCR_PREFIX, original_plugin.service_name), original_plugin.discovery_function, original_plugin.discovery_default_parameters, original_plugin.discovery_ruleset_name, original_plugin.discovery_ruleset_type, original_plugin.check_function, original_plugin.check_default_parameters, original_plugin.check_ruleset_name, original_plugin.cluster_check_function, original_plugin.module, )
def fixture_check_plugin(monkeypatch): return CheckPlugin( CheckPluginName("unit_test_check_plugin"), [ParsedSectionName("norris")], "Unit Test", None, # type: ignore[arg-type] # irrelevant for test None, # type: ignore[arg-type] # irrelevant for test None, # type: ignore[arg-type] # irrelevant for test None, # type: ignore[arg-type] # irrelevant for test None, # type: ignore[arg-type] # irrelevant for test None, # type: ignore[arg-type] # irrelevant for test None, # type: ignore[arg-type] # irrelevant for test None, # type: ignore[arg-type] # irrelevant for test None, # type: ignore[arg-type] # irrelevant for test )
def _get_test_check_plugin(**kwargs) -> CheckPlugin: return CheckPlugin( **{ # type: ignore[arg-type] **{ "name": None, "sections": None, "service_name": None, "discovery_function": None, "discovery_default_parameters": None, "discovery_ruleset_name": None, "discovery_ruleset_type": None, "check_function": None, "check_default_parameters": None, "check_ruleset_name": None, "cluster_check_function": None, "module": None, }, **kwargs, } )
def check_plugin_fixture(monkeypatch, agent_section): check_plugin = CheckPlugin( CheckPluginName("unit_test_check_plugin"), [agent_section.parsed_section_name], "Unit Test", None, # type: ignore[arg-type] # irrelevant for test None, # type: ignore[arg-type] # irrelevant for test None, # type: ignore[arg-type] # irrelevant for test None, # type: ignore[arg-type] # irrelevant for test None, # type: ignore[arg-type] # irrelevant for test None, # type: ignore[arg-type] # irrelevant for test None, # type: ignore[arg-type] # irrelevant for test None, # type: ignore[arg-type] # irrelevant for test None, # type: ignore[arg-type] # irrelevant for test ) monkeypatch.setitem( agent_based_register._config.registered_check_plugins, check_plugin.name, check_plugin, ) yield check_plugin
def create_check_plugin( *, name: str, sections: Optional[List[str]] = None, service_name: str, discovery_function: Callable, discovery_default_parameters: Optional[Dict] = None, discovery_ruleset_name: Optional[str] = None, discovery_ruleset_type: RuleSetType = "merged", check_function: Callable, check_default_parameters: Optional[Dict] = None, check_ruleset_name: Optional[str] = None, cluster_check_function: Optional[Callable] = None, module: Optional[str] = None, validate_item: bool = True, validate_kwargs: bool = True, ) -> CheckPlugin: """Return an CheckPlugin object after validating and converting the arguments one by one For a detailed description of the parameters please refer to the exposed function in the 'register' namespace of the API. """ plugin_name = CheckPluginName(name) subscribed_sections = create_subscribed_sections(sections, plugin_name) requires_item = _requires_item(service_name) if validate_kwargs: _validate_kwargs( plugin_name=plugin_name, subscribed_sections=subscribed_sections, service_name=service_name, requires_item=requires_item, discovery_function=discovery_function, discovery_default_parameters=discovery_default_parameters, discovery_ruleset_name=discovery_ruleset_name, discovery_ruleset_type=discovery_ruleset_type, check_function=check_function, check_default_parameters=check_default_parameters, check_ruleset_name=check_ruleset_name, cluster_check_function=cluster_check_function, ) disco_func = _filter_discovery(discovery_function, requires_item, validate_item) disco_ruleset_name = RuleSetName( discovery_ruleset_name) if discovery_ruleset_name else None cluster_check_function = (unfit_for_clustering_wrapper(check_function) if cluster_check_function is None else _filter_check(cluster_check_function)) return CheckPlugin( name=plugin_name, sections=subscribed_sections, service_name=service_name, discovery_function=disco_func, discovery_default_parameters=discovery_default_parameters, discovery_ruleset_name=disco_ruleset_name, discovery_ruleset_type=discovery_ruleset_type, check_function=_filter_check(check_function), check_default_parameters=check_default_parameters, check_ruleset_name=RuleSetName(check_ruleset_name) if check_ruleset_name else None, cluster_check_function=cluster_check_function, module=module, )
def test_check_table__get_static_check_entries( monkeypatch: MonkeyPatch, check_group_parameters: LegacyCheckParameters) -> None: hostname = HostName("hostname") static_parameters_default = {"levels": (1, 2, 3, 4)} static_checks: Dict[str, List] = { "ps": [(("ps", "item", static_parameters_default), [], [hostname], {})], } ts = Scenario() ts.add_host(hostname) ts.set_option("static_checks", static_checks) ts.set_ruleset( "checkgroup_parameters", { "ps": [(check_group_parameters, [hostname], [], {})], }, ) config_cache = ts.apply(monkeypatch) monkeypatch.setattr( agent_based_register, "get_check_plugin", lambda cpn: CheckPlugin( CheckPluginName("ps"), [], "Process item", lambda: [], None, None, "merged", lambda: [], {}, RuleSetName("ps"), None, None, ), ) host_config = config_cache.get_host_config(hostname) static_check_parameters = [ service.parameters for service in check_table._get_static_check_entries( config_cache, host_config) ] entries = config._get_checkgroup_parameters( config_cache, hostname, "ps", "item", "Process item", ) assert len(entries) == 1 assert entries[0] == check_group_parameters assert len(static_check_parameters) == 1 static_check_parameter = static_check_parameters[0] assert static_check_parameter == TimespecificParameters(( TimespecificParameterSet(static_parameters_default, ()), TimespecificParameterSet({}, ()), ))
def test_discover_scaleio_devices(scaleio_devices: CheckPlugin) -> None: assert list(scaleio_devices.discovery_function(_SECTION)) == [ Service(item="devices"), ]