mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 23:33:12 +09:00
Show System metrics in hosts page
Implement simple collection of basic real time system metrics of hosts and the controller for the purpose of displaying them in the Web UI. The metrics are collected by the controller at a configurable interval and pushed out to web clients currently subscribed to the metrics.
This commit is contained in:
1 parent
69bdc8e884
commit
d64bf6015e
13 files changed
+409
-7
No files matched your search
@@ -17,6 +17,7 @@ export interface ControllerConfigFields {
|
||||
"controller.heartbeat_interval": number;
|
||||
"controller.session_timeout": number;
|
||||
"controller.metrics_timeout": number;
|
||||
"controller.system_metrics_interval": number;
|
||||
"controller.proxy_stream_timeout": number;
|
||||
"controller.default_mod_pack_id": number | null;
|
||||
"controller.default_role_id": number | null;
|
||||
@@ -126,6 +127,12 @@ export class ControllerConfig extends classes.Config<ControllerConfigFields> {
|
||||
type: "number",
|
||||
initialValue: 8,
|
||||
},
|
||||
"controller.system_metrics_interval": {
|
||||
title: "System Metrics Interval",
|
||||
description: "Interval in seconds to collect and update system metrics for the Web UI",
|
||||
type: "number",
|
||||
initialValue: 10,
|
||||
},
|
||||
"controller.proxy_stream_timeout": {
|
||||
title: "Proxy Stream Timeout",
|
||||
description: "Timeout in seconds for proxy streams to start flowing.",
|
||||
|
||||
@@ -204,6 +204,108 @@ export class LogMessageEvent {
|
||||
}
|
||||
}
|
||||
|
||||
export class SystemMetrics {
|
||||
constructor(
|
||||
/**
|
||||
* Id of the host these metrics originate from, or the string
|
||||
* "controller" if these metrics are for the controller.
|
||||
*/
|
||||
public id: number | "controller",
|
||||
public coreRatios: number[],
|
||||
public memoryCapacity: number,
|
||||
public memoryAvailable: number,
|
||||
public diskCapacity: number,
|
||||
public diskAvailable: number,
|
||||
/** Millisecond Unix timestamp this entry was last updated at */
|
||||
public updatedAt: number,
|
||||
public isDeleted: boolean,
|
||||
) { }
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
"id": Type.Union([Type.Number(), Type.Literal("controller")]),
|
||||
"coreRatios": Type.Array(Type.Number()),
|
||||
"memoryCapacity": Type.Number(),
|
||||
"memoryAvailable": Type.Number(),
|
||||
"diskCapacity": Type.Number(),
|
||||
"diskAvailable": Type.Number(),
|
||||
"updatedAt": Type.Number(),
|
||||
"isDeleted": Type.Boolean(),
|
||||
});
|
||||
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
return new this(
|
||||
json.id,
|
||||
json.coreRatios,
|
||||
json.memoryCapacity,
|
||||
json.memoryAvailable,
|
||||
json.diskCapacity,
|
||||
json.diskAvailable,
|
||||
json.updatedAt,
|
||||
json.isDeleted,
|
||||
);
|
||||
}
|
||||
|
||||
get cpuCapacity() {
|
||||
return this.coreRatios.length;
|
||||
}
|
||||
|
||||
get cpuUsed() {
|
||||
return this.coreRatios.reduce((a, b) => a + b, 0);
|
||||
}
|
||||
|
||||
get cpuAvailable() {
|
||||
return this.cpuCapacity - this.cpuUsed;
|
||||
}
|
||||
|
||||
get cpuRatio() {
|
||||
return this.cpuUsed / this.cpuCapacity;
|
||||
}
|
||||
|
||||
get memoryUsed() {
|
||||
return this.memoryCapacity - this.memoryAvailable;
|
||||
}
|
||||
|
||||
get memoryRatio() {
|
||||
return (this.memoryCapacity - this.memoryAvailable) / this.memoryCapacity;
|
||||
}
|
||||
|
||||
get diskUsed() {
|
||||
return this.diskCapacity - this.diskAvailable;
|
||||
}
|
||||
|
||||
get diskRatio() {
|
||||
return (this.diskCapacity - this.diskAvailable) / this.diskCapacity;
|
||||
}
|
||||
}
|
||||
|
||||
export class SystemMetricsRequest {
|
||||
declare ["constructor"]: typeof SystemMetricsRequest;
|
||||
static type = "request" as const;
|
||||
static src = "controller" as const;
|
||||
static dst = "host" as const;
|
||||
static Response = SystemMetrics;
|
||||
}
|
||||
|
||||
export class SystemMetricsUpdateEvent {
|
||||
declare ["constructor"]: typeof SystemMetricsUpdateEvent;
|
||||
static type = "event" as const;
|
||||
static src = "controller" as const;
|
||||
static dst = "control" as const;
|
||||
static permission = "core.system_metrics.subscribe" as const;
|
||||
|
||||
constructor(
|
||||
public updates: SystemMetrics[],
|
||||
) { }
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
"updates": Type.Array(SystemMetrics.jsonSchema),
|
||||
});
|
||||
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
return new this(json.updates.map(update => SystemMetrics.fromJSON(update)));
|
||||
}
|
||||
}
|
||||
|
||||
export class DebugDumpWsRequest {
|
||||
declare ["constructor"]: typeof DebugDumpWsRequest;
|
||||
static type = "request" as const;
|
||||
|
||||
@@ -25,6 +25,8 @@ export const dataClasses: (RequestClass<unknown, unknown> | EventClass<unknown>)
|
||||
controller.LogSetSubscriptionsRequest,
|
||||
controller.LogQueryRequest,
|
||||
controller.LogMessageEvent,
|
||||
controller.SystemMetricsRequest,
|
||||
controller.SystemMetricsUpdateEvent,
|
||||
controller.DebugDumpWsRequest,
|
||||
controller.DebugWsMessageEvent,
|
||||
|
||||
|
||||
@@ -129,6 +129,13 @@ definePermission({
|
||||
title: "Modify controller config",
|
||||
description: "Modify the controller config or entries of the controller config.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.system_metrics.subscribe",
|
||||
title: "Subscribe to system metrics updates",
|
||||
description:
|
||||
"Subscribe to be notified when the system metrics detailing cpu, memory and disk usage for the " +
|
||||
"controller and hosts are updated.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.host.list",
|
||||
title: "List hosts",
|
||||
|
||||
@@ -2,6 +2,7 @@ import fs from "fs-extra";
|
||||
import os from "os";
|
||||
import util from "util";
|
||||
import { Gauge } from "./prometheus";
|
||||
import { SystemMetrics } from "./data";
|
||||
|
||||
function cpuModel() {
|
||||
const model = os.cpus()[0].model;
|
||||
@@ -119,3 +120,45 @@ export const systemDiskAvailableBytes = new Gauge(
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
function minZip<T>(a: T[], b: T[]): [T, T][] {
|
||||
const length = Math.min(a.length, b.length);
|
||||
const result = [];
|
||||
result.length = length;
|
||||
for (let i = 0; i < length; i++) {
|
||||
result[i] = [a[i], b[i]] as [T, T];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
let previousTotalCpuMs: number[] = [];
|
||||
let previousIdleCpuMs: number[] = [];
|
||||
export async function gatherSystemMetrics(id: SystemMetrics["id"]) {
|
||||
const cpus = os.cpus();
|
||||
const currentTotalCpuMs = cpus.map(({ times }) => times.user + times.idle + times.irq + times.nice + times.sys);
|
||||
const currentIdleCpuMs = cpus.map(({ times }) => times.idle);
|
||||
const deltaTotalCpuMs = minZip(previousTotalCpuMs, currentTotalCpuMs).map(([prev, curr]) => curr - prev);
|
||||
const deltaIdleCpuMs = minZip(previousIdleCpuMs, currentIdleCpuMs).map(([prev, curr]) => curr - prev);
|
||||
const cpuUsage = minZip(deltaTotalCpuMs, deltaIdleCpuMs).map(([total, idle]) => (total - idle) / total);
|
||||
previousTotalCpuMs = currentTotalCpuMs;
|
||||
previousIdleCpuMs = currentIdleCpuMs;
|
||||
let diskCapacity = 0;
|
||||
let diskAvailable = 0;
|
||||
if (fs.statfs) { // TODO: remove this check once minimum supported Node.js >= v18.15.0.
|
||||
const statFsAsync = util.promisify(fs.statfs);
|
||||
const stats = await statFsAsync(".");
|
||||
diskCapacity = stats.blocks * stats.bsize;
|
||||
diskAvailable = stats.bavail * stats.bsize;
|
||||
}
|
||||
|
||||
return new SystemMetrics(
|
||||
id,
|
||||
cpuUsage,
|
||||
os.totalmem(),
|
||||
os.freemem(),
|
||||
diskCapacity,
|
||||
diskAvailable,
|
||||
Date.now(),
|
||||
false,
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user