system-monitor: CPU temperature in bTop (Temperature card under Memory)
- Backend: readTemps() scans /sys/class/hwmon for all temp sensors and picks the CPU one (k10temp/coretemp/zenpower Tctl/Tdie/Package); added to /stats.temp. - BtopView: a Temperature card at lg:col-start-2 (under Memory, second row) — big CPU °C (color-graded), its sensor label, and the other sensors (GPU, NVMe, wifi…) listed beneath. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import os from 'node:os';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { readFile, readdir } from 'node:fs/promises';
|
||||
import { execFile } from 'node:child_process';
|
||||
import { promisify } from 'node:util';
|
||||
import { createRouter } from '../../create-router';
|
||||
@@ -102,6 +102,41 @@ async function readProcesses() {
|
||||
}
|
||||
|
||||
// GET /stats — one snapshot. CPU% comes from two /proc/stat samples ~120ms apart.
|
||||
// Read every hwmon temperature sensor (name/label/°C) and pick the CPU one (k10temp/coretemp/…).
|
||||
async function readTemps() {
|
||||
const sensors: { name: string; label: string; celsius: number }[] = [];
|
||||
let dirs: string[];
|
||||
try {
|
||||
dirs = await readdir('/sys/class/hwmon');
|
||||
} catch {
|
||||
return { cpuC: null as number | null, cpuLabel: null as string | null, sensors };
|
||||
}
|
||||
for (const d of dirs) {
|
||||
const base = `/sys/class/hwmon/${d}`;
|
||||
const name = (await readFile(`${base}/name`, 'utf8').catch(() => '')).trim();
|
||||
let files: string[];
|
||||
try {
|
||||
files = await readdir(base);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
for (const f of files) {
|
||||
const m = f.match(/^temp(\d+)_input$/);
|
||||
if (!m) continue;
|
||||
const milli = Number.parseInt((await readFile(`${base}/${f}`, 'utf8').catch(() => '')).trim(), 10);
|
||||
if (!Number.isFinite(milli)) continue;
|
||||
const label = (await readFile(`${base}/temp${m[1]}_label`, 'utf8').catch(() => '')).trim();
|
||||
sensors.push({ name, label: label || `temp${m[1]}`, celsius: Math.round(milli / 100) / 10 });
|
||||
}
|
||||
}
|
||||
const CPU_DRIVERS = ['k10temp', 'zenpower', 'coretemp', 'k8temp', 'cpu_thermal'];
|
||||
const cpu =
|
||||
sensors.find((s) => CPU_DRIVERS.includes(s.name.toLowerCase()) && /tctl|tdie|package|composite|core 0/i.test(s.label)) ??
|
||||
sensors.find((s) => CPU_DRIVERS.includes(s.name.toLowerCase())) ??
|
||||
null;
|
||||
return { cpuC: cpu?.celsius ?? null, cpuLabel: cpu ? `${cpu.name} · ${cpu.label}` : null, sensors };
|
||||
}
|
||||
|
||||
systemMonitorRouter.get('/stats', async (ctx) => {
|
||||
const first = await readCpuSample().catch(() => null);
|
||||
await new Promise((r) => setTimeout(r, 120));
|
||||
@@ -117,10 +152,11 @@ systemMonitorRouter.get('/stats', async (ctx) => {
|
||||
})
|
||||
: [];
|
||||
|
||||
const [mem, disks, processes] = await Promise.all([
|
||||
const [mem, disks, processes, temp] = await Promise.all([
|
||||
readMem().catch(() => null),
|
||||
readDisks(),
|
||||
readProcesses(),
|
||||
readTemps().catch(() => null),
|
||||
]);
|
||||
|
||||
const cpus = os.cpus();
|
||||
@@ -138,6 +174,7 @@ systemMonitorRouter.get('/stats', async (ctx) => {
|
||||
mem,
|
||||
disks,
|
||||
processes,
|
||||
temp,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user