-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
142 lines (113 loc) · 4.42 KB
/
index.js
File metadata and controls
142 lines (113 loc) · 4.42 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const path = require('path');
const si = require('systeminformation');
class MonitoringDashboard {
constructor(options = {}) {
this.port = options.port || 3000;
this.showCpuLoad = options.showCpuLoad !== false;
this.showMemoryUsage = options.showMemoryUsage !== false;
this.showPing = options.showPing !== false;
this.refreshInterval = options.refreshInterval || 1000;
this.app = express();
this.server = http.createServer(this.app);
this.io = socketIo(this.server);
this.setupRoutes();
this.setupSocket();
}
setupRoutes() {
this.app.use(express.static(path.join(__dirname, 'public')));
this.app.get('/monitoring', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
this.app.get('/api/cpu', async (req, res) => {
const cpuLoad = await this.getCpuLoad();
res.json({ cpuLoad });
});
this.app.get('/api/memory', async (req, res) => {
const memoryUsage = await this.getMemoryUsage();
res.json({ memoryUsage });
});
this.app.get('/api/ping', async (req, res) => {
const ping = await this.getPing();
res.json({ ping });
});
this.app.get('/monitoring/api/cpu', async (req, res) => {
const tryapi = "Try /api/cpu"
res.json({ tryapi });
});
this.app.get('/monitoring/api/memory', async (req, res) => {
const tryapi = "Try /api/memory"
res.json({ tryapi });
});
this.app.get('/monitoring/api/ping', async (req, res) => {
const tryapi = "Try /api/ping"
res.json({ tryapi });
});
this.app.get('/api/uptime', async (req, res) => {
const time = await si.time();
const uptime = `${(time.uptime / 60).toFixed(1)} minutes`;
res.json({ uptime });
});
this.app.get('/api/cputemp', async (req, res) => {
si.cpuTemperature().then(data => res.json({ temp: data.main.toFixed(2) }))
.catch(err => res.status(500).json({ error: 'Unable to retrieve CPU temperature (Maybe you are on battery pc?)' }));
});
this.app.get('/api/hostname', async (req, res) => {
const system = await si.osInfo();
res.json({ hostname: system.hostname });
});
this.app.get('/api/platform', async (req, res) => {
const system = await si.osInfo();
res.json({ platform: `${system.platform} (${system.arch})` });
});
this.app.get('/api/user', async (req, res) => {
const user = await si.users();
res.json({ user: user[0]?.user || 'Unknown' });
});
}
setupSocket() {
this.io.on('connection', (socket) => {
console.log('Client connected');
setInterval(async () => {
const data = {};
if (this.showCpuLoad) data.cpuLoad = await this.getCpuLoad();
if (this.showMemoryUsage) data.memoryUsage = await this.getMemoryUsage();
if (this.showPing) data.ping = await this.getPing();
if (this.showCpuTemp) data.cputemp = await this.getCpuTemp();
socket.emit('stats', data);
}, this.refreshInterval);
});
}
async getCpuLoad() {
const cpu = await si.currentLoad();
return cpu.currentLoad.toFixed(2);
}
async getCpuTemp() {
const cputemp = await si.cpuTemperature();
return cputemp.main.toFixed(2);
}
async getMemoryUsage() {
const memory = await si.mem();
return ((memory.active / memory.total) * 100).toFixed(2);
}
async getPing() {
return await si.inetLatency();
}
startServer() {
this.server.listen(this.port, () => {
console.log(`Monitoring Dashboard running on http://127.0.0.1:${this.port}/monitoring`);
});
}
stopServer() {
this.server.close(() => {
console.log('Monitoring Dashboard server has been stopped');
});
if (this.intervalId) {
clearInterval(this.intervalId);
}
this.io.close();
}
}
module.exports = MonitoringDashboard;