Skip to content

Commit

Permalink
agent_based discovery: move code
Browse files Browse the repository at this point in the history
Change-Id: I7cc1487444c9d037a83e2f12c0469ec752fe7a2e
  • Loading branch information
mo-ki committed Nov 14, 2023
1 parent 930170d commit b3eea4e
Show file tree
Hide file tree
Showing 12 changed files with 561 additions and 238 deletions.
17 changes: 2 additions & 15 deletions cmk/base/api/agent_based/register/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

import cmk.utils.debug
import cmk.utils.paths
from cmk.utils.plugin_loader import load_plugins_with_exceptions

from cmk.base.api.agent_based.register._config import (
from ._config import (
add_check_plugin,
add_discovery_ruleset,
add_host_label_ruleset,
Expand Down Expand Up @@ -38,16 +34,7 @@
set_discovery_ruleset,
set_host_label_ruleset,
)


def load_all_plugins() -> list[str]:
errors = []
for plugin, exception in load_plugins_with_exceptions("cmk.base.plugins.agent_based"):
errors.append(f"Error in agent based plugin {plugin}: {exception}\n")
if cmk.utils.debug.enabled():
raise exception
return errors

from ._discover import load_all_plugins

__all__ = [
"add_check_plugin",
Expand Down
123 changes: 123 additions & 0 deletions cmk/base/api/agent_based/register/_discover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#!/usr/bin/env python3
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.


import cmk.utils.debug
from cmk.utils.plugin_loader import load_plugins_with_exceptions

from cmk.agent_based.v2alpha import (
AgentSection,
CheckPlugin,
InventoryPlugin,
SimpleSNMPSection,
SNMPSection,
)

from ._config import (
add_check_plugin,
add_discovery_ruleset,
add_host_label_ruleset,
add_inventory_plugin,
add_section_plugin,
is_registered_check_plugin,
is_registered_inventory_plugin,
is_registered_section_plugin,
)
from .check_plugins import create_check_plugin
from .inventory_plugins import create_inventory_plugin
from .section_plugins import create_agent_section_plugin, create_snmp_section_plugin


def load_all_plugins() -> list[str]:
errors = []
for plugin, exception in load_plugins_with_exceptions("cmk.base.plugins.agent_based"):
errors.append(f"Error in agent based plugin {plugin}: {exception}\n")
if cmk.utils.debug.enabled():
raise exception
return errors


def register_agent_section(section: AgentSection, full_module: str) -> None:
section_plugin = create_agent_section_plugin(
name=section.name,
parsed_section_name=section.parsed_section_name,
parse_function=section.parse_function,
host_label_function=section.host_label_function,
host_label_default_parameters=section.host_label_default_parameters,
host_label_ruleset_name=section.host_label_ruleset_name,
host_label_ruleset_type=section.host_label_ruleset_type,
supersedes=section.supersedes,
full_module=full_module,
)

if is_registered_section_plugin(section_plugin.name):
raise ValueError(f"duplicate section definition: {section_plugin.name}")

add_section_plugin(section_plugin)
if section_plugin.host_label_ruleset_name is not None:
add_host_label_ruleset(section_plugin.host_label_ruleset_name)


def register_snmp_section(section: SimpleSNMPSection | SNMPSection, full_module: str) -> None:
section_plugin = create_snmp_section_plugin(
name=section.name,
parsed_section_name=section.parsed_section_name,
parse_function=section.parse_function,
host_label_function=section.host_label_function,
host_label_default_parameters=section.host_label_default_parameters,
host_label_ruleset_name=section.host_label_ruleset_name,
host_label_ruleset_type=section.host_label_ruleset_type,
detect_spec=section.detect,
fetch=section.fetch,
supersedes=section.supersedes,
full_module=full_module,
)

if is_registered_section_plugin(section_plugin.name):
raise ValueError(f"duplicate section definition: {section_plugin.name}")

add_section_plugin(section_plugin)
if section_plugin.host_label_ruleset_name is not None:
add_host_label_ruleset(section_plugin.host_label_ruleset_name)


def register_check_plugin(check: CheckPlugin, full_module: str) -> None:
plugin = create_check_plugin(
name=check.name,
sections=check.sections,
service_name=check.service_name,
discovery_function=check.discovery_function,
discovery_default_parameters=check.discovery_default_parameters,
discovery_ruleset_name=check.discovery_ruleset_name,
discovery_ruleset_type=check.discovery_ruleset_type,
check_function=check.check_function,
check_default_parameters=check.check_default_parameters,
check_ruleset_name=check.check_ruleset_name,
cluster_check_function=check.cluster_check_function,
full_module=full_module,
)

if is_registered_check_plugin(plugin.name):
raise ValueError(f"duplicate check plugin definition: {plugin.name}")

add_check_plugin(plugin)
if plugin.discovery_ruleset_name is not None:
add_discovery_ruleset(plugin.discovery_ruleset_name)


def register_inventory_plugin(inventory: InventoryPlugin, full_module: str) -> None:
plugin = create_inventory_plugin(
name=inventory.name,
sections=inventory.sections,
inventory_function=inventory.inventory_function,
inventory_default_parameters=inventory.inventory_default_parameters,
inventory_ruleset_name=inventory.inventory_ruleset_name,
full_module=full_module,
)

if is_registered_inventory_plugin(plugin.name):
raise ValueError(f"duplicate inventory plugin definition: {plugin.name}")

add_inventory_plugin(plugin)
2 changes: 1 addition & 1 deletion cmk/base/api/agent_based/register/check_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
from cmk.base.api.agent_based.register.utils import (
create_subscribed_sections,
ITEM_VARIABLE,
RuleSetType,
validate_default_parameters,
validate_function_arguments,
validate_ruleset_type,
)

from cmk.agent_based.v1 import IgnoreResults, Metric, Result, Service
from cmk.agent_based.v1.register import RuleSetType

MANAGEMENT_DESCR_PREFIX = "Management Interface: "

Expand Down
Loading

0 comments on commit b3eea4e

Please sign in to comment.