-
Notifications
You must be signed in to change notification settings - Fork 474
/
Copy pathsnmp.py
120 lines (89 loc) · 3.51 KB
/
snmp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/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 logging
from collections.abc import Iterator, Mapping, Sequence
from pathlib import Path
from types import ModuleType
from typing import Any, Final, NamedTuple
from cmk.utils.sectionname import SectionMap, SectionName
from cmk.snmplib import (
BackendSNMPTree,
SNMPBackend,
SNMPBackendEnum,
SNMPDetectSpec,
SNMPHostConfig,
)
from .snmp_backend import ClassicSNMPBackend, StoredWalkSNMPBackend
inline: ModuleType | None
try:
from .cee.snmp_backend import inline # type: ignore[import,no-redef,unused-ignore]
except ImportError:
inline = None
__all__ = ["SNMPPluginStoreItem", "SNMPPluginStore", "make_backend"]
_force_stored_walks = False
def force_stored_walks() -> None:
global _force_stored_walks
_force_stored_walks = True
def get_force_stored_walks() -> bool:
return _force_stored_walks
def make_backend(
snmp_config: SNMPHostConfig,
logger: logging.Logger,
*,
use_cache: bool | None = None,
stored_walk_path: Path,
) -> SNMPBackend:
if use_cache is None:
use_cache = get_force_stored_walks()
if use_cache or snmp_config.snmp_backend is SNMPBackendEnum.STORED_WALK:
return StoredWalkSNMPBackend(
snmp_config, logger, path=stored_walk_path / snmp_config.hostname
)
if inline and snmp_config.snmp_backend is SNMPBackendEnum.INLINE:
return inline.InlineSNMPBackend(snmp_config, logger)
if snmp_config.snmp_backend is SNMPBackendEnum.CLASSIC:
return ClassicSNMPBackend(snmp_config, logger)
raise NotImplementedError(f"Unknown SNMP backend: {snmp_config.snmp_backend}")
class SNMPPluginStoreItem(NamedTuple):
trees: Sequence[BackendSNMPTree]
detect_spec: SNMPDetectSpec
inventory: bool
@classmethod
def deserialize(cls, serialized: Mapping[str, Any]) -> "SNMPPluginStoreItem":
return cls(
[BackendSNMPTree.from_json(tree) for tree in serialized["trees"]],
SNMPDetectSpec.from_json(serialized["detect_spec"]),
serialized["inventory"],
)
def serialize(self) -> Mapping[str, Any]:
return {
"trees": [tree.to_json() for tree in self.trees],
"detect_spec": self.detect_spec.to_json(),
"inventory": self.inventory,
}
class SNMPPluginStore(SectionMap[SNMPPluginStoreItem]):
def __init__(
self,
store: SectionMap[SNMPPluginStoreItem] | None = None,
) -> None:
self._store: Final[SectionMap[SNMPPluginStoreItem]] = store if store else {}
def __repr__(self) -> str:
return f"{type(self).__name__}({self._store!r})"
def __getitem__(self, key: SectionName) -> SNMPPluginStoreItem:
return self._store.__getitem__(key)
def __iter__(self) -> Iterator[SectionName]:
return self._store.__iter__()
def __len__(self) -> int:
return self._store.__len__()
@classmethod
def deserialize(cls, serialized: Mapping[str, Any]) -> "SNMPPluginStore":
return cls(
{
SectionName(k): SNMPPluginStoreItem.deserialize(v)
for k, v in serialized["plugin_store"].items()
}
)
def serialize(self) -> Mapping[str, Any]:
return {"plugin_store": {str(k): v.serialize() for k, v in self.items()}}