2023-07-27 05:31:52 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-02-16 14:09:41 +00:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2022-09-17 18:27:08 +00:00
|
|
|
import si from 'systeminformation';
|
|
|
|
import Xev from 'xev';
|
|
|
|
import * as osUtils from 'os-utils';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-07-02 07:02:32 +00:00
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import type { OnApplicationShutdown } from '@nestjs/common';
|
|
|
|
|
|
|
|
const ev = new Xev();
|
|
|
|
|
|
|
|
const interval = 2000;
|
|
|
|
|
|
|
|
const roundCpu = (num: number) => Math.round(num * 1000) / 1000;
|
|
|
|
const round = (num: number) => Math.round(num * 10) / 10;
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ServerStatsService implements OnApplicationShutdown {
|
2023-07-02 07:02:32 +00:00
|
|
|
private intervalId: NodeJS.Timer | null = null;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
constructor(
|
2023-07-02 07:02:32 +00:00
|
|
|
private metaService: MetaService,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Report server stats regularly
|
|
|
|
*/
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-07-02 07:02:32 +00:00
|
|
|
public async start(): Promise<void> {
|
|
|
|
if (!(await this.metaService.fetch(true)).enableServerMachineStats) return;
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const log = [] as any[];
|
|
|
|
|
|
|
|
ev.on('requestServerStatsLog', x => {
|
|
|
|
ev.emit(`serverStatsLog:${x.id}`, log.slice(0, x.length ?? 50));
|
|
|
|
});
|
|
|
|
|
|
|
|
const tick = async () => {
|
|
|
|
const cpu = await cpuUsage();
|
|
|
|
const memStats = await mem();
|
|
|
|
const netStats = await net();
|
|
|
|
const fsStats = await fs();
|
|
|
|
|
|
|
|
const stats = {
|
|
|
|
cpu: roundCpu(cpu),
|
|
|
|
mem: {
|
2023-04-29 21:47:00 +00:00
|
|
|
used: round(memStats.total - memStats.available),
|
2022-09-17 18:27:08 +00:00
|
|
|
active: round(memStats.active),
|
|
|
|
},
|
|
|
|
net: {
|
|
|
|
rx: round(Math.max(0, netStats.rx_sec)),
|
|
|
|
tx: round(Math.max(0, netStats.tx_sec)),
|
|
|
|
},
|
|
|
|
fs: {
|
|
|
|
r: round(Math.max(0, fsStats.rIO_sec ?? 0)),
|
|
|
|
w: round(Math.max(0, fsStats.wIO_sec ?? 0)),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
ev.emit('serverStats', stats);
|
|
|
|
log.unshift(stats);
|
|
|
|
if (log.length > 200) log.pop();
|
|
|
|
};
|
|
|
|
|
|
|
|
tick();
|
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
this.intervalId = setInterval(tick, interval);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-05-29 04:21:26 +00:00
|
|
|
public dispose(): void {
|
2023-07-02 07:02:32 +00:00
|
|
|
if (this.intervalId) {
|
|
|
|
clearInterval(this.intervalId);
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
2023-05-29 04:21:26 +00:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public onApplicationShutdown(signal?: string | undefined): void {
|
|
|
|
this.dispose();
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CPU STAT
|
|
|
|
function cpuUsage(): Promise<number> {
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
osUtils.cpuUsage((cpuUsage) => {
|
|
|
|
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 ?? { rIO_sec: 0, wIO_sec: 0 };
|
|
|
|
}
|