forked from lsongdev/kelp-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
33 lines (27 loc) · 841 Bytes
/
start.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
const fs = require('fs');
const http = require('http');
const EventSource = require('server-send-events');
const CpuCollector = require('./cpu');
const pkg = require('./package.json');
const send = res =>
fs.createReadStream(__dirname + '/monitor.html').pipe(res);
const es = new EventSource();
const server = http.createServer((req, res) => {
if (es.match(req, '/usage')) {
const client = es.handle(req, res);
client.on('error', () => { });
} else {
send(res);
}
});
const { KELP_MONITOR_PORT: port = 59757 } = process.env;
server.listen(port, () => {
console.log(`[${pkg.name}@${pkg.version}] listening on port http://localhost:${port}`);
const cpuInspector = new CpuCollector();
setInterval(() => {
es.send({
cpu: cpuInspector.ratio(),
memory: process.memoryUsage(),
});
}, 2000);
});