Files
napalm-openwrt/napalm_openwrt/zabbix_agent.py
T
Christian Manivong 053137aff7
driver / lint (pull_request) Successful in 15s
driver / standard (pull_request) Successful in 18s
driver / types (pull_request) Successful in 1m7s
driver / test (3.13) (pull_request) Successful in 1m1s
driver / test (3.12) (pull_request) Successful in 1m10s
driver / lowest (pull_request) Successful in 39s
driver / build (pull_request) Successful in 38s
driver / security (pull_request) Successful in 1m11s
CI / driver (pull_request) Successful in 2m30s
driver / publish (pull_request) Skipped
feat(zabbix): the feed's Zabbix agent through ZabbixAgentMixin (NetOrk/netork#2571)
An access point runs no container engine. configure_zabbix_agent(settings)
installs zabbix-agentd from the feed after loading the package index, writes the
caller's values into /etc/zabbix_agentd.conf (the old file stays as .bak),
enables the agent in UCI, without which its init script does not start it, and
opens tcp/ListenPort for the allowed sources in the zone that owns the
management address where a firewall runs. Zabbix publishes no repository for
OpenWrt; the feed is the only source.

The SNMP agent's firewall step writes its rule through _allow_in_management_zone
now, split into the zone, the rule and the reload; the rule is the same.
Needs napalm-device-types 4.40.0.
2026-11-17 04:50:55 +01:00

120 lines
4.8 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The feed's Zabbix agent, the steps of napalm-device-types' ``ZabbixAgentMixin``.
OpenWrt's feed has the C agent, ``zabbix-agentd`` (NetOrk/netork#2571). Its
configuration is ``/etc/zabbix_agentd.conf``; its init script starts it only while
UCI says ``zabbix_agentd.general.enabled=1``, and runs it as ``zabbix-agent``.
Zabbix publishes no repository for OpenWrt, so the feed is the only source.
The package index lives in RAM and is gone after every restart; the template loads
it first (``refresh_available_updates``). The firewall step opens tcp/``listen_port``
in the zone that owns the management address, the way the SNMP agent's step opens
udp/161 -- and only where a firewall runs: an access point often runs none.
"""
from __future__ import annotations
import ipaddress
from typing import TYPE_CHECKING, Any
from napalm_device_types import (
FixStepOutcomeDict,
ZabbixAgentMixin,
ZabbixAgentSettingsDict,
zabbix_agent_config,
)
if TYPE_CHECKING: # pragma: no cover
from napalm_device_types import CommandResult
CONFIG = "/etc/zabbix_agentd.conf"
#: UCI's switch the init script reads; the section is made where it is missing.
_ENABLE = (
"uci -q get zabbix_agentd.general >/dev/null || uci set zabbix_agentd.general=zabbix_agentd; "
"uci set zabbix_agentd.general.enabled='1' && uci commit zabbix_agentd"
)
#: Prints ``on`` while a packet filter runs: fw4's table, or fw3's enabled service.
_FIREWALL_RUNS = (
"{ nft list table inet fw4 >/dev/null 2>&1 || /etc/init.d/firewall enabled; } && echo on"
)
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 OpenWrtZabbixAgentMixin(ZabbixAgentMixin):
"""``zabbix-agentd`` from the feed, enabled in UCI."""
ZABBIX_AGENT_PACKAGE = "zabbix-agentd"
ZABBIX_AGENT_SERVICE = "zabbix_agentd"
#: The feed is the only source.
_add_zabbix_repository = None # type: ignore[assignment]
def _write_zabbix_agent_config(self, settings: ZabbixAgentSettingsDict) -> FixStepOutcomeDict:
read = self.run_command(f"cat {CONFIG}", privileged=True)
if read.exit_code:
return _failed(read, f"reading {CONFIG}")
text, changes = zabbix_agent_config(read.stdout, settings)
if changes:
# The old file next to it; cat keeps owner and mode (zabbix-agent's).
written = self.run_command(
f"cp -p {CONFIG} {CONFIG}.bak && cat > {CONFIG}",
privileged=True,
stdin=text.encode(),
)
if written.exit_code:
return _failed(written, f"writing {CONFIG}")
enabled = self.run_command(_ENABLE, privileged=True)
if enabled.exit_code:
return _failed(enabled, "enabling the agent in UCI")
said = "; ".join(changes) if changes else f"{CONFIG} already says it"
return {"ok": True, "output": f"{said}; enabled in UCI"}
def _allow_zabbix_agent_sources(self, settings: ZabbixAgentSettingsDict) -> FixStepOutcomeDict:
sources = [
str(ipaddress.ip_network(s.strip(), strict=False)) for s in settings["allowed_sources"]
]
if not sources:
return {"ok": True, "output": "no sources given; firewall unchanged"}
if self._send_command(_FIREWALL_RUNS).strip() != "on":
return {"ok": True, "output": "no firewall runs; nothing to open"}
outcome = self._allow_in_management_zone(
key="allow_zabbix_agent",
name="Allow-Zabbix-Agent",
proto="tcp",
port=int(settings["listen_port"]),
sources=sources,
word="zabbix",
)
return {"ok": bool(outcome["ok"]), "output": str(outcome["output"])}
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 _send_command(self, command: str) -> str: ...
def _allow_in_management_zone(
self, *, key: str, name: str, proto: str, port: int, sources: list[str], word: str
) -> dict[str, Any]: ...