aboutsummaryrefslogtreecommitdiff
path: root/modules/core/module-servermon.js
blob: 23407db4f563369500f453a1f98273b62a9200e7 (plain)
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
// 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());
            }
        }