@sirenodeck/sirenodeck 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,975 @@
1
+ import "./main.mjs";
2
+ import { existsSync } from "node:fs";
3
+ import { cpus, freemem, loadavg, totalmem, uptime } from "node:os";
4
+ import { execFile } from "node:child_process";
5
+ import { readFile, readdir, statfs } from "node:fs/promises";
6
+ import { promisify } from "node:util";
7
+ //#region src/builtin-addons/system-status/domain/live-metrics.ts
8
+ const execFile$1 = promisify(execFile);
9
+ function clampPercent(value) {
10
+ if (!Number.isFinite(value)) return 0;
11
+ return Math.min(100, Math.max(0, Math.round(value)));
12
+ }
13
+ let prevCpuSample = null;
14
+ async function probeCpu() {
15
+ const list = cpus();
16
+ if (list.length === 0) return { available: false };
17
+ let idle = 0;
18
+ let total = 0;
19
+ for (const c of list) {
20
+ idle += c.times.idle;
21
+ total += c.times.user + c.times.nice + c.times.sys + c.times.idle + c.times.irq;
22
+ }
23
+ const prev = prevCpuSample;
24
+ if (prev === null || total <= prev.total) {
25
+ prevCpuSample = {
26
+ idle,
27
+ total
28
+ };
29
+ return {
30
+ available: true,
31
+ max: 100,
32
+ percentage: 0,
33
+ unit: "%",
34
+ value: 0
35
+ };
36
+ }
37
+ const dTotal = total - prev.total;
38
+ const dIdle = idle - prev.idle;
39
+ prevCpuSample = {
40
+ idle,
41
+ total
42
+ };
43
+ const pct = dTotal > 0 ? clampPercent((1 - dIdle / dTotal) * 100) : 0;
44
+ return {
45
+ available: true,
46
+ max: 100,
47
+ percentage: pct,
48
+ unit: "%",
49
+ value: pct
50
+ };
51
+ }
52
+ async function probeRam() {
53
+ const total = totalmem();
54
+ const used = total - freemem();
55
+ if (total <= 0) return {
56
+ available: false,
57
+ unit: "%"
58
+ };
59
+ const pct = clampPercent(used / total * 100);
60
+ return {
61
+ available: true,
62
+ max: total,
63
+ percentage: pct,
64
+ unit: "%",
65
+ value: pct
66
+ };
67
+ }
68
+ const DARWIN_CMD_TIMEOUT_MS = 2e3;
69
+ const darwinRun = async (binary, args) => {
70
+ try {
71
+ const { stdout } = await execFile$1(binary, [...args], {
72
+ timeout: DARWIN_CMD_TIMEOUT_MS,
73
+ signal: AbortSignal.timeout(DARWIN_CMD_TIMEOUT_MS)
74
+ });
75
+ return stdout;
76
+ } catch {
77
+ return null;
78
+ }
79
+ };
80
+ const SWAP_FIELD = (name) => new RegExp(`${name}\\s*=\\s*([\\d.]+)([KMG])`, "i");
81
+ const SWAP_UNIT_BYTES = {
82
+ K: 1024,
83
+ M: 1024 ** 2,
84
+ G: 1024 ** 3
85
+ };
86
+ const parseDarwinSwap = (raw) => {
87
+ const total = SWAP_FIELD("total").exec(raw);
88
+ const used = SWAP_FIELD("used").exec(raw);
89
+ if (total?.[1] === void 0 || used?.[1] === void 0) return null;
90
+ const toBytes = (m) => Number.parseFloat(m[1]) * (SWAP_UNIT_BYTES[(m[2] ?? "M").toUpperCase()] ?? 0);
91
+ const totalBytes = toBytes(total);
92
+ const usedBytes = toBytes(used);
93
+ if (!Number.isFinite(totalBytes) || totalBytes <= 0) return null;
94
+ return {
95
+ totalBytes,
96
+ usedBytes
97
+ };
98
+ };
99
+ const parseDarwinBattery = (raw) => {
100
+ const m = /(\d{1,3})%/.exec(raw);
101
+ if (m?.[1] === void 0) return null;
102
+ const pct = Number.parseInt(m[1], 10);
103
+ return Number.isFinite(pct) ? pct : null;
104
+ };
105
+ const parseDarwinNetstat = (raw) => {
106
+ const lines = raw.trim().split("\n");
107
+ const header = lines[0]?.trim().split(/\s+/) ?? [];
108
+ const ibytes = header.indexOf("Ibytes");
109
+ const obytes = header.indexOf("Obytes");
110
+ if (ibytes === -1 || obytes === -1) return null;
111
+ let rx = 0;
112
+ let tx = 0;
113
+ let counted = false;
114
+ for (const line of lines.slice(1)) {
115
+ const cols = line.trim().split(/\s+/);
116
+ const name = cols[0];
117
+ if (name === void 0) continue;
118
+ if (NETWORK_IFACE_SKIP_RE.test(name)) continue;
119
+ if (!(cols[2] ?? "").startsWith("<Link")) continue;
120
+ const i = Number.parseInt(cols[ibytes] ?? "", 10);
121
+ const o = Number.parseInt(cols[obytes] ?? "", 10);
122
+ if (!Number.isFinite(i) || !Number.isFinite(o)) continue;
123
+ rx += i;
124
+ tx += o;
125
+ counted = true;
126
+ }
127
+ return counted ? {
128
+ rx,
129
+ tx
130
+ } : null;
131
+ };
132
+ async function probeSwap() {
133
+ if (process.platform === "darwin") {
134
+ const raw = await darwinRun("sysctl", ["-n", "vm.swapusage"]);
135
+ const parsed = raw === null ? null : parseDarwinSwap(raw);
136
+ if (parsed === null) return {
137
+ available: false,
138
+ unit: "%"
139
+ };
140
+ const pct = clampPercent(parsed.usedBytes / parsed.totalBytes * 100);
141
+ return {
142
+ available: true,
143
+ max: parsed.totalBytes,
144
+ percentage: pct,
145
+ unit: "%",
146
+ value: pct
147
+ };
148
+ }
149
+ if (process.platform !== "linux") return {
150
+ available: false,
151
+ unit: "%"
152
+ };
153
+ if (!existsSync("/proc/meminfo")) return {
154
+ available: false,
155
+ unit: "%"
156
+ };
157
+ try {
158
+ const raw = await readFile("/proc/meminfo", "utf8");
159
+ const totalKb = Number.parseInt(/SwapTotal:\s*(\d+)\s*kB/.exec(raw)?.[1] ?? "", 10);
160
+ const freeKb = Number.parseInt(/SwapFree:\s*(\d+)\s*kB/.exec(raw)?.[1] ?? "", 10);
161
+ if (!Number.isFinite(totalKb) || totalKb <= 0) return {
162
+ available: false,
163
+ unit: "%"
164
+ };
165
+ const totalBytes = totalKb * 1024;
166
+ const pct = clampPercent(Math.max(0, (totalKb - freeKb) * 1024) / totalBytes * 100);
167
+ return {
168
+ available: true,
169
+ max: totalBytes,
170
+ percentage: pct,
171
+ unit: "%",
172
+ value: pct
173
+ };
174
+ } catch {
175
+ return {
176
+ available: false,
177
+ unit: "%"
178
+ };
179
+ }
180
+ }
181
+ async function probeDisk() {
182
+ try {
183
+ const stats = await statfs("/");
184
+ const total = Number(stats.blocks) * Number(stats.bsize);
185
+ const used = (Number(stats.blocks) - Number(stats.bfree)) * Number(stats.bsize);
186
+ if (total <= 0) return {
187
+ available: false,
188
+ unit: "%"
189
+ };
190
+ const pct = clampPercent(used / total * 100);
191
+ return {
192
+ available: true,
193
+ max: total,
194
+ percentage: pct,
195
+ unit: "%",
196
+ value: pct
197
+ };
198
+ } catch {
199
+ return {
200
+ available: false,
201
+ unit: "%"
202
+ };
203
+ }
204
+ }
205
+ async function probeNetwork() {
206
+ const count = cpus().length > 0 ? 1 : 0;
207
+ return {
208
+ available: count > 0,
209
+ unit: "inter.",
210
+ value: count
211
+ };
212
+ }
213
+ async function probeBattery() {
214
+ if (process.platform === "darwin") {
215
+ const raw = await darwinRun("pmset", ["-g", "batt"]);
216
+ const pct = raw === null ? null : parseDarwinBattery(raw);
217
+ if (pct === null) return {
218
+ available: false,
219
+ unit: "%"
220
+ };
221
+ const clamped = clampPercent(pct);
222
+ return {
223
+ available: true,
224
+ max: 100,
225
+ percentage: clamped,
226
+ unit: "%",
227
+ value: clamped
228
+ };
229
+ }
230
+ if (process.platform !== "linux") return {
231
+ available: false,
232
+ unit: "%"
233
+ };
234
+ if (!existsSync("/sys/class/power_supply/BAT0/capacity")) return {
235
+ available: false,
236
+ unit: "%"
237
+ };
238
+ try {
239
+ const raw = await readFile("/sys/class/power_supply/BAT0/capacity", "utf8");
240
+ const pct = clampPercent(Number.parseInt(raw.trim(), 10));
241
+ return {
242
+ available: true,
243
+ max: 100,
244
+ percentage: pct,
245
+ unit: "%",
246
+ value: pct
247
+ };
248
+ } catch {
249
+ return {
250
+ available: false,
251
+ unit: "%"
252
+ };
253
+ }
254
+ }
255
+ const MACMON_SHARE_MS = 2e3;
256
+ let sharedMacmon = null;
257
+ let inFlightMacmon = null;
258
+ const finite = (v) => typeof v === "number" && Number.isFinite(v) ? v : null;
259
+ const parseMacmonSample = (raw) => {
260
+ const line = raw.split("\n").find((l) => l.trim().startsWith("{"));
261
+ if (line === void 0) return null;
262
+ let data;
263
+ try {
264
+ data = JSON.parse(line);
265
+ } catch {
266
+ return null;
267
+ }
268
+ const temp = data["temp"] ?? {};
269
+ const fans = Array.isArray(data["fans"]) ? data["fans"] : [];
270
+ let fanRpm = null;
271
+ for (const fan of fans) {
272
+ const rpm = finite(fan["rpm"]);
273
+ if (rpm !== null && rpm > 0) fanRpm = Math.max(fanRpm ?? 0, rpm);
274
+ }
275
+ return {
276
+ cpuTemp: finite(temp["cpu_temp_avg"]),
277
+ gpuTemp: finite(temp["gpu_temp_avg"]),
278
+ fanRpm
279
+ };
280
+ };
281
+ async function readMacmon() {
282
+ if (sharedMacmon !== null && Date.now() - sharedMacmon.at < MACMON_SHARE_MS) return sharedMacmon.value;
283
+ if (inFlightMacmon !== null) return inFlightMacmon;
284
+ inFlightMacmon = (async () => {
285
+ const raw = await darwinRun("macmon", [
286
+ "pipe",
287
+ "--samples",
288
+ "1",
289
+ "--interval",
290
+ "200"
291
+ ]);
292
+ const value = raw === null ? null : parseMacmonSample(raw);
293
+ sharedMacmon = {
294
+ at: Date.now(),
295
+ value
296
+ };
297
+ return value;
298
+ })().finally(() => {
299
+ inFlightMacmon = null;
300
+ });
301
+ return inFlightMacmon;
302
+ }
303
+ async function probeTemperature() {
304
+ if (process.platform === "darwin") {
305
+ const value = (await readMacmon())?.cpuTemp ?? null;
306
+ return value === null ? {
307
+ available: false,
308
+ unit: "°C"
309
+ } : {
310
+ available: true,
311
+ value,
312
+ unit: "°C"
313
+ };
314
+ }
315
+ if (process.platform !== "linux") return {
316
+ available: false,
317
+ unit: "°C"
318
+ };
319
+ try {
320
+ if (existsSync("/sys/class/hwmon")) {
321
+ const entries = await readdir("/sys/class/hwmon");
322
+ const hwmons = [];
323
+ for (const e of entries) {
324
+ const namePath = `/sys/class/hwmon/${e}/name`;
325
+ if (!existsSync(namePath)) continue;
326
+ const name = (await readFile(namePath, "utf8")).trim();
327
+ hwmons.push({
328
+ dir: `/sys/class/hwmon/${e}`,
329
+ name
330
+ });
331
+ }
332
+ const matched = hwmons.map((h, i) => ({
333
+ h,
334
+ i
335
+ })).filter(({ h }) => CPU_HWMON_NAME_PATTERNS.some((re) => re.test(h.name))).filter(({ h }) => !/^acpitz$/i.test(h.name)).sort((a, b) => a.i - b.i)[0];
336
+ if (matched) {
337
+ const temp = `${matched.h.dir}/temp1_input`;
338
+ if (existsSync(temp)) {
339
+ const raw = await readFile(temp, "utf8");
340
+ const milli = Number.parseInt(raw.trim(), 10);
341
+ if (Number.isFinite(milli) && milli > 0) return {
342
+ available: true,
343
+ unit: "°C",
344
+ value: Math.round(milli / 1e3)
345
+ };
346
+ }
347
+ }
348
+ }
349
+ } catch {}
350
+ if (!existsSync("/sys/class/thermal/thermal_zone0/temp")) return {
351
+ available: false,
352
+ unit: "°C"
353
+ };
354
+ try {
355
+ const raw = await readFile("/sys/class/thermal/thermal_zone0/temp", "utf8");
356
+ const milli = Number.parseInt(raw.trim(), 10);
357
+ if (!Number.isFinite(milli)) return {
358
+ available: false,
359
+ unit: "°C"
360
+ };
361
+ return {
362
+ available: true,
363
+ unit: "°C",
364
+ value: Math.round(milli / 1e3)
365
+ };
366
+ } catch {
367
+ return {
368
+ available: false,
369
+ unit: "°C"
370
+ };
371
+ }
372
+ }
373
+ async function probeUptime() {
374
+ return {
375
+ available: true,
376
+ value: Math.round(uptime())
377
+ };
378
+ }
379
+ async function probeFrequency() {
380
+ const fromOs = cpus().find((c) => typeof c.speed === "number" && c.speed > 0)?.speed;
381
+ if (typeof fromOs === "number" && fromOs > 0) return {
382
+ available: true,
383
+ unit: "GHz",
384
+ value: Number((fromOs / 1e3).toFixed(2))
385
+ };
386
+ if (process.platform === "linux" && existsSync("/proc/cpuinfo")) try {
387
+ const m = (await readFile("/proc/cpuinfo", "utf8")).match(/cpu MHz\s*:\s*([\d.]+)/);
388
+ if (m && m[1]) {
389
+ const mhz = Number.parseFloat(m[1]);
390
+ if (Number.isFinite(mhz) && mhz > 0) return {
391
+ available: true,
392
+ unit: "GHz",
393
+ value: Number((mhz / 1e3).toFixed(2))
394
+ };
395
+ }
396
+ } catch {}
397
+ return {
398
+ available: false,
399
+ unit: "GHz"
400
+ };
401
+ }
402
+ async function probeLoad() {
403
+ const [one] = loadavg();
404
+ if (one === void 0 || !Number.isFinite(one)) return { available: false };
405
+ return {
406
+ available: true,
407
+ value: Number(one.toFixed(2))
408
+ };
409
+ }
410
+ async function probeProcesses() {
411
+ if (process.platform === "linux" && existsSync("/proc")) try {
412
+ const count = (await readdir("/proc")).filter((e) => /^\d+$/.test(e)).length;
413
+ return {
414
+ available: count > 0,
415
+ unit: "procs",
416
+ value: count
417
+ };
418
+ } catch {
419
+ return {
420
+ available: false,
421
+ unit: "procs"
422
+ };
423
+ }
424
+ if (process.platform === "darwin") {
425
+ const raw = await darwinRun("ps", ["-Ao", "pid="]);
426
+ if (raw === null) return {
427
+ available: false,
428
+ unit: "procs"
429
+ };
430
+ const count = raw.split("\n").filter((l) => l.trim().length > 0).length;
431
+ return {
432
+ available: count > 0,
433
+ unit: "procs",
434
+ value: count
435
+ };
436
+ }
437
+ return {
438
+ available: false,
439
+ unit: "procs"
440
+ };
441
+ }
442
+ async function probeCpuBoost() {
443
+ if (process.platform !== "linux") return {
444
+ available: false,
445
+ unit: ""
446
+ };
447
+ try {
448
+ if (existsSync("/sys/devices/system/cpu/cpufreq/boost")) {
449
+ const raw = await readFile("/sys/devices/system/cpu/cpufreq/boost", "utf8");
450
+ const n = Number.parseInt(raw.trim(), 10);
451
+ if (n === 0 || n === 1) return {
452
+ available: true,
453
+ unit: "",
454
+ value: n
455
+ };
456
+ }
457
+ if (existsSync("/sys/devices/system/cpu/intel_pstate/no_turbo")) {
458
+ const raw = await readFile("/sys/devices/system/cpu/intel_pstate/no_turbo", "utf8");
459
+ const n = Number.parseInt(raw.trim(), 10);
460
+ if (n === 0 || n === 1) return {
461
+ available: true,
462
+ unit: "",
463
+ value: n === 0 ? 1 : 0
464
+ };
465
+ }
466
+ } catch {}
467
+ return {
468
+ available: false,
469
+ unit: ""
470
+ };
471
+ }
472
+ const CPU_HWMON_NAME_PATTERNS = [
473
+ /^k10temp$/i,
474
+ /^zenpower$/i,
475
+ /^coretemp$/i,
476
+ /^cpu\b/i,
477
+ /^acpitz$/i
478
+ ];
479
+ async function probeCpuVoltages() {
480
+ if (process.platform !== "linux") return {
481
+ available: false,
482
+ unit: "V"
483
+ };
484
+ if (!existsSync("/sys/class/hwmon")) return {
485
+ available: false,
486
+ unit: "V"
487
+ };
488
+ try {
489
+ const entries = await readdir("/sys/class/hwmon");
490
+ const hwmons = [];
491
+ for (const e of entries) {
492
+ const namePath = `/sys/class/hwmon/${e}/name`;
493
+ if (!existsSync(namePath)) continue;
494
+ const name = (await readFile(namePath, "utf8")).trim();
495
+ hwmons.push({
496
+ dir: `/sys/class/hwmon/${e}`,
497
+ name
498
+ });
499
+ }
500
+ const matched = hwmons.map((h, i) => ({
501
+ h,
502
+ i
503
+ })).filter(({ h }) => CPU_HWMON_NAME_PATTERNS.some((re) => re.test(h.name))).sort((a, b) => a.i - b.i)[0];
504
+ if (!matched) return {
505
+ available: false,
506
+ unit: "V"
507
+ };
508
+ const in0 = `${matched.h.dir}/in0_input`;
509
+ if (!existsSync(in0)) return {
510
+ available: false,
511
+ unit: "V"
512
+ };
513
+ const raw = await readFile(in0, "utf8");
514
+ const mv = Number.parseInt(raw.trim(), 10);
515
+ if (!Number.isFinite(mv) || mv <= 0) return {
516
+ available: false,
517
+ unit: "V"
518
+ };
519
+ return {
520
+ available: true,
521
+ unit: "V",
522
+ value: Number((mv / 1e3).toFixed(2))
523
+ };
524
+ } catch {
525
+ return {
526
+ available: false,
527
+ unit: "V"
528
+ };
529
+ }
530
+ }
531
+ const DISKSTATS_SKIP_RE = /^(loop|ram|dm-|md|drbd)\d*|^(sr|nbd)\d+$|^[a-z]+\d+_|^dm-/;
532
+ let prevDiskIoSample = null;
533
+ let prevDarwinDiskIoSample = null;
534
+ async function readDiskstatsSectors() {
535
+ if (!existsSync("/proc/diskstats")) return null;
536
+ try {
537
+ const raw = await readFile("/proc/diskstats", "utf8");
538
+ let totalSectors = 0;
539
+ for (const line of raw.split("\n")) {
540
+ const parts = line.trim().split(/\s+/);
541
+ if (parts.length < 14) continue;
542
+ const name = parts[2];
543
+ if (!name || DISKSTATS_SKIP_RE.test(name)) continue;
544
+ if (/^(nvme\d+n\d+)p\d+$/.test(name)) continue;
545
+ const readSectors = Number.parseInt(parts[5] ?? "", 10);
546
+ const writtenSectors = Number.parseInt(parts[9] ?? "", 10);
547
+ if (!Number.isFinite(readSectors) || !Number.isFinite(writtenSectors)) continue;
548
+ totalSectors += readSectors + writtenSectors;
549
+ }
550
+ return totalSectors;
551
+ } catch {
552
+ return null;
553
+ }
554
+ }
555
+ const parseDarwinDiskBytes = (raw) => {
556
+ let total = 0;
557
+ let seen = false;
558
+ for (const m of raw.matchAll(/"Bytes \((?:Read|Write)\)"\s*=\s*(\d+)/g)) {
559
+ const n = Number.parseInt(m[1] ?? "", 10);
560
+ if (!Number.isFinite(n)) continue;
561
+ total += n;
562
+ seen = true;
563
+ }
564
+ return seen ? total : null;
565
+ };
566
+ async function readDarwinDiskBytes() {
567
+ const raw = await darwinRun("ioreg", [
568
+ "-c",
569
+ "IOBlockStorageDriver",
570
+ "-r",
571
+ "-d",
572
+ "1",
573
+ "-w",
574
+ "0"
575
+ ]);
576
+ return raw === null ? null : parseDarwinDiskBytes(raw);
577
+ }
578
+ async function probeDiskIo() {
579
+ if (process.platform === "darwin") {
580
+ const bytes = await readDarwinDiskBytes();
581
+ if (bytes === null) return {
582
+ available: false,
583
+ unit: "B/s"
584
+ };
585
+ const now = Date.now();
586
+ const prev = prevDarwinDiskIoSample;
587
+ if (prev === null || bytes < prev.bytes) {
588
+ prevDarwinDiskIoSample = {
589
+ ts: now,
590
+ bytes
591
+ };
592
+ return {
593
+ available: true,
594
+ unit: "B/s",
595
+ value: 0
596
+ };
597
+ }
598
+ const dtSec = Math.max(.001, (now - prev.ts) / 1e3);
599
+ const delta = bytes - prev.bytes;
600
+ prevDarwinDiskIoSample = {
601
+ ts: now,
602
+ bytes
603
+ };
604
+ return {
605
+ available: true,
606
+ unit: "B/s",
607
+ value: Math.round(delta / dtSec)
608
+ };
609
+ }
610
+ if (process.platform !== "linux") return {
611
+ available: false,
612
+ unit: "B/s"
613
+ };
614
+ const sectors = await readDiskstatsSectors();
615
+ if (sectors === null) return {
616
+ available: false,
617
+ unit: "B/s"
618
+ };
619
+ const now = Date.now();
620
+ const prev = prevDiskIoSample;
621
+ if (prev === null || sectors < prev.sectors) {
622
+ prevDiskIoSample = {
623
+ ts: now,
624
+ sectors
625
+ };
626
+ return {
627
+ available: true,
628
+ unit: "B/s",
629
+ value: 0
630
+ };
631
+ }
632
+ const dtSec = Math.max(.001, (now - prev.ts) / 1e3);
633
+ const dSectors = sectors - prev.sectors;
634
+ prevDiskIoSample = {
635
+ ts: now,
636
+ sectors
637
+ };
638
+ return {
639
+ available: true,
640
+ unit: "B/s",
641
+ value: Math.round(dSectors * 512 / dtSec)
642
+ };
643
+ }
644
+ async function probeFanRpm() {
645
+ if (process.platform === "darwin") {
646
+ const value = (await readMacmon())?.fanRpm ?? null;
647
+ return value === null ? {
648
+ available: false,
649
+ unit: "RPM"
650
+ } : {
651
+ available: true,
652
+ value,
653
+ unit: "RPM"
654
+ };
655
+ }
656
+ if (process.platform !== "linux") return {
657
+ available: false,
658
+ unit: "RPM"
659
+ };
660
+ if (!existsSync("/sys/class/hwmon")) return {
661
+ available: false,
662
+ unit: "RPM"
663
+ };
664
+ try {
665
+ const entries = await readdir("/sys/class/hwmon");
666
+ for (const e of entries) {
667
+ const dir = `/sys/class/hwmon/${e}`;
668
+ const fans = (await readdir(dir).catch(() => [])).filter((f) => /^fan\d+_input$/.test(f)).sort();
669
+ for (const f of fans) {
670
+ const raw = await readFile(`${dir}/${f}`, "utf8").catch(() => "");
671
+ const rpm = Number.parseInt(raw.trim(), 10);
672
+ if (Number.isFinite(rpm) && rpm > 0) return {
673
+ available: true,
674
+ unit: "RPM",
675
+ value: rpm
676
+ };
677
+ }
678
+ }
679
+ return {
680
+ available: false,
681
+ unit: "RPM"
682
+ };
683
+ } catch {
684
+ return {
685
+ available: false,
686
+ unit: "RPM"
687
+ };
688
+ }
689
+ }
690
+ async function readAmdGpuBusyPercent() {
691
+ if (!existsSync("/sys/class/drm")) return null;
692
+ const cards = (await readdir("/sys/class/drm").catch(() => [])).filter((c) => /^card\d+$/.test(c));
693
+ for (const card of cards) {
694
+ const p = `/sys/class/drm/${card}/device/gpu_busy_percent`;
695
+ if (!existsSync(p)) continue;
696
+ const raw = await readFile(p, "utf8").catch(() => "");
697
+ const pct = Number.parseInt(raw.trim(), 10);
698
+ if (Number.isFinite(pct) && pct >= 0 && pct <= 100) return pct;
699
+ }
700
+ return null;
701
+ }
702
+ async function readAmdGpuTempMilliC() {
703
+ if (!existsSync("/sys/class/drm")) return null;
704
+ const cards = (await readdir("/sys/class/drm").catch(() => [])).filter((c) => /^card\d+$/.test(c));
705
+ for (const card of cards) {
706
+ const devDir = `/sys/class/drm/${card}/device`;
707
+ if (!existsSync(devDir)) continue;
708
+ const hwmons = (await readdir(devDir).catch(() => [])).filter((e) => /^hwmon\d+$/.test(e));
709
+ for (const h of hwmons) {
710
+ const t = `${devDir}/${h}/temp1_input`;
711
+ if (!existsSync(t)) continue;
712
+ const raw = await readFile(t, "utf8").catch(() => "");
713
+ const milli = Number.parseInt(raw.trim(), 10);
714
+ if (Number.isFinite(milli) && milli > 0) return milli;
715
+ }
716
+ }
717
+ return null;
718
+ }
719
+ async function tryNvidiaSmi(field) {
720
+ try {
721
+ const { stdout } = await execFile$1("nvidia-smi", [`--query-gpu=${field}`, "--format=csv,noheader,nounits"], {
722
+ timeout: 1500,
723
+ signal: AbortSignal.timeout(1500)
724
+ });
725
+ const first = stdout.trim().split("\n")[0]?.trim() ?? "";
726
+ const n = Number.parseInt(first, 10);
727
+ return Number.isFinite(n) ? n : null;
728
+ } catch {
729
+ return null;
730
+ }
731
+ }
732
+ const parseDarwinGpuUtilization = (raw) => {
733
+ const m = /"Device Utilization %"\s*=\s*(\d+)/.exec(raw);
734
+ if (m?.[1] === void 0) return null;
735
+ const pct = Number.parseInt(m[1], 10);
736
+ return Number.isFinite(pct) ? pct : null;
737
+ };
738
+ async function probeGpuUsage() {
739
+ if (process.platform === "darwin") {
740
+ const raw = await darwinRun("ioreg", [
741
+ "-r",
742
+ "-d",
743
+ "1",
744
+ "-w",
745
+ "0",
746
+ "-c",
747
+ "IOAccelerator"
748
+ ]);
749
+ const pct = raw === null ? null : parseDarwinGpuUtilization(raw);
750
+ if (pct === null) return {
751
+ available: false,
752
+ unit: "%"
753
+ };
754
+ const clamped = clampPercent(pct);
755
+ return {
756
+ available: true,
757
+ max: 100,
758
+ percentage: clamped,
759
+ unit: "%",
760
+ value: clamped
761
+ };
762
+ }
763
+ if (process.platform !== "linux") return {
764
+ available: false,
765
+ unit: "%"
766
+ };
767
+ const pct = await readAmdGpuBusyPercent();
768
+ if (pct !== null) return {
769
+ available: true,
770
+ max: 100,
771
+ percentage: clampPercent(pct),
772
+ unit: "%",
773
+ value: clampPercent(pct)
774
+ };
775
+ const nvidia = await tryNvidiaSmi("utilization.gpu");
776
+ if (nvidia !== null) return {
777
+ available: true,
778
+ max: 100,
779
+ percentage: clampPercent(nvidia),
780
+ unit: "%",
781
+ value: clampPercent(nvidia)
782
+ };
783
+ return {
784
+ available: false,
785
+ unit: "%"
786
+ };
787
+ }
788
+ async function probeGpuTemp() {
789
+ if (process.platform === "darwin") {
790
+ const value = (await readMacmon())?.gpuTemp ?? null;
791
+ return value === null ? {
792
+ available: false,
793
+ unit: "°C"
794
+ } : {
795
+ available: true,
796
+ value,
797
+ unit: "°C"
798
+ };
799
+ }
800
+ if (process.platform !== "linux") return {
801
+ available: false,
802
+ unit: "°C"
803
+ };
804
+ const milli = await readAmdGpuTempMilliC();
805
+ if (milli !== null) return {
806
+ available: true,
807
+ unit: "°C",
808
+ value: Math.round(milli / 1e3)
809
+ };
810
+ const nvidia = await tryNvidiaSmi("temperature.gpu");
811
+ if (nvidia !== null && nvidia > 0) return {
812
+ available: true,
813
+ unit: "°C",
814
+ value: nvidia
815
+ };
816
+ return {
817
+ available: false,
818
+ unit: "°C"
819
+ };
820
+ }
821
+ const NETWORK_IFACE_SKIP_RE = /^(lo|docker|veth|br-|tun|tap|virbr|vmnet|vboxnet|awdl|llw|bridge|ipv6tnl|sit|gre|ip6gre|ipip|gretap|erspan)/;
822
+ let prevNetworkSample = null;
823
+ async function readNetBytes(field) {
824
+ if (!existsSync("/sys/class/net")) return null;
825
+ const ifaces = await readdir("/sys/class/net").catch(() => []);
826
+ let total = 0;
827
+ let counted = false;
828
+ for (const iface of ifaces) {
829
+ if (NETWORK_IFACE_SKIP_RE.test(iface)) continue;
830
+ const p = `/sys/class/net/${iface}/statistics/${field}`;
831
+ if (!existsSync(p)) continue;
832
+ const raw = await readFile(p, "utf8").catch(() => "");
833
+ const n = Number.parseInt(raw.trim(), 10);
834
+ if (Number.isFinite(n) && n >= 0) {
835
+ total += n;
836
+ counted = true;
837
+ }
838
+ }
839
+ return counted ? total : null;
840
+ }
841
+ async function readNetTotals() {
842
+ if (process.platform === "darwin") {
843
+ const raw = await darwinRun("netstat", ["-ibn"]);
844
+ return raw === null ? null : parseDarwinNetstat(raw);
845
+ }
846
+ const rx = await readNetBytes("rx_bytes");
847
+ const tx = await readNetBytes("tx_bytes");
848
+ if (rx === null || tx === null) return null;
849
+ return {
850
+ rx,
851
+ tx
852
+ };
853
+ }
854
+ async function readNetworkDelta() {
855
+ const totals = await readNetTotals();
856
+ if (totals === null) return null;
857
+ const { rx, tx } = totals;
858
+ const now = Date.now();
859
+ const prev = prevNetworkSample;
860
+ if (prev === null || rx < prev.rx || tx < prev.tx) {
861
+ prevNetworkSample = {
862
+ ts: now,
863
+ rx,
864
+ tx
865
+ };
866
+ return null;
867
+ }
868
+ const dt = Math.max(.001, (now - prev.ts) / 1e3);
869
+ prevNetworkSample = {
870
+ ts: now,
871
+ rx,
872
+ tx
873
+ };
874
+ return {
875
+ dt,
876
+ rx: rx - prev.rx,
877
+ tx: tx - prev.tx
878
+ };
879
+ }
880
+ const NETWORK_DELTA_SHARE_MS = 250;
881
+ let sharedNetworkDelta = null;
882
+ let inFlightNetworkDelta = null;
883
+ async function readSharedNetworkDelta() {
884
+ if (sharedNetworkDelta !== null && Date.now() - sharedNetworkDelta.at < NETWORK_DELTA_SHARE_MS) return sharedNetworkDelta.value;
885
+ if (inFlightNetworkDelta !== null) return inFlightNetworkDelta;
886
+ inFlightNetworkDelta = (async () => {
887
+ try {
888
+ const value = await readNetworkDelta();
889
+ sharedNetworkDelta = {
890
+ at: Date.now(),
891
+ value
892
+ };
893
+ return value;
894
+ } finally {
895
+ inFlightNetworkDelta = null;
896
+ }
897
+ })();
898
+ return inFlightNetworkDelta;
899
+ }
900
+ async function probeNetworkRead() {
901
+ if (process.platform !== "linux" && process.platform !== "darwin") return {
902
+ available: false,
903
+ unit: "B/s"
904
+ };
905
+ const delta = await readSharedNetworkDelta();
906
+ if (delta === null) return {
907
+ available: false,
908
+ unit: "B/s"
909
+ };
910
+ return {
911
+ available: true,
912
+ unit: "B/s",
913
+ value: Math.round(delta.rx / delta.dt)
914
+ };
915
+ }
916
+ async function probeNetworkWrite() {
917
+ if (process.platform !== "linux" && process.platform !== "darwin") return {
918
+ available: false,
919
+ unit: "B/s"
920
+ };
921
+ const delta = await readSharedNetworkDelta();
922
+ if (delta === null) return {
923
+ available: false,
924
+ unit: "B/s"
925
+ };
926
+ return {
927
+ available: true,
928
+ unit: "B/s",
929
+ value: Math.round(delta.tx / delta.dt)
930
+ };
931
+ }
932
+ const PROBES = {
933
+ cpu: probeCpu,
934
+ ram: probeRam,
935
+ swap: probeSwap,
936
+ disk: probeDisk,
937
+ network: probeNetwork,
938
+ battery: probeBattery,
939
+ temperature: probeTemperature,
940
+ uptime: probeUptime,
941
+ frequency: probeFrequency,
942
+ load: probeLoad,
943
+ processes: probeProcesses,
944
+ "cpu-boost": probeCpuBoost,
945
+ "cpu-voltages": probeCpuVoltages,
946
+ "disk-io": probeDiskIo,
947
+ "fan-rpm": probeFanRpm,
948
+ "gpu-temp": probeGpuTemp,
949
+ "gpu-usage": probeGpuUsage,
950
+ "network-read": probeNetworkRead,
951
+ "network-write": probeNetworkWrite
952
+ };
953
+ async function probeMetric(id) {
954
+ const probe = PROBES[id];
955
+ try {
956
+ const r = await probe();
957
+ return {
958
+ available: r.available,
959
+ id,
960
+ label: id,
961
+ ...r.max !== void 0 ? { max: r.max } : {},
962
+ ...r.percentage !== void 0 ? { percentage: r.percentage } : {},
963
+ ...r.unit !== void 0 ? { unit: r.unit } : {},
964
+ ...r.value !== void 0 ? { value: r.value } : {}
965
+ };
966
+ } catch {
967
+ return {
968
+ available: false,
969
+ id,
970
+ label: id
971
+ };
972
+ }
973
+ }
974
+ //#endregion
975
+ export { probeMetric };