2020-01-29 21:37:25 +02:00
|
|
|
import * as si from 'systeminformation';
|
2017-06-08 19:03:54 +03:00
|
|
|
import Xev from 'xev';
|
2019-02-03 16:41:27 +02:00
|
|
|
import * as osUtils from 'os-utils';
|
2017-06-08 19:03:54 +03:00
|
|
|
|
|
|
|
const ev = new Xev();
|
|
|
|
|
2020-01-29 21:37:25 +02:00
|
|
|
const interval = 2000;
|
2018-06-11 00:48:25 +03:00
|
|
|
|
2017-06-08 19:03:54 +03:00
|
|
|
/**
|
2018-06-08 22:14:26 +03:00
|
|
|
* Report server stats regularly
|
2017-06-08 19:03:54 +03:00
|
|
|
*/
|
|
|
|
export default function() {
|
2020-01-29 21:37:25 +02:00
|
|
|
const log = [] as any[];
|
2018-06-08 19:45:25 +03:00
|
|
|
|
2018-08-18 21:32:01 +03:00
|
|
|
ev.on('requestServerStatsLog', x => {
|
2020-01-29 21:37:25 +02:00
|
|
|
ev.emit(`serverStatsLog:${x.id}`, log.slice(0, x.length || 50));
|
2018-06-08 19:45:25 +03:00
|
|
|
});
|
|
|
|
|
2018-06-11 00:48:25 +03:00
|
|
|
async function tick() {
|
2018-07-27 11:43:04 +03:00
|
|
|
const cpu = await cpuUsage();
|
2020-01-29 21:37:25 +02:00
|
|
|
const memStats = await mem();
|
|
|
|
const netStats = await net();
|
|
|
|
const fsStats = await fs();
|
2018-07-27 11:31:19 +03:00
|
|
|
|
|
|
|
const stats = {
|
2020-01-29 21:37:25 +02:00
|
|
|
cpu: cpu,
|
2018-07-27 11:31:19 +03:00
|
|
|
mem: {
|
2020-01-29 21:37:25 +02:00
|
|
|
used: memStats.used,
|
|
|
|
active: memStats.active,
|
2018-07-27 11:31:19 +03:00
|
|
|
},
|
2020-01-29 21:37:25 +02:00
|
|
|
net: {
|
|
|
|
rx: Math.max(0, netStats.rx_sec),
|
|
|
|
tx: Math.max(0, netStats.tx_sec),
|
|
|
|
},
|
|
|
|
fs: {
|
|
|
|
r: Math.max(0, fsStats.rIO_sec),
|
|
|
|
w: Math.max(0, fsStats.wIO_sec),
|
|
|
|
}
|
2018-07-27 11:31:19 +03:00
|
|
|
};
|
|
|
|
ev.emit('serverStats', stats);
|
2018-08-18 21:32:01 +03:00
|
|
|
log.unshift(stats);
|
|
|
|
if (log.length > 200) log.pop();
|
2018-06-11 00:48:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tick();
|
|
|
|
|
|
|
|
setInterval(tick, interval);
|
2017-06-08 19:03:54 +03:00
|
|
|
}
|
2018-07-27 11:31:19 +03:00
|
|
|
|
|
|
|
// CPU STAT
|
2018-07-27 12:42:58 +03:00
|
|
|
function cpuUsage() {
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
osUtils.cpuUsage((cpuUsage: number) => {
|
|
|
|
res(cpuUsage);
|
|
|
|
});
|
|
|
|
});
|
2018-07-27 11:31:19 +03:00
|
|
|
}
|
|
|
|
|
2020-01-29 21:37:25 +02:00
|
|
|
// MEMORY STAT
|
|
|
|
async function mem() {
|
|
|
|
const data = await si.mem();
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NETWORK STAT
|
|
|
|
async function net() {
|
|
|
|
const iface = await si.networkInterfaceDefault();
|
|
|
|
const data = await si.networkStats(iface);
|
|
|
|
return data[0];
|
2018-07-27 11:31:19 +03:00
|
|
|
}
|
|
|
|
|
2020-01-29 21:37:25 +02:00
|
|
|
// FS STAT
|
|
|
|
async function fs() {
|
|
|
|
const data = await si.disksIO().catch(() => ({ rIO_sec: 0, wIO_sec: 0 }));
|
|
|
|
return data;
|
2018-07-27 11:43:04 +03:00
|
|
|
}
|