Files
Termix/src/backend/ssh/widgets/uptime-collector.ts
T

32 lines
940 B
TypeScript
Raw Normal View History

2025-11-17 09:46:05 -06:00
import type { Client } from "ssh2";
import { execCommand } from "./common-utils.js";
export async function collectUptimeMetrics(client: Client): Promise<{
seconds: number | null;
formatted: string | null;
}> {
let uptimeSeconds: number | null = null;
let uptimeFormatted: string | null = null;
try {
const uptimeOut = await execCommand(client, "cat /proc/uptime");
const uptimeParts = uptimeOut.stdout.trim().split(/\s+/);
if (uptimeParts.length >= 1) {
uptimeSeconds = Number(uptimeParts[0]);
if (Number.isFinite(uptimeSeconds)) {
const days = Math.floor(uptimeSeconds / 86400);
const hours = Math.floor((uptimeSeconds % 86400) / 3600);
const minutes = Math.floor((uptimeSeconds % 3600) / 60);
uptimeFormatted = `${days}d ${hours}h ${minutes}m`;
}
}
+5
2026-03-08 18:02:14 -05:00
} catch {
// expected
}
2025-11-17 09:46:05 -06:00
return {
seconds: uptimeSeconds,
formatted: uptimeFormatted,
};
}