Files
napalm-proxmox/tests/test_zabbix_agent.py
T
Christian Manivong 92108f0ff3
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 46s
driver / test (3.12) (pull_request) Successful in 57s
driver / lowest (pull_request) Successful in 26s
driver / test (3.13) (pull_request) Successful in 54s
driver / build (pull_request) Successful in 24s
driver / security (pull_request) Successful in 48s
CI / driver (pull_request) Successful in 2m13s
driver / publish (pull_request) Skipped
feat(zabbix): a native Zabbix agent on both drivers through ZabbixAgentMixin (NetOrk/netork#2571)
A node runs no container engine. configure_zabbix_agent(settings) installs
Debian's zabbix-agent, and where Debian has none, Zabbix's from its 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 in ufw where the node has it; pve-firewall's rules are the
operator's, as for the SNMP agent.

The SNMP agent's ufw step is the shared _allow_sources(allowed, port, proto) in
ProxmoxHostMixin now; the rule it writes for udp/161 is the same.
Needs napalm-device-types 4.40.0.
2026-11-17 04:50:55 +01:00

147 lines
5.0 KiB
Python

"""configure_zabbix_agent on a Proxmox node (NetOrk/netork#2571).
A node runs no container engine, so the agent cannot run as a container there. The
template installs Debian's ``zabbix-agent`` -- or Zabbix's, where Debian has none -- and
enables and restarts it; the node writes the configuration and opens ufw where there is
one. pve-firewall's rules are the operator's.
"""
from __future__ import annotations
import base64
import re
import pytest
from napalm_proxmox import ProxmoxBackupDriver, ProxmoxDriver
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"
DEBIAN = 'PRETTY_NAME="Debian GNU/Linux 13 (trixie)"\nID=debian\nVERSION_ID="13"\n'
SETTINGS = {
"server": "zabbix.example.com",
"server_active": "zabbix.example.com",
"listen_port": 10050,
"allowed_sources": ["10.40.100.254"],
"hostname": "pve-01",
"release": "7.4",
}
class _Node:
"""The node's root shell: files it reads, packages Debian has, what is refused."""
def __init__(self, *, config=STOCK, ufw=True, debian_has_it=True, refuse=""):
self.config = config
self.ufw = ufw
self.debian_has_it = debian_has_it
self.installed = False
self.refuse = refuse
self.ran: list[str] = []
def __call__(self, command: str) -> str:
self.ran.append(command)
out, rc = self._answer(command)
for marker in ("__PVE_RC=", "__SVC_RC="):
if marker in command:
return f"{out}\n{marker}{rc}"
return out
def _answer(self, command: str) -> tuple[str, int]:
if self.refuse and self.refuse in command:
return "E: refused", 1
if command.startswith("dpkg-query"):
return ("zabbix-agent\tinstalled\n" if self.installed else "curl\tinstalled\n"), 0
if f"cat {CONFIG}" in command:
return self.config, 0
if "cat /etc/os-release" in command:
return DEBIAN, 0
if "command -v ufw" in command:
return ("/usr/sbin/ufw", 0) if self.ufw else ("", 1)
if "repo.zabbix.com" in command:
self.debian_has_it = True
return "ok", 0
if "apt-get install" in command and "zabbix-agent" in command:
if not self.debian_has_it:
return "E: Unable to locate package zabbix-agent", 100
self.installed = True
return "ok", 0
if "base64 -d" in command and CONFIG in command:
self.config = base64.b64decode(re.search(r"echo (\S+) \|", command).group(1)).decode()
return "ok", 0
@pytest.fixture
def node():
return _Node()
@pytest.fixture(params=[ProxmoxDriver, ProxmoxBackupDriver], ids=["pve", "pbs"])
def driver(request, node):
d = request.param("pve", "root@pam", "pw")
d._exec_ssh_command = node
return d
def test_installs_writes_opens_and_starts(driver, node):
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 any("apt-get install" in c and "zabbix-agent" in c for c in node.ran)
assert any("systemctl" in c and "restart" in c and "zabbix-agent" in c for c in node.ran)
def test_the_config_gets_the_caller_s_values_and_keeps_the_rest(driver, node):
driver.configure_zabbix_agent(SETTINGS)
assert "Server=zabbix.example.com\n" in node.config
assert "Hostname=pve-01\n" in node.config
assert node.config.startswith("PidFile=/run/zabbix/zabbix_agentd.pid\n")
assert any(f"cp -p {CONFIG} {CONFIG}.bak" in c for c in node.ran)
def test_a_config_that_already_says_it_is_not_written(driver, node):
driver.configure_zabbix_agent(SETTINGS)
node.ran.clear()
result = driver.configure_zabbix_agent(SETTINGS)
assert result["success"] is True
assert not any("base64 -d" in c for c in node.ran)
def test_without_debian_s_package_zabbix_s_repository_is_added(driver, node):
node.debian_has_it = False
result = driver.configure_zabbix_agent(SETTINGS)
assert result["success"] is True, result["output"]
repository = next(c for c in node.ran if "repo.zabbix.com" in c)
assert "zabbix-release_latest_7.4+debian13_all.deb" in repository
def test_ufw_opens_the_listen_port_for_the_sources(driver, node):
driver.configure_zabbix_agent(SETTINGS)
assert any("ufw allow from 10.40.100.254/32 to any port 10050 proto tcp" in c for c in node.ran)
def test_without_ufw_the_firewall_is_the_operator_s(driver, node):
node.ufw = False
result = driver.configure_zabbix_agent(SETTINGS)
assert result["success"] is True
assert "pve-firewall rules are the operator's" in result["output"]
assert not any("ufw allow" in c for c in node.ran)
def test_a_config_that_cannot_be_read_stops_the_fix(driver, node):
node.refuse = f"cat {CONFIG}"
result = driver.configure_zabbix_agent(SETTINGS)
assert result["success"] is False
assert result["steps"][-1]["step"] == "configure"