1
0
mirror of https://github.com/misskey-dev/misskey synced 2024-12-20 09:38:29 +09:00
misskey/src/daemons/server-stats.ts
syuilo f6154dc0af
v12 (#5712)
Co-authored-by: MeiMei <30769358+mei23@users.noreply.github.com>
Co-authored-by: Satsuki Yanagi <17376330+u1-liquid@users.noreply.github.com>
2020-01-30 04:37:25 +09:00

77 lines
1.4 KiB
TypeScript

import * as si from 'systeminformation';
import Xev from 'xev';
import * as osUtils from 'os-utils';
const ev = new Xev();
const interval = 2000;
/**
* Report server stats regularly
*/
export default function() {
const log = [] as any[];
ev.on('requestServerStatsLog', x => {
ev.emit(`serverStatsLog:${x.id}`, log.slice(0, x.length || 50));
});
async function tick() {
const cpu = await cpuUsage();
const memStats = await mem();
const netStats = await net();
const fsStats = await fs();
const stats = {
cpu: cpu,
mem: {
used: memStats.used,
active: memStats.active,
},
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),
}
};
ev.emit('serverStats', stats);
log.unshift(stats);
if (log.length > 200) log.pop();
}
tick();
setInterval(tick, interval);
}
// CPU STAT
function cpuUsage() {
return new Promise((res, rej) => {
osUtils.cpuUsage((cpuUsage: number) => {
res(cpuUsage);
});
});
}
// 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];
}
// FS STAT
async function fs() {
const data = await si.disksIO().catch(() => ({ rIO_sec: 0, wIO_sec: 0 }));
return data;
}