Skip to content

fix(metrics): CPU usage shown inverted on Linux (~99% when idle) - #45

Open
flavioduque wants to merge 1 commit into
alanfrigo:mainfrom
flavioduque:fix/cpu-metric-inverted
Open

flavioduque wants to merge 1 commit into
alanfrigo:mainfrom
flavioduque:fix/cpu-metric-inverted

Conversation

@flavioduque

Copy link
Copy Markdown

Problem

On Linux the dashboard reports ~99% CPU while the host is idle.

getCpuUsage() runs:

top -l 1 -n 0 | grep 'CPU usage' || mpstat 1 1 | tail -1 | awk '{print 100 - $NF}'

top -l is macOS-only, so on Linux the mpstat branch runs. That awk already prints usage (100 - %idle), but the code treats the number as idle and computes 100 - value again. The result is inverted: an idle server shows ~99%, a busy one shows a low value.

Also, when measurement throws, the route returned Math.floor(Math.random() * 30) + 10, which is a made-up number shown as real data.

Fix

  • The mpstat branch now prints %idle (awk '{print $NF}'), which is what the parsing code expects. The macOS path is unchanged.
  • If the command fails, fall back to the existing os.cpus() calculation instead of a random value.

Verification

Ran inside the running panel container (Alpine, BusyBox top, sysstat mpstat) with the command string read straight from route.ts and the same parsing:

command output panel shows real idle (mpstat)
before 1.5 99% 97.24%
after 99.37 1% 99.00%

Not covered by an automated test: the function shells out to mpstat/top. I can extract the parsing into a pure helper with a tests/*.test.ts case if you prefer.

🤖 Generated with Claude Code

…age, code subtracted from 100 again) and no longer invents a random value when measurement fails

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant