Migrated to local rust api
This commit is contained in:
parent
4bfb87448f
commit
db20819ad2
3 changed files with 10 additions and 208 deletions
|
|
@ -1,205 +1,7 @@
|
||||||
import { exec } from "child_process";
|
import { NextResponse } from 'next/server';
|
||||||
import { promisify } from "util";
|
|
||||||
import fs from "fs/promises";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
|
|
||||||
const execAsync = promisify(exec);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper to read files asynchronously
|
|
||||||
*/
|
|
||||||
async function readFile(path: string): Promise<string | null> {
|
|
||||||
try {
|
|
||||||
const data = await fs.readFile(path, "utf8");
|
|
||||||
return data.trim();
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper to run shell commands asynchronously
|
|
||||||
*/
|
|
||||||
async function runCommand(cmd: string): Promise<string | null> {
|
|
||||||
try {
|
|
||||||
const { stdout } = await execAsync(cmd, { timeout: 3000 });
|
|
||||||
return stdout.trim();
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getMemory() {
|
|
||||||
const raw = await readFile("/proc/meminfo");
|
|
||||||
if (!raw) return null;
|
|
||||||
const lines: Record<string, number> = {};
|
|
||||||
for (const line of raw.split("\n")) {
|
|
||||||
const [key, val] = line.split(":");
|
|
||||||
if (key && val) {
|
|
||||||
lines[key.trim()] = parseInt(val.trim());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const total = lines["MemTotal"] ?? 0;
|
|
||||||
const available = lines["MemAvailable"] ?? 0;
|
|
||||||
const used = total - available;
|
|
||||||
return {
|
|
||||||
total: Math.round(total / 1024),
|
|
||||||
used: Math.round(used / 1024),
|
|
||||||
available: Math.round(available / 1024),
|
|
||||||
percent: Math.round((used / total) * 100),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getCpu() {
|
|
||||||
const sample = async () => {
|
|
||||||
const raw = await readFile("/proc/stat");
|
|
||||||
if (!raw) return null;
|
|
||||||
const line = raw.split("\n")[0];
|
|
||||||
const parts = line.split(/\s+/).slice(1).map(Number);
|
|
||||||
const idle = parts[3];
|
|
||||||
const total = parts.reduce((a, b) => a + b, 0);
|
|
||||||
return { idle, total };
|
|
||||||
};
|
|
||||||
|
|
||||||
const s1 = await sample();
|
|
||||||
// Non-blocking delay (200ms) to calculate CPU delta
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
||||||
const s2 = await sample();
|
|
||||||
|
|
||||||
if (!s1 || !s2) return null;
|
|
||||||
|
|
||||||
const idleDiff = s2.idle - s1.idle;
|
|
||||||
const totalDiff = s2.total - s1.total;
|
|
||||||
const percent = Math.round((1 - idleDiff / totalDiff) * 100);
|
|
||||||
|
|
||||||
// Parse CPU Info without spawning extra shell processes
|
|
||||||
const cpuInfo = (await readFile("/proc/cpuinfo")) || "";
|
|
||||||
const model = cpuInfo.match(/model name\s+:\s+(.*)/)?.[1] || "Unknown";
|
|
||||||
const cores = cpuInfo.split("\n").filter((l) => l.startsWith("processor")).length;
|
|
||||||
|
|
||||||
return {
|
|
||||||
percent,
|
|
||||||
model: model.trim(),
|
|
||||||
cores,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getTemperature() {
|
|
||||||
const paths = [
|
|
||||||
"/sys/class/thermal/thermal_zone0/temp",
|
|
||||||
"/sys/class/thermal/thermal_zone1/temp",
|
|
||||||
"/sys/class/hwmon/hwmon0/temp1_input",
|
|
||||||
];
|
|
||||||
for (const path of paths) {
|
|
||||||
const raw = await readFile(path);
|
|
||||||
if (raw) return Math.round(parseInt(raw) / 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
const sensors = await runCommand("sensors 2>/dev/null | grep 'Core 0' | head -1");
|
|
||||||
if (sensors) {
|
|
||||||
const match = sensors.match(/\+(\d+\.\d+)/);
|
|
||||||
if (match) return parseFloat(match[1]);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getDisk() {
|
|
||||||
const raw = await runCommand("df -B1 /");
|
|
||||||
if (!raw) return null;
|
|
||||||
const lines = raw.split("\n");
|
|
||||||
const parts = lines[1].split(/\s+/);
|
|
||||||
const total = parseInt(parts[1]);
|
|
||||||
const used = parseInt(parts[2]);
|
|
||||||
const available = parseInt(parts[3]);
|
|
||||||
return {
|
|
||||||
total: Math.round(total / 1024 / 1024 / 1024),
|
|
||||||
used: Math.round(used / 1024 / 1024 / 1024),
|
|
||||||
available: Math.round(available / 1024 / 1024 / 1024),
|
|
||||||
percent: Math.round((used / total) * 100),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getUptime() {
|
|
||||||
const raw = await readFile("/proc/uptime");
|
|
||||||
if (!raw) return null;
|
|
||||||
const seconds = parseFloat(raw.split(" ")[0]);
|
|
||||||
const days = Math.floor(seconds / 86400);
|
|
||||||
const hours = Math.floor((seconds % 86400) / 3600);
|
|
||||||
const minutes = Math.floor((seconds % 3600) / 60);
|
|
||||||
return { seconds, days, hours, minutes };
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getNetwork() {
|
|
||||||
const raw = await readFile("/proc/net/dev");
|
|
||||||
if (!raw) return null;
|
|
||||||
const ifaces: Record<string, { rx: number; tx: number }> = {};
|
|
||||||
for (const line of raw.split("\n").slice(2)) {
|
|
||||||
const parts = line.trim().split(/\s+/);
|
|
||||||
if (parts.length < 10) continue;
|
|
||||||
const name = parts[0].replace(":", "");
|
|
||||||
if (name === "lo") continue;
|
|
||||||
ifaces[name] = {
|
|
||||||
rx: parseInt(parts[1]),
|
|
||||||
tx: parseInt(parts[9]),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return ifaces;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getServices() {
|
|
||||||
const services = ["caddy", "syncthing", "sshd", "cloudflare-dyndns"];
|
|
||||||
const result: Record<string, string> = {};
|
|
||||||
|
|
||||||
// Run all status checks in parallel
|
|
||||||
await Promise.all(
|
|
||||||
services.map(async (svc) => {
|
|
||||||
const status = await runCommand(`systemctl is-active ${svc}`);
|
|
||||||
result[svc] = status ?? "unknown";
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getLoadAverage() {
|
|
||||||
const raw = await readFile("/proc/loadavg");
|
|
||||||
if (!raw) return null;
|
|
||||||
const parts = raw.split(" ");
|
|
||||||
return {
|
|
||||||
"1m": parseFloat(parts[0]),
|
|
||||||
"5m": parseFloat(parts[1]),
|
|
||||||
"15m": parseFloat(parts[2]),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
const res = await fetch('http://localhost:3001/stats');
|
||||||
// Parallelize all data gathering
|
const data = await res.json();
|
||||||
const [memory, cpu, disk, uptime, network, services, loadAvg, temperature] =
|
return NextResponse.json(data);
|
||||||
await Promise.all([
|
|
||||||
getMemory(),
|
|
||||||
getCpu(),
|
|
||||||
getDisk(),
|
|
||||||
getUptime(),
|
|
||||||
getNetwork(),
|
|
||||||
getServices(),
|
|
||||||
getLoadAverage(),
|
|
||||||
getTemperature(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
timestamp: new Date().toISOString(),
|
|
||||||
memory,
|
|
||||||
cpu,
|
|
||||||
disk,
|
|
||||||
uptime,
|
|
||||||
network,
|
|
||||||
services,
|
|
||||||
loadAvg,
|
|
||||||
temperature,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("API Error:", error);
|
|
||||||
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,22 +10,22 @@ export default function StatsGrid({ stats }: StatsGridProps) {
|
||||||
<div className="grid grid-cols-2 md:grid-cols-4 gap-3.5 mb-11">
|
<div className="grid grid-cols-2 md:grid-cols-4 gap-3.5 mb-11">
|
||||||
<StatCard
|
<StatCard
|
||||||
label="CPU"
|
label="CPU"
|
||||||
value={stats ? `${stats.cpu.percent}%` : "—"}
|
value={stats ? `${stats.cpu.percent.toFixed(1)}%` : "—"}
|
||||||
sub={stats?.cpu.model.replace(/\(R\)/g, "").replace(/\(TM\)/g, "").trim()}
|
sub={stats?.cpu.model.replace(/\(R\)/g, "").replace(/\(TM\)/g, "").trim()}
|
||||||
percent={stats?.cpu.percent}
|
percent={stats?.cpu.percent.toFixed(1)}
|
||||||
delay={0}
|
delay={0}
|
||||||
/>
|
/>
|
||||||
<StatCard
|
<StatCard
|
||||||
label="Memory"
|
label="Memory"
|
||||||
value={stats ? `${stats.memory.percent}%` : "—"}
|
value={stats ? `${(stats.memory.percent)}%` : "—"}
|
||||||
sub={stats ? `${stats.memory.used} MB / ${stats.memory.total} MB` : ""}
|
sub={stats ? `${(stats.memory.used / 1000).toFixed(1)} GB / ${(stats.memory.total / 1000).toFixed(1)} GB` : ""}
|
||||||
percent={stats?.memory.percent}
|
percent={stats?.memory.percent}
|
||||||
delay={60}
|
delay={60}
|
||||||
/>
|
/>
|
||||||
<StatCard
|
<StatCard
|
||||||
label="Disk"
|
label="Disk"
|
||||||
value={stats ? `${stats.disk.percent}%` : "—"}
|
value={stats ? `${stats.disk.percent}%` : "—"}
|
||||||
sub={stats ? `${stats.disk.used} GB / ${stats.disk.total} GB` : ""}
|
sub={stats ? `${(stats.disk.used / 1024).toFixed(1)} GB / ${(stats.disk.total / 1024).toFixed(0)} GB` : ""}
|
||||||
percent={stats?.disk.percent}
|
percent={stats?.disk.percent}
|
||||||
delay={120}
|
delay={120}
|
||||||
/>
|
/>
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import type { NextConfig } from "next";
|
import type { NextConfig } from "next";
|
||||||
|
|
||||||
const nextConfig: NextConfig = {
|
const nextConfig: NextConfig = {
|
||||||
allowedDevOrigins: ['dashboard.jackmechem.dev'],
|
allowedDevOrigins: ['dashboard.jackmechem.dev', 'localhost'],
|
||||||
output: 'standalone',
|
output: 'standalone',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue