feat(system-info): include disk usage in health output

This commit is contained in:
William Valentin
2026-02-18 18:04:35 -08:00
parent cdba111831
commit 9de52b7d93
3 changed files with 43 additions and 0 deletions
+1
View File
@@ -42,6 +42,7 @@ describe('system.info tool', () => {
'Node.js:',
'Uptime:',
'Memory Total:',
'Disk',
];
for (const field of expectedFields) {
+30
View File
@@ -1,4 +1,5 @@
import os from 'os';
import { statfsSync } from 'fs';
import type { Tool, ToolResult } from '../types.js';
function formatUptime(seconds: number): string {
@@ -12,6 +13,34 @@ function formatBytes(bytes: number): string {
return (bytes / (1024 * 1024 * 1024)).toFixed(2) + ' GB';
}
function getDiskUsageSummary(): string[] {
try {
const rootPath = process.platform === 'win32' ? process.cwd().slice(0, 3) : '/';
const stat = statfsSync(rootPath);
const blockSize = typeof stat.bsize === 'number' ? stat.bsize : Number(stat.bsize);
const totalBlocks = typeof stat.blocks === 'number' ? stat.blocks : Number(stat.blocks);
const freeBlocks = typeof stat.bavail === 'number' ? stat.bavail : Number(stat.bavail);
if (!Number.isFinite(blockSize) || !Number.isFinite(totalBlocks) || !Number.isFinite(freeBlocks)) {
return ['Disk: unavailable'];
}
const totalBytes = totalBlocks * blockSize;
const freeBytes = freeBlocks * blockSize;
const usedBytes = Math.max(0, totalBytes - freeBytes);
const usedPct = totalBytes > 0 ? (usedBytes / totalBytes) * 100 : 0;
return [
`Disk Path: ${rootPath}`,
`Disk Total: ${formatBytes(totalBytes)}`,
`Disk Free: ${formatBytes(freeBytes)}`,
`Disk Used: ${formatBytes(usedBytes)} (${usedPct.toFixed(1)}%)`,
];
} catch {
return ['Disk: unavailable'];
}
}
export const systemInfoTool: Tool = {
name: 'system.info',
description: 'Get current system information including date, time, hostname, OS, platform, architecture, uptime, Node.js version, and memory usage.',
@@ -52,6 +81,7 @@ export const systemInfoTool: Tool = {
`Node.js: ${process.version}`,
`Memory Total: ${formatBytes(totalMem)}`,
`Memory Free: ${formatBytes(freeMem)}`,
...getDiskUsageSummary(),
`Working Dir: ${process.cwd()}`,
];