"""NAPALM ``get_environment`` from host quick stats and hardware sensors.""" from __future__ import annotations from typing import Any _SENSORS = "runtime.healthSystemRuntime.systemHealthInfo.numericSensorInfo" _MB = 1024 * 1024 def _reading(sensor: dict[str, Any]) -> float: return float(sensor.get("currentReading", 0)) * 10 ** int(sensor.get("unitModifier", 0)) def _health(sensor: dict[str, Any]) -> str: return (sensor.get("healthState") or {}).get("key", "unknown") def _cpu_percent(host: dict[str, Any]) -> float: hardware = host.get("summary.hardware") or {} capacity = int(hardware.get("cpuMhz") or 0) * int(hardware.get("numCpuCores") or 0) used = (host.get("summary.quickStats") or {}).get("overallCpuUsage") or 0 return round(used / capacity * 100, 2) if capacity else 0.0 def _add_sensors(env: dict[str, Any], host: dict[str, Any], prefix: str) -> None: for sensor in host.get(_SENSORS) or []: name = f"{prefix}{sensor.get('name', '')}" kind = sensor.get("sensorType") health = _health(sensor) if kind == "temperature": env["temperature"][name] = { "temperature": _reading(sensor), "is_alert": health in ("yellow", "red"), "is_critical": health == "red", } elif kind == "fan": env["fans"][name] = {"status": health == "green"} elif kind == "power": env["power"][name] = { "status": health == "green", "capacity": -1.0, "output": _reading(sensor), } def environment(hosts: list[dict[str, Any]]) -> dict[str, Any]: """One host: keyed like a single device. Several (vCenter): keyed by host.""" env: dict[str, Any] = { "fans": {}, "temperature": {}, "power": {}, "cpu": {}, "memory": {"available_ram": 0, "used_ram": 0}, } single = len(hosts) == 1 for host in hosts: key = "0" if single else host.get("name", host["_moref"]) env["cpu"][key] = {"%usage": _cpu_percent(host)} hardware = host.get("summary.hardware") or {} stats = host.get("summary.quickStats") or {} env["memory"]["available_ram"] += int(hardware.get("memorySize") or 0) env["memory"]["used_ram"] += int(stats.get("overallMemoryUsage") or 0) * _MB _add_sensors(env, host, "" if single else f"{key}/") return env