From 395177b1b1e7ba175a094c26ccc9b4a51f1b68fc Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Tue, 1 Dec 2026 12:47:08 +0100 Subject: [PATCH] feat(host-storage): a host's disks, volumes and drive health without the storage role (NetOrk/netork#2896) HostStorageMixin declares get_disks, get_disk_pools and get_volumes in StorageDriver's shapes, and get_disk_smart, drive health per disk, with no role: a Windows or Linux host reports its storage and stays a host. DiskSmartDict names the keys Proxmox's get_disk_smart already fills and adds the highest temperature and the uncorrected read and write errors. HostVolumeDict adds a volume's file system and health to LogicalVolumeDict. A disk that does not say reads temperature -1 and health "unknown", never a zero or a "healthy" nobody measured. Additive: nothing changes for StorageDriver or its drivers. --- CHANGELOG.md | 15 ++++ README.md | 10 +++ docs/STRUCTURE.md | 2 +- napalm_device_types/__init__.py | 5 ++ napalm_device_types/host_storage.py | 126 ++++++++++++++++++++++++++++ tests/test_host_storage.py | 80 ++++++++++++++++++ 6 files changed, 237 insertions(+), 1 deletion(-) create mode 100644 napalm_device_types/host_storage.py create mode 100644 tests/test_host_storage.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 847c85a..049a341 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,21 @@ recorded here; their history is the git log. ## [Unreleased] +### Added +- `HostStorageMixin`: a host's disks, pools, volumes and drive health without the storage + role (NetOrk/netork#2896, #2705). It declares `get_disks`, `get_disk_pools` and + `get_volumes` in `StorageDriver`'s shapes, and `get_disk_smart()`, a dict of + `DiskSmartDict` keyed by the disk's slot: model, serial, type, size, health (`"ok"`, + `"warning"`, `"failed"`, `"unknown"`), wearout, temperature, percentage used, available + spare, reallocated sectors, power-on hours, and optionally the highest temperature and + the uncorrected read and write errors -- `None` where the drive does not report one. + These are the keys Proxmox's `get_disk_smart` already fills. A disk that does not say + reads temperature `-1` and health `"unknown"` in `get_disks` and is left out of + `get_disk_smart`. Nothing changes for `StorageDriver`. +- `HostVolumeDict`: a `LogicalVolumeDict` with an optional `file_system` and `health`. +- New values in existing string fields: a disk's `type` may be `"unknown"`, a pool's + `type` `"storage-spaces"` and its `level` `"parity"`. + ## [4.55.0] - 2026-12-01 ### Added diff --git a/README.md b/README.md index 8248567..9a00b84 100644 --- a/README.md +++ b/README.md @@ -682,6 +682,16 @@ DHCP server handed out. It is apart from `DhcpServerMixin`, since an access poin leases without being a DHCP server anybody manages; a driver mixes it in itself. A failed read raises; a device that serves none returns `[]`. +`HostStorageMixin` (NetOrk/netork#2896) carries a host's own storage without the storage +role: `get_disks`, `get_disk_pools`, `get_volumes` in `StorageDriver`'s shapes and +`get_disk_smart`, drive health per disk (`DiskSmartDict`: health, temperature, wear, +power-on hours, uncorrected errors; `None` where the drive does not say). A Windows or +Linux host mixes it in and stays a host; a storage appliance that reads drive health may +mix it in beside its role. A volume may carry `HostVolumeDict`'s file system and health. +Where a disk does not say -- a virtual disk, a RAID controller's logical disk -- its +temperature is `-1` and its health `"unknown"`, never a zero or a `"healthy"` nobody +measured, and `get_disk_smart` leaves it out. + ### Fingerprints `DeviceTypeDriver` carries what discovery matches a device against: `PORT_SPECS`, diff --git a/docs/STRUCTURE.md b/docs/STRUCTURE.md index 31cf0d4..d7b7504 100644 --- a/docs/STRUCTURE.md +++ b/docs/STRUCTURE.md @@ -36,7 +36,7 @@ application SNMP polling, ping sweeps, discovery scoring, guest | Kind | Rule | Modules | |---|---|---| | **Role** | Declares only, under `TYPE_CHECKING`. No runtime method. | `access_point`, `switch`, `firewall`, `hypervisor`, `os`, `storage`, `phone`, `media`, `residential_gateway`, `management_controller`, `backup_server` | -| **Capability** | Declares only. | `packages`, `services`, `updates`, `nat_vpn`, `mac_acl`, `config_lifecycle`, `interface_filter`, `host_reboot`, `dhcp_leases`, `wireless_writes`, `ap_config` | +| **Capability** | Declares only. | `packages`, `services`, `updates`, `nat_vpn`, `mac_acl`, `config_lifecycle`, `interface_filter`, `host_reboot`, `dhcp_leases`, `wireless_writes`, `ap_config`, `host_storage` | | **Template** | A concrete method that runs a fixed sequence of abstract steps. A driver implements the steps, or overrides the whole method when its device does it in one go. All values come from the caller. | the fix routines below | | **Contract types** | Interfaces and data shapes other packages implement or fill. | `CommandChannelMixin`, `CommandResult`, `ByteStream`; the declaration of `ContainerEngineMixin`; the reader mixins' public method declarations; the SNMP profile type; `models` | | **Reconciler** | Concrete and vendor-neutral, without I/O of its own. | `firewall_rules` and `dhcp` diff/apply, `_matching`, `_dhcp_order`, `lag` | diff --git a/napalm_device_types/__init__.py b/napalm_device_types/__init__.py index b6ff305..45903a5 100644 --- a/napalm_device_types/__init__.py +++ b/napalm_device_types/__init__.py @@ -51,6 +51,7 @@ instead of being restated on every role that happens to need it: * :class:`~napalm_device_types.host_status.HostStatusMixin` * :class:`~napalm_device_types.host_reboot.HostRebootMixin` * :class:`~napalm_device_types.host_shutdown.HostShutdownMixin` +* :class:`~napalm_device_types.host_storage.HostStorageMixin` * :class:`~napalm_device_types.interface_filter.InterfaceFilterMixin` * :class:`~napalm_device_types.kernel.KernelFactsMixin` * :class:`~napalm_device_types.listening.ListeningSocketsMixin` @@ -216,6 +217,7 @@ from napalm_device_types.host_firewall import ( from napalm_device_types.host_reboot import HostRebootMixin from napalm_device_types.host_shutdown import HostShutdownMixin from napalm_device_types.host_status import HOST_STATUS_COMMAND, HostStatusMixin, parse_host_status +from napalm_device_types.host_storage import DiskSmartDict, HostStorageMixin, HostVolumeDict from napalm_device_types.hypervisor import HypervisorDriver from napalm_device_types.interface_filter import InterfaceFilterMixin from napalm_device_types.kernel import ( @@ -614,6 +616,9 @@ __all__ = [ "HostStatusMixin", "HostRebootMixin", "HostShutdownMixin", + "HostStorageMixin", + "HostVolumeDict", + "DiskSmartDict", "HypervisorDriver", "InterfaceFilterMixin", "MacAclMixin", diff --git a/napalm_device_types/host_storage.py b/napalm_device_types/host_storage.py new file mode 100644 index 0000000..80e1f4b --- /dev/null +++ b/napalm_device_types/host_storage.py @@ -0,0 +1,126 @@ +"""A host's own disks, volumes and drive health (NetOrk/netork#2896, #2705). + +:class:`~napalm_device_types.storage.StorageDriver` is a *role*: a driver that inherits +it is shown as a storage device. A Windows server or a Linux host has disks, pools and +volumes too, and is still a host. :class:`HostStorageMixin` carries the same getters -- +and the drive health reading, :meth:`~HostStorageMixin.get_disk_smart` -- with no role, +so an OS driver reports them and stays what it is. A storage appliance that also reads +drive health may mix it in beside its role. + +Declared under ``if TYPE_CHECKING``, like every capability: a driver has a getter at +runtime only when it reads that part, so ``hasattr`` tells a caller which parts to ask +for. A getter that cannot look raises; one that looked and found none returns empty. + +The shapes are the storage role's (``PhysicalDiskDict``, ``DiskPoolDict``, +``LogicalVolumeDict``) with two additions a host fills where it can: +:class:`HostVolumeDict`, a volume with its file system and health, and +:class:`DiskSmartDict`, a disk's drive health. +""" + +from typing import TYPE_CHECKING, NotRequired, TypedDict + +from napalm_device_types.models import ( + DiskPoolDict, + LogicalVolumeDict, + PhysicalDiskDict, +) + + +class HostVolumeDict(LogicalVolumeDict): + """A volume as a host reports it: a ``LogicalVolumeDict`` and, where known, the + file system on it and the volume's own health.""" + + file_system: NotRequired[str] # "NTFS", "ReFS", "ext4", "xfs"; empty when raw + health: NotRequired[str] # "healthy", "warning", "failed", "unknown" + + +class DiskSmartDict(TypedDict): + """One disk's drive health: its self-reported counters, ``None`` where the drive or + its controller does not report one. + + The keys up to ``power_on_hours`` are what drivers filled before the contract named + them (Proxmox reads them through its API from smartctl); the rest are optional. + """ + + model: str + serial: str + type: str # "hdd", "ssd", "nvme", "unknown" + size: int # bytes + #: The drive's verdict: "ok" (smartctl's "passed" where a driver passes it through), + #: "warning", "failed", or "unknown". + health: str + wearout: int | None # life left, percent (100 = new) + temperature: int | None # Celsius + percentage_used: int | None # wear, percent of the rated endurance used + available_spare: int | None # percent + reallocated_sectors: int | None + power_on_hours: int | None + temperature_max: NotRequired[int | None] # Celsius, the highest the drive recorded + read_errors_uncorrected: NotRequired[int | None] + write_errors_uncorrected: NotRequired[int | None] + + +class HostStorageMixin: + """A host's disks, pools, volumes and drive health, without the storage role.""" + + if TYPE_CHECKING: + + def get_disks(self) -> list[PhysicalDiskDict]: + """ + The host's physical disks, as ``StorageDriver.get_disks`` documents them. + + ``temperature`` is ``-1`` and ``health`` ``"unknown"`` where the disk does not + say -- a virtual disk, or a logical disk of a hardware RAID controller -- never + a zero or a ``"healthy"`` nobody measured. ``type`` is ``"unknown"`` where + the host cannot tell the medium. A virtual disk the host builds from other + disks (a Storage Spaces or md volume) is no physical disk; its members are. + """ + ... + + def get_disk_pools(self) -> dict[str, DiskPoolDict]: + """ + The pools the host builds from its disks, as ``StorageDriver.get_disk_pools`` + documents them; ``{}`` when it has none. Besides the role's types, ``type`` + may be ``"storage-spaces"`` and ``level`` ``"parity"``. + """ + ... + + def get_volumes(self) -> dict[str, LogicalVolumeDict]: + """ + The host's mounted volumes, as ``StorageDriver.get_volumes`` documents them, + keyed by where they are mounted. Each may carry :class:`HostVolumeDict`'s + optional keys. A field the host has no notion of reads empty: ``pool`` ``""`` + where the volume belongs to none, ``compression`` ``""`` where unknown. + """ + ... + + def get_disk_smart(self) -> dict[str, DiskSmartDict]: + """ + Drive health per disk, keyed as ``get_disks``' ``slot``. + + Only disks that report something are listed: a virtual disk with no + counters is left out rather than listed with nothing in it, so a host + whose disks report nothing returns ``{}``. + + Example:: + + { + "PhysicalDisk0": { + "model": "Samsung SSD 980 PRO 1TB", + "serial": "S5GXNX0T123456", + "type": "nvme", + "size": 1000204886016, + "health": "ok", + "wearout": None, + "temperature": 41, + "percentage_used": 3, + "available_spare": None, + "reallocated_sectors": None, + "power_on_hours": 5120, + "temperature_max": 68, + "read_errors_uncorrected": 0, + "write_errors_uncorrected": 0, + }, + } + """ + ... diff --git a/tests/test_host_storage.py b/tests/test_host_storage.py new file mode 100644 index 0000000..e9e9eb0 --- /dev/null +++ b/tests/test_host_storage.py @@ -0,0 +1,80 @@ +"""HostStorageMixin: a host's disks, volumes and drive health without being a storage +device (NetOrk/netork#2896, #2705). + +``StorageDriver`` is a role: a driver that inherits it is shown as storage. A Windows or +Linux host has disks too, and is still a host. The mixin carries the same getters with no +role, so an OS driver reports its disks and stays what it is. +""" + +from __future__ import annotations + +import inspect +from typing import get_type_hints + +from napalm_device_types import ( + DiskSmartDict, + HostStorageMixin, + HostVolumeDict, + OSDriver, + StorageDriver, +) +from napalm_device_types.models import LogicalVolumeDict +from napalm_device_types.roles import primary_role_of, role_keys_of + +GETTERS = ("get_disks", "get_disk_pools", "get_volumes", "get_disk_smart") + + +def test_declared_not_implemented(): + """hasattr is the application's capability probe; only a driver that reads a part + may answer True.""" + for name in GETTERS: + assert not hasattr(HostStorageMixin, name), name + + +def test_declarations_document_the_contract(): + source = inspect.getsource(HostStorageMixin) + for name in GETTERS: + assert f"def {name}(self)" in source, name + + +def test_carries_no_role(): + class WindowsHost(HostStorageMixin, OSDriver): + ROLE = "windows" + + assert role_keys_of(WindowsHost) == ["windows"] + assert primary_role_of(WindowsHost) == "windows" + + +def test_a_storage_device_with_the_mixin_stays_storage(): + class Nas(StorageDriver, HostStorageMixin, OSDriver): + pass + + assert primary_role_of(Nas) == "storage" + + +def test_disk_smart_keeps_the_shape_drivers_already_fill(): + """Proxmox's ``get_disk_smart`` filled these keys before there was a contract; the + contract names them, and adds the error counters and the highest temperature.""" + hints = get_type_hints(DiskSmartDict) + assert { + "model", + "serial", + "type", + "size", + "health", + "wearout", + "temperature", + "percentage_used", + "available_spare", + "reallocated_sectors", + "power_on_hours", + } <= set(hints) + assert {"temperature_max", "read_errors_uncorrected", "write_errors_uncorrected"} <= ( + DiskSmartDict.__optional_keys__ + ) + + +def test_a_host_volume_is_a_logical_volume_with_optional_extras(): + assert set(get_type_hints(LogicalVolumeDict)) <= set(get_type_hints(HostVolumeDict)) + assert {"file_system", "health"} <= HostVolumeDict.__optional_keys__ + assert LogicalVolumeDict.__required_keys__ <= HostVolumeDict.__required_keys__