Files
Netcatty/electron/bridges/sshBridge/monitorDiskIo.cjs
T
27f26d8e69 feat(monitor): 优化磁盘IO展示与系统分区告警 [4030] (#4031)
* feat(monitor): 优化磁盘IO展示与系统分区告警 [4030]

* fix(monitor): 修复磁盘监控评审问题 [4030]

* fix(monitor): align Partitions bars with Memory; hide bare / label

Label-above-bar for partition list; single-root `/` hides the label and
centers the bar like Memory/CPU (unchanged). Multi-disk keeps top align +
system alerts. Disk IO adds a spacer row to track usage labels.

Review crops under .github/review/ for PR discussion (drop before merge OK).

* fix(monitor): keep bare-/ align; multi-disk labels stay left

Address Chris density feedback: keep the single root label hidden and vertically centered with Memory, and restore side-by-side labels for multiple disks. Remove the Disk I/O spacer and update the review crops.

* chore(monitor): drop PR #4031 discussion review crops

---------

Co-authored-by: wendeng.dong <wendeng.dong@sailuntire.com>
Co-authored-by: 陈大猫 <16399091+binaricat@users.noreply.github.com>
2026-10-27 23:54:58 -07:00

66 lines
3.6 KiB
JavaScript

"use strict";
// Read whole hardware-backed block devices only. Partitions live below these
// entries; dm/md/loop/zram layers have no device link and would double-count I/O.
// Linux block-stat sectors are always 512 bytes, regardless of device sector size.
// No iostat dependency, extra exec, sampling sleep, or privileged access is needed.
const DISK_IO_ASSIGNMENT = String.raw`diskio=$(if [ -r /proc/uptime ]; then awk '{printf "%s;",$1}' /proc/uptime; for d in /sys/block/*; do [ -e "$d/device" ] && [ -r "$d/stat" ] || continue; awk -v path="$d" 'NF>=7 {n=split(path,a,"/");printf "%s:%s:%s,",a[n],$3,$7}' "$d/stat"; done; fi)`;
// Match mounts by kernel device number: aliases, LVM and bind mounts need no
// /dev name guessing. Keep these counters separate from the physical total.
const PARTITION_IO_ASSIGNMENT = String.raw`partitionio=$(if [ -r /proc/uptime ]; then awk 'NR==1 {printf "%s;",$1;next} NF>=7 {n=split(FILENAME,a,"/");gsub(/:/,"-",a[n-1]);printf "%s:%s:%s,",a[n-1],$3,$7}' /proc/uptime /sys/dev/block/*/stat 2>/dev/null; fi); diskmounts=$(awk '{gsub(/:/,"-",$3);printf "%s:%s,",$5,$3}' /proc/self/mountinfo 2>/dev/null)`;
function withPartitionIo(disks, mounts, current, previous) {
const devices = new Map();
for (const entry of (mounts || "").split(",")) {
const separator = entry.lastIndexOf(":");
if (separator < 0) continue;
const mount = entry.slice(0, separator).replace(/\\([0-7]{3})/g, (_, octal) => String.fromCharCode(parseInt(octal, 8)));
devices.set(mount, entry.slice(separator + 1));
}
return disks.map(disk => {
const name = devices.get(disk.mountPoint);
const sample = snapshot => snapshot?.devices.has(name)
? { timestamp: snapshot.timestamp, devices: new Map([[name, snapshot.devices.get(name)]]) }
: null;
const rates = diskIoRates(sample(current), sample(previous));
return rates.diskReadSpeed == null ? disk : { ...disk, ...rates };
});
}
function parseDiskIo(text) {
if (typeof text !== "string") return null;
const [time, counters, ...extra] = text.trim().split(";");
const timestamp = Number(time);
if (!time || extra.length || !Number.isFinite(timestamp) || timestamp < 0 || !counters) return null;
const devices = new Map();
for (const entry of counters.replace(/,$/, "").split(",")) {
const fields = entry.split(":");
const [name, read, write] = fields;
if (fields.length !== 3 || !/^[a-zA-Z0-9_.!-]+$/.test(name) || devices.has(name) || !/^\d+$/.test(read) || !/^\d+$/.test(write)) return null;
const readSectors = Number(read);
const writeSectors = Number(write);
if (!Number.isSafeInteger(readSectors) || !Number.isSafeInteger(writeSectors)) return null;
devices.set(name, { readSectors, writeSectors });
}
return { timestamp, devices };
}
function diskIoRates(current, previous) {
const unavailable = { diskReadSpeed: null, diskWriteSpeed: null };
if (!current || !previous || current.devices.size !== previous.devices.size) return unavailable;
const seconds = current.timestamp - previous.timestamp;
if (!(seconds > 0)) return unavailable;
let read = 0;
let write = 0;
for (const [name, counters] of current.devices) {
const before = previous.devices.get(name);
if (!before || counters.readSectors < before.readSectors || counters.writeSectors < before.writeSectors) return unavailable;
read += (counters.readSectors - before.readSectors) * 512;
write += (counters.writeSectors - before.writeSectors) * 512;
}
return { diskReadSpeed: read / seconds, diskWriteSpeed: write / seconds };
}
module.exports = { DISK_IO_ASSIGNMENT, PARTITION_IO_ASSIGNMENT, parseDiskIo, diskIoRates, withPartitionIo };