Files
napalm-proxmox/napalm_proxmox/zabbix_agent.py
T
Christian Manivong 370aeed482 feat(zabbix): an active-only agent closes the port netOrk opened for it (NetOrk/netork#2693)
Every rule the firewall step adds in ufw is recorded in napalm-device-types'
opened_ports record, only when ufw did not have it before. For an active-only
agent, _revoke_zabbix_agent_sources removes exactly the agent's recorded rules
and reports what ufw has left for the port.
2026-11-26 08:21:53 +01:00

79 lines
3.3 KiB
Python

"""A native Zabbix agent on a Proxmox node: napalm-device-types' ``ZabbixAgentMixin``.
A node runs no container engine (NetOrk/netork#2571). The template installs Debian's
``zabbix-agent`` -- or, where Debian has none, Zabbix's from its repository -- and
enables and restarts it. This writes the caller's values into
``/etc/zabbix/zabbix_agentd.conf`` and opens tcp/``listen_port`` in ufw where the node
has it; pve-firewall's rules are the operator's, as for the SNMP agent. The node's
shell runs as root.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from napalm_device_types import (
ZABBIX_AGENT_PURPOSE,
FixStepOutcomeDict,
ZabbixAgentMixin,
ZabbixAgentSettingsDict,
zabbix_agent_config,
zabbix_repository_command,
)
CONFIG = "/etc/zabbix/zabbix_agentd.conf"
class ProxmoxZabbixAgentMixin(ZabbixAgentMixin):
"""``zabbix-agent`` from Debian, or else from Zabbix's repository."""
def _add_zabbix_repository(self, release: str) -> FixStepOutcomeDict:
os_release, status = self._rc("cat /etc/os-release")
if status != 0:
return {"ok": False, "output": f"reading /etc/os-release: {os_release[-300:]}"}
try:
command = zabbix_repository_command(os_release, release)
except ValueError as exc:
return {"ok": False, "output": str(exc)}
return self._step(command, f"Zabbix {release} repository added")
def _write_zabbix_agent_config(self, settings: ZabbixAgentSettingsDict) -> FixStepOutcomeDict:
text, status = self._rc(f"cat {CONFIG}")
if status != 0:
return {"ok": False, "output": f"reading {CONFIG}: {text[-300:]}"}
# _rc strips the output; the file ends with a newline.
new, changes = zabbix_agent_config(f"{text}\n", settings)
if not changes:
return {"ok": True, "output": f"{CONFIG} already says it"}
kept = self._step(f"cp -p {CONFIG} {CONFIG}.bak", "")
if not kept["ok"]:
return kept
written = self._write_root_file(CONFIG, new)
if not written["ok"]:
return written
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", ZABBIX_AGENT_PURPOSE
)
def _revoke_zabbix_agent_sources(self, settings: ZabbixAgentSettingsDict) -> FixStepOutcomeDict:
"""For an active-only agent: remove the ufw rules recorded for the agent on an
earlier passive run, whatever port they opened (NetOrk/netork#2693)."""
return self._revoke_sources(ZABBIX_AGENT_PURPOSE, int(settings["listen_port"]), "tcp")
if TYPE_CHECKING: # pragma: no cover - ProxmoxHostMixin's
def _rc(self, command: str) -> tuple[str, int]: ...
def _step(self, command: str, done: str) -> FixStepOutcomeDict: ...
def _write_root_file(self, path: str, content: str) -> FixStepOutcomeDict: ...
def _allow_sources(
self, allowed: list[str], port: int, proto: str, purpose: str
) -> FixStepOutcomeDict: ...
def _revoke_sources(self, purpose: str, port: int, proto: str) -> FixStepOutcomeDict: ...