fix(lldp): recognise a stopped lldpd and raise on a failed read
driver / standard (pull_request) Successful in 19s
driver / lint (pull_request) Successful in 15s
driver / test (3.12) (pull_request) Failing after 57s
driver / types (pull_request) Failing after 1m7s
driver / test (3.13) (pull_request) Failing after 50s
driver / lowest (pull_request) Failing after 23s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Failing after 1m9s
CI / driver (pull_request) Failing after 2m35s
driver / publish (pull_request) Skipped

The running check looked for "active" anywhere in the answer, and
systemctl answers "inactive" for a stopped lldpd, so lldpctl was asked
anyway. The check now prints one word, running or stopped, and is
compared whole. With lldpd running, lldpctl runs as napalm-device-types'
LLDPCTL_KEYVALUE_COMMAND: a failure (socket out of the login's reach, a
daemon restarting) raises with lldpctl's message instead of reading as
a host without neighbours. A missing or stopped lldpd still answers {}.

Closes #66
This commit is contained in:
Christian Manivong committed 2026-11-02 08:53:52 +01:00
1 parent a3d95d72b9
commit d3189806d3
3 files changed
+125 -15

No files matched your search

+11 -11
View File
@@ -28,6 +28,7 @@ from napalm.base.netmiko_helpers import netmiko_args
from napalm_device_types import (
APT_UPGRADABLE_COMMAND,
DNF_SECURITY_COMMAND,
LLDPCTL_KEYVALUE_COMMAND,
AptProxyMixin,
ByteStream,
CommandResult,
@@ -130,8 +131,11 @@ _DNF_RC_RE = re.compile(r"^__DNF_RC=(\d+)\s*$", re.MULTILINE)
#: Restart the host two seconds later, detached from this session: the launcher's
#: exit status comes back before the host goes down, and closing the session
#: cannot take the restart with it.
#: lldpd's neighbours; a failure prints nothing and reads as none, as before (#58).
_LLDPCTL_COMMAND = "lldpctl -f keyvalue 2>/dev/null || true"
#: ``running`` when systemd has lldpd active or a process of that name runs, else ``stopped``.
_LLDPD_STATE_COMMAND = (
"if systemctl is-active --quiet lldpd 2>/dev/null || pgrep -x lldpd >/dev/null 2>&1; "
"then echo running; else echo stopped; fi"
)
_REBOOT_DETACHED = "sh -c '(trap \"\" HUP; sleep 2; /sbin/reboot) </dev/null >/dev/null 2>&1 &'"
#: Why ``run_command`` refuses stdin to a privileged command while a sudo
@@ -772,21 +776,17 @@ class LinuxDriver(
neighbour of a port is its own entry, and one whose chassis id is a MAC
address carries it as ``mac``. Returns an empty dict when lldpd is
absent or stopped — does NOT attempt to start the daemon.
:raises ValueError: when lldpd runs but lldpctl fails (socket out of reach).
"""
# Check lldpctl is available
if not self._send("command -v lldpctl 2>/dev/null").strip():
return {}
# Check lldpd is active (systemd or fallback to pgrep)
running = self._send(
"systemctl is-active lldpd 2>/dev/null || "
"service lldpd status 2>/dev/null | grep -q running && echo active || "
"pgrep -x lldpd >/dev/null 2>&1 && echo active || true"
).strip()
if "active" not in running:
# The whole state: systemctl's "inactive" holds the word "active" (#66).
if self._send(_LLDPD_STATE_COMMAND).strip() != "running":
return {}
return lldp_neighbors(parse_lldpctl_keyvalue(self._send(_LLDPCTL_COMMAND)))
return lldp_neighbors(parse_lldpctl_keyvalue(self._send(LLDPCTL_KEYVALUE_COMMAND)))
def get_interfaces(self) -> dict[str, Any]:
"""Return interface operational data."""