2023-01-12 21:40:33 -07:00
|
|
|
import si from "systeminformation";
|
|
|
|
import Xev from "xev";
|
|
|
|
import * as osUtils from "os-utils";
|
2023-07-02 17:20:40 -06:00
|
|
|
import { fetchMeta } from "@/misc/fetch-meta.js";
|
2023-05-25 02:23:59 -06:00
|
|
|
import meilisearch from "../db/meilisearch.js";
|
2017-06-08 10:03:54 -06:00
|
|
|
|
2022-04-16 23:42:13 -06:00
|
|
|
const ev = new Xev();
|
2017-06-08 10:03:54 -06:00
|
|
|
|
2020-01-29 12:37:25 -07:00
|
|
|
const interval = 2000;
|
2018-06-10 15:48:25 -06:00
|
|
|
|
2021-01-03 06:38:32 -07:00
|
|
|
const roundCpu = (num: number) => Math.round(num * 1000) / 1000;
|
|
|
|
const round = (num: number) => Math.round(num * 10) / 10;
|
|
|
|
|
2017-06-08 10:03:54 -06:00
|
|
|
/**
|
2018-06-08 13:14:26 -06:00
|
|
|
* Report server stats regularly
|
2017-06-08 10:03:54 -06:00
|
|
|
*/
|
2023-01-12 21:40:33 -07:00
|
|
|
export default function () {
|
2020-01-29 12:37:25 -07:00
|
|
|
const log = [] as any[];
|
2018-06-08 10:45:25 -06:00
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
ev.on("requestServerStatsLog", (x) => {
|
2020-01-29 12:37:25 -07:00
|
|
|
ev.emit(`serverStatsLog:${x.id}`, log.slice(0, x.length || 50));
|
2018-06-08 10:45:25 -06:00
|
|
|
});
|
|
|
|
|
2023-07-08 17:19:55 -06:00
|
|
|
fetchMeta().then((meta) => {
|
|
|
|
if (!meta.enableServerMachineStats) return;
|
|
|
|
}
|
|
|
|
);
|
2023-07-02 17:20:40 -06:00
|
|
|
|
2018-06-10 15:48:25 -06:00
|
|
|
async function tick() {
|
2018-07-27 02:43:04 -06:00
|
|
|
const cpu = await cpuUsage();
|
2020-01-29 12:37:25 -07:00
|
|
|
const memStats = await mem();
|
|
|
|
const netStats = await net();
|
|
|
|
const fsStats = await fs();
|
2023-05-25 01:53:04 -06:00
|
|
|
const meilisearchStats = await meilisearchStatus();
|
2018-07-27 02:31:19 -06:00
|
|
|
|
|
|
|
const stats = {
|
2021-01-03 06:38:32 -07:00
|
|
|
cpu: roundCpu(cpu),
|
2018-07-27 02:31:19 -06:00
|
|
|
mem: {
|
2021-02-26 09:29:29 -07:00
|
|
|
used: round(memStats.used - memStats.buffers - memStats.cached),
|
2021-01-03 06:38:32 -07:00
|
|
|
active: round(memStats.active),
|
2018-07-27 02:31:19 -06:00
|
|
|
},
|
2020-01-29 12:37:25 -07:00
|
|
|
net: {
|
2021-01-03 06:38:32 -07:00
|
|
|
rx: round(Math.max(0, netStats.rx_sec)),
|
|
|
|
tx: round(Math.max(0, netStats.tx_sec)),
|
2020-01-29 12:37:25 -07:00
|
|
|
},
|
|
|
|
fs: {
|
2022-02-18 22:28:08 -07:00
|
|
|
r: round(Math.max(0, fsStats.rIO_sec ?? 0)),
|
|
|
|
w: round(Math.max(0, fsStats.wIO_sec ?? 0)),
|
2021-12-09 07:58:30 -07:00
|
|
|
},
|
2023-05-25 19:06:41 -06:00
|
|
|
meilisearch: meilisearchStats,
|
2018-07-27 02:31:19 -06:00
|
|
|
};
|
2023-01-12 21:40:33 -07:00
|
|
|
ev.emit("serverStats", stats);
|
2018-08-18 12:32:01 -06:00
|
|
|
log.unshift(stats);
|
|
|
|
if (log.length > 200) log.pop();
|
2018-06-10 15:48:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
tick();
|
|
|
|
|
|
|
|
setInterval(tick, interval);
|
2017-06-08 10:03:54 -06:00
|
|
|
}
|
2018-07-27 02:31:19 -06:00
|
|
|
|
|
|
|
// CPU STAT
|
2022-02-18 22:28:08 -07:00
|
|
|
function cpuUsage(): Promise<number> {
|
2018-07-27 03:42:58 -06:00
|
|
|
return new Promise((res, rej) => {
|
2022-02-18 22:28:08 -07:00
|
|
|
osUtils.cpuUsage((cpuUsage) => {
|
2018-07-27 03:42:58 -06:00
|
|
|
res(cpuUsage);
|
|
|
|
});
|
|
|
|
});
|
2018-07-27 02:31:19 -06:00
|
|
|
}
|
|
|
|
|
2020-01-29 12:37:25 -07: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 02:31:19 -06:00
|
|
|
}
|
|
|
|
|
2020-01-29 12:37:25 -07:00
|
|
|
// FS STAT
|
|
|
|
async function fs() {
|
|
|
|
const data = await si.disksIO().catch(() => ({ rIO_sec: 0, wIO_sec: 0 }));
|
2021-03-19 08:26:44 -06:00
|
|
|
return data || { rIO_sec: 0, wIO_sec: 0 };
|
2018-07-27 02:43:04 -06:00
|
|
|
}
|
2023-05-25 01:53:04 -06:00
|
|
|
|
2023-05-28 22:04:54 -06:00
|
|
|
// MEILI STAT
|
2023-05-25 01:53:04 -06:00
|
|
|
async function meilisearchStatus() {
|
|
|
|
if (meilisearch) {
|
|
|
|
return meilisearch.serverStats();
|
|
|
|
} else {
|
2023-05-26 02:55:51 -06:00
|
|
|
return {
|
|
|
|
health: "unconfigured",
|
|
|
|
size: 0,
|
|
|
|
indexed_count: 0,
|
|
|
|
};
|
2023-05-25 01:53:04 -06:00
|
|
|
}
|
|
|
|
}
|