aboutsummaryrefslogtreecommitdiff
path: root/modules/core/module-servermon.js
diff options
context:
space:
mode:
Diffstat (limited to 'modules/core/module-servermon.js')
-rw-r--r--modules/core/module-servermon.js107
1 files changed, 107 insertions, 0 deletions
diff --git a/modules/core/module-servermon.js b/modules/core/module-servermon.js
new file mode 100644
index 0000000..23407db
--- /dev/null
+++ b/modules/core/module-servermon.js
@@ -0,0 +1,107 @@
+// Extracted from newOS4 index.html — ServerMonitorModule
+ class ServerMonitorModule {
+ constructor() {
+ this.commands = {}; this.terminal = null;
+ this.servers = this.loadServers(); this.monitorInterval = null;
+ }
+ getHelpInfo() {
+ return {
+ 'server': 'Add a server to monitor: server <name> <url>',
+ 'servers': 'List all monitored servers',
+ 'server-check': 'Check a server status: server-check <name-or-index>',
+ 'server-remove': 'Remove a server: server-remove <name-or-index>',
+ 'monitor': 'Toggle auto-monitoring (checks every 60s)'
+ };
+ }
+ loadServers() {
+ try {
+ const saved = localStorage.getItem('webx11_servers');
+ return saved ? JSON.parse(saved) : [];
+ } catch (e) { return []; }
+ }
+ saveServers() {
+ localStorage.setItem('webx11_servers', JSON.stringify(this.servers));
+ }
+ registerCommands() {
+ this.commands['server'] = (args) => this.addServer(args);
+ this.commands['servers'] = () => this.listServers();
+ this.commands['server-check'] = (args) => this.checkServer(args);
+ this.commands['server-remove'] = (args) => this.removeServer(args);
+ this.commands['monitor'] = (args) => this.toggleMonitor(args);
+ }
+ addServer(args) {
+ if (args.length < 2) {
+ this.terminal.print("Usage: server <name> <url>", "warning"); return;
+ }
+ const name = args[0], url = args[1];
+ this.servers.push({ name, url, status: 'unknown', lastCheck: null });
+ this.saveServers();
+ this.terminal.print(`Server '${name}' added`, "success");
+ }
+ listServers() {
+ if (this.servers.length === 0) {
+ this.terminal.print("No servers configured", "info"); return;
+ }
+ this.terminal.print("=== Monitored Servers ===", "info");
+ this.servers.forEach((s, i) => {
+ const statusIcon = s.status === 'online' ? '🟢' : s.status === 'offline' ? '🔴' : '⚪';
+ this.terminal.print(`${i + 1}. ${statusIcon} ${s.name} - ${s.url}`);
+ if (s.lastCheck) { this.terminal.print(` Last check: ${s.lastCheck}`, "info"); }
+ });
+ }
+ async checkServer(args) {
+ if (!args[0]) {
+ this.terminal.print("Usage: server-check <name-or-index>", "warning"); return;
+ }
+ const server = this.findServer(args[0]);
+ if (!server) {
+ this.terminal.print("Server not found", "error"); return;
+ }
+ this.terminal.print(`Checking ${server.name}...`, "info");
+ try {
+ const start = Date.now();
+ const response = await fetch(server.url, { method: 'HEAD', mode: 'no-cors' });
+ const latency = Date.now() - start;
+ server.status = 'online'; server.lastCheck = new Date().toLocaleString(); server.latency = latency;
+ this.saveServers();
+ this.terminal.print(`✅ ${server.name} is online (${latency}ms)`, "success");
+ } catch (e) {
+ server.status = 'offline'; server.lastCheck = new Date().toLocaleString();
+ this.saveServers();
+ this.terminal.print(`❌ ${server.name} is offline`, "error");
+ }
+ }
+ removeServer(args) {
+ if (!args[0]) {
+ this.terminal.print("Usage: server-remove <name-or-index>", "warning"); return;
+ }
+ const index = this.findServerIndex(args[0]);
+ if (index === -1) {
+ this.terminal.print("Server not found", "error"); return;
+ }
+ this.servers.splice(index, 1);
+ this.saveServers();
+ this.terminal.print("Server removed", "success");
+ }
+ toggleMonitor(args) {
+ if (this.monitorInterval) {
+ clearInterval(this.monitorInterval);
+ this.monitorInterval = null;
+ this.terminal.print("Auto-monitor stopped", "warning");
+ } else {
+ this.terminal.print("Auto-monitor started (every 60s)", "success");
+ this.monitorInterval = setInterval(() => {
+ this.servers.forEach((s, i) => { this.checkServer([String(i + 1)]); });
+ }, 60000);
+ }
+ }
+ findServer(identifier) {
+ const index = this.findServerIndex(identifier);
+ return index !== -1 ? this.servers[index] : null;
+ }
+ findServerIndex(identifier) {
+ const idx = parseInt(identifier) - 1;
+ if (!isNaN(idx) && idx >= 0 && idx < this.servers.length) { return idx; }
+ return this.servers.findIndex(s => s.name.toLowerCase() === identifier.toLowerCase());
+ }
+ }