mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 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
@@ -1,9 +1,14 @@
|
||||
import React from "react";
|
||||
import { Typography } from "antd";
|
||||
import { Descriptions, Typography } from "antd";
|
||||
|
||||
import PluginExtra from "./PluginExtra";
|
||||
import LogConsole from "./LogConsole";
|
||||
import {
|
||||
MetricCpuRatio, MetricCpuUsed, MetricMemoryRatio, MetricMemoryUsed,
|
||||
MetricDiskUsed, MetricDiskRatio,
|
||||
} from "./system_metrics";
|
||||
import { useAccount } from "../model/account";
|
||||
import { useSystemMetrics } from "../model/system_metrics";
|
||||
import ControllerConfigTree from "./ControllerConfigTree";
|
||||
import PageLayout from "./PageLayout";
|
||||
|
||||
@@ -12,9 +17,19 @@ const { Title } = Typography;
|
||||
|
||||
export default function ControllerPage() {
|
||||
let account = useAccount();
|
||||
const [systemMetrics] = useSystemMetrics();
|
||||
const metrics = systemMetrics.get("controller");
|
||||
|
||||
return <PageLayout nav={[{ name: "Controller" }]}>
|
||||
<h2>Controller</h2>
|
||||
<Descriptions bordered size="small" column={{ xs: 1, md: 2, lg: 2, xl: 2, xxl: 2 }}>
|
||||
<Descriptions.Item label="CPU Usage"><MetricCpuRatio metrics={metrics} /></Descriptions.Item>
|
||||
<Descriptions.Item label="Cores"><MetricCpuUsed metrics={metrics} /></Descriptions.Item>
|
||||
<Descriptions.Item label="Memory Usage"><MetricMemoryRatio metrics={metrics} /></Descriptions.Item>
|
||||
<Descriptions.Item label="Memory"><MetricMemoryUsed metrics={metrics} /></Descriptions.Item>
|
||||
<Descriptions.Item label="Disk Usage"><MetricDiskRatio metrics={metrics} /></Descriptions.Item>
|
||||
<Descriptions.Item label="Disk"><MetricDiskUsed metrics={metrics} /></Descriptions.Item>
|
||||
</Descriptions>
|
||||
{account.hasPermission("core.log.follow") && <>
|
||||
<Title level={5} style={{ marginTop: 16 }}>Console</Title>
|
||||
<LogConsole controller={true} />
|
||||
|
||||
@@ -13,7 +13,12 @@ import { useHost } from "../model/host";
|
||||
import PageHeader from "./PageHeader";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PluginExtra from "./PluginExtra";
|
||||
import {
|
||||
MetricCpuRatio, MetricCpuUsed, MetricMemoryRatio, MetricMemoryUsed,
|
||||
MetricDiskUsed, MetricDiskRatio,
|
||||
} from "./system_metrics";
|
||||
import { formatTimestamp } from "../util/time_format";
|
||||
import { useSystemMetrics } from "../model/system_metrics";
|
||||
|
||||
const { Title } = Typography;
|
||||
|
||||
@@ -23,6 +28,8 @@ export default function HostViewPage() {
|
||||
let control = useContext(ControlContext);
|
||||
let account = useAccount();
|
||||
let [instances] = useInstances();
|
||||
const [systemMetrics] = useSystemMetrics();
|
||||
const metrics = systemMetrics.get(hostId);
|
||||
const [host, synced] = useHost(hostId);
|
||||
const hostInstances = new Map([...instances].filter(([_id, instance]) => instance.assignedHost === hostId));
|
||||
|
||||
@@ -63,20 +70,27 @@ export default function HostViewPage() {
|
||||
title={host.name || String(hostId)}
|
||||
extra={hostButtons}
|
||||
/>
|
||||
<Descriptions bordered size="small" column={{ xs: 1, sm: 2, xl: 4 }}>
|
||||
<Descriptions bordered size="small" column={{ xs: 1, md: 2, lg: 2, xl: 2, xxl: 2 }}>
|
||||
<Descriptions.Item label="Name">{host["name"]}</Descriptions.Item>
|
||||
<Descriptions.Item label="Connected">
|
||||
<Tag color={host["connected"] ? "#389e0d" : "#cf1322"}>
|
||||
{host["connected"] ? "Connected" : "Disconnected"}
|
||||
</Tag>
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="CPU Usage"><MetricCpuRatio metrics={metrics} /></Descriptions.Item>
|
||||
<Descriptions.Item label="Cores"><MetricCpuUsed metrics={metrics} /></Descriptions.Item>
|
||||
<Descriptions.Item label="Memory Usage"><MetricMemoryRatio metrics={metrics} /></Descriptions.Item>
|
||||
<Descriptions.Item label="Memory"><MetricMemoryUsed metrics={metrics} /></Descriptions.Item>
|
||||
<Descriptions.Item label="Disk Usage"><MetricDiskRatio metrics={metrics} /></Descriptions.Item>
|
||||
<Descriptions.Item label="Disk"><MetricDiskUsed metrics={metrics} /></Descriptions.Item>
|
||||
<Descriptions.Item label="Agent">{host["agent"]}</Descriptions.Item>
|
||||
<Descriptions.Item label="Version">{host["version"]}</Descriptions.Item>
|
||||
{
|
||||
host.tokenValidAfter
|
||||
&& <Descriptions.Item label="Tokens valid after:">
|
||||
{formatTimestamp(host.tokenValidAfter*1000)}
|
||||
</Descriptions.Item>
|
||||
? <Descriptions.Item label="Tokens valid after:">
|
||||
{formatTimestamp(host.tokenValidAfter*1000)}
|
||||
</Descriptions.Item>
|
||||
: null
|
||||
}
|
||||
</Descriptions>
|
||||
{account.hasPermission("core.instance.list") && <>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import React, { useContext, useRef, useState, useEffect } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { Button, Form, InputNumber, Modal, Table, Tag, Typography } from "antd";
|
||||
import { Button, Flex, Form, InputNumber, Modal, Progress, Switch, Table, Tag, Typography } from "antd";
|
||||
import CopyOutlined from "@ant-design/icons/lib/icons/CopyOutlined";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
@@ -10,7 +10,12 @@ import ControlContext from "./ControlContext";
|
||||
import PageHeader from "./PageHeader";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PluginExtra from "./PluginExtra";
|
||||
import {
|
||||
MetricCpuRatio, MetricCpuUsed, MetricMemoryRatio, MetricMemoryUsed,
|
||||
MetricDiskUsed, MetricDiskRatio,
|
||||
} from "./system_metrics";
|
||||
import { useHosts } from "../model/host";
|
||||
import { useSystemMetrics } from "../model/system_metrics";
|
||||
import notify, { notifyErrorHandler } from "../util/notify";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
@@ -180,11 +185,13 @@ function CopyButton({ text, message }: { text:string, message:string }) {
|
||||
</Button>;
|
||||
}
|
||||
|
||||
|
||||
export default function HostsPage() {
|
||||
let account = useAccount();
|
||||
let navigate = useNavigate();
|
||||
let [hosts] = useHosts();
|
||||
const [metrics] = useSystemMetrics();
|
||||
const [showRatios, setShowRatios] = useState(true);
|
||||
const [showNumbers, setShowNumbers] = useState(false);
|
||||
|
||||
return <PageLayout nav={[{ name: "Hosts" }]}>
|
||||
<PageHeader
|
||||
@@ -192,6 +199,7 @@ export default function HostsPage() {
|
||||
extra={account.hasPermission("core.host.generate_token") ? <GenerateHostTokenButton /> : undefined}
|
||||
/>
|
||||
<Table
|
||||
style={{ overflowX: "auto" }}
|
||||
columns={[
|
||||
{
|
||||
title: "Name",
|
||||
@@ -199,6 +207,50 @@ export default function HostsPage() {
|
||||
defaultSortOrder: "ascend",
|
||||
sorter: (a, b) => strcmp(a.name, b.name),
|
||||
},
|
||||
{
|
||||
title: "CPU%",
|
||||
sorter: (a, b) => (metrics.get(a.id)?.cpuRatio ?? 0) - (metrics.get(b.id)?.cpuRatio ?? 0),
|
||||
render: (_, host) => <MetricCpuRatio metrics={metrics.get(host.id)} />,
|
||||
hidden: !showRatios,
|
||||
},
|
||||
{
|
||||
title: "Cores",
|
||||
sorter: (a, b) => (
|
||||
(metrics.get(a.id)?.cpuUsed ?? 0) - (metrics.get(b.id)?.cpuUsed ?? 0)
|
||||
),
|
||||
render: (_, host) => <MetricCpuUsed metrics={metrics.get(host.id)} />,
|
||||
hidden: !showNumbers,
|
||||
},
|
||||
{
|
||||
title: "Mem%",
|
||||
sorter: (a, b) => (
|
||||
(metrics.get(a.id)?.memoryRatio ?? 0) - (metrics.get(b.id)?.memoryRatio ?? 0)
|
||||
),
|
||||
render: (_, host) => <MetricMemoryRatio metrics={metrics.get(host.id)} />,
|
||||
hidden: !showRatios,
|
||||
},
|
||||
{
|
||||
title: "Memory",
|
||||
sorter: (a, b) => (
|
||||
(metrics.get(a.id)?.memoryUsed ?? 0) - (metrics.get(b.id)?.memoryUsed ?? 0)
|
||||
),
|
||||
render: (_, host) => <MetricMemoryUsed metrics={metrics.get(host.id)} />,
|
||||
hidden: !showNumbers,
|
||||
},
|
||||
{
|
||||
title: "Disk%",
|
||||
sorter: (a, b) => (metrics.get(a.id)?.diskAvailable ?? 0) - (metrics.get(b.id)?.diskAvailable ?? 0),
|
||||
render: (_, host) => <MetricDiskRatio metrics={metrics.get(host.id)} />,
|
||||
hidden: !showRatios,
|
||||
},
|
||||
{
|
||||
title: "Disk",
|
||||
sorter: (a, b) => (
|
||||
(metrics.get(a.id)?.diskUsed ?? 0) - (metrics.get(b.id)?.diskUsed ?? 0)
|
||||
),
|
||||
render: (_, host) => <MetricDiskUsed metrics={metrics.get(host.id)} />,
|
||||
hidden: !showNumbers,
|
||||
},
|
||||
{
|
||||
title: "Agent",
|
||||
dataIndex: "agent",
|
||||
@@ -235,6 +287,16 @@ export default function HostsPage() {
|
||||
},
|
||||
})}
|
||||
/>
|
||||
<Flex wrap="wrap" style={{ margin: "16px 0 0 0" }} gap="small">
|
||||
<label style={{ width: "14em", display: "flex", justifyContent: "space-between", marginRight: 16 }}>
|
||||
Show metric ratios:
|
||||
<Switch checked={showRatios} onChange={checked => { setShowRatios(checked); }} />
|
||||
</label>
|
||||
<label style={{ width: "14em", display: "flex", justifyContent: "space-between", marginRight: 16 }}>
|
||||
Show metric numbers:
|
||||
<Switch checked={showNumbers} onChange={checked => { setShowNumbers(checked); }} />
|
||||
</label>
|
||||
</Flex>
|
||||
<PluginExtra component="HostsPage" />
|
||||
</PageLayout>;
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
import React from "react";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { Progress } from "antd";
|
||||
|
||||
export function MetricCpuRatio(props: { metrics?: lib.SystemMetrics }) {
|
||||
if (!props.metrics) {
|
||||
return "N/A";
|
||||
}
|
||||
const ratio = props.metrics.cpuRatio;
|
||||
return <Progress status="normal" percent={Math.ceil(ratio * 100)}/>;
|
||||
}
|
||||
|
||||
export function MetricCpuUsed(props: { metrics?: lib.SystemMetrics }) {
|
||||
if (!props.metrics) {
|
||||
return "N/A";
|
||||
}
|
||||
const used = props.metrics.cpuUsed.toLocaleString(undefined, { maximumFractionDigits: 1 });
|
||||
const capacity = props.metrics.cpuCapacity;
|
||||
return `${used} / ${capacity}`;
|
||||
}
|
||||
|
||||
export function MetricMemoryRatio(props: { metrics?: lib.SystemMetrics }) {
|
||||
if (!props.metrics) {
|
||||
return "N/A";
|
||||
}
|
||||
const ratio = props.metrics.memoryRatio;
|
||||
return <Progress status="normal" percent={Math.ceil(ratio * 100)}/>;
|
||||
}
|
||||
|
||||
export function MetricMemoryUsed(props: { metrics?: lib.SystemMetrics }) {
|
||||
if (!props.metrics) {
|
||||
return "N/A";
|
||||
}
|
||||
const used = lib.formatBytes(props.metrics.memoryUsed);
|
||||
const capacity = lib.formatBytes(props.metrics.memoryCapacity);
|
||||
return `${used} / ${capacity}`;
|
||||
}
|
||||
|
||||
export function MetricDiskRatio(props: { metrics?: lib.SystemMetrics }) {
|
||||
if (!props.metrics) {
|
||||
return "N/A";
|
||||
}
|
||||
const ratio = props.metrics.diskRatio;
|
||||
return <Progress status="normal" percent={Math.ceil(ratio * 100)}/>;
|
||||
}
|
||||
|
||||
export function MetricDiskUsed(props: { metrics?: lib.SystemMetrics }) {
|
||||
if (!props.metrics) {
|
||||
return "N/A";
|
||||
}
|
||||
const used = lib.formatBytes(props.metrics.diskUsed);
|
||||
const capacity = lib.formatBytes(props.metrics.diskCapacity);
|
||||
return `${used} / ${capacity}`;
|
||||
}
|
||||
Reference in new issue
Block a user