diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f72609..1b09cbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,17 @@ All notable changes to this driver. The format follows - Python 3.12 is the floor (`requires-python = ">=3.12"`): netOrk runs every driver on 3.12, so 3.10 and 3.11 are no longer tested (NAPALM/driver-standard#6). +### Fixed +- `get_disks()` reports each disk's SMART verdict: `healthy` when smartctl's overall + self-assessment passed (SCSI/SAS: health status `OK`), `failed` when it failed or a + SCSI disk predicts a failure, `unknown` only when the disk gives none. The driver + asked smartctl without `-H`, which never prints the verdict: a disk read `unknown`, + and an ATA disk with an attribute table read `failed` from that table's + `WHEN_FAILED` column, whatever its state. +- `get_disks()` puts a disk in the md array only when the disk itself or one of its + partitions is a member: `sda` no longer joins the array of `sdaa1`, nor `nvme0n1` + that of `nvme0n11`. `/proc/mdstat` is read once per call instead of once per disk. + ## [0.3.0] - 2026-10-23 The first release on PyPI. diff --git a/napalm_openmediavault/openmediavault.py b/napalm_openmediavault/openmediavault.py index e77871e..61fb886 100644 --- a/napalm_openmediavault/openmediavault.py +++ b/napalm_openmediavault/openmediavault.py @@ -50,6 +50,54 @@ _HOSTNAME_MODULES = frozenset( _DIRTY_MODULES = "/var/lib/openmediavault/dirtymodules.json" +#: Identity, overall health verdict and attributes; ``-H`` alone prints the verdict. +_SMARTCTL = "smartctl -iHA /dev/{name} 2>/dev/null" +#: The overall verdict of an ATA or NVMe disk: ``PASSED`` or ``FAILED!``. +_SMART_VERDICT_RE = re.compile(r"^SMART overall-health self-assessment test result:\s*(\S+)", re.M) +#: A SCSI/SAS disk's: ``OK``, or the failure it predicts. +_SMART_SCSI_STATUS_RE = re.compile(r"^SMART Health Status:\s*(.+?)\s*$", re.M) +_SMART_VERDICTS = {"PASSED": "healthy", "FAILED": "failed"} +#: An md array's line in /proc/mdstat, and its members: ``sda1[0]``, ``sdc1[1](F)``. +_MDSTAT_ARRAY_RE = re.compile(r"^(md\d+)\s*:(.*)$", re.M) +_MDSTAT_MEMBER_RE = re.compile(r"(\S+?)\[\d+\]") + + +def smart_health(output: str) -> str: + """The overall SMART verdict in ``smartctl -H`` output. + + ``"healthy"`` or ``"failed"``; ``"unknown"`` when smartctl printed none (no + SMART support, a USB bridge without pass-through). + """ + verdict = _SMART_VERDICT_RE.search(output) + if verdict: + return _SMART_VERDICTS.get(verdict.group(1).rstrip("!"), "unknown") + status = _SMART_SCSI_STATUS_RE.search(output) + if status: + return "healthy" if status.group(1) == "OK" else "failed" + return "unknown" + + +def md_members(mdstat: str) -> dict[str, str]: + """Each member device in ``/proc/mdstat``, mapped to its array: ``{"sda1": "md1"}``.""" + return { + member: array + for array, rest in _MDSTAT_ARRAY_RE.findall(mdstat) + for member in _MDSTAT_MEMBER_RE.findall(rest) + } + + +def _pool_of(disk: str, members: dict[str, str]) -> str: + """The array *disk* or one of its partitions is a member of, or ``""``. + + Linux names a partition by appending its number, after a ``p`` when the disk's + name ends in a digit: ``sda`` owns ``sda1``, ``nvme0n1`` owns ``nvme0n1p1``; + ``sdaa1`` is not ``sda``'s, nor ``nvme0n11`` (another namespace) ``nvme0n1``'s. + """ + partition = r"p\d+" if disk[-1:].isdigit() else r"\d+" + own = re.compile(rf"{re.escape(disk)}(?:{partition})?") + return next((array for member, array in members.items() if own.fullmatch(member)), "") + + class OpenMediaVaultDriver(OmvLocalAccountsMixin, StorageDriver, LinuxDriver): """NAPALM driver for OpenMediaVault NAS systems. @@ -157,8 +205,9 @@ class OpenMediaVaultDriver(OmvLocalAccountsMixin, StorageDriver, LinuxDriver): def get_disks(self) -> list[PhysicalDiskDict]: """Return physical disk inventory. - Uses ``lsblk -J`` for device enumeration and ``smartctl -iA`` - (via sudo) for health, temperature, and RPM data. + Uses ``lsblk -J`` for device enumeration, ``smartctl -iHA`` (via sudo) + for health, temperature, and RPM data, and ``/proc/mdstat``, read once, + for array membership. Each entry contains: @@ -200,6 +249,7 @@ class OpenMediaVaultDriver(OmvLocalAccountsMixin, StorageDriver, LinuxDriver): except Exception: return result + members = self._md_members() for dev in lsblk.get("blockdevices", []): if dev.get("type") != "disk": continue @@ -221,29 +271,7 @@ class OpenMediaVaultDriver(OmvLocalAccountsMixin, StorageDriver, LinuxDriver): else: disk_type = "hdd" - temperature = -1 - health = "unknown" - rpm = 0 - - try: - smart = self._sudo(f"smartctl -iA /dev/{name} 2>/dev/null", read_timeout=15) - if "PASSED" in smart: - health = "healthy" - elif "FAILED" in smart: - health = "failed" - temp_m = re.search( - r"(?:Temperature_Celsius|Current Temperature|Temperature:)" - r"[^\n]*?(\d+)\s*(?:Celsius|\(|$)", - smart, - ) - if temp_m: - temperature = int(temp_m.group(1)) - if disk_type == "hdd": - rpm_m = re.search(r"Rotation Rate:\s+(\d+)\s+rpm", smart) - if rpm_m: - rpm = int(rpm_m.group(1)) - except Exception: - logger.debug("%s: smartctl for %s: ignored", self.hostname, name, exc_info=True) + health, temperature, rpm = self._smart(name, disk_type) result.append( PhysicalDiskDict( @@ -256,22 +284,40 @@ class OpenMediaVaultDriver(OmvLocalAccountsMixin, StorageDriver, LinuxDriver): rpm=rpm, temperature=temperature, health=health, - pool=self._disk_to_pool(name), + pool=_pool_of(name, members), ) ) return result - def _disk_to_pool(self, disk_name: str) -> str: - """Return the md array a disk belongs to, or empty string.""" + def _smart(self, name: str, disk_type: str) -> tuple[str, int, int]: + """A disk's SMART health, temperature (``-1`` if unknown) and rpm (``0``).""" + health, temperature, rpm = "unknown", -1, 0 try: - mdstat = self._send("cat /proc/mdstat 2>/dev/null") - for line in mdstat.splitlines(): - if disk_name in line and re.match(r"^md\d+", line): - return line.split(":")[0].strip() + smart = self._sudo(_SMARTCTL.format(name=name), read_timeout=15) + health = smart_health(smart) + temp_m = re.search( + r"(?:Temperature_Celsius|Current Temperature|Temperature:)" + r"[^\n]*?(\d+)\s*(?:Celsius|\(|$)", + smart, + ) + if temp_m: + temperature = int(temp_m.group(1)) + if disk_type == "hdd": + rpm_m = re.search(r"Rotation Rate:\s+(\d+)\s+rpm", smart) + if rpm_m: + rpm = int(rpm_m.group(1)) + except Exception: + logger.debug("%s: smartctl for %s: ignored", self.hostname, name, exc_info=True) + return health, temperature, rpm + + def _md_members(self) -> dict[str, str]: + """The md array of each member device, from one read of ``/proc/mdstat``.""" + try: + return md_members(self._send("cat /proc/mdstat 2>/dev/null")) except Exception: logger.debug("%s: /proc/mdstat not read: ignored", self.hostname, exc_info=True) - return "" + return {} # ── md RAID pools ────────────────────────────────────────────────────────── diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index 9f2e60f..6af16f8 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -1,11 +1,11 @@ { "coverage_floor": 15.0, "function_complexity": { - "napalm_openmediavault/openmediavault.py::OpenMediaVaultDriver.get_disks": 18 + "napalm_openmediavault/openmediavault.py::OpenMediaVaultDriver.get_disks": 12 }, "function_lines": { "napalm_openmediavault/openmediavault.py::OpenMediaVaultDriver.get_disk_pools": 81, - "napalm_openmediavault/openmediavault.py::OpenMediaVaultDriver.get_disks": 107, + "napalm_openmediavault/openmediavault.py::OpenMediaVaultDriver.get_disks": 87, "napalm_openmediavault/openmediavault.py::OpenMediaVaultDriver.get_storage_services": 58, "napalm_openmediavault/openmediavault.py::OpenMediaVaultDriver.get_volumes": 90 }, diff --git a/tests/test_disks.py b/tests/test_disks.py new file mode 100644 index 0000000..924c656 --- /dev/null +++ b/tests/test_disks.py @@ -0,0 +1,232 @@ +"""get_disks: the overall SMART verdict, and the md array each disk belongs to. + +smartctl prints the overall verdict only for ``-H``; ``-i`` (identity) and ``-A`` +(attributes) never do. The fake host below answers smartctl the way it builds its +output: one section per flag asked for, so a command without ``-H`` gets no verdict. + +The sections are in the form smartmontools 7.3 prints them (ATA, NVMe and SCSI), +cut to the lines that matter here; they are not recorded from a device. +""" + +from __future__ import annotations + +import json +import re + +import pytest + +from napalm_openmediavault.openmediavault import OpenMediaVaultDriver, smart_health + +_BANNER = ( + "smartctl 7.3 2022-02-28 r5338 [x86_64-linux-6.1.0-26-amd64] (local build)\n" + "Copyright (C) 2002-22, Bruce Allen, Christian Franke, www.smartmontools.org\n" +) + +ATA_INFO = """\ +=== START OF INFORMATION SECTION === +Model Family: HGST Ultrastar 7K6 +Device Model: HGST HUS726T6TALE6L4 +Serial Number: K1AAAAAA +Rotation Rate: 7200 rpm +SMART support is: Available - device has SMART capability. +SMART support is: Enabled +""" +ATA_PASSED = "SMART overall-health self-assessment test result: PASSED\n" +ATA_FAILED = ( + "SMART overall-health self-assessment test result: FAILED!\n" + "Drive failure expected in less than 24 hours. SAVE ALL DATA.\n" +) +ATA_ATTRIBUTES = """\ +SMART Attributes Data Structure revision number: 16 +Vendor Specific SMART Attributes with Thresholds: +ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE + 1 Raw_Read_Error_Rate 0x000b 100 100 016 Pre-fail Always - 0 +194 Temperature_Celsius 0x0002 176 176 000 Old_age Always - 34 (Min/Max 20/45) +""" + +NVME_INFO = """\ +=== START OF INFORMATION SECTION === +Model Number: Samsung SSD 980 PRO 1TB +Serial Number: S5AAAAAAAAAAAAA +""" +NVME_PASSED = "SMART overall-health self-assessment test result: PASSED\n" +NVME_ATTRIBUTES = """\ +SMART/Health Information (NVMe Log 0x02) +Critical Warning: 0x00 +Temperature: 41 Celsius +""" + +SCSI_OK = "SMART Health Status: OK\n" +SCSI_PREDICTED = ( + "SMART Health Status: FIRMWARE IMPENDING FAILURE TOO MANY BLOCK REASSIGNS [asc=5d, ascq=64]\n" +) + +USB_NO_SMART = ( + "SMART support is: Unavailable - device lacks SMART capability.\n" + "\n" + "A mandatory SMART command failed: exiting. To continue, add one or more '-T permissive' options.\n" +) + + +def smartctl(flags: str, info: str, health: str, attributes: str) -> str: + """What ``smartctl -`` prints: the banner, then one section per flag.""" + out = _BANNER + "\n" + if "i" in flags: + out += info + "\n" + if "H" in flags or "A" in flags: + out += "=== START OF READ SMART DATA SECTION ===\n" + if "H" in flags: + out += health + "\n" + if "A" in flags: + out += attributes + return out + + +class Nas: + """The NAS as ``_send`` and ``_sudo`` see it: lsblk, /proc/mdstat and smartctl.""" + + def __init__(self, disks: dict[str, tuple[str, str, str, str]], mdstat: str = "") -> None: + #: name -> (tran, info, health, attributes) + self.disks = disks + self.mdstat = mdstat + self.commands: list[str] = [] + + def send(self, command: str, read_timeout: float = 100) -> str: + self.commands.append(command) + if command.startswith("lsblk "): + return json.dumps( + { + "blockdevices": [ + { + "name": name, + "size": 6001175126016, + "type": "disk", + "rota": tran != "nvme", + "vendor": "", + "model": "m", + "serial": "s", + "tran": tran, + } + for name, (tran, *_rest) in self.disks.items() + ] + } + ) + if command.startswith("cat /proc/mdstat"): + return self.mdstat + raise AssertionError(command) + + def sudo(self, command: str, read_timeout: float = 100) -> str: + self.commands.append(command) + m = re.match(r"smartctl -(\w+) /dev/(\w+)", command) + assert m, command + _tran, info, health, attributes = self.disks[m.group(2)] + return smartctl(m.group(1), info, health, attributes) + + +def _driver(nas: Nas) -> OpenMediaVaultDriver: + driver = OpenMediaVaultDriver("nas", "admin", "p") + driver._send = nas.send # type: ignore[method-assign] + driver._sudo = nas.sudo # type: ignore[method-assign] + return driver + + +def _by_slot(nas: Nas) -> dict[str, dict]: + return {d["slot"]: dict(d) for d in _driver(nas).get_disks()} + + +# ── the overall verdict ───────────────────────────────────────────────────────── + + +def test_a_passing_ata_disk_is_healthy(): + nas = Nas({"sda": ("sata", ATA_INFO, ATA_PASSED, ATA_ATTRIBUTES)}) + + disk = _by_slot(nas)["/dev/sda"] + + assert disk["health"] == "healthy" + assert (disk["temperature"], disk["rpm"]) == (34, 7200) + + +def test_a_failing_ata_disk_is_failed(): + nas = Nas({"sda": ("sata", ATA_INFO, ATA_FAILED, ATA_ATTRIBUTES)}) + + assert _by_slot(nas)["/dev/sda"]["health"] == "failed" + + +def test_a_passing_nvme_disk_is_healthy(): + nas = Nas({"nvme0n1": ("nvme", NVME_INFO, NVME_PASSED, NVME_ATTRIBUTES)}) + + disk = _by_slot(nas)["/dev/nvme0n1"] + + assert (disk["health"], disk["temperature"]) == ("healthy", 41) + + +@pytest.mark.parametrize( + ("output", "health"), + [ + (ATA_PASSED, "healthy"), + (ATA_FAILED, "failed"), + (NVME_PASSED, "healthy"), + (SCSI_OK, "healthy"), + (SCSI_PREDICTED, "failed"), + (USB_NO_SMART, "unknown"), + ("", "unknown"), + # The attribute table's WHEN_FAILED column is no verdict. + (ATA_ATTRIBUTES, "unknown"), + ], + ids=[ + "ata-passed", + "ata-failed", + "nvme-passed", + "scsi-ok", + "scsi-predicted", + "no-smart", + "empty", + "when-failed-column", + ], +) +def test_smart_health_reads_the_verdict_line_only(output, health): + assert smart_health(_BANNER + output) == health + + +# ── md membership ─────────────────────────────────────────────────────────────── + +MDSTAT = """\ +Personalities : [raid1] [linear] [multipath] [raid0] [raid6] [raid5] [raid4] [raid10] +md0 : active raid1 sdab1[1] sdaa1[0] + 976630464 blocks super 1.2 [2/2] [UU] + +md1 : active raid1 sdb1[1] sdc1[0](F) + 5860389888 blocks super 1.2 [2/1] [U_] + +md2 : active raid1 nvme0n1p1[0] nvme1n1[1] + 976630464 blocks super 1.2 [2/2] [UU] + +md3 : active raid1 nvme2n11[0] nvme2n12[1] + 976630464 blocks super 1.2 [2/2] [UU] + +unused devices: +""" + + +def test_a_disk_is_in_the_array_its_partition_or_itself_is_in(): + names = ["sda", "sdaa", "sdc", "nvme0n1", "nvme1n1", "nvme2n1"] + nas = Nas({n: ("sata", ATA_INFO, ATA_PASSED, ATA_ATTRIBUTES) for n in names}, MDSTAT) + + pools = {slot: d["pool"] for slot, d in _by_slot(nas).items()} + + assert pools == { + "/dev/sda": "", # sdaa1 and sdab1 are not sda's + "/dev/sdaa": "md0", + "/dev/sdc": "md1", # a failed member is still a member + "/dev/nvme0n1": "md2", + "/dev/nvme1n1": "md2", + "/dev/nvme2n1": "", # nvme2n11 is another namespace, not a partition + } + + +def test_mdstat_is_read_once_for_all_disks(): + nas = Nas({n: ("sata", ATA_INFO, ATA_PASSED, ATA_ATTRIBUTES) for n in ("sda", "sdb")}, MDSTAT) + + _driver(nas).get_disks() + + assert sum(c.startswith("cat /proc/mdstat") for c in nas.commands) == 1