feat(zabbix): an agent that only connects out listens on no port (NetOrk/netork#2683)
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m7s
driver / test (3.12) (pull_request) Successful in 1m21s
driver / test (3.13) (pull_request) Successful in 1m13s
driver / lowest (pull_request) Successful in 37s
driver / build (pull_request) Successful in 28s
driver / security (pull_request) Successful in 55s
CI / driver (pull_request) Successful in 55s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m7s
driver / test (3.12) (pull_request) Successful in 1m21s
driver / test (3.13) (pull_request) Successful in 1m13s
driver / lowest (pull_request) Successful in 37s
driver / build (pull_request) Successful in 28s
driver / security (pull_request) Successful in 55s
CI / driver (pull_request) Successful in 55s
driver / publish (pull_request) Skipped
ZabbixAgentSettingsDict.active_only: zabbix_agent_config writes StartAgents=0 and comments Server out (mandatory only while StartAgents is not 0); ServerActive is required instead. configure_zabbix_agent's firewall step opens nothing for it. An agent that is not active only gets a StartAgents=0 line commented out.
This commit is contained in:
1 parent
e729b935a4
commit
b1ea364674
5 files changed
+143
-14
No files matched your search
@@ -215,7 +215,8 @@ class ZabbixAgentMixin:
|
||||
where those have none, from Zabbix's repository for ``settings["release"]``
|
||||
(:func:`~napalm_device_types.zabbix_agent.zabbix_repository_command`) --, write
|
||||
its configuration (:func:`~napalm_device_types.zabbix_agent.zabbix_agent_config`),
|
||||
let the allowed sources through the host's firewall, enable and restart it.
|
||||
let the allowed sources through the host's firewall -- not for an agent that is
|
||||
``active_only``, which listens on no port --, enable and restart it.
|
||||
Installing and starting are generic, over ``ensure_package`` and
|
||||
``manage_service`` with ``ZABBIX_AGENT_PACKAGE`` and ``ZABBIX_AGENT_SERVICE``.
|
||||
Whether Zabbix reaches it is the caller's to ask Zabbix.
|
||||
@@ -242,7 +243,7 @@ class ZabbixAgentMixin:
|
||||
[
|
||||
("install", lambda: self._install_zabbix_agent(settings)),
|
||||
("configure", lambda: self._write_zabbix_agent_config(settings)),
|
||||
("firewall", lambda: self._allow_zabbix_agent_sources(settings)),
|
||||
("firewall", lambda: self._open_zabbix_agent_port(settings)),
|
||||
("start", self._start_zabbix_agent),
|
||||
]
|
||||
)
|
||||
@@ -276,6 +277,13 @@ class ZabbixAgentMixin:
|
||||
return ""
|
||||
return f"package index not loaded: {str(loaded.get('output', '')).strip()}"
|
||||
|
||||
def _open_zabbix_agent_port(self, settings: ZabbixAgentSettingsDict) -> FixStepOutcomeDict:
|
||||
"""The driver's firewall step; nothing for an agent that only connects out,
|
||||
which listens on no port (NetOrk/netork#2683)."""
|
||||
if settings.get("active_only"):
|
||||
return {"ok": True, "output": "active only: the agent listens on no port"}
|
||||
return self._allow_zabbix_agent_sources(settings)
|
||||
|
||||
def _start_zabbix_agent(self) -> FixStepOutcomeDict:
|
||||
return enable_and_restart(self.manage_service, self.ZABBIX_AGENT_SERVICE)
|
||||
|
||||
@@ -306,7 +314,8 @@ class ZabbixAgentMixin:
|
||||
self, settings: ZabbixAgentSettingsDict
|
||||
) -> FixStepOutcomeDict:
|
||||
"""Let ``settings["allowed_sources"]`` reach tcp/``listen_port``; ``ok`` with
|
||||
nothing to do where the host has no firewall of its own, or for []."""
|
||||
nothing to do where the host has no firewall of its own, or for []. Not
|
||||
called for an ``active_only`` agent."""
|
||||
...
|
||||
|
||||
|
||||
|
||||
@@ -12,6 +12,13 @@ reads and writes the file and runs the command.
|
||||
``Hostname`` -- the host's technical name in Zabbix, which an active agent sends.
|
||||
Every other line stays as it was; a key the file does not set is added at its end.
|
||||
|
||||
**Active only** (NetOrk/netork#2683). An agent whose site only lets it connect out
|
||||
gets ``StartAgents=0``: Zabbix's agent then runs no passive checks and listens on no
|
||||
port. ``Server`` is mandatory only while ``StartAgents`` is not 0, so it is commented
|
||||
out -- nobody may ask -- and ``ServerActive`` is required instead. An agent that is not
|
||||
active only gets a ``StartAgents=0`` line commented out, since it would keep the agent
|
||||
from listening; another count of listeners stays.
|
||||
|
||||
**The repository.** Zabbix publishes a ``zabbix-release`` package per release line
|
||||
and distribution that adds its repository and key. Since 7.2 it lives under
|
||||
``<release>/release/<distribution>/``; up to 7.0 under ``<release>/<distribution>/``,
|
||||
@@ -39,6 +46,9 @@ class ZabbixAgentSettingsDict(TypedDict):
|
||||
# the server's release line ("7.4"): Zabbix's repository to fall back to where the
|
||||
# host's own packages have no agent; absent: no fallback
|
||||
release: NotRequired[str]
|
||||
# the agent only connects out: StartAgents=0, Server commented out (``server`` may be
|
||||
# ""), ServerActive required, and the firewall step opens nothing; absent: False
|
||||
active_only: NotRequired[bool]
|
||||
|
||||
|
||||
#: Addresses, comma-separated, as ``Server`` and ``ServerActive`` take them: names,
|
||||
@@ -49,7 +59,7 @@ _HOSTNAME = re.compile(r"^[A-Za-z0-9._ -]+$")
|
||||
#: A release line, as Zabbix's repository names it.
|
||||
_RELEASE = re.compile(r"^(\d+)\.(\d+)$")
|
||||
|
||||
_KEYS = ("Server", "ServerActive", "ListenPort", "Hostname")
|
||||
_KEYS = ("Server", "ServerActive", "ListenPort", "Hostname", "StartAgents")
|
||||
_LINE = re.compile(r"^(\s*)(" + "|".join(_KEYS) + r")\s*=\s*(.*?)\s*$")
|
||||
|
||||
REPOSITORY = "https://repo.zabbix.com/zabbix"
|
||||
@@ -72,8 +82,11 @@ def check_zabbix_agent_settings(settings: ZabbixAgentSettingsDict) -> None:
|
||||
|
||||
:raises ValueError: naming the setting.
|
||||
"""
|
||||
_check_addresses("Server", str(settings.get("server", "")), required=True)
|
||||
_check_addresses("ServerActive", str(settings.get("server_active", "")), required=False)
|
||||
active_only = settings.get("active_only", False)
|
||||
if not isinstance(active_only, bool):
|
||||
raise ValueError(f"active_only is True or False, not {active_only!r}")
|
||||
_check_addresses("Server", str(settings.get("server", "")), required=not active_only)
|
||||
_check_addresses("ServerActive", str(settings.get("server_active", "")), required=active_only)
|
||||
hostname = settings.get("hostname")
|
||||
if hostname is not None and not _HOSTNAME.match(hostname):
|
||||
raise ValueError(f"not a Zabbix host name: {hostname!r}")
|
||||
@@ -91,34 +104,49 @@ def _check_addresses(key: str, value: str, *, required: bool) -> None:
|
||||
|
||||
|
||||
def _wanted(settings: ZabbixAgentSettingsDict) -> dict[str, str]:
|
||||
"""The value of each key the caller sets; "" comments the key out."""
|
||||
active_only = bool(settings.get("active_only"))
|
||||
wanted = {
|
||||
"Server": settings["server"],
|
||||
"Server": "" if active_only else settings["server"],
|
||||
"ServerActive": settings.get("server_active", ""),
|
||||
"ListenPort": str(settings["listen_port"]),
|
||||
}
|
||||
if settings.get("hostname"):
|
||||
wanted["Hostname"] = str(settings["hostname"])
|
||||
if active_only:
|
||||
wanted["StartAgents"] = "0"
|
||||
return wanted
|
||||
|
||||
|
||||
def _unwanted(settings: ZabbixAgentSettingsDict) -> dict[str, str]:
|
||||
"""Lines commented out where they hold this value, though the caller sets no value
|
||||
for their key: ``StartAgents=0`` would keep an agent that is not active only from
|
||||
listening."""
|
||||
return {} if settings.get("active_only") else {"StartAgents": "0"}
|
||||
|
||||
|
||||
def zabbix_agent_config(text: str, settings: ZabbixAgentSettingsDict) -> tuple[str, list[str]]:
|
||||
"""*text*, an agent's configuration file, with *settings*; and what changed, one
|
||||
``"Key: old → new"`` each.
|
||||
|
||||
An empty ``server_active`` comments ``ServerActive`` out: the agent then runs
|
||||
passive checks only.
|
||||
passive checks only. ``active_only`` is the other way round: ``StartAgents=0``
|
||||
and ``Server`` commented out.
|
||||
"""
|
||||
wanted = _wanted(settings)
|
||||
wanted, unwanted = _wanted(settings), _unwanted(settings)
|
||||
lines: list[str] = []
|
||||
changes: list[str] = []
|
||||
seen: set[str] = set()
|
||||
for line in text.splitlines(keepends=True):
|
||||
match = _LINE.match(line.rstrip("\r\n"))
|
||||
if not match or match.group(2) not in wanted:
|
||||
key = match.group(2) if match else ""
|
||||
if match and key in wanted:
|
||||
seen.add(key)
|
||||
lines.append(_set_line(line, match, wanted, changes))
|
||||
elif match and unwanted.get(key) == match.group(3):
|
||||
lines.append(_set_line(line, match, {key: ""}, changes))
|
||||
else:
|
||||
lines.append(line)
|
||||
continue
|
||||
seen.add(match.group(2))
|
||||
lines.append(_set_line(line, match, wanted, changes))
|
||||
_add_missing(
|
||||
lines, [key for key in _KEYS if wanted.get(key) and key not in seen], wanted, changes
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user