-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (47 loc) · 1.71 KB
/
index.js
File metadata and controls
57 lines (47 loc) · 1.71 KB
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
const express = require("express");
const app = express();
const shell = require("shelljs");
const path = require("path");
// Serve the static files from the React app
app.use(express.static(path.join(__dirname, "client/build")));
// API endpoint to get the CPU and RAM usage data
app.get("/usage", (req, res) => {
const totalMem = shell.exec("awk '/^MemTotal:/{print $2}' /proc/meminfo", {
silent: true,
}).stdout;
const freeMem = shell.exec("awk '/^MemFree:/{print $2}' /proc/meminfo", {
silent: true,
}).stdout;
const usedMem = totalMem - freeMem;
const ramUsage = (usedMem / totalMem) * 100;
const cpuUsage = shell
.exec("top -bn1 | awk '/%Cpu/{print $2}' | cut -d'.' -f1", { silent: true })
.stdout.trim();
res.json({
ram: ramUsage,
cpu: cpuUsage,
});
});
// API endpoint to get system information
app.get("/system", (req, res) => {
//const cpuInfo = shell.exec("cat /proc/cpuinfo", { silent: true }).stdout;
const cpuInfo = shell.exec(" grep 'model name' /proc/cpuinfo | awk -F ': ' '{print $2}' |head -n 1", { silent: true }).stdout;
const maxClockFreq = shell
.exec("lscpu | grep 'CPU max MHz' | awk '{print $4}'", { silent: true })
.stdout.trim();
const maxAvailableRam = shell
.exec("awk '/^MemTotal/ {print $2 / 1024}' /proc/meminfo", { silent: true })
.stdout.trim();
res.json({
cpuInfo: cpuInfo,
maxClockFreq: maxClockFreq,
maxAvailableRam: maxAvailableRam,
});
});
// Handles any requests that don't match the above
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname + "/client/build/index.html"));
});
const port = process.env.PORT || 5000;
app.listen(port);
console.log(`Live RAM and CPU usage app listening on port ${port}`);