Skip to content

Commit

Permalink
legacy check_api: drop get_check_api_context
Browse files Browse the repository at this point in the history
Even legacy plugins import properly these days.

Change-Id: If189bf2af30e40791dc63ea2485ce7446fb1efaa
  • Loading branch information
mo-ki committed Oct 15, 2024
1 parent 97d14f2 commit 9aaec35
Show file tree
Hide file tree
Showing 12 changed files with 11 additions and 45 deletions.
3 changes: 1 addition & 2 deletions bin/check_mk
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import cmk.utils.paths
from cmk.utils.log import console

import cmk.base.utils
from cmk.base import check_api, config, profiling
from cmk.base import config, profiling
from cmk.base.modes import modes

from cmk import trace
Expand Down Expand Up @@ -120,7 +120,6 @@ try:
# the configuration may refer to check config variable names.
if mode_name not in modes.non_checks_options():
errors = config.load_all_plugins(
check_api.get_check_api_context,
local_checks_dir=cmk.utils.paths.local_checks_dir,
checks_dir=cmk.utils.paths.checks_dir,
)
Expand Down
3 changes: 1 addition & 2 deletions cmk/base/automations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from cmk.automations.results import ABCAutomationResult

from cmk.base import check_api, config, profiling
from cmk.base import config, profiling

from cmk import trace

Expand Down Expand Up @@ -62,7 +62,6 @@ def execute(self, cmd: str, args: list[str]) -> Any:
):
log.setup_console_logging()
config.load_all_plugins(
check_api.get_check_api_context,
local_checks_dir=paths.local_checks_dir,
checks_dir=paths.checks_dir,
)
Expand Down
4 changes: 1 addition & 3 deletions cmk/base/automations/check_mk.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
import cmk.base.core
import cmk.base.nagios_utils
import cmk.base.parent_scan
from cmk.base import check_api, config, core_config, notify, server_side_calls, sources
from cmk.base import config, core_config, notify, server_side_calls, sources
from cmk.base.api.agent_based.value_store import ValueStoreManager
from cmk.base.automations import Automation, automations, MKAutomationError
from cmk.base.checkers import (
Expand Down Expand Up @@ -995,7 +995,6 @@ def execute(self, args: list[str]) -> SetAutochecksResult:
# (See autochecks.set_autochecks_of_cluster())
if hostname in config_cache.hosts_config.clusters:
config.load_all_plugins(
check_api.get_check_api_context,
local_checks_dir=local_checks_dir,
checks_dir=checks_dir,
)
Expand Down Expand Up @@ -1887,7 +1886,6 @@ def execute(self, args: list[str]) -> GetConfigurationResult:

if missing_variables:
config.load_all_plugins(
check_api.get_check_api_context,
local_checks_dir=local_checks_dir,
checks_dir=checks_dir,
)
Expand Down
9 changes: 0 additions & 9 deletions cmk/base/check_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

from cmk.utils.legacy_check_api import LegacyCheckDefinition as LegacyCheckDefinition

from cmk.base.config import CheckContext as _CheckContext

from cmk.agent_based import v1 as _v1

__all__ = [
Expand All @@ -30,7 +28,6 @@
"passwordstore_get_cmdline",
"LegacyResult",
"LegacyCheckResult",
"get_check_api_context",
"LegacyCheckDefinition",
]

Expand All @@ -52,12 +49,6 @@
LegacyCheckResult = Generator[tuple[int, str] | tuple[int, str, list[_MetricTuple]], None, None]


def get_check_api_context() -> _CheckContext:
"""This is called from cmk.base code to get the Check API things. Don't
use this from checks."""
return {k: v for k, v in globals().items() if k in __all__}


# .
# .--Check API-----------------------------------------------------------.
# | ____ _ _ _ ____ ___ |
Expand Down
18 changes: 5 additions & 13 deletions cmk/base/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,6 @@ def get_http_proxy(http_proxy: tuple[str, str]) -> HTTPProxyConfig:


def load_all_plugins(
get_check_api_context: GetCheckApiContext,
*,
local_checks_dir: Path,
checks_dir: str,
Expand All @@ -1413,7 +1412,7 @@ def load_all_plugins(
with tracer.start_as_current_span("discover_legacy_check_plugins"):
filelist = _get_plugin_paths(str(local_checks_dir), checks_dir)

errors.extend(load_checks(get_check_api_context, filelist))
errors.extend(load_checks(filelist))

return errors

Expand All @@ -1436,10 +1435,9 @@ def _get_plugin_paths(*dirs: str) -> list[str]:
# especially in tests.
@tracer.start_as_current_span("load_legacy_checks")
def load_checks(
get_check_api_context: GetCheckApiContext,
filelist: list[str],
) -> list[str]:
discovered_legacy_checks = discover_legacy_checks(get_check_api_context, filelist)
discovered_legacy_checks = discover_legacy_checks(filelist)

section_errors, sections = _make_agent_and_snmp_sections(
discovered_legacy_checks.sane_check_info, discovered_legacy_checks.plugin_files
Expand All @@ -1465,7 +1463,6 @@ class _DiscoveredLegacyChecks:


def discover_legacy_checks(
get_check_api_context: GetCheckApiContext,
filelist: list[str],
) -> _DiscoveredLegacyChecks:
loaded_files: set[str] = set()
Expand All @@ -1483,7 +1480,7 @@ def discover_legacy_checks(
continue # skip already loaded files (e.g. from local)

try:
check_context = new_check_context(get_check_api_context)
check_context = new_check_context()

# Make a copy of known plug-in names, we need to track them for nagios config generation
known_checks = {str(k) for k in check_info}
Expand Down Expand Up @@ -1521,17 +1518,12 @@ def discover_legacy_checks(
)


# Constructs a new check context dictionary. It contains the whole check API.
def new_check_context(get_check_api_context: GetCheckApiContext) -> CheckContext:
def new_check_context() -> CheckContext:
# Add the data structures where the checks register with Checkmk
context = {
return {
"check_info": check_info,
"special_agent_info": special_agent_info,
}
# NOTE: For better separation it would be better to copy the values, but
# this might consume too much memory, so we simply reference them.
context |= get_check_api_context()
return context


def plugin_pathnames_in_directory(path: str) -> list[str]:
Expand Down
4 changes: 2 additions & 2 deletions cmk/base/core_nagios/_host_check_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from cmk.checkengine.submitters import get_submitter

import cmk.base.utils
from cmk.base import check_api, config
from cmk.base import config
from cmk.base.api.agent_based.register import register_plugin_by_type
from cmk.base.core_nagios import HostCheckConfig
from cmk.base.modes.check_mk import mode_check
Expand Down Expand Up @@ -91,7 +91,7 @@ def main() -> int:
if debug:
cmk.ccc.debug.enable()

config.load_checks(check_api.get_check_api_context, CONFIG.checks_to_load)
config.load_checks(CONFIG.checks_to_load)

config.load_packed_config(LATEST_CONFIG)

Expand Down
2 changes: 0 additions & 2 deletions cmk/update_config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
# to a specific layer in the future, but for the the moment we need to deal
# with it.
from cmk.base import config as base_config
from cmk.base.check_api import get_check_api_context

from cmk.gui import main_modules
from cmk.gui.exceptions import MKUserError
Expand Down Expand Up @@ -290,7 +289,6 @@ def _check_failed_gui_plugins(logger: logging.Logger) -> None:

def _initialize_base_environment() -> None:
base_config.load_all_plugins(
get_check_api_context,
local_checks_dir=paths.local_checks_dir,
checks_dir=paths.checks_dir,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import cmk.base.config as base_config
from cmk.base.api.agent_based import register as agent_based_register
from cmk.base.check_api import get_check_api_context

from cmk.gui.exceptions import MKUserError
from cmk.gui.session import SuperUserContext
Expand Down Expand Up @@ -671,7 +670,6 @@ def __call__(self, logger: Logger, conflict_mode: ConflictMode) -> None:
if conflict_mode in (ConflictMode.INSTALL, ConflictMode.KEEP_OLD):
return
base_config.load_all_plugins(
get_check_api_context,
local_checks_dir=paths.local_checks_dir,
checks_dir=paths.checks_dir,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from cmk.utils.paths import local_checks_dir

from cmk.base.check_api import get_check_api_context
from cmk.base.config import load_checks, plugin_pathnames_in_directory

from cmk.gui.exceptions import MKUserError
Expand All @@ -20,9 +19,7 @@ class PreUpdateLegacyCheckPlugins(PreUpdateAction):
"""Load all legacy checks plugins before the real update happens"""

def __call__(self, logger: Logger, conflict_mode: ConflictMode) -> None:
errors = "".join(
load_checks(get_check_api_context, plugin_pathnames_in_directory(str(local_checks_dir)))
)
errors = "".join(load_checks(plugin_pathnames_in_directory(str(local_checks_dir))))
if errors:
logger.error(errors)
if continue_per_users_choice(
Expand Down
2 changes: 0 additions & 2 deletions cmk/validate_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
)

from cmk.base import ( # pylint: disable=cmk-module-layer-violation
check_api,
server_side_calls,
)
from cmk.base.api.agent_based.register import ( # pylint: disable=cmk-module-layer-violation
Expand Down Expand Up @@ -86,7 +85,6 @@ def to_result(step: ValidationStep, errors: Sequence[str]) -> ActiveCheckResult:

def _validate_agent_based_plugin_loading() -> ActiveCheckResult:
errors = load_all_plugins(
check_api.get_check_api_context,
local_checks_dir=paths.local_checks_dir,
checks_dir=paths.checks_dir,
)
Expand Down
2 changes: 0 additions & 2 deletions tests/extension_compatibility/_helper_failed_base_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@

from cmk.utils import paths

from cmk.base.check_api import get_check_api_context
from cmk.base.config import load_all_plugins

print(
json.dumps(
load_all_plugins(
get_check_api_context,
local_checks_dir=paths.local_checks_dir,
checks_dir=paths.checks_dir,
)
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ class FixRegister:
def __init__(self) -> None:
# Local import to have faster pytest initialization
from cmk.base import ( # pylint: disable=bad-option-value,import-outside-toplevel,cmk-module-layer-violation
check_api,
config,
)
from cmk.base.api.agent_based import ( # pylint: disable=bad-option-value,import-outside-toplevel,cmk-module-layer-violation
Expand All @@ -280,7 +279,6 @@ def __init__(self) -> None:
assert not config.check_info

errors = config.load_all_plugins(
check_api.get_check_api_context,
local_checks_dir=repo_path() / "no-such-path-but-thats-ok",
checks_dir=str(repo_path() / "cmk/base/legacy_checks"),
)
Expand Down

0 comments on commit 9aaec35

Please sign in to comment.