From 9c165860282d491839b21e9eb6ddc9380e8b410f Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 5 Nov 2026 00:24:33 +0100 Subject: [PATCH] feat(log-sources): which logs a PVE or PBS node writes Both drivers mix in napalm-device-types' LogSourcesMixin (4.30.0) and carry its command over the exec path, which runs as root either way. Refs NetOrk/netork#2049 --- CHANGELOG.md | 4 +++ napalm_proxmox/backup_driver.py | 2 ++ napalm_proxmox/driver.py | 2 ++ napalm_proxmox/host_mixin.py | 5 ++++ pyproject.toml | 2 +- tests/test_log_sources.py | 43 +++++++++++++++++++++++++++++++++ 6 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/test_log_sources.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 5dcff46..35860b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- Log sources on both drivers (NetOrk/netork#2049), through napalm-device-types' + `LogSourcesMixin` (4.30.0): `get_log_sources()` reads the units journald holds entries for + and the log files running processes hold open, with the service or container writing each, + over the exec path as root. - Scheduled jobs on both drivers (NetOrk/netork#1933, #56), through napalm-device-types' `PosixScheduledJobsMixin`: `get_scheduled_jobs()` reads `/etc/crontab`, `/etc/cron.d`, the user crontabs, the run-parts directories, `/etc/anacrontab` and every systemd timer diff --git a/napalm_proxmox/backup_driver.py b/napalm_proxmox/backup_driver.py index ec496d9..6ec1134 100644 --- a/napalm_proxmox/backup_driver.py +++ b/napalm_proxmox/backup_driver.py @@ -23,6 +23,7 @@ from napalm_device_types import ( KernelFactsMixin, LicenseMixin, ListeningSocketsMixin, + LogSourcesMixin, OpenSshServerMixin, PortSpec, ServicePackagesMixin, @@ -58,6 +59,7 @@ class ProxmoxBackupDriver( KernelFactsMixin, ServicePackagesMixin, ListeningSocketsMixin, + LogSourcesMixin, SystemIdentityMixin, SystemdServicesMixin, ProxmoxScheduledJobsMixin, diff --git a/napalm_proxmox/driver.py b/napalm_proxmox/driver.py index 137b5c0..8eccd6f 100644 --- a/napalm_proxmox/driver.py +++ b/napalm_proxmox/driver.py @@ -34,6 +34,7 @@ from napalm_device_types import ( KernelFactsMixin, LicenseMixin, ListeningSocketsMixin, + LogSourcesMixin, OpenSshServerMixin, PortSpec, ServicePackagesMixin, @@ -93,6 +94,7 @@ class ProxmoxDriver( KernelFactsMixin, ServicePackagesMixin, ListeningSocketsMixin, + LogSourcesMixin, SystemIdentityMixin, SystemdServicesMixin, ProxmoxScheduledJobsMixin, diff --git a/napalm_proxmox/host_mixin.py b/napalm_proxmox/host_mixin.py index 6ab2eb4..363fffc 100644 --- a/napalm_proxmox/host_mixin.py +++ b/napalm_proxmox/host_mixin.py @@ -75,6 +75,11 @@ class ProxmoxHostMixin: the exec path, which runs as root either way.""" return str(self._exec_ssh_command(command)) + def _run_log_sources_command(self, command: str, *, privileged: bool) -> str: + """The transport for ``LogSourcesMixin.get_log_sources``: the exec path, + which runs as root either way.""" + return str(self._exec_ssh_command(command)) + def get_packages(self) -> list[PackageDict]: """Return installed Debian packages with available-update info. diff --git a/pyproject.toml b/pyproject.toml index c942197..81c1398 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ classifiers = [ requires-python = ">=3.12" dependencies = [ "napalm>=5.0.0", - "napalm_device_types>=4.27.0", # PosixScheduledJobsMixin + "napalm_device_types>=4.30.0", # LogSourcesMixin "paramiko>=5.0.0", # CVE-2026-44405; imported directly for the node's SSH session (_base.py) "proxmoxer>=2.0.0", "netaddr>=0.9.0", diff --git a/tests/test_log_sources.py b/tests/test_log_sources.py new file mode 100644 index 0000000..e76003f --- /dev/null +++ b/tests/test_log_sources.py @@ -0,0 +1,43 @@ +"""`get_log_sources`: which logs the node writes, and which service writes each. + +pveproxy, pvedaemon, the Ceph daemons and a backup server's proxy write to +journald and to files under ``/var/log``. The command and its parse are +napalm-device-types'; the driver only carries the command over its exec path, +which already runs as root (NetOrk/netork#2049). +""" + +from __future__ import annotations + +from unittest.mock import patch + +from napalm_device_types import LogSourcesMixin + +from napalm_proxmox.backup_driver import ProxmoxBackupDriver +from napalm_proxmox.driver import ProxmoxDriver + +WIRE = ( + "LOGS_BEGIN\n[journald]\npveproxy.service\n" + "[open]\n/proc/2101/fd/7 /var/log/pveproxy/access.log\n" + "[stat]\n/proc/2101/fd/7 4096 1762250200\n" + "[cgroups]\n2101 0::/system.slice/pveproxy.service\n" + "[comm]\n2101 pveproxy worker\n" + "LOGS_END\n" +) + + +def test_both_drivers_declare_the_contract(): + assert issubclass(ProxmoxDriver, LogSourcesMixin) + assert issubclass(ProxmoxBackupDriver, LogSourcesMixin) + + +def test_it_reads_as_root_over_the_exec_path(driver): + with patch.object(driver, "_exec_ssh_command", return_value=WIRE) as exec_: + reading = driver.get_log_sources() + + [command] = [c.args[0] for c in exec_.call_args_list] + assert command.startswith("sh -c '") + assert reading["attributed"] is True + access = [s for s in reading["sources"] if s["kind"] == "file"] + assert [(s["path"], s["unit"]) for s in access] == [ + ("/var/log/pveproxy/access.log", "pveproxy") + ] -- 2.54.1