Skip to content

Commit

Permalink
simplify config computation
Browse files Browse the repository at this point in the history
Change-Id: I8da602fb6aea1f9c35fc6b74fa2151969e0743f2
  • Loading branch information
mo-ki committed Dec 3, 2024
1 parent cd319c2 commit a81793d
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 76 deletions.
12 changes: 0 additions & 12 deletions cmk/base/automations/check_mk.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,6 @@ def execute(self, args: list[str]) -> ServiceDiscoveryResult:

parser = CMKParser(
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
plugins, hostname, selected_sections=NO_SELECTION
),
selected_sections=NO_SELECTION,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.discovery"),
Expand Down Expand Up @@ -602,9 +599,6 @@ def _execute_discovery(
autochecks_config = config.AutochecksConfigurer(config_cache)
parser = CMKParser(
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
plugins, hostname, selected_sections=NO_SELECTION
),
selected_sections=NO_SELECTION,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.discovery"),
Expand Down Expand Up @@ -729,9 +723,6 @@ def _execute_autodiscovery() -> tuple[Mapping[HostName, DiscoveryResult], bool]:
ruleset_matcher = config_cache.ruleset_matcher
parser = CMKParser(
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
ab_plugins, hostname, selected_sections=NO_SELECTION
),
selected_sections=NO_SELECTION,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.discovery"),
Expand Down Expand Up @@ -2796,9 +2787,6 @@ def execute(self, args: list[str]) -> GetAgentOutputResult:
config_cache.parser_factory(),
source_info.hostname,
source_info.fetcher_type,
checking_sections=config_cache.make_checking_sections(
plugins, hostname, selected_sections=NO_SELECTION
),
persisted_section_dir=make_persisted_section_dir(
source_info.hostname,
fetcher_type=source_info.fetcher_type,
Expand Down
3 changes: 0 additions & 3 deletions cmk/base/checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,12 @@ def __init__(
self,
factory: ParserFactory,
*,
checking_sections: Callable[[HostName], Iterable[SectionName]],
selected_sections: SectionNameCollection,
keep_outdated: bool,
logger: logging.Logger,
) -> None:
self.factory: Final = factory
self.selected_sections: Final = selected_sections
self.checking_sections: Final = checking_sections
self.keep_outdated: Final = keep_outdated
self.logger: Final = logger

Expand All @@ -196,7 +194,6 @@ def __call__(
self.factory,
source.hostname,
source.fetcher_type,
checking_sections=self.checking_sections(source.hostname),
persisted_section_dir=make_persisted_section_dir(
source.hostname,
fetcher_type=source.fetcher_type,
Expand Down
35 changes: 14 additions & 21 deletions cmk/base/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,7 @@ def __init__(self) -> None:
self.__hostgroups: dict[HostName, Sequence[str]] = {}
self.__contactgroups: dict[HostName, Sequence[_ContactgroupName]] = {}
self.__explicit_check_command: dict[HostName, HostCheckCommand] = {}
self.__snmp_fetch_interval: dict[tuple[HostName, SectionName], int | None] = {}
self.__snmp_fetch_interval: dict[HostName, Mapping[SectionName, int | None]] = {}
self.__labels: dict[HostName, Labels] = {}
self.__label_sources: dict[HostName, LabelSources] = {}
self.__notification_plugin_parameters: dict[tuple[HostName, str], Mapping[str, object]] = {}
Expand Down Expand Up @@ -2736,29 +2736,26 @@ def host_check_command(
def missing_sys_description(self, host_name: HostName) -> bool:
return self.ruleset_matcher.get_host_bool_value(host_name, snmp_without_sys_descr)

def snmp_fetch_interval(self, host_name: HostName, section_name: SectionName) -> int | None:
"""Return the fetch interval of SNMP sections in seconds
def snmp_fetch_intervals(self, host_name: HostName) -> Mapping[SectionName, int | None]:
"""Return the configured fetch intervals of SNMP sections in seconds
This has been added to reduce the fetch interval of single SNMP sections
to be executed less frequently than the "Check_MK" service is executed.
"""

def snmp_fetch_interval_impl() -> int | None:
for sections, (_option_id, seconds) in self.ruleset_matcher.get_host_values(
host_name,
snmp_check_interval,
):
if str(section_name) in sections:
return None if seconds is None else round(seconds) # use first match

return None
def snmp_fetch_interval_impl() -> Mapping[SectionName, int | None]:
return {
SectionName(section_name): None if seconds is None else round(seconds)
for sections, (_option_id, seconds) in reversed( # use first match
self.ruleset_matcher.get_host_values(host_name, snmp_check_interval)
)
for section_name in sections
}

with contextlib.suppress(KeyError):
return self.__snmp_fetch_interval[(host_name, section_name)]
return self.__snmp_fetch_interval[host_name]

return self.__snmp_fetch_interval.setdefault(
(host_name, section_name), snmp_fetch_interval_impl()
)
return self.__snmp_fetch_interval.setdefault(host_name, snmp_fetch_interval_impl())

def _collect_hosttags(self, tag_to_group_map: Mapping[TagID, TagGroupID]) -> None:
"""Calculate the effective tags for all configured hosts
Expand Down Expand Up @@ -4023,15 +4020,11 @@ def make_snmp_parser(
*,
keep_outdated: bool,
logger: logging.Logger,
checking_sections: Iterable[SectionName],
) -> SNMPParser:
return SNMPParser(
host_name,
section_store,
persist_periods={
section_name: self._config_cache.snmp_fetch_interval(host_name, section_name)
for section_name in checking_sections
},
persist_periods=self._config_cache.snmp_fetch_intervals(host_name),
host_check_interval=self._config_cache.check_mk_check_interval(host_name),
keep_outdated=keep_outdated,
logger=logger,
Expand Down
21 changes: 0 additions & 21 deletions cmk/base/modes/check_mk.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,9 +696,6 @@ def mode_dump_agent(options: Mapping[str, object], hostname: HostName) -> None:
config_cache.parser_factory(),
source_info.hostname,
source_info.fetcher_type,
checking_sections=config_cache.make_checking_sections(
plugins, hostname, selected_sections=NO_SELECTION
),
persisted_section_dir=make_persisted_section_dir(
source_info.hostname,
fetcher_type=source_info.fetcher_type,
Expand Down Expand Up @@ -1872,9 +1869,6 @@ def mode_check_discovery(
)
parser = CMKParser(
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
plugins, hostname, selected_sections=NO_SELECTION
),
selected_sections=NO_SELECTION,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.discovery"),
Expand Down Expand Up @@ -2175,9 +2169,6 @@ def mode_discover(options: _DiscoveryOptions, args: list[str]) -> None:
config_cache = config.get_config_cache()
parser = CMKParser(
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
plugins, hostname, selected_sections=NO_SELECTION
),
selected_sections=selected_sections,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.discovery"),
Expand Down Expand Up @@ -2384,9 +2375,6 @@ def mode_check(
)
parser = CMKParser(
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
plugins, hostname, selected_sections=NO_SELECTION
),
selected_sections=selected_sections,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.checking"),
Expand Down Expand Up @@ -2625,9 +2613,6 @@ def mode_inventory(options: _InventoryOptions, args: list[str]) -> None:
)
parser = CMKParser(
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
plugins, hostname, selected_sections=NO_SELECTION
),
selected_sections=selected_sections,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.inventory"),
Expand Down Expand Up @@ -2885,9 +2870,6 @@ def mode_inventory_as_check(
)
parser = CMKParser(
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
plugins, hostname, selected_sections=NO_SELECTION
),
selected_sections=NO_SELECTION,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.inventory"),
Expand Down Expand Up @@ -3046,9 +3028,6 @@ def mode_inventorize_marked_hosts(options: Mapping[str, object]) -> None:
plugins = agent_based_register.get_previously_loaded_plugins()
parser = CMKParser(
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
plugins, hostname, selected_sections=NO_SELECTION
),
selected_sections=NO_SELECTION,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.inventory"),
Expand Down
7 changes: 1 addition & 6 deletions cmk/base/sources/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
# conditions defined in the file COPYING, which is part of this source code package.

import logging
from collections.abc import Iterable, Sequence
from collections.abc import Sequence
from pathlib import Path
from typing import Protocol

from cmk.utils.hostaddress import HostName
from cmk.utils.sectionname import SectionName

from cmk.snmplib import SNMPRawDataElem

Expand All @@ -25,7 +24,6 @@ def make_snmp_parser(
hostname: HostName,
section_store: SectionStore[SNMPRawDataElem],
*,
checking_sections: Iterable[SectionName],
keep_outdated: bool,
logger: logging.Logger,
) -> Parser: ...
Expand All @@ -45,8 +43,6 @@ def make_parser(
hostname: HostName,
fetcher_type: FetcherType,
*,
# Always from NO_SELECTION.
checking_sections: Iterable[SectionName],
persisted_section_dir: Path,
keep_outdated: bool,
logger: logging.Logger,
Expand All @@ -58,7 +54,6 @@ def make_parser(
persisted_section_dir,
logger=logger,
),
checking_sections=checking_sections,
keep_outdated=keep_outdated,
logger=logger,
)
Expand Down
15 changes: 5 additions & 10 deletions tests/unit/cmk/base/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1759,18 +1759,15 @@ def test_config_cache_snmp_credentials_of_version(
assert config_cache.snmp_credentials_of_version(hostname, version) == result


@pytest.mark.usefixtures("agent_based_plugins")
@pytest.mark.parametrize(
"hostname, section_name, result",
"hostname, result",
[
(HostName("testhost1"), "uptime", None),
(HostName("testhost2"), "uptime", None),
(HostName("testhost1"), "snmp_uptime", None),
(HostName("testhost2"), "snmp_uptime", 4),
(HostName("testhost1"), {}),
(HostName("testhost2"), {SectionName("snmp_uptime"): 240}),
],
)
def test_snmp_check_interval(
monkeypatch: MonkeyPatch, hostname: HostName, section_name: str, result: int | None
monkeypatch: MonkeyPatch, hostname: HostName, result: Mapping[SectionName, int | None]
) -> None:
ts = Scenario()
ts.add_host(hostname)
Expand All @@ -1784,9 +1781,7 @@ def test_snmp_check_interval(
},
],
)
assert ts.apply(monkeypatch).snmp_fetch_interval(hostname, SectionName(section_name)) == (
60 * result if result else None
)
assert ts.apply(monkeypatch).snmp_fetch_intervals(hostname) == result


def test_http_proxies() -> None:
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/cmk/base/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1290,9 +1290,6 @@ def test_commandline_discovery(
file_cache_options = FileCacheOptions()
parser = CMKParser(
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
agent_based_plugins, hostname, selected_sections=NO_SELECTION
),
selected_sections=NO_SELECTION,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("tests"),
Expand Down

0 comments on commit a81793d

Please sign in to comment.