fix(environment): CPU usage from two reads a second apart

get_environment promised two samples one second apart but read
/proc/stat once, so the figure was the average since boot: a box at
100 % for the last minute showed its lifetime average. It now reads the
cpu line twice, a second apart, and uses the difference.

Closes #10
This commit is contained in:
Christian Manivong committed 2026-11-01 21:45:24 +01:00
1 parent c211ba0d9e
commit 097d9b9581
3 files changed
+96 -11

No files matched your search

+35 -11
View File
@@ -23,6 +23,8 @@ _RC_MARKER = "__OWRT_RC="
_RC_RE = re.compile(rf"^{_RC_MARKER}(\d+)\s*$", re.MULTILINE)
#: What apk prints when its index is not in RAM (after a reboot).
_APK_INDEX_MISSING = re.compile(r"opening from cache .*No such file or directory")
#: The aggregate CPU line of ``/proc/stat``, read twice one second apart.
_CPU_SAMPLES = "grep '^cpu ' /proc/stat; sleep 1; grep '^cpu ' /proc/stat"
def _split_status(raw: str) -> tuple[str, int | None]:
@@ -45,6 +47,36 @@ def _status_result(raw: str, command: str) -> dict[str, Any]:
return {"success": True, "output": output}
def _cpu_counters(line: str) -> tuple[int, int]:
"""``(idle, total)`` jiffies of a ``cpu`` line.
The total counts user, nice, system, idle, iowait, irq and softirq.
"""
fields = [int(x) for x in line.split()[1:8]]
if len(fields) < 7:
raise ValueError(f"not a cpu line: {line!r}")
return fields[3], sum(fields)
def _cpu_usage_percent(output: str) -> float:
"""Busy share of the time between the two ``cpu`` lines of :data:`_CPU_SAMPLES`.
0.0 when there are not two readable lines or the counters did not move.
"""
lines = [line for line in output.splitlines() if line.startswith("cpu ")]
if len(lines) < 2:
return 0.0
try:
idle1, total1 = _cpu_counters(lines[0])
idle2, total2 = _cpu_counters(lines[1])
except ValueError:
return 0.0
elapsed = total2 - total1
if elapsed <= 0:
return 0.0
return (1 - (idle2 - idle1) / elapsed) * 100
class OpenWrtSystemMixin:
"""Mixin providing system-level NAPALM getters.
@@ -54,21 +86,13 @@ class OpenWrtSystemMixin:
def get_environment(self) -> dict[str, Any]:
"""Return device environment data (CPU, memory).
CPU usage from ``/proc/stat`` (two samples 1 second apart via ``awk``).
CPU usage from ``/proc/stat``: the difference of two reads one second
apart, so it is the last second, not the average since boot.
Memory from ``/proc/meminfo``.
"""
cpu_out = self._send_command(
"awk '/^cpu /{idle1=$5; total1=$2+$3+$4+$5+$6+$7+$8} "
"END{print (1-(idle1/total1))*100}' /proc/stat"
)
cpu_pct = _cpu_usage_percent(self._send_command(_CPU_SAMPLES))
mem_out = self._send_command("cat /proc/meminfo")
cpu_pct = 0.0
try:
cpu_pct = float(cpu_out.strip())
except (ValueError, AttributeError):
pass
mem_total = 0
mem_available = 0
for line in mem_out.splitlines():