/
resources.js
54 lines (44 loc) · 1.09 KB
/
resources.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { existsSync } from "fs";
const si = require("systeminformation");
export default async function handler(req, res) {
const { type, target } = req.query;
if (type === "cpu") {
const load = await si.currentLoad();
return res.status(200).json({
cpu: {
usage: load.currentLoad,
load: load.avgLoad,
},
});
}
if (type === "disk") {
if (!existsSync(target)) {
return res.status(404).json({
error: "Target not found",
});
}
const fsSize = await si.fsSize();
return res.status(200).json({
drive: fsSize.find((fs) => fs.mount === target) ?? fsSize.find((fs) => fs.mount === "/"),
});
}
if (type === "memory") {
return res.status(200).json({
memory: await si.mem(),
});
}
if (type === "cputemp") {
return res.status(200).json({
cputemp: await si.cpuTemperature(),
});
}
if (type === "uptime") {
const timeData = await si.time();
return res.status(200).json({
uptime: timeData.uptime,
});
}
return res.status(400).json({
error: "invalid type",
});
}