Merge pull request 'feat(zabbix): a native Zabbix agent through ZabbixAgentMixin (NetOrk/netork#2571)' (#84) from feat/zabbix-agent-native into main
driver / standard (push) Successful in 17s
driver / lint (push) Successful in 13s
driver / test (3.13) (push) Successful in 1m6s
driver / types (push) Successful in 1m18s
driver / test (3.12) (push) Successful in 1m19s
driver / lowest (push) Successful in 33s
driver / build (push) Successful in 31s
driver / security (push) Successful in 1m7s
CI / driver (push) Successful in 2m49s
driver / publish (push) Skipped

This commit was merged in pull request #84.
This commit is contained in:
christianmanivong committed 2026-11-17 04:17:02 +00:00
commit 04ff633290
8 files changed
+360 -25

No files matched your search

+10
View File
@@ -8,6 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- A native Zabbix agent (napalm-device-types `ZabbixAgentMixin`, NetOrk/netork#2571), for a
host without a container engine: `configure_zabbix_agent(settings)` installs
`zabbix-agent` from the host's packages, and where they have none and the caller names
the server's release line, from Zabbix's repository (`zabbix-release` for Debian, Ubuntu,
Raspberry Pi OS and the RHEL family). It writes the caller's values into
`/etc/zabbix/zabbix_agentd.conf` (the old file stays as `.bak`), opens tcp/`listen_port`
for the allowed sources in firewalld, ufw or iptables, enables and restarts the service.
- On an rpm host each package names the source rpm it was built from, as `source_rpm`
(`perf` → `kernel`); absent where rpm names none (`gpg-pubkey`). Red Hat lists `perf`,
`python3-perf` and `bpftool` in every kernel CVE, and the source is what tells them apart
@@ -21,6 +28,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`NotImplementedError`.
### Changed
- Needs napalm-device-types 4.40.0 or later (`ZabbixAgentMixin`).
- The SNMP agent's firewall step is the shared `_allow_sources(allowed, port, proto)`; the
rules it writes for udp/161 are the same.
- `load_replace_candidate` is `None` instead of a method that raises
`NotImplementedError`, so a caller that asks whether the driver can replace a
configuration gets no. netOrk offered a config restore for these devices that
+47
View File
@@ -0,0 +1,47 @@
"""Opening a port in whichever firewall a Linux host runs: firewalld, ufw or iptables.
The firewall step of the agents a fix sets up (SNMP's udp/161, the Zabbix agent's
tcp/``ListenPort``).
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from napalm_device_types import FixStepOutcomeDict
from napalm_linux._snmp_agent import _source, firewalld_allow_port
class OpenPortMixin:
"""``_allow_sources``: the shared firewall step."""
def _allow_sources(self, allowed: list[str], port: int, proto: str) -> FixStepOutcomeDict:
"""Open *proto*/*port* for *allowed* in the host's firewall: firewalld, ufw or
iptables, whichever runs; ``ok`` with nothing to do for none or no sources."""
sources = [_source(s) for s in allowed]
if not sources:
return {"ok": True, "output": "no sources given; firewall unchanged"}
# firewalld first: its hosts have iptables too, and an iptables ACCEPT does
# not lift firewalld's reject (both are nftables tables; a drop in either wins).
if self._send("systemctl is-active firewalld 2>/dev/null") == "active":
done = f"{proto}/{port} open in firewalld for {', '.join(sources)}"
return self._root_outcome(firewalld_allow_port(sources, port, proto), 60, done)
if self._send("command -v ufw 2>/dev/null"):
rules = [f"ufw allow from {s} to any port {port} proto {proto}" for s in sources]
elif self._send("command -v iptables 2>/dev/null"):
rules = [
f"{{ iptables -C INPUT -s {s} -p {proto} --dport {port} -j ACCEPT 2>/dev/null"
f" || iptables -I INPUT -s {s} -p {proto} --dport {port} -j ACCEPT; }}"
for s in sources
]
else:
return {"ok": True, "output": "no host firewall (ufw, iptables); nothing to open"}
done = f"{proto}/{port} open for {', '.join(sources)}"
return self._root_outcome(" && ".join(rules), 30, done)
if TYPE_CHECKING: # pragma: no cover - the driver's
def _send(self, command: str, read_timeout: float = 100) -> str: ...
def _root_outcome(self, command: str, timeout: float, done: str) -> FixStepOutcomeDict: ...
+7 -2
View File
@@ -38,7 +38,12 @@ _FIREWALLD_ZONES = (
def firewalld_allow_snmp(sources: list[str]) -> str:
"""The shell that opens udp/161 for *sources* (already normalised) in firewalld.
"""The shell that opens udp/161 for *sources* (already normalised) in firewalld."""
return firewalld_allow_port(sources, 161, "udp")
def firewalld_allow_port(sources: list[str], port: int, proto: str) -> str:
"""The shell that opens *proto*/*port* for *sources* (already normalised) in firewalld.
A rich rule per source in every zone of ``_FIREWALLD_ZONES``, at runtime and
permanently, each only when the zone lacks it -- no ``--reload``, which would drop
@@ -49,7 +54,7 @@ def firewalld_allow_snmp(sources: list[str]) -> str:
family = f"ipv{ipaddress.ip_network(source).version}"
rule = _shlex_quote(
f'rule family="{family}" source address="{source}"'
' port port="161" protocol="udp" accept'
f' port port="{port}" protocol="{proto}" accept'
)
for scope in ("", "--permanent "):
zone = f'firewall-cmd {scope}--zone="$z"'
+80
View File
@@ -0,0 +1,80 @@
"""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: ...
+5 -21
View File
@@ -78,6 +78,7 @@ from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutExc
from napalm_linux._dpkg import DPKG_QUERY_COMMAND, parse_dpkg_query
from napalm_linux._hostname import HostnameMixin
from napalm_linux._local_accounts import LinuxLocalAccountsMixin
from napalm_linux._open_port import OpenPortMixin
from napalm_linux._ping import parse_ping, ping_command
from napalm_linux._posix import LinuxPosixMixin
from napalm_linux._readings import LinuxProcessReadingsMixin
@@ -87,11 +88,10 @@ from napalm_linux._shell import expect_for, prepare_shell, sudo_without_password
from napalm_linux._snmp_agent import (
_SNMP_INSTALL,
_SNMPD_CONF,
_source,
firewalld_allow_snmp,
render_snmpd_conf,
)
from napalm_linux._sudo_history import LinuxSudoHistoryMixin
from napalm_linux._zabbix_agent import LinuxZabbixAgentMixin
logger = logging.getLogger("napalm_linux")
@@ -272,6 +272,8 @@ class LinuxDriver(
LinuxSudoHistoryMixin,
LinuxLocalAccountsMixin,
SnmpAgentMixin,
LinuxZabbixAgentMixin,
OpenPortMixin,
EnsurePackageMixin,
AptProxyMixin,
KernelFactsMixin,
@@ -1796,25 +1798,7 @@ class LinuxDriver(
return {"ok": True, "output": f"wrote {_SNMPD_CONF}"}
def _allow_snmp_sources(self, settings: SnmpAgentSettingsDict) -> FixStepOutcomeDict:
sources = [_source(s) for s in settings.get("allowed_sources", [])]
if not sources:
return {"ok": True, "output": "no sources given; firewall unchanged"}
# firewalld first: its hosts have iptables too, and an iptables ACCEPT does
# not lift firewalld's reject (both are nftables tables; a drop in either wins).
if self._send("systemctl is-active firewalld 2>/dev/null") == "active":
done = f"udp/161 open in firewalld for {', '.join(sources)}"
return self._root_outcome(firewalld_allow_snmp(sources), 60, done)
if self._send("command -v ufw 2>/dev/null"):
rules = [f"ufw allow from {s} to any port 161 proto udp" for s in sources]
elif self._send("command -v iptables 2>/dev/null"):
rules = [
f"{{ iptables -C INPUT -s {s} -p udp --dport 161 -j ACCEPT 2>/dev/null"
f" || iptables -I INPUT -s {s} -p udp --dport 161 -j ACCEPT; }}"
for s in sources
]
else:
return {"ok": True, "output": "no host firewall (ufw, iptables); nothing to open"}
return self._root_outcome(" && ".join(rules), 30, f"udp/161 open for {', '.join(sources)}")
return self._allow_sources(settings.get("allowed_sources", []), 161, "udp")
def _start_snmp_agent(self) -> FixStepOutcomeDict:
return self._root_outcome(
+1 -1
View File
@@ -34,7 +34,7 @@ classifiers = [
]
dependencies = [
"napalm>=4.0",
"napalm-device-types>=4.37.0",
"napalm-device-types>=4.40.0", # ZabbixAgentMixin
"netmiko>=4.0.0",
"paramiko>=5.0.0", # CVE-2026-44405
]
+1 -1
View File
@@ -20,7 +20,7 @@
"napalm_linux.linux"
],
"python_file_lines": {
"napalm_linux/linux.py": 2009
"napalm_linux/linux.py": 1993
},
"untyped_public": []
}
+209
View File
@@ -0,0 +1,209 @@
"""configure_zabbix_agent on a Linux host without a container engine (NetOrk/netork#2571).
The template (napalm-device-types ``ZabbixAgentMixin``) installs ``zabbix-agent`` from
the host's packages, and from Zabbix's repository where those have none; this driver
adds that repository, writes ``/etc/zabbix/zabbix_agentd.conf`` and opens the port.
"""
from __future__ import annotations
from unittest.mock import MagicMock
import pytest
from napalm_device_types import CommandResult
from napalm_linux import LinuxDriver
CONFIG = "/etc/zabbix/zabbix_agentd.conf"
STOCK = "PidFile=/run/zabbix/zabbix_agentd.pid\nServer=127.0.0.1\nServerActive=127.0.0.1\nHostname=Zabbix server\n"
SETTINGS = {
"server": "zabbix.example.com",
"server_active": "zabbix.example.com",
"listen_port": 10050,
"allowed_sources": ["10.0.0.5"],
"hostname": "web-01",
"release": "7.4",
}
UBUNTU = 'ID=ubuntu\nVERSION_ID="24.04"\nID_LIKE=debian\n'
class _Host:
"""A Linux host: what runs as root, the files it reads, its packages and services."""
def __init__(self, *, config=STOCK, os_release=UBUNTU, firewall="ufw", firewalld=False):
self.config = config
self.os_release = os_release
self.firewall = firewall
self.firewalld = firewalld
self.root: list[str] = []
self.written: list[tuple[str, str]] = []
self.services: list[tuple[str, str]] = []
self.packages: list[str] = []
self.distribution_has_it = True
self.refuse = ""
def sudo_status(self, command, read_timeout=100):
self.root.append(command)
if self.refuse and self.refuse in command:
return ("E: refused", 1)
if "repo.zabbix.com" in command:
self.distribution_has_it = True
return ("", 0)
def send(self, command, read_timeout=100):
if command == "systemctl is-active firewalld 2>/dev/null":
return "active" if self.firewalld else "inactive"
if command == f"command -v {self.firewall} 2>/dev/null":
return f"/usr/sbin/{self.firewall}"
return ""
def run_command(self, command, *, privileged=False, timeout=60, stdin=None):
if self.refuse and self.refuse in command:
return CommandResult("", "Permission denied", 1)
if command == "cat /etc/os-release":
return CommandResult(self.os_release, "", 0)
if command == f"cat {CONFIG}" and privileged:
return CommandResult(self.config, "", 0)
return CommandResult("", "", 0)
def install_file(self, path, content):
self.written.append((path, content))
self.config = content
return CommandResult("", "", 0)
def ensure_package(self, name):
self.packages.append(name)
ok = self.distribution_has_it
return {"success": ok, "steps": [], "output": "installed" if ok else "E: Unable to locate"}
def manage_service(self, name, action):
self.services.append((action, name))
return {"success": True, "output": ""}
@pytest.fixture
def host():
return _Host()
@pytest.fixture
def driver(host):
d = LinuxDriver("h", "admin", "p")
d._device = MagicMock()
d._pkg_manager = "apt"
d._root = True
d._sudo_status = host.sudo_status
d._send = host.send
d.run_command = host.run_command
d._install_file_as_root = host.install_file
d.ensure_package = host.ensure_package
d.manage_service = host.manage_service
d.refresh_available_updates = lambda: {"success": True, "output": ""}
return d
def test_installs_writes_opens_and_starts(driver, host):
result = driver.configure_zabbix_agent(SETTINGS)
assert result["success"] is True, result["output"]
assert [s["step"] for s in result["steps"]] == ["install", "configure", "firewall", "start"]
assert host.packages == ["zabbix-agent"]
assert host.services == [("enable", "zabbix-agent"), ("restart", "zabbix-agent")]
def test_the_config_gets_the_caller_s_values_and_keeps_the_rest(driver, host):
driver.configure_zabbix_agent(SETTINGS)
path, content = host.written[0]
assert path == CONFIG
assert "Server=zabbix.example.com\n" in content
assert "Hostname=web-01\n" in content
assert content.startswith("PidFile=/run/zabbix/zabbix_agentd.pid\n")
assert f"cp -p {CONFIG} {CONFIG}.bak" in host.root
def test_a_config_that_already_says_it_is_not_written(driver, host):
driver.configure_zabbix_agent(SETTINGS)
host.written.clear()
result = driver.configure_zabbix_agent(SETTINGS)
assert result["success"] is True
assert host.written == []
def test_a_config_that_cannot_be_read_stops_the_fix(driver, host):
host.refuse = f"cat {CONFIG}"
result = driver.configure_zabbix_agent(SETTINGS)
assert result["success"] is False
assert result["steps"][-1]["step"] == "configure"
assert host.services == []
def test_without_the_distribution_s_package_zabbix_s_repository_is_added(driver, host):
host.distribution_has_it = False
result = driver.configure_zabbix_agent(SETTINGS)
assert result["success"] is True, result["output"]
repository = next(c for c in host.root if "repo.zabbix.com" in c)
assert (
"https://repo.zabbix.com/zabbix/7.4/release/ubuntu/pool/main/z/zabbix-release/"
"zabbix-release_latest_7.4+ubuntu24.04_all.deb"
) in repository
assert "dpkg -i" in repository
assert host.packages == ["zabbix-agent", "zabbix-agent"]
def test_a_distribution_zabbix_publishes_nothing_for_fails_the_install(driver, host):
host.distribution_has_it = False
host.os_release = 'ID=arch\nVERSION_ID="rolling"\n'
result = driver.configure_zabbix_agent(SETTINGS)
assert result["success"] is False
assert "Zabbix publishes no repository for arch" in result["output"]
assert not any("repo.zabbix.com" in c for c in host.root)
def test_ufw_opens_the_listen_port_for_the_sources(driver, host):
driver.configure_zabbix_agent(SETTINGS)
assert "ufw allow from 10.0.0.5/32 to any port 10050 proto tcp" in host.root
def test_iptables_opens_the_listen_port_for_the_sources(driver, host):
host.firewall = "iptables"
driver.configure_zabbix_agent(SETTINGS)
assert any(
"iptables -I INPUT -s 10.0.0.5/32 -p tcp --dport 10050 -j ACCEPT" in c for c in host.root
)
def test_a_running_firewalld_gets_a_rich_rule_for_tcp(driver, host):
host.firewalld = True
driver.configure_zabbix_agent(SETTINGS)
rule = next(c for c in host.root if "firewall-cmd" in c)
assert (
'rule family="ipv4" source address="10.0.0.5/32" port port="10050" protocol="tcp" accept'
in rule
)
def test_no_sources_change_no_firewall(driver, host):
result = driver.configure_zabbix_agent({**SETTINGS, "allowed_sources": []})
assert result["success"] is True
assert not any("ufw" in c or "iptables" in c or "firewall-cmd" in c for c in host.root)
def test_the_snmp_rule_is_still_udp_161(driver, host):
driver.configure_snmp_agent({"community": "c0mm", "allowed_sources": ["10.0.0.5"]})
assert "ufw allow from 10.0.0.5/32 to any port 161 proto udp" in host.root