-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
129 lines (112 loc) · 3.33 KB
/
server.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import express from "express";
import dotenv from "dotenv";
import cors from "cors";
import compression from "compression";
import { readFile } from "fs/promises";
import { dirSize, execShellCommand } from "./lib/helper.js";
const { version } = JSON.parse(await readFile("./package.json"));
const config = dotenv.config().parsed;
const app = express();
const port = config.SERVER_PORT;
const logfile = config.LOGFILE;
const nodeBin = config.NODE_BIN;
console.log(new Date(), "Welcome to IAG Node Monitor", version);
console.log(new Date(), "Starting up...");
if (!nodeBin) {
console.error("Node binary not defined");
process.exit(1);
} else {
console.log(new Date(), "Node binary:", nodeBin);
}
if (!port) {
console.error("Server port not defined");
process.exit(1);
}
if (!logfile) {
console.error("Logfile location not defined");
process.exit(1);
} else {
console.log(new Date(), "Logfile location:", logfile);
}
app.use(cors());
app.use(compression());
app.use(express.static("public"));
// LOGFILE
app.get("/api/logfile", async (req, res) => {
let data = await readFile(logfile, "utf8");
let log = data.split("\n").map((el) => decodeURIComponent(el));
let json = [];
for (let l of log) {
if (l) {
let obj = JSON.parse(l);
if (obj.message.includes("=>")) {
obj.text = JSON.parse(
decodeURIComponent(obj.message.split("=>")[1]?.trim())
);
obj.message = obj.message.split("=>")[0].trim();
}
// EXCLUDE PING
if (obj.level == "info" && obj.message.includes("ping success")) continue;
if (obj.level == "info" && obj.message.includes("requesting heartbeat"))
continue;
json.push(obj);
}
}
res.json(json);
});
// STATS
app.get("/api/stats", async (req, res) => {
let json = {};
const nodeVersion = await execShellCommand(nodeBin + " --version");
const info = await execShellCommand(nodeBin + " get:info");
for (let line of info.split("\n")) {
if (line.includes("=>")) {
let [key, value] = line.split(":");
json[
key
.replace("=>", "")
.replace(/[^a-z0-9\s]/gi, "")
.replace(/ +/g, "")
.trim()
] = value.trim();
}
}
json.monitorVersion = version;
json.nodeVersion = nodeVersion;
res.json(json);
});
// STATUS
app.get("/api/status", async (req, res) => {
let json = {};
const info = await execShellCommand(nodeBin + " get:status");
json.up = info.includes("up and running");
json.timestamp = new Date().toISOString();
res.json(json);
});
// UPDATE
app.get("/api/update", async (req, res) => {
const data = await execShellCommand("tail -n 20 " + logfile);
let log = data.split("\n").map((el) => decodeURIComponent(el));
let json = [];
for (let l of log) {
if (l) {
let obj = JSON.parse(l);
if (obj.message.includes("=>")) {
obj.text = JSON.parse(
decodeURIComponent(obj.message.split("=>")[1]?.trim())
);
obj.message = obj.message.split("=>")[0].trim();
}
// EXCLUDE PING
if (obj.level == "info" && obj.message.includes("ping success")) continue;
if (obj.level == "info" && obj.message.includes("requesting heartbeat"))
continue;
json.push(obj);
}
}
res.json(json);
});
// STARTING SERVER
app.listen(port, () => {
console.log(new Date(), "Server started on port", port);
});