forked from misskey-dev/misskey
-
Notifications
You must be signed in to change notification settings - Fork 12
/
server-stats.ts
62 lines (52 loc) · 1.54 KB
/
server-stats.ts
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
import * as os from 'os';
import * as systeminformation from 'systeminformation';
import * as diskusage from 'diskusage';
import * as Deque from 'double-ended-queue';
import Xev from 'xev';
import * as osUtils from 'os-utils';
import config from '../config';
const ev = new Xev();
const interval = 3000;
/**
* Report server stats regularly
*/
export default function() {
const log = new Deque<any>();
ev.on('requestServerStatsLog', x => {
ev.emit(`serverStatsLog:${x.id}`, log.toArray().slice(0, x.length || 50));
});
async function tick() {
const cpu = await cpuUsage();
const mem = await systeminformation.mem();
const cpuSpeed = (await systeminformation.cpuCurrentSpeed()).avg;
const disk = await diskusage.check(os.platform() == 'win32' ? 'c:' : '/');
mem.used = mem.used - mem.buffers - mem.cached;
// |- used -|- buffer-|- cache -|- free -|
// |-- active --|-- available --|- free -|
const stats = {
cpu_usage: config.hideServerInfo ? -1 : cpu,
cpu_speed: config.hideServerInfo ? -1 : cpuSpeed,
mem: config.hideServerInfo ? -1 : mem,
disk: config.hideServerInfo ? -1 : disk,
os_uptime: config.hideServerInfo ? -1 : os.uptime(),
process_uptime: config.hideServerInfo ? -1 : process.uptime()
};
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) => {
try {
osUtils.cpuUsage((cpuUsage: number) => {
res(cpuUsage);
});
} catch (e) {
rej(e);
}
});
}