From 29872243baa745f49ef2d5e0658568bce28dfeb7 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sat, 7 Nov 2026 14:46:00 +0100 Subject: [PATCH] feat(host-status): get_host_status through HostStatusMixin The router's host status, read by napalm-device-types' command over an SSH exec channel: whether it needs a reboot, how full its filesystems are, and, with napalm-device-types 4.32, whether apk or opkg was left halfway. Refs NetOrk/netork#851 --- CHANGELOG.md | 4 ++ README.md | 1 + napalm_openwrt/openwrt.py | 10 +++++ tests/unit/test_host_status.py | 70 ++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 tests/unit/test_host_status.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 9518dd2..2872be4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ recorded here; their history is the git log. ## [Unreleased] ### Added +- `get_host_status()` (napalm-device-types `HostStatusMixin`), over an SSH exec channel: + whether the router needs a reboot, how full its filesystems are, and -- with + napalm-device-types 4.32 or newer -- whether apk (OpenWrt 25.12) or opkg (before) was + left halfway, and which of the two it read (NetOrk/netork#851). - Scheduled jobs (NetOrk/netork#1933, #32), through napalm-device-types' `PosixScheduledJobsMixin`: `get_scheduled_jobs()` reads every crontab in `/etc/crontabs` -- BusyBox crond has no `/etc/crontab`, `cron.d`, run-parts, anacron diff --git a/README.md b/README.md index 515f5c7..a91b3aa 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ device.close() | `get_lldp_neighbors` | ✅ | Requires `lldpd` installed and running (raises otherwise); reads only, never starts or configures it | | `get_lldp_neighbors_detail` | ✅ | Requires `lldpd` installed and running (raises otherwise); reads only, never starts or configures it | | `get_listening_sockets` | ✅ | busybox `netstat -lntup`, procd service from the process's cgroup (napalm-device-types `ListeningSocketsMixin`); over an SSH exec channel | +| `get_host_status` | ✅ | napalm-device-types `HostStatusMixin`: whether the router needs a reboot, how full `/` (the overlay) is, and whether apk or opkg was left halfway (apk: a package with broken files or a broken script; opkg: one left `unpacked`; needs napalm-device-types 4.32); over an SSH exec channel | | `get_scheduled_jobs()` | ✅ | BusyBox crond's `/etc/crontabs/`, time zone from `system.@system[0].zonename`, owner from `opkg search` (napalm-device-types `PosixScheduledJobsMixin`) | | `manage_scheduled_job(key, action, digest=)` | ✅ | enable, disable, run now (bounded by `timeout` where the router has it; another user's job through `start-stop-daemon -c`) | | `put_scheduled_job(spec)` / `delete_scheduled_job(key, digest=)` | ✅ | an application's own job: two marked lines in the user's crontab; `@daily` and the other aliases as five fields, `@reboot` refused (BusyBox crond has no special times) | diff --git a/napalm_openwrt/openwrt.py b/napalm_openwrt/openwrt.py index b780912..bfa99fd 100644 --- a/napalm_openwrt/openwrt.py +++ b/napalm_openwrt/openwrt.py @@ -36,6 +36,7 @@ from napalm_device_types import ( SnmpAgentMixin, ) from napalm_device_types.channel import open_stream_on_transport, run_on_transport +from napalm_device_types.host_status import HostStatusMixin from napalm_device_types.listening import ListeningSocketsMixin from napalm_device_types.ssh_host_keys import HostKeyCheck, SshHostKeyMismatch from netmiko import ConnectHandler @@ -80,6 +81,7 @@ class OpenWrtDriver( OpenWrtRoutingMixin, OpenWrtScheduledJobsMixin, ListeningSocketsMixin, + HostStatusMixin, SnmpAgentMixin, OpenWrtLldpAgentMixin, EnsurePackageMixin, @@ -248,6 +250,14 @@ class OpenWrtDriver( reason = result.stderr.strip() or result.stdout.strip() raise RuntimeError(reason or f"reboot exited with status {result.exit_code}") + def _run_host_status_command(self, command: str) -> str: + """Run the host status command for ``HostStatusMixin``; it needs no root. + + Over an SSH exec channel of its own: busybox ash cuts a typed line at 512 + characters, and the command is longer. + """ + return self.run_command(command).stdout + def _run_listening_sockets_command(self, command: str, *, privileged: bool) -> str: """Run the listening-socket command for ``ListeningSocketsMixin``. diff --git a/tests/unit/test_host_status.py b/tests/unit/test_host_status.py new file mode 100644 index 0000000..7cc70b2 --- /dev/null +++ b/tests/unit/test_host_status.py @@ -0,0 +1,70 @@ +"""The host status of an OpenWrt device (napalm-device-types ``HostStatusMixin``). + +The command and its parse are napalm-device-types': whether the router needs a +reboot, how full its filesystems are, and whether apk (OpenWrt 25.12) or opkg +(before) was left halfway (NetOrk/netork#851). The driver only carries the command +across, over an SSH exec channel, because busybox ash cuts an interactive line at +512 characters and the command is longer. + +The report is shaped after what the command prints on OpenWrt 25.12 (no systemd, +no apt, one kernel under ``/lib/modules``, ``/`` an overlay); it still has to be +checked against a real router. +""" + +from __future__ import annotations + +from unittest.mock import MagicMock, patch + +from napalm_device_types import CommandResult +from napalm_device_types.host_status import HOST_STATUS_COMMAND, parse_host_status + +from napalm_openwrt.openwrt import OpenWrtDriver + +REPORT = """HSTAT_BEGIN +[kernel] +6.12.57 +[modules] +6.12.57 +[pm-running] +[apk-audit] +s luci-app-sqm +[filesystems] +Filesystem 1024-blocks Used Available Capacity Mounted on +overlayfs:/overlay 93184 5132 83392 6% / +tmpfs 245696 304 245392 0% /tmp +[timers] +apt-daily-upgrade.timer +dnf-automatic.timer +dnf-automatic-install.timer +[dnf-automatic] +HSTAT_END +""" + + +def _driver() -> OpenWrtDriver: + with patch("napalm_openwrt.openwrt.ConnectHandler"): + drv = OpenWrtDriver(hostname="192.0.2.15", username="root", password="") + drv.device = MagicMock() + return drv + + +def test_the_host_status_is_napalm_device_types_reading(): + drv = _driver() + with patch( + "napalm_openwrt.openwrt.run_on_transport", return_value=CommandResult(REPORT, "", 0) + ): + status = drv.get_host_status() + + assert status == parse_host_status(REPORT) + assert status["reboot_required"] is False + + +def test_the_command_goes_over_an_exec_channel_not_the_shell(): + drv = _driver() + transport = drv.device.remote_conn_pre.get_transport.return_value + done = CommandResult(REPORT, "", 0) + with patch("napalm_openwrt.openwrt.run_on_transport", return_value=done) as run: + drv.get_host_status() + + assert run.call_args.args[:2] == (transport, HOST_STATUS_COMMAND) + drv.device.send_command.assert_not_called() -- 2.54.1