Skip to content

Commit 2fa9c07

Browse files
committed
oid range: add parsing
This reduces the wishful typing in default_config/base. Note that at this time we still need to claim `Sequence[RuleSpec[Mapping[str, object]]]` to avoid a complaining ruleset matcher. Change-Id: I87d2ca1b723a0df974d31ce439b79d77cced8aba
1 parent b143c17 commit 2fa9c07

4 files changed

Lines changed: 29 additions & 10 deletions

File tree

cmk/base/config.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@
117117
SSCRules,
118118
)
119119
from cmk.server_side_calls_backend.config_processing import PreprocessingResult
120-
from cmk.snmplib import ( # these are required in the modules' namespace to load the configuration!
120+
from cmk.snmplib import ( # some of these are required in the modules' namespace to load the configuration!
121+
parse_oid_range_config,
121122
SNMPBackendEnum,
122123
SNMPContextConfig,
123124
SNMPCredentials,
@@ -1826,14 +1827,11 @@ def _snmp_version(v2_enabled: bool, credentials: SNMPCredentials) -> SNMPVersion
18261827
),
18271828
bulk_walk_size_of=self._bulk_walk_size(host_name),
18281829
timing=self._snmp_timing(host_name),
1829-
oid_range_limits={
1830-
SectionName(name): rule
1831-
for name, rule in reversed(
1832-
self.ruleset_matcher.get_host_values_all(
1833-
host_name, snmp_limit_oid_range, self.label_manager.labels_of_host
1834-
)
1830+
oid_range_limits=parse_oid_range_config(
1831+
self.ruleset_matcher.get_host_values_all(
1832+
host_name, snmp_limit_oid_range, self.label_manager.labels_of_host
18351833
)
1836-
},
1834+
),
18371835
snmpv3_contexts=[
18381836
SNMPContextConfig(
18391837
section=SectionName(name) if name is not None else None,

cmk/base/default_config/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from cmk.checkengine.exitspec import ExitSpec
1212
from cmk.fetchers import IPMICredentials
1313
from cmk.server_side_calls_backend import ConfigSet as SSCConfigSet
14-
from cmk.snmplib import RangeLimit, SNMPCredentials, SNMPTiming
14+
from cmk.snmplib import SNMPCredentials, SNMPTiming
1515
from cmk.utils.host_storage import FolderAttributesForBase
1616
from cmk.utils.labels import Labels
1717
from cmk.utils.notify_types import Contact, ContactName
@@ -88,7 +88,7 @@
8888
non_inline_snmp_hosts: list[RuleSpec[object]] = []
8989

9090
# Ruleset to recduce fetched OIDs of a check, only inline SNMP
91-
snmp_limit_oid_range: list[RuleSpec[tuple[str, Sequence[RangeLimit]]]] = []
91+
snmp_limit_oid_range: Sequence[RuleSpec[object]] = []
9292
# Ruleset to customize bulk size
9393
snmp_bulk_size: list[RuleSpec[int]] = []
9494
snmp_default_community = "public"

cmk/snmplib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from ._typedefs import OID as OID
2020
from ._typedefs import OIDRange as OIDRange
2121
from ._typedefs import OIDSpecLike as OIDSpecLike
22+
from ._typedefs import parse_oid_range_config as parse_oid_range_config
2223
from ._typedefs import RangeLimit as RangeLimit
2324
from ._typedefs import SNMPBackend as SNMPBackend
2425
from ._typedefs import SNMPBackendEnum as SNMPBackendEnum

cmk/snmplib/_typedefs.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,26 @@
2626
SNMPRowInfo = list[tuple[OID, SNMPRawValue]]
2727

2828

29+
def parse_oid_range_config(
30+
rule_values: Sequence[object],
31+
) -> Mapping[SectionName, Sequence[RangeLimit]]:
32+
"""Parse the OID range limits from the given config values."""
33+
return {
34+
SectionName(str(v[0])): [_parse_range_limit(l) for l in v[1]]
35+
for v in reversed(rule_values)
36+
if isinstance(v, tuple)
37+
}
38+
39+
40+
def _parse_range_limit(raw: object) -> RangeLimit:
41+
match raw:
42+
case ("first" | "last" as position, int(limit)):
43+
return position, limit
44+
case ("mid", tuple((int(), int())) as oid_range):
45+
return "mid", oid_range
46+
raise ValueError(raw)
47+
48+
2949
class SNMPContextTimeout(MKSNMPError):
3050
pass
3151

0 commit comments

Comments
 (0)