Files
napalm-linux/napalm_linux/_zabbix_agent.py
T
Christian Manivong 2521f9f462
driver / lint (pull_request) Successful in 14s
driver / standard (pull_request) Successful in 17s
driver / types (pull_request) Successful in 59s
driver / test (3.13) (pull_request) Successful in 57s
driver / test (3.12) (pull_request) Successful in 1m2s
driver / lowest (pull_request) Successful in 40s
driver / build (pull_request) Successful in 37s
driver / security (pull_request) Successful in 1m17s
CI / driver (pull_request) Successful in 2m32s
driver / publish (pull_request) Skipped
feat(zabbix): a native Zabbix agent through ZabbixAgentMixin (NetOrk/netork#2571)
configure_zabbix_agent(settings) installs zabbix-agent from the host's packages,
and where they have none, from Zabbix's repository for the server's release
line. It writes the caller's values into /etc/zabbix/zabbix_agentd.conf (the old
file stays as .bak) and opens tcp/ListenPort for the allowed sources in
firewalld, ufw or iptables.

The firewall step of the SNMP agent is the shared _allow_sources(allowed, port,
proto) now, in _open_port.py; the rules it writes for udp/161 are the same.
Needs napalm-device-types 4.40.0.
2026-11-17 04:58:46 +01:00

81 lines
3.1 KiB
Python

"""A native Zabbix agent, the steps of napalm-device-types' ``ZabbixAgentMixin``.
For a host without a container engine (NetOrk/netork#2571). The template installs
``zabbix-agent`` -- the C agent every distribution that packages Zabbix has, and
Zabbix's own repository too -- and enables and restarts its service. This writes the
caller's values into ``/etc/zabbix/zabbix_agentd.conf`` (where both put it), adds
Zabbix's repository where the host's own packages have none, and opens the port in
whichever firewall runs.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from napalm_device_types import (
FixStepOutcomeDict,
ZabbixAgentMixin,
ZabbixAgentSettingsDict,
zabbix_agent_config,
zabbix_repository_command,
)
if TYPE_CHECKING: # pragma: no cover
from napalm_device_types import CommandResult
CONFIG = "/etc/zabbix/zabbix_agentd.conf"
def _failed(result: CommandResult, what: str) -> FixStepOutcomeDict:
detail = (result.stderr or result.stdout or "").strip()[-300:]
return {"ok": False, "output": f"{what}: {detail or result.exit_code}"}
class LinuxZabbixAgentMixin(ZabbixAgentMixin):
"""``zabbix-agent`` from the distribution, or else from Zabbix's repository."""
def _add_zabbix_repository(self, release: str) -> FixStepOutcomeDict:
os_release = self.run_command("cat /etc/os-release", timeout=30)
try:
command = zabbix_repository_command(os_release.stdout, release)
except ValueError as exc:
return {"ok": False, "output": str(exc)}
return self._root_outcome(command, 300, f"Zabbix {release} repository added")
def _write_zabbix_agent_config(self, settings: ZabbixAgentSettingsDict) -> FixStepOutcomeDict:
read = self.run_command(f"cat {CONFIG}", privileged=True, timeout=30)
if read.exit_code:
return _failed(read, f"reading {CONFIG}")
text, changes = zabbix_agent_config(read.stdout, settings)
if not changes:
return {"ok": True, "output": f"{CONFIG} already says it"}
kept = self._root_outcome(f"cp -p {CONFIG} {CONFIG}.bak", 30, "")
if not kept["ok"]:
return kept
written = self._install_file_as_root(CONFIG, text)
if written.exit_code:
return _failed(written, f"writing {CONFIG}")
return {"ok": True, "output": "; ".join(changes)}
def _allow_zabbix_agent_sources(self, settings: ZabbixAgentSettingsDict) -> FixStepOutcomeDict:
return self._allow_sources(settings["allowed_sources"], int(settings["listen_port"]), "tcp")
if TYPE_CHECKING: # pragma: no cover - the driver's
def run_command(
self,
command: str,
*,
privileged: bool = False,
timeout: float = 60,
stdin: bytes | None = None,
) -> CommandResult: ...
def _root_outcome(self, command: str, timeout: float, done: str) -> FixStepOutcomeDict: ...
def _install_file_as_root(self, path: str, content: str) -> CommandResult: ...
def _allow_sources(
self, allowed: list[str], port: int, proto: str
) -> FixStepOutcomeDict: ...