"""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