-
Notifications
You must be signed in to change notification settings - Fork 1
/
NeXonWebMonitor.js
64 lines (53 loc) · 2.02 KB
/
NeXonWebMonitor.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
import express from "express";
import axios from "axios";
import http from "http";
import { Server } from "socket.io";
import { promises as fs } from "fs";
import dotenv from "dotenv";
dotenv.config();
const app = express();
const server = http.createServer(app);
const io = new Server(server, {
cors: { origin: "*", methods: ["GET", "POST"] }
});
const port = process.env.PORT || 3000;
const checkInterval = process.env.CHECK_EVERY_SECOND || 10000;
async function loadConfig() {
const configData = await fs.readFile("./config.json", "utf-8");
return JSON.parse(configData);
}
async function pingNode(domain, port, protocol = "http", retries = 2, timeout = 5000) {
const url = `${protocol}://${domain}:${port}`;
try {
const response = await axios.get(url, { timeout, validateStatus: false });
if (response.status >= 200 && response.status < 300) {
return { status: "up", details: "Site is operational" };
} else {
return { status: "down", details: `Error: ${response.status} ${response.statusText}` };
}
} catch (error) {
if (retries > 0) return pingNode(domain, port, protocol, retries - 1, timeout);
return { status: "down", details: error.message || "Unknown error" };
}
}
async function checkSiteStatus(config) {
const statuses = await Promise.all(
config.Sites.map(async (site) => {
const { status, details } = await pingNode(site.domain, site.port);
return { name: site.name, status, details };
})
);
const incidents = statuses.filter(site => site.status === "down");
io.emit("statusUpdate", statuses);
io.emit("incidentUpdate", incidents);
}
app.use(express.static("public"));
let config = { Sites: [] };
async function initServer() {
config = await loadConfig();
checkSiteStatus(config);
setInterval(() => checkSiteStatus(config), checkInterval);
server.listen(port, () => console.log(`Server running on http://localhost:${port}`));
}
io.on("connection", () => checkSiteStatus(config));
initServer();