Files
Homepage/src/pages/api/widgets/resources.js

39 lines
748 B
JavaScript
Raw Normal View History

import { existsSync } from "fs";
2022-09-07 16:53:24 +03:00
import { cpu, drive, mem } from "node-os-utils";
2022-08-24 10:44:35 +03:00
export default async function handler(req, res) {
2022-08-27 00:55:13 +03:00
const { type, target } = req.query;
2022-08-24 10:44:35 +03:00
2022-09-07 16:53:24 +03:00
if (type === "cpu") {
2022-08-27 00:55:13 +03:00
return res.status(200).json({
cpu: {
usage: await cpu.usage(1000),
load: cpu.loadavgTime(5),
},
});
2022-09-07 16:53:24 +03:00
}
if (type === "disk") {
if (!existsSync(target)) {
return res.status(404).json({
error: "Target not found",
});
}
2022-08-27 00:55:13 +03:00
return res.status(200).json({
drive: await drive.info(target || "/"),
});
2022-09-07 16:53:24 +03:00
}
if (type === "memory") {
2022-08-27 00:55:13 +03:00
return res.status(200).json({
memory: await mem.info(),
});
}
2022-09-07 16:53:24 +03:00
return res.status(400).json({
error: "invalid type",
});
2022-08-24 10:44:35 +03:00
}