diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b09cbf..c3fdda7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,9 @@ All notable changes to this driver. The format follows - `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. +- `get_disks()` reports a SCSI/SAS disk's temperature (`Current Drive Temperature`); + it read `-1` for every SAS disk. A disk that prints `0 C` there, as QEMU's virtual + disks do, still reads `-1`. ## [0.3.0] - 2026-10-23 diff --git a/napalm_openmediavault/openmediavault.py b/napalm_openmediavault/openmediavault.py index 61fb886..dc4b0d4 100644 --- a/napalm_openmediavault/openmediavault.py +++ b/napalm_openmediavault/openmediavault.py @@ -57,6 +57,12 @@ _SMART_VERDICT_RE = re.compile(r"^SMART overall-health self-assessment test resu #: 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 ATA disk's temperature attribute, an NVMe disk's ``Temperature:`` line. +_SMART_TEMP_RE = re.compile( + r"(?:Temperature_Celsius|Current Temperature|Temperature:)[^\n]*?(\d+)\s*(?:Celsius|\(|$)" +) +#: A SCSI/SAS disk's; a QEMU disk without a sensor prints ``0 C``. +_SMART_SCSI_TEMP_RE = re.compile(r"^Current Drive Temperature:\s+(\d+) C\s*$", re.M) #: 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+\]") @@ -77,6 +83,17 @@ def smart_health(output: str) -> str: return "unknown" +def smart_temperature(output: str) -> int: + """A disk's temperature in Celsius from ``smartctl -A`` output; ``-1`` if none.""" + found = _SMART_TEMP_RE.search(output) + if found: + return int(found.group(1)) + scsi = _SMART_SCSI_TEMP_RE.search(output) + if scsi and int(scsi.group(1)) > 0: + return int(scsi.group(1)) + return -1 + + def md_members(mdstat: str) -> dict[str, str]: """Each member device in ``/proc/mdstat``, mapped to its array: ``{"sda1": "md1"}``.""" return { @@ -296,13 +313,7 @@ class OpenMediaVaultDriver(OmvLocalAccountsMixin, StorageDriver, LinuxDriver): try: 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)) + temperature = smart_temperature(smart) if disk_type == "hdd": rpm_m = re.search(r"Rotation Rate:\s+(\d+)\s+rpm", smart) if rpm_m: diff --git a/tests/test_disks.py b/tests/test_disks.py index 2df6971..2c43b40 100644 --- a/tests/test_disks.py +++ b/tests/test_disks.py @@ -144,6 +144,23 @@ def test_a_passing_nvme_disk_is_healthy(): assert (disk["health"], disk["temperature"]) == ("healthy", 40) +def test_a_sas_disk_reports_its_drive_temperature(): + nas = Nas({"sdb": ("", SAS_OK)}) + + disk = _by_slot(nas)["/dev/sdb"] + + assert (disk["health"], disk["temperature"]) == ("healthy", 28) + + +def test_a_disk_without_smart_has_no_temperature(): + # QEMU prints "Current Drive Temperature: 0 C" for a disk that has no sensor. + nas = Nas({"sda": ("", QEMU_NO_SMART)}) + + disk = _by_slot(nas)["/dev/sda"] + + assert (disk["health"], disk["temperature"]) == ("unknown", -1) + + @pytest.mark.parametrize( ("output", "health"), [