-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
resources.js
63 lines (52 loc) · 1.42 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
55
56
57
58
59
60
61
62
63
import { existsSync } from "fs";
import createLogger from "utils/logger";
const logger = createLogger("resources");
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();
logger.debug("fsSize:", JSON.stringify(fsSize));
return res.status(200).json({
drive: fsSize.find((fs) => fs.mount === target) ?? fsSize.find((fs) => fs.mount === "/"),
});
}
if (type === "memory") {
const memory = await si.mem();
logger.debug("memory:", JSON.stringify(memory));
return res.status(200).json({
memory,
});
}
if (type === "cputemp") {
const cputemp = await si.cpuTemperature();
logger.debug("cputemp:", JSON.stringify(cputemp));
return res.status(200).json({
cputemp,
});
}
if (type === "uptime") {
const timeData = await si.time();
logger.debug("timeData:", JSON.stringify(timeData));
return res.status(200).json({
uptime: timeData.uptime,
});
}
return res.status(400).json({
error: "invalid type",
});
}