|
21 | 21 | BACKUP_MAX_AGE_HOURS, |
22 | 22 | HOMELAB_DISKS, |
23 | 23 | HOMELAB_HOST_ROOT, |
| 24 | + HOMELAB_TEMP_ALERT_C, |
24 | 25 | IN_CONTAINER, |
25 | 26 | REPO_DIR, |
26 | 27 | STATE_DIR, |
|
54 | 55 | # e.g. /host. Disks, mount checks and RUN are read under it; /proc needs no |
55 | 56 | # prefix, since uptime, load and memory are the host's anyway. |
56 | 57 | HOST_ROOT = HOMELAB_HOST_ROOT |
| 58 | +# Temperature sensors, read from this process's own /sys and never under |
| 59 | +# HOST_ROOT: sysfs is a mount of its own, so a bind mount of the host's / usually |
| 60 | +# doesn't contain it, while Docker mounts the host's sysfs read-only into the |
| 61 | +# container without masking class/thermal or class/hwmon. |
| 62 | +SYS = Path("/sys") |
| 63 | +# hwmon drivers for the CPU's (or SoC's) temperature, read only when no thermal |
| 64 | +# zone has a reading (e.g. AMD's k10temp); others (nvme, iwlwifi, ...) never are. |
| 65 | +CPU_HWMON_DRIVERS = frozenset({"coretemp", "k10temp", "zenpower", "cpu_thermal", "soc_thermal"}) |
| 66 | +# Above this a reading is junk; at or below 0 °C it's a sensor's "no value" |
| 67 | +# (some zones say -273200). |
| 68 | +MAX_PLAUSIBLE_C = 150 |
| 69 | +# HOMELAB_TEMP_ALERT_C: alert at or above it (None: never). An open alert clears |
| 70 | +# only TEMP_HYSTERESIS_C below it, so a reading hovering there doesn't alert and |
| 71 | +# clear every check. |
| 72 | +TEMP_ALERT_C = HOMELAB_TEMP_ALERT_C |
| 73 | +TEMP_HYSTERESIS_C = 5 |
| 74 | +# Sensor labels come from sysfs; longer ones are cut. |
| 75 | +MAX_SENSOR_LABEL_CHARS = 40 |
57 | 76 |
|
58 | 77 |
|
59 | 78 | def on_host(path: str) -> str: |
@@ -90,6 +109,7 @@ class Health: |
90 | 109 | containers: list[Container] | None = None # None if Docker couldn't be asked |
91 | 110 | updates: list[str] | None = None # upgradable packages; None if apt couldn't be asked |
92 | 111 | reboot: list[str] | None = None # packages waiting for a reboot; None if none is |
| 112 | + temperature: tuple[str, float] | None = None # (sensor label, °C); None: no sensor |
93 | 113 | # Programs that aren't installed ("systemctl", "apt"), or "docker" if |
94 | 114 | # nothing answers at DOCKER_HOST: their reading is None, and the card says |
95 | 115 | # so instead of "Couldn't check". |
@@ -135,6 +155,42 @@ def read_memory() -> tuple[float, float] | None: |
135 | 155 | return fields["MemTotal"] - fields["MemAvailable"], fields["MemTotal"] |
136 | 156 |
|
137 | 157 |
|
| 158 | +def read_sys(path: Path) -> str | None: |
| 159 | + """A sysfs file's text; None if unreadable (some zones answer EINVAL).""" |
| 160 | + try: |
| 161 | + return path.read_text().strip() |
| 162 | + except (OSError, UnicodeDecodeError): |
| 163 | + return None |
| 164 | + |
| 165 | + |
| 166 | +def celsius(millidegrees: str | None) -> float | None: |
| 167 | + """A sensor's millidegrees as °C, or None if it isn't a plausible reading.""" |
| 168 | + if millidegrees is None or not is_number(millidegrees) or len(millidegrees) > 9: |
| 169 | + return None # also "-273200": is_number() takes digits only |
| 170 | + value = int(millidegrees) / 1000 |
| 171 | + return value if 0 < value <= MAX_PLAUSIBLE_C else None |
| 172 | + |
| 173 | + |
| 174 | +def read_temperature() -> tuple[str, float] | None: |
| 175 | + """The hottest plausible CPU temperature as (sensor label, °C): from the |
| 176 | + thermal zones, else from the CPU's hwmon driver. None if there's no sensor.""" |
| 177 | + readings: list[tuple[str, float]] = [] |
| 178 | + for zone in sorted((SYS / "class" / "thermal").glob("thermal_zone*")): |
| 179 | + if (value := celsius(read_sys(zone / "temp"))) is not None: |
| 180 | + readings.append((read_sys(zone / "type") or zone.name, value)) |
| 181 | + if not readings: |
| 182 | + for hwmon in sorted((SYS / "class" / "hwmon").glob("hwmon*")): |
| 183 | + if (name := read_sys(hwmon / "name")) not in CPU_HWMON_DRIVERS: |
| 184 | + continue |
| 185 | + for sensor in sorted(hwmon.glob("temp*_input")): |
| 186 | + if (value := celsius(read_sys(sensor))) is not None: |
| 187 | + readings.append((name, value)) |
| 188 | + if not readings: |
| 189 | + return None |
| 190 | + label, value = max(readings, key=lambda reading: reading[1]) |
| 191 | + return label[:MAX_SENSOR_LABEL_CHARS], value |
| 192 | + |
| 193 | + |
138 | 194 | def disk_space(path: str) -> tuple[float, float] | None: |
139 | 195 | try: |
140 | 196 | usage = shutil.disk_usage(path) |
@@ -288,6 +344,7 @@ async def read_health() -> Health: |
288 | 344 | containers=docker, |
289 | 345 | updates=updates, |
290 | 346 | reboot=reboot, |
| 347 | + temperature=read_temperature(), |
291 | 348 | missing=frozenset(missing), |
292 | 349 | in_container=IN_CONTAINER, |
293 | 350 | host_root=HOST_ROOT is not None, |
@@ -359,6 +416,16 @@ def problems(health: Health, now: float) -> tuple[dict[str, Problem], set[str]]: |
359 | 416 | if health.reboot |
360 | 417 | else "An update installed since the last boot needs it.", |
361 | 418 | ) |
| 419 | + if TEMP_ALERT_C is None or health.temperature is None: |
| 420 | + unknown.add("temp") # no alert set, or no sensor: an open alert stays as it is |
| 421 | + else: |
| 422 | + label, value = health.temperature |
| 423 | + if value >= TEMP_ALERT_C: |
| 424 | + found["temp"] = Problem( |
| 425 | + "Running hot", f"{escape(label)} at {value:.0f} °C (alert at {TEMP_ALERT_C} °C)." |
| 426 | + ) |
| 427 | + elif value >= TEMP_ALERT_C - TEMP_HYSTERESIS_C: |
| 428 | + unknown.add("temp") # just below the alert: neither opens nor clears one |
362 | 429 | b = health.backups |
363 | 430 | if b is None: # backup monitoring is off |
364 | 431 | unknown.add("backup") |
@@ -446,6 +513,9 @@ def health_lines(health: Health, now: float) -> list[str]: |
446 | 513 | if health.load is not None: |
447 | 514 | one, five, fifteen = health.load |
448 | 515 | up.append(f"load {one:.2f} {five:.2f} {fifteen:.2f} ({os.cpu_count() or '?'} cores)") |
| 516 | + # A temperature that's alerting is described once, by the warning below. |
| 517 | + if health.temperature is not None and "temp" not in found: |
| 518 | + up.append(f"{health.temperature[1]:.0f} °C") |
449 | 519 | if up: |
450 | 520 | lines.append(" · ".join(up)) |
451 | 521 | if health.memory is not None: |
@@ -602,10 +672,13 @@ def incidents_since(start: float) -> list[Incident]: |
602 | 672 |
|
603 | 673 |
|
604 | 674 | def switched_off(key: str) -> bool: |
605 | | - """An alert whose check is turned off: backups without BACKUP_DIR; units, and |
606 | | - reboots without HOMELAB_HOST_ROOT, in a container.""" |
| 675 | + """An alert whose check is turned off: backups without BACKUP_DIR; the |
| 676 | + temperature without HOMELAB_TEMP_ALERT_C; units, and reboots without |
| 677 | + HOMELAB_HOST_ROOT, in a container.""" |
607 | 678 | if key == "backup": |
608 | 679 | return BACKUP_DIR is None |
| 680 | + if key == "temp": |
| 681 | + return TEMP_ALERT_C is None |
609 | 682 | if key == "reboot": |
610 | 683 | return IN_CONTAINER and HOST_ROOT is None |
611 | 684 | return IN_CONTAINER and key.startswith("unit:") |
|
0 commit comments