diff --git a/napalm_openwrt/zabbix_agent.py b/napalm_openwrt/zabbix_agent.py index 59716a3..ef7e010 100644 --- a/napalm_openwrt/zabbix_agent.py +++ b/napalm_openwrt/zabbix_agent.py @@ -121,28 +121,15 @@ class OpenWrtZabbixAgentMixin(ZabbixAgentMixin): and are named. The firewall reloads where one runs. """ port = int(settings["listen_port"]) - sections = self._parse_uci_sections(self._send_command("uci show firewall 2>/dev/null")) - rules = {key: opts for key, opts in sections.items() if opts.get("_type") == "rule"} - mine = sorted(key for key in rules if _netork_rule(key)) - others = sorted( - key - for key, opts in rules.items() - if key not in mine and self._uci_scalar(opts.get("dest_port", "")) == str(port) - ) + mine, others = self._agent_rules(port) lines: list[str] = [] ok = True if mine: - deletes = "; ".join(f"uci -q delete firewall.{key}" for key in mine) - done = self._rc_step(f"{deletes}; uci commit firewall", "") - if not done["ok"]: - return {"ok": False, "output": str(done["output"])} - lines.append(f"removed the rule(s) netOrk wrote for the agent: {', '.join(mine)}") - if self._send_command(_FIREWALL_RUNS).strip() == "on": - reload_out = self._send_command( - "fw4 reload 2>&1 || /etc/init.d/firewall reload 2>&1" - ).strip() - ok = not self._fw_reload_failed(reload_out) - lines.append(f"reload: {reload_out or 'ok'}") + removed = self._delete_rules(mine) + if not removed["ok"]: + return removed + lines.append(str(removed["output"])) + ok = self._reload_if_running(lines) else: lines.append("netOrk wrote no rule for the agent; nothing removed") if others: @@ -152,6 +139,38 @@ class OpenWrtZabbixAgentMixin(ZabbixAgentMixin): ) return {"ok": ok, "output": "; ".join(lines)} + def _agent_rules(self, port: int) -> tuple[list[str], list[str]]: + """The rule sections netOrk wrote for the agent, and the others for *port*.""" + sections = self._parse_uci_sections(self._send_command("uci show firewall 2>/dev/null")) + rules = {key: opts for key, opts in sections.items() if opts.get("_type") == "rule"} + mine = sorted(key for key in rules if _netork_rule(key)) + others = sorted( + key + for key, opts in rules.items() + if key not in mine and self._uci_scalar(opts.get("dest_port", "")) == str(port) + ) + return mine, others + + def _delete_rules(self, keys: list[str]) -> FixStepOutcomeDict: + deletes = "; ".join(f"uci -q delete firewall.{key}" for key in keys) + done = self._rc_step(f"{deletes}; uci commit firewall", "") + if not done["ok"]: + return {"ok": False, "output": str(done["output"])} + return { + "ok": True, + "output": f"removed the rule(s) netOrk wrote for the agent: {', '.join(keys)}", + } + + def _reload_if_running(self, lines: list[str]) -> bool: + """Reload the firewall where one runs; False where it refused the ruleset.""" + if self._send_command(_FIREWALL_RUNS).strip() != "on": + return True + reload_out = self._send_command( + "fw4 reload 2>&1 || /etc/init.d/firewall reload 2>&1" + ).strip() + lines.append(f"reload: {reload_out or 'ok'}") + return not self._fw_reload_failed(reload_out) + if TYPE_CHECKING: # pragma: no cover - the driver's def run_command(