86 lines
3.4 KiB
Python
86 lines
3.4 KiB
Python
"""The SMBIOS identity of a Windows host: what ties it to its BMC (NetOrk/netork#2888).
|
|
|
|
:class:`WindowsSystemIdentityMixin` gives the driver ``get_system_identity`` with the
|
|
shape and the placeholder rules of napalm-device-types' ``SystemIdentityMixin``, which
|
|
reads Linux' DMI files. Windows reads the same SMBIOS tables through its CIM classes,
|
|
in one script of its own -- not folded into ``get_facts``, so a class only the identity
|
|
needs cannot fail the facts:
|
|
|
|
* ``Win32_ComputerSystemProduct``: ``UUID`` (the system UUID, which Windows decodes from
|
|
SMBIOS 2.6+ the way dmidecode does) and ``IdentifyingNumber`` (the product serial);
|
|
* ``Win32_BaseBoard``: ``SerialNumber``, of the first board;
|
|
* ``Win32_BIOS``: ``Manufacturer``, ``SMBIOSBIOSVersion`` and ``ReleaseDate``. CIM gives
|
|
the date as midnight UTC; it travels as Unix time and is read as that UTC day.
|
|
|
|
Vendor placeholders ("To be filled by O.E.M.", all-F UUIDs) read as empty
|
|
(``clean_system_identity``).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from napalm_device_types import clean_system_identity
|
|
from napalm_device_types.system_identity import SystemIdentityDict
|
|
|
|
from napalm_windows._reading import json_object
|
|
|
|
_PS_SYSTEM_IDENTITY = r"""
|
|
$ErrorActionPreference = 'Stop'
|
|
$product = Get-CimInstance -ClassName Win32_ComputerSystemProduct
|
|
$board = @(Get-CimInstance -ClassName Win32_BaseBoard) | Select-Object -First 1
|
|
$bios = Get-CimInstance -ClassName Win32_BIOS
|
|
[pscustomobject]@{
|
|
uuid = [string]$product.UUID
|
|
product_serial = [string]$product.IdentifyingNumber
|
|
board_serial = if ($board) { [string]$board.SerialNumber } else { $null }
|
|
bios_vendor = [string]$bios.Manufacturer
|
|
bios_version = [string]$bios.SMBIOSBIOSVersion
|
|
bios_date = if ($bios.ReleaseDate) {
|
|
([DateTimeOffset]$bios.ReleaseDate).ToUnixTimeSeconds()
|
|
} else { $null }
|
|
} | ConvertTo-Json -Compress
|
|
"""
|
|
|
|
|
|
def _text(raw: dict[str, Any], key: str) -> str:
|
|
return str(raw.get(key) or "")
|
|
|
|
|
|
def _utc_day(value: Any) -> str:
|
|
"""Unix time as its UTC day, ``YYYY-MM-DD``; empty for none."""
|
|
if not isinstance(value, int | float) or isinstance(value, bool):
|
|
return ""
|
|
return datetime.fromtimestamp(value, UTC).date().isoformat()
|
|
|
|
|
|
def parse_system_identity(raw: dict[str, Any]) -> SystemIdentityDict:
|
|
"""``_PS_SYSTEM_IDENTITY``'s JSON as napalm-device-types' identity, placeholders empty."""
|
|
return clean_system_identity(
|
|
system_uuid=_text(raw, "uuid"),
|
|
product_serial=_text(raw, "product_serial"),
|
|
board_serial=_text(raw, "board_serial"),
|
|
bios_vendor=_text(raw, "bios_vendor"),
|
|
bios_version=_text(raw, "bios_version"),
|
|
bios_date=_utc_day(raw.get("bios_date")),
|
|
)
|
|
|
|
|
|
class WindowsSystemIdentityMixin:
|
|
"""``get_system_identity`` from the CIM classes; the driver carries the script."""
|
|
|
|
if TYPE_CHECKING: # pragma: no cover - declared for type checkers only
|
|
|
|
def _run_raw(self, script: str) -> str: ...
|
|
|
|
def get_system_identity(self) -> SystemIdentityDict:
|
|
"""The host's SMBIOS identity and system firmware, each value empty when unknown
|
|
or a vendor placeholder; see the module.
|
|
|
|
:raises ValueError: when the reading came back empty.
|
|
"""
|
|
return parse_system_identity(
|
|
json_object(self._run_raw(_PS_SYSTEM_IDENTITY), "system identity")
|
|
)
|