chore(release): 0.9.0 #50
@@ -121,28 +121,15 @@ class OpenWrtZabbixAgentMixin(ZabbixAgentMixin):
|
|||||||
and are named. The firewall reloads where one runs.
|
and are named. The firewall reloads where one runs.
|
||||||
"""
|
"""
|
||||||
port = int(settings["listen_port"])
|
port = int(settings["listen_port"])
|
||||||
sections = self._parse_uci_sections(self._send_command("uci show firewall 2>/dev/null"))
|
mine, others = self._agent_rules(port)
|
||||||
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)
|
|
||||||
)
|
|
||||||
lines: list[str] = []
|
lines: list[str] = []
|
||||||
ok = True
|
ok = True
|
||||||
if mine:
|
if mine:
|
||||||
deletes = "; ".join(f"uci -q delete firewall.{key}" for key in mine)
|
removed = self._delete_rules(mine)
|
||||||
done = self._rc_step(f"{deletes}; uci commit firewall", "")
|
if not removed["ok"]:
|
||||||
if not done["ok"]:
|
return removed
|
||||||
return {"ok": False, "output": str(done["output"])}
|
lines.append(str(removed["output"]))
|
||||||
lines.append(f"removed the rule(s) netOrk wrote for the agent: {', '.join(mine)}")
|
ok = self._reload_if_running(lines)
|
||||||
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'}")
|
|
||||||
else:
|
else:
|
||||||
lines.append("netOrk wrote no rule for the agent; nothing removed")
|
lines.append("netOrk wrote no rule for the agent; nothing removed")
|
||||||
if others:
|
if others:
|
||||||
@@ -152,6 +139,38 @@ class OpenWrtZabbixAgentMixin(ZabbixAgentMixin):
|
|||||||
)
|
)
|
||||||
return {"ok": ok, "output": "; ".join(lines)}
|
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
|
if TYPE_CHECKING: # pragma: no cover - the driver's
|
||||||
|
|
||||||
def run_command(
|
def run_command(
|
||||||
|
|||||||
Reference in new issue
Block a user