nverify-watchdog 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +114 -0
- package/package.json +15 -0
package/index.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
const { io } = require("socket.io-client");
|
|
2
|
+
const os = require("os");
|
|
3
|
+
|
|
4
|
+
class NVerifyWatchDog {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.socket = null;
|
|
7
|
+
this.serverName = "Unknown Server";
|
|
8
|
+
this.defaultUrl = "https://nverify-service-development.up.railway.app";
|
|
9
|
+
this.isConnecting = false;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} serverName - The name of THIS backend
|
|
14
|
+
* @param {string} nverifyUrl - (Optional) Override default URL
|
|
15
|
+
*/
|
|
16
|
+
connect(serverName, nverifyUrl = this.defaultUrl) {
|
|
17
|
+
if (this.socket) return; // Prevent multiple connections
|
|
18
|
+
|
|
19
|
+
this.serverName = serverName;
|
|
20
|
+
const baseUrl = nverifyUrl.replace(/\/$/, "");
|
|
21
|
+
|
|
22
|
+
console.log(`[NVerify Agent] Initializing connection to ${baseUrl}...`);
|
|
23
|
+
|
|
24
|
+
// Force WebSocket to avoid Railway/Proxy XHR polling issues
|
|
25
|
+
this.socket = io(`${baseUrl}/admin-monitor`, {
|
|
26
|
+
transports: ["websocket"],
|
|
27
|
+
reconnection: true,
|
|
28
|
+
reconnectionAttempts: Infinity,
|
|
29
|
+
reconnectionDelay: 2000,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
this.socket.on("connect", () => {
|
|
33
|
+
console.log(
|
|
34
|
+
`[NVerify Agent] SUCCESSFULLY connected to Hub as: ${this.serverName}`,
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
this.socket.on("connect_error", (err) => {
|
|
39
|
+
// We use original console.error here (not hijacked) to avoid loops
|
|
40
|
+
process.stderr.write(
|
|
41
|
+
`[NVerify Agent] Connection Error: ${err.message}\n`,
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Start sending metrics every 2 seconds
|
|
46
|
+
setInterval(() => this._sendMetrics(), 2000);
|
|
47
|
+
|
|
48
|
+
// Hijack console.log to automatically send all logs to NVerify
|
|
49
|
+
this._hijackLogs();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
_sendMetrics() {
|
|
53
|
+
// Only send if the socket is actually open
|
|
54
|
+
if (!this.socket || !this.socket.connected) return;
|
|
55
|
+
|
|
56
|
+
const totalRam = os.totalmem();
|
|
57
|
+
const freeRam = os.freemem();
|
|
58
|
+
const usedRam = totalRam - freeRam;
|
|
59
|
+
|
|
60
|
+
const metrics = {
|
|
61
|
+
serverName: this.serverName,
|
|
62
|
+
processUptime: process.uptime(),
|
|
63
|
+
systemUptime: os.uptime(),
|
|
64
|
+
cpuLoad: os.loadavg(),
|
|
65
|
+
memory: {
|
|
66
|
+
totalMB: (totalRam / 1024 / 1024).toFixed(2),
|
|
67
|
+
usedMB: (usedRam / 1024 / 1024).toFixed(2),
|
|
68
|
+
usagePercentage: ((usedRam / totalRam) * 100).toFixed(2),
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
this.socket.emit("agent-metrics", metrics);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
_hijackLogs() {
|
|
76
|
+
const originalLog = console.log;
|
|
77
|
+
const originalWarn = console.warn;
|
|
78
|
+
const originalError = console.error;
|
|
79
|
+
|
|
80
|
+
const sendToHub = (level, ...args) => {
|
|
81
|
+
// CRITICAL: Only emit if socket is connected.
|
|
82
|
+
// If we emit during a connection error, we create an infinite loop.
|
|
83
|
+
if (this.socket && this.socket.connected) {
|
|
84
|
+
const message = args
|
|
85
|
+
.map((a) => (typeof a === "object" ? JSON.stringify(a) : a))
|
|
86
|
+
.join(" ");
|
|
87
|
+
|
|
88
|
+
this.socket.emit("live-log", {
|
|
89
|
+
serverName: this.serverName,
|
|
90
|
+
level,
|
|
91
|
+
message,
|
|
92
|
+
timestamp: new Date(),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
console.log = (...args) => {
|
|
98
|
+
originalLog(...args);
|
|
99
|
+
sendToHub("info", ...args);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
console.warn = (...args) => {
|
|
103
|
+
originalWarn(...args);
|
|
104
|
+
sendToHub("warn", ...args);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
console.error = (...args) => {
|
|
108
|
+
originalError(...args);
|
|
109
|
+
sendToHub("error", ...args);
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
module.exports = new NVerifyWatchDog();
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nverify-watchdog",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "gods-eye server monitor agent",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"author": "",
|
|
7
|
+
"type": "commonjs",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"socket.io-client": "^4.8.3"
|
|
14
|
+
}
|
|
15
|
+
}
|