Read the CPU temperature instead of the alarm threshold beside it

lm-sensors prints high= and crit= on the same line as the reading, so scraping
the line for its largest number reported a flat 100C on Intel while the shell's
last-field parse got a bare ")" and never fired at all. One parse in the
adapter now, preferring Tdie over Tctl since Tctl carries a +27C offset.
This commit is contained in:
Gmer4Lfe
2026-08-21 08:17:36 -04:00
parent a07019e3aa
commit 4a29e7bc99
4 changed files with 81 additions and 9 deletions
+40
View File
@@ -203,6 +203,46 @@ platform_get_temp_thresholds() {
echo "${hdd_hot:-45} ${hdd_max:-55} ${ssd_hot:-60} ${ssd_max:-70}"
}
# ──────────────────────────────────────────────────────────────────────────────────────────────
# platform_get_cpu_temp
# Writes the CPU temperature in °C to stdout as a decimal, or nothing if no sensor answers.
# Returns 1 when nothing could be read, so a caller can tell "no sensor" from "cold".
#
# Two things make this harder than it looks, and both produced wrong numbers for months:
#
# 1. lm-sensors prints the alarm thresholds on the same line as the reading:
# Core 0: +63.0 C (high = +80.0 C, crit = +100.0 C)
# Anything that scrapes numbers off the whole line and takes the largest reports 100 —
# the critical threshold — as the current temperature, on every Intel box, forever. The
# parenthetical is stripped before a single digit is read.
#
# 2. On AMD, Tctl is not the die temperature. It is a control value carrying a fixed offset
# (+27°C on Threadripper), which is why HOST1 read 70 while the die was at 43. Tdie is the
# real measurement and is preferred wherever both are published.
#
# Preference order: Tdie, then Intel's package sensor, then the board's own CPU Temp, then the
# hottest individual core. The first label that answers wins; within a label the maximum is
# taken, because a multi-die part publishes one line per die and the hottest is the one that
# matters.
# ──────────────────────────────────────────────────────────────────────────────────────────────
platform_get_cpu_temp() {
command -v sensors >/dev/null 2>&1 || return 1
local out label value
out=$(sensors 2>/dev/null | sed 's/(.*//') # drop "(high = ..., crit = ...)"
[[ -n "$out" ]] || return 1
for label in 'Tdie' 'Package id 0' 'CPU Temp' 'Core '; do
value=$(echo "$out" | grep -i "^[[:space:]]*${label}" \
| grep -oE '[+-]?[0-9]+\.[0-9]+' | sort -n | tail -1)
if [[ -n "$value" ]]; then
echo "${value#+}" # bash printf tolerates a leading +, PHP casts fine, awk does not
return 0
fi
done
return 1
}
# ──────────────────────────────────────────────────────────────────────────────────────────────
# platform_is_maintenance_running
# Returns 0 if a parity check or sync is currently in progress.