/* * Copyright (C) 2017 Red Hat, Inc. * SPDX-License-Identifier: LGPL-2.1-or-later */ import React, { useState, createRef } from 'react'; import { Alert } from "@patternfly/react-core/dist/esm/components/Alert/index.js"; import { Breadcrumb, BreadcrumbItem } from "@patternfly/react-core/dist/esm/components/Breadcrumb/index.js"; import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; import { Card, CardBody, CardHeader, CardTitle } from "@patternfly/react-core/dist/esm/components/Card/index.js"; import { Gallery } from "@patternfly/react-core/dist/esm/layouts/Gallery/index.js"; import { DescriptionList, DescriptionListDescription, DescriptionListGroup, DescriptionListTerm } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js"; import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; import { Grid, GridItem } from "@patternfly/react-core/dist/esm/layouts/Grid/index.js"; import { Icon } from "@patternfly/react-core/dist/esm/components/Icon/index.js"; import { Modal, ModalBody, ModalFooter, ModalHeader } from '@patternfly/react-core/dist/esm/components/Modal/index.js'; import { Page, PageGroup, PageSection, PageBreadcrumb } from "@patternfly/react-core/dist/esm/components/Page/index.js"; import { Popover } from "@patternfly/react-core/dist/esm/components/Popover/index.js"; import { Progress, ProgressVariant } from "@patternfly/react-core/dist/esm/components/Progress/index.js"; import { Stack, StackItem } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js"; import { Switch } from "@patternfly/react-core/dist/esm/components/Switch/index.js"; import { Content, ContentVariants } from "@patternfly/react-core/dist/esm/components/Content/index.js"; import { Tooltip } from "@patternfly/react-core/dist/esm/components/Tooltip/index.js"; import { Table, Thead, Td, Th, Tr, Tbody, TableGridBreakpoint, TableVariant, TableText } from '@patternfly/react-table'; import { AngleRightIcon, AngleDownIcon, ExclamationTriangleIcon, ExclamationCircleIcon, CogIcon, ExternalLinkAltIcon, ResourcesFullIcon, ResourcesAlmostFullIcon, ResourcesAlmostEmptyIcon } from '@patternfly/react-icons'; import cockpit from 'cockpit'; import * as machine_info from "../lib/machine-info.js"; import * as service from "service"; import * as timeformat from "timeformat"; import { superuser } from "superuser"; import { journal } from "journal"; import { read_os_release } from "os-release"; import { get_manifest_config_matchlist } from "utils"; import { useObject, useEvent, useInit } from "hooks.js"; import { WithDialogs, useDialogs } from "dialogs.jsx"; import { SimpleSelect } from "cockpit-components-simple-select.jsx"; import { CheckboxSelect } from "cockpit-components-checkbox-select.jsx"; import { EmptyStatePanel } from "../lib/cockpit-components-empty-state.jsx"; import { JournalOutput } from "cockpit-components-logs-panel.jsx"; import { install_dialog } from "cockpit-components-install-dialog.jsx"; import { ModalError } from "cockpit-components-inline-notification.jsx"; import { FirewalldRequest } from "cockpit-components-firewalld-request.jsx"; import "./metrics.scss"; import "journal.css"; import { getPackageManager } from 'packagemanager.js'; const MSEC_PER_H = 3600000; const INTERVAL = 5000; const SAMPLES_PER_H = MSEC_PER_H / INTERVAL; const SAMPLES_PER_MIN = SAMPLES_PER_H / 60; const SVG_YMAX = (SAMPLES_PER_MIN - 1); const LOAD_HOURS = 12; const _ = cockpit.gettext; // format Date as YYYY-MM-DD HH:mm:ss UTC which is human friendly and systemd compatible const formatUTC_ISO = t => `${t.getUTCFullYear()}-${t.getUTCMonth() + 1}-${t.getUTCDate()} ${t.getUTCHours()}:${t.getUTCMinutes()}:${t.getUTCSeconds()} UTC`; // podman's containers cgroup const podmanCgroupRe = /libpod-(?[a-z|0-9]{64})\.scope$/; // cgroup userid const useridCgroupRe = /user-(?\d+).slice/; // keep track of maximum values for unbounded data, so that we can normalize it properly // pre-init them to avoid inflating noise let scaleSatCPU = 4; let scaleUseDisks = 10000; // KB/s let scaleUseNetwork = 100000; // B/s let numCpu = 1; let memTotal; // bytes let swapTotal; // bytes, can be undefined const machine_info_promise = machine_info.cpu_ram_info(); machine_info_promise.then(info => { numCpu = info.cpus; memTotal = info.memory; swapTotal = info.swap; }); // round up to the nearest number that has all zeroes except for the first digit // avoids over-aggressive scaling, but needs scaling more often const scaleForValue = x => { const scale = Math.pow(10, Math.floor(Math.log10(x))); // this can be tweaked towards "less rescaling" with an additional scalar, like "x * 1.5 / scale" return Math.ceil(x / scale) * scale; }; const RESOURCES = { use_cpu: { name: _("CPU usage"), event_description: _("CPU"), // all in msec/s normalize: ([nice, user, sys]) => (nice + user + sys) / 1000 / numCpu, format: ([nice, user, sys]) => `${_("nice")}: ${Math.round(nice / 10)}%, ${_("user")}: ${Math.round(user / 10)}%, ${_("sys")}: ${Math.round(sys / 10)}%`, }, sat_cpu: { name: _("Load"), event_description: _("Load"), // unitless, unbounded, dynamic scaling for normalization normalize: load => Math.min(load, scaleSatCPU) / scaleSatCPU, format: load => cockpit.format_number(load), }, use_memory: { name: _("Memory usage"), event_description: _("Memory"), // assume used == total - available normalize: ([totalKiB, availKiB]) => 1 - (availKiB / totalKiB), format: ([totalKiB, availKiB]) => `${cockpit.format_bytes((totalKiB - availKiB) * 1024)} / ${cockpit.format_bytes(totalKiB * 1024)}`, }, sat_memory: { name: _("Swap out"), event_description: _("Swap"), // page/s, unbounded, and mostly 0; just categorize into "nothing" (most of the time), // "a little" (< 1000 pages), and "a lot" (> 1000 pages) normalize: swapout => swapout > 1000 ? 1 : (swapout > 1 ? 0.3 : 0), format: swapout => cockpit.format(cockpit.ngettext("$0 page", "$0 pages", Math.floor(swapout)), Math.floor(swapout)), }, use_disks: { name: _("Disk I/O"), event_description: _("Disk I/O"), // KiB/s, unbounded, dynamic scaling for normalization normalize: KiBps => KiBps / scaleUseDisks, format: KiBps => cockpit.format_bytes_per_sec(KiBps * 1024), }, use_network: { name: _("Network I/O"), event_description: _("Network I/O"), // B/s, unbounded, dynamic scaling for normalization normalize: bps => bps / scaleUseNetwork, format: bps => cockpit.format_bytes_per_sec(bps), }, }; const CURRENT_METRICS = [ { name: "cpu.basic.user", derive: "rate" }, { name: "cpu.basic.system", derive: "rate" }, { name: "cpu.basic.nice", derive: "rate" }, { name: "memory.used" }, { name: "memory.swap-used" }, { name: "disk.all.read", units: "bytes", derive: "rate" }, { name: "disk.all.written", units: "bytes", derive: "rate" }, { name: "network.interface.rx", units: "bytes", derive: "rate" }, { name: "network.interface.tx", units: "bytes", derive: "rate" }, { name: "cgroup.cpu.usage", derive: "rate" }, { name: "cgroup.memory.usage" }, { name: "cpu.core.user", derive: "rate" }, { name: "cpu.core.system", derive: "rate" }, { name: "cpu.core.nice", derive: "rate" }, { name: "disk.dev.read", units: "bytes", derive: "rate" }, { name: "disk.dev.written", units: "bytes", derive: "rate" }, { name: "mount.total", units: "bytes" }, { name: "mount.used", units: "bytes" }, ]; const CPU_TEMPERATURE_METRICS = [ { name: "cpu.temperature" }, ]; const PRIVILEGED_METRICS = [ { name: "disk.cgroup.read", units: "bytes", derive: "rate" }, { name: "disk.cgroup.written", units: "bytes", derive: "rate" }, ]; const HISTORY_METRICS = [ // CPU utilization { name: "kernel.all.cpu.nice", derive: "rate" }, { name: "kernel.all.cpu.user", derive: "rate" }, { name: "kernel.all.cpu.sys", derive: "rate" }, // CPU saturation { name: "kernel.all.load" }, // memory utilization (unit: KiB) { name: "mem.physmem" }, // mem.util.used is useless, it includes cache (unit: KiB) { name: "mem.util.available" }, // memory saturation { name: "swap.pagesout", derive: "rate" }, // disk utilization; despite the name, the unit is in KiB! (pminfo -d -F disk.all.total_bytes) { name: "disk.all.total_bytes", derive: "rate" }, // network utilization { name: "network.interface.total.bytes", derive: "rate" }, ]; function debug() { if (window.debugging == "all" || window.debugging?.includes("metrics")) console.debug.apply(console, arguments); } // metrics channel samples are compressed, see // https://github.com/cockpit-project/cockpit/blob/main/doc/protocol.md#payload-metrics1 // samples is the compressed metrics channel value, state the last valid values (initialize once to empty array) function decompress_samples(samples, state) { samples.forEach((sample, i) => { if (sample instanceof Array) { if (!state[i]) // uninitialized, create empty array state[i] = []; sample.forEach((inst, k) => { if (typeof inst === 'number') state[i][k] = inst; }); } else if (typeof sample === 'number') { state[i] = sample; } }); } function make_rows(rows, rowProps, columnLabels) { return rows.map((columns, rowIndex) => {columns.map((column, columnIndex) => {column} )} ); } async function get_pcp_packages() { const os_release = await read_os_release(); const pcp_packages = ["pcp"]; // PCP contains the Python module on Arch Linux, for all other distro's it is split up. if (os_release.ID !== "arch") { pcp_packages.push("python3-pcp"); } return pcp_packages; } class CurrentMetrics extends React.Component { constructor(props) { super(props); this.metrics_channel = null; this.temperature_channel = null; this.privileged_channel = null; this.samples = []; this.temperatureSamples = []; this.privilegedSamples = []; this.netInterfacesNames = []; this.cgroupCPUNames = []; this.cgroupMemoryNames = []; this.cgroupDiskNames = []; this.disksNames = []; this.cpuTemperature = null; this.cpuTemperatureColors = { textColor: "", iconColor: "", icon: null, }; this.state = { userid: null, memUsed: 0, // bytes swapUsed: null, // bytes cpuUsed: 0, // percentage cpuCoresUsed: [], // [ percentage ] loadAvg: null, // [ 1min, 5min, 15min ] disksRead: 0, // B/s disksWritten: 0, // B/s mounts: [], // [{ target (string), use (percent), avail (bytes) }] netInterfacesRx: [], netInterfacesTx: [], topServicesCPU: [], // [ { name, percent } ] topServicesMemory: [], // [ { name, bytes } ] topServicesDiskIO: [], // [ [ name, read, write ] ] podNameMapping: {}, // { uid -> containerid -> name } }; this.onVisibilityChange = this.onVisibilityChange.bind(this); this.onMetricsUpdate = this.onMetricsUpdate.bind(this); this.onTemperatureUpdate = this.onTemperatureUpdate.bind(this); this.onPrivilegedMetricsUpdate = this.onPrivilegedMetricsUpdate.bind(this); this.updateLoad = this.updateLoad.bind(this); cockpit.addEventListener("visibilitychange", this.onVisibilityChange); this.onVisibilityChange(); // there is no internal metrics channel for load yet; see https://github.com/cockpit-project/cockpit/pull/14510 this.updateLoad(); } componentDidMount() { superuser.addEventListener("changed", () => this.setState({ podNameMapping: {} })); cockpit.user().then(user => this.setState({ userid: user.id })); } onVisibilityChange() { if (cockpit.hidden && this.temperature_channel !== null) { this.temperature_channel.removeEventListener("message", this.onTemperatureUpdate); this.temperature_channel.close(); this.temperature_channel = null; } if (cockpit.hidden && this.privileged_channel !== null) { this.privileged_channel.removeEventListener("message", this.onPrivilegedMetricsUpdate); this.privileged_channel.close(); this.privileged_channel = null; } if (cockpit.hidden && this.metrics_channel !== null) { this.metrics_channel.removeEventListener("message", this.onMetricsUpdate); this.metrics_channel.close(); this.metrics_channel = null; return; } if (!cockpit.hidden && (this.temperature_channel === null)) { this.temperature_channel = cockpit.channel({ payload: "metrics1", source: "internal", interval: INTERVAL, metrics: CPU_TEMPERATURE_METRICS }); this.temperature_channel.addEventListener("close", (ev, error) => console.warn("CPU temperature metric closed:", error)); this.temperature_channel.addEventListener("message", this.onTemperatureUpdate); } // requires sudo access in order to sample every cgroup // limited access only allows sampling of current user's cgroups if (!cockpit.hidden && (this.privileged_channel === null)) { this.privileged_channel = cockpit.channel({ superuser: "try", payload: "metrics1", source: "internal", interval: INTERVAL, metrics: PRIVILEGED_METRICS }); this.privileged_channel.addEventListener("message", this.onPrivilegedMetricsUpdate); } if (!cockpit.hidden && this.metrics_channel === null) { this.metrics_channel = cockpit.channel({ payload: "metrics1", source: "internal", interval: INTERVAL, metrics: CURRENT_METRICS }); this.metrics_channel.addEventListener("message", this.onMetricsUpdate); } } updateLoad() { cockpit.file("/proc/loadavg").read() .then(content => { // format: three load averages, then process counters; e.g.: 0.67 1.00 0.78 2/725 87151 this.setState({ loadAvg: content.split(' ').slice(0, 3) }); // update it again regularly window.setTimeout(this.updateLoad, 5000); }) .catch(ex => { console.warn("Failed to read /proc/loadavg:", ex.toString()); this.setState({ loadAvg: null }); }); } onTemperatureUpdate(event, message) { debug("current CPU temperature message", message); const data = JSON.parse(message); if (!Array.isArray(data)) { return; } data.forEach(temperatureSamples => decompress_samples(temperatureSamples, this.temperatureSamples)); if (this.temperatureSamples[0].length > 0) { this.cpuTemperature = Math.round(Math.max(...this.temperatureSamples[0])); } else { // close the channel when bridge couldn't sample temperature this.temperature_channel.close("No samples received"); return; } if (this.cpuTemperature <= 80) { this.cpuTemperatureColors.textColor = ""; this.cpuTemperatureColors.iconColor = ""; this.cpuTemperatureColors.icon = null; } else if (this.cpuTemperature < 95) { this.cpuTemperatureColors.textColor = "text-color-warning"; this.cpuTemperatureColors.iconColor = "icon-color-warning"; this.cpuTemperatureColors.icon = ; } else { this.cpuTemperatureColors.textColor = "text-color-critical"; this.cpuTemperatureColors.iconColor = "icon-color-critical"; this.cpuTemperatureColors.icon = ; } } onMetricsUpdate(event, message) { debug("current metrics message", message); const data = JSON.parse(message); // reset state on meta messages if (!Array.isArray(data)) { this.samples = []; console.assert(data.metrics[7].name === 'network.interface.rx'); this.netInterfacesNames = data.metrics[7].instances.slice(); console.assert(data.metrics[9].name === 'cgroup.cpu.usage'); this.cgroupCPUNames = data.metrics[9].instances.slice(); this.cgroupMemoryNames = data.metrics[10].instances.slice(); console.assert(data.metrics[14].name === 'disk.dev.read'); this.disksNames = data.metrics[14].instances.slice(); console.assert(data.metrics[16].name === 'mount.total'); this.mountPoints = data.metrics[16].instances.slice(); debug("metrics message was meta, new net instance names", JSON.stringify(this.netInterfacesNames)); return; } data.forEach(samples => decompress_samples(samples, this.samples)); const newState = {}; // CPU metrics are in ms/s; divide by 10 to get percentage if (typeof this.samples[0] === 'number') { const cpu = Math.round((this.samples[0] + this.samples[1] + this.samples[2]) / 10 / numCpu); newState.cpuUsed = cpu; } newState.memUsed = this.samples[3]; newState.swapUsed = this.samples[4]; if (typeof this.samples[5] === 'number') newState.disksRead = this.samples[5]; if (typeof this.samples[6] === 'number') newState.disksWritten = this.samples[6]; newState.netInterfacesRx = this.samples[7]; newState.netInterfacesTx = this.samples[8]; // Collect CPU cores newState.cpuCoresUsed = []; if (this.samples[11] && this.samples[11].length == this.samples[12].length && this.samples[12].length == this.samples[13].length) { for (let i = 0; i < this.samples[11].length; i++) { // CPU cores metrics are in ms/s; divide by 10 to get percentage newState.cpuCoresUsed[i] = Math.round((this.samples[11][i] + this.samples[12][i] + this.samples[13][i]) / 10); } } // return [ { [key, value, is_user, is_container, userid | cgroup] } ] list of the biggest n values const n_biggest = (names, values, n) => { const merged = []; names.forEach((k, i) => { const v = values[i]; // filter out invalid values, the empty (root) cgroup, non-services if (k.endsWith('.service') && typeof v === 'number' && v != 0) { const is_user = k.match(/^user.*user@\d+\.service.+/); const label = k.replace(/.*\//, '').replace(/\.service$/, ''); // only keep cgroup basenames, and drop redundant .service suffix merged.push([label, v, is_user, false, k]); } // filter out podman containers, but only for the logged in // user or root user if the user is privileged. Other users // containers will show up under the user@uid cgroup const matches = k.match(podmanCgroupRe); if (matches && v) { let is_user = false; let uid = 0; const containerid = matches.groups.containerid; const umatches = k.match(useridCgroupRe); if (umatches) { is_user = true; uid = parseInt(umatches.groups.userid); } if (uid === 0 || this.state.userid == uid) { merged.push([containerid, v, is_user, true, uid]); } } }); merged.sort((a, b) => b[1] - a[1]); return merged.slice(0, n); }; // top 5 CPU and memory consuming systemd units const topServicesCPU = n_biggest(this.cgroupCPUNames, this.samples[9], 5); newState.topServicesCPU = topServicesCPU.map( ([key, value, is_user, is_container, userid]) => this.cgroupRow(key, is_user, is_container, userid, Number(value / 10 / numCpu).toFixed(1)) // usec/s → percent ); const topServicesMemory = n_biggest(this.cgroupMemoryNames, this.samples[10], 5); newState.topServicesMemory = topServicesMemory.map( ([key, value, is_user, is_container, userid]) => this.cgroupRow(key, is_user, is_container, userid, cockpit.format_bytes(value)) ); const notMappedContainers = topServicesMemory.concat(topServicesCPU).filter(([key, value, is_user, is_container, userid]) => is_container && this.getCachedPodName(userid, key) === undefined); if (notMappedContainers.length !== 0) { this.update_podman_name_mapping(notMappedContainers); } const mountsTotal = this.samples[16]; const mountsUsed = this.samples[17]; newState.mounts = mountsTotal.map((mountTotal, i) => { return { target: this.mountPoints[i], size: mountTotal, avail: mountTotal - mountsUsed[i], use: Math.round(mountsUsed[i] / mountTotal * 100), }; }); this.setState(newState); } onPrivilegedMetricsUpdate(event, message) { debug("process metrics message", message); const data = JSON.parse(message); if (!Array.isArray(data)) { this.cgroupDiskNames = data.metrics[0].instances.slice(); return; } data.forEach(privilegedSamples => decompress_samples(privilegedSamples, this.privilegedSamples)); // return [ name, read, write, isUser, isContainer, uid | cgroup ] const n_biggest = (n, names, valuesA, valuesB) => { const merged = []; const userSlices = {}; names.forEach((name, i) => { // filter out invalid values, the empty (root) cgroup, non-services if (name.endsWith('.service')) { // only keep cgroup basenames, and drop redundant .service suffix const label = name.replace(/.*\//, '').replace(/\.service$/, ''); const isUser = name.match(useridCgroupRe); merged.push([label, valuesA[i], valuesB[i], isUser, false, name]); return; } // filter out podman containers, but only for the logged in // user or root user if the user is privileged. Other users // containers will show up under the user@uid cgroup const matches = name.match(/libpod-(?[a-z|0-9]{64})\.scope/); if (matches && valuesA[i] !== undefined && valuesB[i] !== undefined) { const containerid = matches.groups.containerid; const umatches = name.match(useridCgroupRe); const isUser = !!umatches; const uid = parseInt(umatches?.groups.userid) || 0; if (uid === 0 || this.state.userid == uid) { merged.push([containerid, valuesA[i], valuesB[i], isUser, true, uid]); return; } } // combine user slices into user@ID // { name: [ read, write ] } const umatches = name.match(useridCgroupRe); if (umatches) { if (userSlices[umatches.groups.userid] === undefined) { userSlices[umatches.groups.userid] = [valuesA[i], valuesB[i]]; } else { userSlices[umatches.groups.userid][0] += valuesA[i]; userSlices[umatches.groups.userid][1] += valuesB[i]; } } }); Object.keys(userSlices).forEach((key) => { merged.push(["user@" + key, userSlices[key][0], userSlices[key][1], false, false]); }); // sort by overall (read + write) disk usage merged.sort((a, b) => (b[1] + b[2]) - (a[1] + a[2])); return merged.slice(0, n); }; const newState = {}; const topServicesDiskIO = n_biggest(5, this.cgroupDiskNames, this.privilegedSamples[0], this.privilegedSamples[1]); newState.topServicesDiskIO = topServicesDiskIO.filter(([_, read, write, ..._rest]) => read !== 0 || write !== 0).map(([name, read, write, isUser, isContainer, uid]) => { return this.cgroupRow(name, isUser, isContainer, uid, read > 1 ? cockpit.format_bytes_per_sec(read) : 0, write > 1 ? cockpit.format_bytes_per_sec(write) : 0); }); this.setState(newState); } getCachedPodName = (uid, containerid) => this.state.podNameMapping[uid] && this.state.podNameMapping[uid][containerid]; cgroupRow = (name, is_user, is_container, uid, ...values) => { const podman_installed = cockpit.manifests?.podman; const cgroupClickHandler = (name, isUser, isContainer, uid) => { if (isContainer) { const containerName = this.getCachedPodName(uid, name); if (containerName) { cockpit.jump("/podman#/?name=" + containerName); } else { cockpit.jump("/podman"); } } else { cockpit.jump("/system/services#/" + name + ".service" + (isUser ? "?owner=user" : "")); } }; let name_text = ( ); if (is_container && !podman_installed) { name_text = (
{name_text}
); } const values_text = values.map((value, idx) => { return {value}; }); return [name_text, ...values_text]; }; /** * Look up the container names using podman ps for the given cgroups. */ update_podman_name_mapping = cgroups => { // New mapping state const podNameMapping = {}; const promises = cgroups.map(([containerid, value, is_user, is_container, userid]) => { if (!(userid in podNameMapping)) { podNameMapping[userid] = {}; } // Always initialize the cache for when we hit an error. podNameMapping[userid][containerid] = null; if ((userid === 0 && !superuser.allowed) && userid !== this.state.userid) { return null; } return cockpit.spawn(["podman", "ps", "--format", "json"], { superuser: userid === 0 ? "required" : null }) .then(result => [result, userid]); }).filter(prom => prom !== null); Promise.all(promises).then(results => { for (const [output, uid] of results) { try { const containers = JSON.parse(output); for (const container of containers) { podNameMapping[uid][container.Id] = container.Names[0]; } } catch (err) { console.error("podman ps outputs invalid JSON", err.toString()); } } }) .catch(err => console.error("could not obtain podman names:", err)) .finally(() => this.setState(prevState => ({ podNameMapping: { ...prevState.podNameMapping, ...podNameMapping } }))); }; render() { const memUsedFraction = memTotal ? this.state.memUsed / memTotal : 0; const memAvail = memTotal ? (memTotal - this.state.memUsed) : 0; const num_cpu_str = cockpit.format(cockpit.ngettext("$0 CPU", "$0 CPUs", numCpu), numCpu); const netIO = this.netInterfacesNames.map((iface, i) => [ , this.state.netInterfacesRx[i] >= 1 ? cockpit.format_bytes_per_sec(this.state.netInterfacesRx[i]) : "0", this.state.netInterfacesTx[i] >= 1 ? cockpit.format_bytes_per_sec(this.state.netInterfacesTx[i]) : "0", ]); let swapProgress; if (swapTotal) { const swapUsedFraction = this.state.swapUsed / swapTotal; const swapAvail = swapTotal - this.state.swapUsed; swapProgress = ( 0.9 ? ProgressVariant.danger : swapUsedFraction >= 0.8 ? ProgressVariant.warning : null} label={ cockpit.format(_("$0 available"), cockpit.format_bytes(swapAvail)) } /> ); } let cores = null; let topCore = null; let allCpus = null; let cpu_label = null; if (this.state.cpuCoresUsed.length > 1) { const top_cores = this.state.cpuCoresUsed.map((v, i) => [i, v]).sort((a, b) => b[1] - a[1]) .slice(0, 16); cores = ( {top_cores.map(c => { cockpit.format(_("Core $0"), c[0]) } {c[1]}% ) } ); cpu_label = (  { cockpit.format(_("average: $0%"), this.state.cpuUsed) }  { cockpit.format(_("max: $0%"), top_cores[0][1]) } ); topCore = 90 ? ProgressVariant.danger : top_cores[0][1] >= 80 ? ProgressVariant.warning : ProgressVariant.info } measureLocation="none" />; allCpus = ( ); } else { cpu_label = this.state.cpuUsed + '%'; } const disksUsage = (this.disksNames.length > 0 && this.samples[14] && this.samples[15]) ? ( this.disksNames.map((name, i) => [ name, this.samples[14][i] >= 1 ? cockpit.format_bytes_per_sec(this.samples[14][i]) : "0", this.samples[15][i] >= 1 ? cockpit.format_bytes_per_sec(this.samples[15][i]) : "0", ]) ) : []; let allDisks = null; const rowPropsDisks = row => ({ 'device-name': row[0] }); const diskColumns = [_("Device"), _("Read"), _("Write")]; if (disksUsage.length > 1) { const disksTableContent = ( {diskColumns.map(col => )} {make_rows(disksUsage, rowPropsDisks, diskColumns)}
{col}
); allDisks = ( ); } // first element is the jump button, key is interface name const rowPropsIface = row => ({ 'data-interface': row[0].key }); const rowPropsDiskIO = row => ({ 'cgroup-name': row[0] }); const topServicesCPUColumns = [_("Service"), "%"]; const topServicesMemoryColumns = [_("Service"), _("Used")]; const ifaceColumns = [_("Interface"), _("In"), _("Out")]; return ( { _("CPU") } { this.cpuTemperature !== null && {this.cpuTemperatureColors.icon}   { cockpit.format("$0 °C", this.cpuTemperature) } }
90 ? ProgressVariant.danger : this.state.cpuUsed >= 80 ? ProgressVariant.warning : null } title={ num_cpu_str } label={ cpu_label } /> {topCore} {allCpus}
{ this.state.loadAvg && { _("Load") } { _("1 min") }: { this.state.loadAvg[0] }, { _("5 min") }: { this.state.loadAvg[1] }, { _("15 min") }: { this.state.loadAvg[2] } } { this.state.topServicesCPU.length > 0 && {make_rows(this.state.topServicesCPU, undefined, topServicesCPUColumns)}
{_("Service")} %
}
{ _("Memory") }
0.9 ? ProgressVariant.danger : memUsedFraction >= 0.8 ? ProgressVariant.warning : null} label={ memAvail ? cockpit.format(_("$0 available"), cockpit.format_bytes(memAvail)) : "" } /> {swapProgress}
{ this.state.topServicesMemory.length > 0 && {make_rows(this.state.topServicesMemory, undefined, topServicesMemoryColumns)}
{_("Service")} {_("Used")}
}
{ _("Disks") } { _("Read") } { this.state.disksRead >= 1 ? cockpit.format_bytes_per_sec(this.state.disksRead) : "0" } { _("Write") } { this.state.disksWritten >= 1 ? cockpit.format_bytes_per_sec(this.state.disksWritten) : "0" }
{allDisks}
{ this.state.mounts.map(info => { let progress = ( 90 ? ProgressVariant.danger : info.use >= 80 ? ProgressVariant.warning : null} title={info.target} label={ cockpit.format(_("$0 free"), cockpit.format_bytes(info.avail)) } /> ); if (cockpit.manifests?.storage) progress = ; return ( {progress} ); }) }
{ this.state.topServicesDiskIO.length > 0 && {make_rows(this.state.topServicesDiskIO, rowPropsDiskIO, [_("Service"), _("Read"), _("Write")])}
{_("Service")} {_("Read")} {_("Write")}
}
{ _("Network") } {ifaceColumns.map(col => )} {make_rows(netIO, rowPropsIface, ifaceColumns)}
{col}
); } } const SvgGraph = ({ data, resource, have_sat }) => { const dataPoints = key => ( "0 0, " + // start polygon at (0, 0) data.map((samples, index) => (samples && typeof samples[key] === 'number') ? parseInt(samples[key] * 100).toString() + "% " + (((index / SVG_YMAX) * 100).toString() + "%") : "").join(", ") + ", 0 " + ((data.length - 1) / SVG_YMAX) * 100 + "%" // close polygon ); return ( <>
{ have_sat &&
} ); }; class MetricsMinute extends React.Component { constructor(props) { super(props); this.state = { logs: null, logsUrl: null, }; this.onHover = this.onHover.bind(this); this.findLogs = this.findLogs.bind(this); this.spikesButtonRef = createRef(); } componentDidMount() { if (this.props.isExpanded && this.props.events) this.findLogs(this.props.events.start - 4, this.props.events.end + 4); // +- 20s } onHover(ev) { // FIXME - throttle debounce this const bounds = ev.target.getBoundingClientRect(); const offsetY = (ev.clientY - bounds.y) / bounds.height; const indexOffset = Math.floor((1 - offsetY) * SAMPLES_PER_MIN); const sample = this.props.rawData[indexOffset]; if (!sample) return; const time = this.props.startTime + this.props.minute * 60000 + indexOffset * INTERVAL; let tooltip = timeformat.timeSeconds(time) + "\n\n"; Object.entries(sample).forEach(([t, v]) => { if (v !== null && v !== undefined) tooltip += `${RESOURCES[t].name}: ${RESOURCES[t].format(v)}\n`; }); ev.target.setAttribute("title", tooltip); } findLogs(start, end) { const timestamp = this.props.startTime + (this.props.minute * 60000); const start_minute = Math.floor(start / SAMPLES_PER_MIN); const start_second = (start - (start_minute * SAMPLES_PER_MIN)) * (60 / SAMPLES_PER_MIN); const end_minute = Math.floor(end / SAMPLES_PER_MIN); const end_second = (end - (end_minute * SAMPLES_PER_MIN)) * (60 / SAMPLES_PER_MIN); const time = new Date(timestamp); time.setUTCMinutes(start_minute); time.setUTCSeconds(start_second); const since = formatUTC_ISO(time); time.setUTCMinutes(end_minute); time.setUTCSeconds(end_second); const until = formatUTC_ISO(time); const match = { priority: "info", since, until, follow: false, count: 10 }; const journalctl = journal.journalctl(match); const out = new JournalOutput(match); out.render_day_header = () => { return null }; const render = journal.renderer(out); journalctl.stream(entries => { entries.forEach(entry => render.prepend(entry)); render.prepend_flush(); }) .then(() => { let logsUrl; if (out.logs.length === 0) { // without logs, increase verbosity and time range (-15 mins to + 1 min) const since = formatUTC_ISO(new Date(timestamp - 15 * 60000)); const until = formatUTC_ISO(new Date(timestamp + 60000)); logsUrl = `/system/logs/#/?priority=debug&since=${encodeURIComponent(since)}&until=${encodeURIComponent(until)}&follow=false`; } else { // with logs, show the exact minute and same log level as on the metrics page logsUrl = `/system/logs/#/?priority=info&since=${encodeURIComponent(since)}&until=${encodeURIComponent(until)}&follow=false`; } this.setState({ logs: out.logs, logsUrl }); }); } render() { const first = this.props.data.find(i => i !== null); const graphs = Object.keys(this.props.selectedVisibility).filter(itm => this.props.selectedVisibility[itm]).map(resource => { // not all resources have a saturation metric let have_sat = !!RESOURCES["sat_" + resource]; // If there is no swap, don't render it if (resource === "memory" && !swapTotal) have_sat = false; let graph = null; if (this.props.events) { // render full SVG graphs for "expanded" minutes with events graph = ; } else if (first) { // render simple bars for "compressed" minutes without events graph = <>
{ have_sat &&
} ; } return ( ); }); let desc; if (this.props.isExpanded && this.props.booted) { const timestamp = this.props.startTime + (this.props.minute * 60000); desc = (
{_("Boot")}
); } else if (this.props.isExpanded && this.props.events) { const timestamp = this.props.startTime + (this.props.minute * 60000); const logsPanel = ( <> {(this.state.logs?.length && this.state.logsUrl) && }
{this.state.logs?.length ? this.state.logs : _("No log entries")}
); const resourceDesc = ( {this.props.events.events.map(t => RESOURCES[t].event_description).join(", ")} ); desc = ( <>
{this.state.logs?.length > 0 ? : {resourceDesc}}
{this.state.logs?.length > 0 && } ); } return (
{ this.props.isExpanded && desc }
{ graphs }
); } } class MetricsHour extends React.Component { constructor(props) { super(props); this.state = { minuteGraphs: [], minute_events: {}, isHourExpanded: false, dataItems: 0, }; this.updateGraphs = this.updateGraphs.bind(this); } componentDidMount() { this.updateGraphs(this.props.data, this.props.startTime, this.props.selectedVisibility); } shouldComponentUpdate(nextProps, nextState) { if (this.state.dataItems !== nextProps.data.length || this.state.isHourExpanded !== nextState.isHourExpanded || this.props.startTime !== nextProps.startTime || this.props.boots !== nextProps.boots || Object.keys(this.props.selectedVisibility).some(itm => this.props.selectedVisibility[itm] != nextProps.selectedVisibility[itm])) { this.updateGraphs(nextProps.data, nextProps.startTime, nextProps.selectedVisibility, nextState.isHourExpanded); return false; } return true; } // data: type → SAMPLES_PER_H objects from startTime updateGraphs(data, startTime, selectedVisibility, isHourExpanded) { const filteredData = data.map(sample => Object.keys(sample) .filter(key => selectedVisibility[key.split("_")[1]]) .reduce((cur, key) => Object.assign(cur, { [key]: sample[key] }), {})); // Normalize data const normData = filteredData.map(sample => { if (sample === null) return null; const n = {}; for (const type in sample) n[type] = (sample[type] !== null && sample[type] !== undefined) ? RESOURCES[type].normalize(sample[type]) : null; return n; }); // Count minutes to render let minutes = 60; if (this.props.clipLeading) { // When clipping of empty leading minutes is allowed, find the highest 5 minute interval with valid data let m = 55; for (; m >= 0; m = m - 5) { const dataOffset = m * SAMPLES_PER_MIN; const dataSlice = normData.slice(dataOffset, dataOffset + SAMPLES_PER_MIN * 5); if (dataSlice.some(i => i !== null && i !== undefined)) break; } minutes = m + 5; } // Compute spike events const minute_events = {}; for (const type in RESOURCES) { let prev_val = data[0] ? data[0][type] : null; normData.forEach((samples, i) => { if (samples === null) return; const value = samples[type]; // either high enough slope, or crossing the 80% threshold if (prev_val !== null && (value - prev_val > 0.25 || (prev_val < 0.75 && value >= 0.8))) { const minute = Math.floor(i / SAMPLES_PER_MIN); if (minute_events[minute] === undefined) minute_events[minute] = { events: [], start: i - 1 }; minute_events[minute].end = i; // For every minute show each type of event max once if (minute_events[minute].events.indexOf(type) === -1) minute_events[minute].events.push(type); } prev_val = value; }); } const minuteGraphs = []; for (let minute = minutes - 1; minute >= 0; --minute) { const dataOffset = minute * SAMPLES_PER_MIN; const dataSlice = normData.slice(dataOffset, dataOffset + SAMPLES_PER_MIN); const rawSlice = this.props.data.slice(dataOffset, dataOffset + SAMPLES_PER_MIN); const is_boot = this.props.boots.includes(minute); minuteGraphs.push( ); } this.setState((_, prevProps) => ({ isHourExpanded, minute_events, minuteGraphs, dataItems: prevProps.data.length })); } render() { const hourDesc = ( this.setState({ isHourExpanded })} startTime={this.props.startTime} isHourExpanded={this.state.isHourExpanded} /> ); return (
{hourDesc} {!this.state.isHourExpanded ?
{this.state.minuteGraphs}
: this.state.minuteGraphs}
); } } const HourDescription = ({ minute_events, isHourExpanded, onToggleHourExpanded, startTime }) => { const event_types = {}; Object.keys(RESOURCES).forEach(t => { event_types[t] = 0 }); Object.values(minute_events).forEach(event => { event.events.forEach(t => { event_types[t] += 1 }) }); const spikes = Object.values(event_types).reduce((acc, event_type_count) => acc + event_type_count, 0); return ( {spikes > 0 &&
} /> { dialogError && } { _("Collect metrics") } (pmlogger.service) } onChange={(_event, enable) => { // pmproxy needs pmlogger, auto-disable it setDialogLoggerValue(enable); if (!enable) setDialogProxyValue(false); }} /> { _("Export to network") } (pmproxy.service) } isDisabled={ !dialogLoggerValue } onChange={(_event, enable) => setDialogProxyValue(enable)} /> ); }; const PCPConfig = ({ buttonVariant, firewalldRequest }) => { const Dialogs = useDialogs(); const [packageInstallStatus, setPackageInstallStatus] = useState(null); const s_pmlogger = useObject(() => service.proxy("pmlogger.service"), null, []); const s_pmproxy = useObject(() => service.proxy("pmproxy.service"), null, []); // redis.service on Fedora/RHEL, redis-server.service on Debian/Ubuntu with an Alias=redis const s_redis = useObject(() => service.proxy("redis.service"), null, []); const s_redis_server = useObject(() => service.proxy("redis-server.service"), null, []); const s_valkey = useObject(() => service.proxy("valkey.service"), null, []); useEvent(superuser, "changed"); useEvent(s_pmlogger, "changed"); useEvent(s_pmproxy, "changed"); useEvent(s_redis, "changed"); useEvent(s_redis_server, "changed"); useEvent(s_valkey, "changed"); debug("PCPConfig s_pmlogger.state", s_pmlogger.state); debug("PCPConfig s_pmproxy state", s_pmproxy.state, "redis exists", s_redis.exists, "state", s_redis.state, "redis-server exists", s_redis_server.exists, "state", s_redis_server.state, "valkey exists", s_valkey.exists, "state", s_valkey.state); if (!superuser.allowed) return null; function show_dialog() { setPackageInstallStatus(null); Dialogs.show( setPackageInstallStatus("done")} />); } return ( ); }; class MetricsHistory extends React.Component { constructor(props) { super(props); // metrics data: hour timestamp → array of SAMPLES_PER_H objects of { type → value } or null this.data = {}; // timestamp of the most recent sample that we got (for auto-refresh) this.most_recent = 0; // Oldest read data this.oldest_timestamp = 0; // Timestamp representing today midnight to calculate other days for date select this.today_midnight = null; this.columns = [["cpu", _("CPU")], ["memory", _("Memory")], ["disks", _("Disk I/O")], ["network", _("Network")]]; this.state = { hours: [], // available hours for rendering in descending order loading: true, // show loading indicator metricsAvailable: true, pmLoggerState: null, error: null, selectedDate: null, packageManager: null, isBeibootBridge: false, isPythonPCPInstalled: null, selectedVisibility: this.columns.reduce((a, v) => ({ ...a, [v[0]]: true }), {}), boots: [], // journalctl --list-boots as [{started: Date, ended: Date}] }; this.handleMoreData = this.handleMoreData.bind(this); this.handleSelect = this.handleSelect.bind(this); this.handleInstall = this.handleInstall.bind(this); /* supervise pmlogger.service, to diagnose missing history */ this.pmlogger_service = service.proxy("pmlogger.service"); this.pmlogger_service.addEventListener("changed", () => { if (!invalidService(this.pmlogger_service) && this.pmlogger_service.state !== this.state.pmLoggerState) { // when it got enabled while the page is running (e.g. through Settings dialog), start data collection if (!this.state.metricsAvailable && runningService(this.pmlogger_service)) this.initialLoadData(); this.setState({ pmLoggerState: this.pmlogger_service.state }); } }); // FIXME: load less up-front, load more when scrolling machine_info_promise.then(() => this.initialLoadData()); cockpit.addEventListener("visibilitychange", () => { // update history metrics when in auto-update mode if (!cockpit.hidden && this.history_refresh_timer) this.load_data(this.most_recent); }); } // load and render the last 24 hours (plus current one) initially; this needs numCpu initialized for correct scaling initialLoadData() { cockpit.spawn(["date", "+%s"]) .then(out => { const now = parseInt(out.trim()) * 1000; const current_hour = Math.floor(now / MSEC_PER_H) * MSEC_PER_H; this.most_recent = current_hour; this.today_midnight = new Date(current_hour).setHours(0, 0, 0, 0); const selectedDate = parseInt(cockpit.location.options.date) || this.today_midnight; if (selectedDate !== this.today_midnight) this.load_data(selectedDate, 24 * SAMPLES_PER_H, true); else this.load_data(current_hour - LOAD_HOURS * MSEC_PER_H, undefined, true); this.setState({ metricsAvailable: true, selectedDate, }); }) .catch(ex => this.setState({ error: ex.toString() })); } async componentDidMount() { let packageManager = false; try { await getPackageManager(); packageManager = true; } catch (err) { packageManager = false; } // HACK: See https://github.com/cockpit-project/cockpit/issues/19143 let cmdline = ""; try { cmdline = await cockpit.file("/proc/self/cmdline").read(); } catch (_ex) {} const isBeibootBridge = cmdline?.includes("ic# cockpit-bridge"); this.setState({ packageManager, isBeibootBridge }); try { // Only 14 days of metrics are shown // Requires superuser on Debian/Ubuntu, on Fedora/Arch users in the wheel group can list without superuser. const output = await cockpit.spawn(["journalctl", "--list-boots", "--since", "-15d", "--output", "json"], { superuser: "try" }); const list_boots = JSON.parse(output); const boots = list_boots.map(boot => { return { started: new Date(boot?.first_entry / 1000), ended: new Date(boot?.last_entry / 1000), current_boot: boot?.index === 0, }; }); this.setState({ boots }); } catch (exc) { console.warn("journalctl --list-boots failed", exc); } } handleMoreData() { this.load_data(this.oldest_timestamp - (LOAD_HOURS * MSEC_PER_H), LOAD_HOURS * SAMPLES_PER_H, true); } handleSelect(sel) { // Stop fetching of new data if (this.history_refresh_timer !== null) { window.clearTimeout(this.history_refresh_timer); this.history_refresh_timer = null; } this.oldest_timestamp = 0; cockpit.location.go([], Object.assign(cockpit.location.options, { date: sel })); this.setState({ selectedDate: sel, hours: [], }, () => this.load_data(sel, sel === this.today_midnight ? undefined : 24 * SAMPLES_PER_H, true)); } async handleInstall() { install_dialog(await get_pcp_packages()) .then(() => this.initialLoadData()) .catch(() => null); // ignore cancel } load_data(load_timestamp, limit, show_spinner) { if (show_spinner) this.setState({ loading: true }); this.oldest_timestamp = this.oldest_timestamp > load_timestamp || this.oldest_timestamp === 0 ? load_timestamp : this.oldest_timestamp; let current_hour; // hour of timestamp, from most recent meta message let hour_index; // index within data[current_hour] array const current_sample = []; // last valid value, for decompression const new_hours = new Set(); // newly seen hours during this load this.history_refresh_timer = null; const metrics = cockpit.channel({ payload: "metrics1", interval: INTERVAL, source: "pcp-archive", timestamp: load_timestamp, limit, metrics: HISTORY_METRICS, "omit-instances": ["lo"], }); metrics.addEventListener("message", (event, message) => { debug("history metrics message", message); message = JSON.parse(message); const init_current_hour = () => { if (!this.data[current_hour]) this.data[current_hour] = []; // When limit is considered only add hours in this time range if (!limit || load_timestamp + (limit * INTERVAL) >= current_hour) new_hours.add(current_hour); }; // meta message if (!Array.isArray(message)) { current_hour = Math.floor(message.timestamp / MSEC_PER_H) * MSEC_PER_H; init_current_hour(); hour_index = Math.floor((message.timestamp - current_hour) / INTERVAL); console.assert(hour_index < SAMPLES_PER_H); debug("message is metadata; time stamp", message.timestamp, "=", timeformat.dateTime(message.timestamp), "for current_hour", current_hour, "=", timeformat.dateTime(current_hour), "hour_index", hour_index); return; } debug("message is", message.length, "samples data for current hour", current_hour, "=", timeformat.dateTime(current_hour)); message.forEach((samples, i) => { decompress_samples(samples, current_sample); /* don't overwrite existing data with null data; this often happens at the first * data point when "rate" metrics cannot be calculated yet */ if (typeof current_sample[0] !== 'number' && this.data[current_hour][hour_index]) { debug("load_data", load_timestamp, ": ignoring sample #", i, ":", JSON.stringify(current_sample), "current data sample", JSON.stringify(this.data[current_hour][hour_index])); return; } // TODO: eventually track/display this by-interface? const use_network = current_sample[8].reduce((acc, cur) => acc + cur, 0); const sat_cpu = typeof current_sample[3][1] === 'number' ? current_sample[3][1] : null; // instances: (15min, 1min, 5min), pick 1min this.data[current_hour][hour_index] = { use_cpu: typeof current_sample[2] === 'number' ? [current_sample[0], current_sample[1], current_sample[2]] : null, sat_cpu, use_memory: typeof current_sample[5] === 'number' ? [current_sample[4], current_sample[5]] : null, sat_memory: current_sample[6], use_disks: current_sample[7], use_network, }; // keep track of maximums of unbounded values, for dynamic scaling if (sat_cpu > scaleSatCPU) scaleSatCPU = scaleForValue(sat_cpu); if (current_sample[7] > scaleUseDisks) scaleUseDisks = scaleForValue(current_sample[7]); if (use_network > scaleUseNetwork) scaleUseNetwork = scaleForValue(use_network); if (++hour_index === SAMPLES_PER_H) { current_hour += MSEC_PER_H; hour_index = 0; init_current_hour(); debug("hour overflow, advancing to", current_hour, "=", timeformat.dateTime(current_hour)); } }); // update most recent sample timestamp this.most_recent = Math.max(this.most_recent, current_hour + (hour_index - 5) * INTERVAL); debug("most recent timestamp is now", this.most_recent, "=", timeformat.dateTime(this.most_recent)); }); metrics.addEventListener("close", (event, message) => { if (message.problem) { debug("could not load metrics:", message.problem); this.setState({ loading: false, metricsAvailable: false, isPythonPCPInstalled: message?.message !== "python3-pcp not installed", }); } else { this.setState({ isPythonPCPInstalled: true }); debug("loaded metrics for timestamp", timeformat.dateTime(load_timestamp), "new hours", JSON.stringify(Array.from(new_hours))); new_hours.forEach(hour => debug("hour", hour, "data", JSON.stringify(this.data[hour]))); const hours = Array.from(new Set([...this.state.hours, ...new_hours])); // sort in descending order hours.sort((a, b) => b - a); // re-render this.setState({ hours, loading: false }); // trigger automatic update every minute when visible if (!limit) { this.history_refresh_timer = window.setTimeout(() => { if (!cockpit.hidden) this.load_data(this.most_recent); }, 60000); } } metrics.close(); }); } render() { // on a single machine, cockpit-pcp depends on pcp; but this may not be the case in the beiboot scenario, // so additionally check if pcp is available on the logged in target machine if (this.state.isPythonPCPInstalled === false || this.pmlogger_service.exists === false) return {_("Install PCP support")}} />; if (!this.state.metricsAvailable) { let action; let paragraph; if (this.pmlogger_service.state === 'stopped') { paragraph = _("pmlogger.service is not running"); action = ; } else { if (this.pmlogger_service.state === 'failed') paragraph = _("pmlogger.service has failed"); else /* running, or initialization hangs */ paragraph = _("pmlogger.service is failing to collect data"); action = ; } return ; } if (this.state.error) return ; let nodata_alert = null; const lastHourIndex = this.state.hours.length - 1; if (!this.state.loading && this.state.hours.length > 0 && this.oldest_timestamp < this.state.hours[lastHourIndex]) { let t1; let t2; if (this.state.hours[lastHourIndex] - this.oldest_timestamp < 24 * MSEC_PER_H) { t1 = timeformat.time(this.oldest_timestamp); t2 = timeformat.time(this.state.hours[lastHourIndex]); } else { t1 = timeformat.dateTime(this.oldest_timestamp); t2 = timeformat.dateTime(this.state.hours[lastHourIndex]); } nodata_alert = ; } if (!this.state.loading && this.state.hours.length === 0) nodata_alert = ; // generate selection of last 14 days const options = Array(15).fill() .map((_undef, i) => { const date = this.today_midnight - i * 86400000; const text = i == 0 ? _("Today") : timeformat.weekdayDate(date); return { value: date, content: text }; }); function Label(props) { return (
1 ? " have-saturation" : "")}> {props.label} { props.items.map(i => {i}) }
); } const columnVisibilityMenuItems = this.columns.map(itm => { return { value: itm[0], content: itm[1], "data-label": itm[0], }; }); const selections = ( this.columns .filter(itm => this.state.selectedVisibility[itm[0]]) .map(itm => itm[0]) ); return (
{ this.setState(prevState => ({ selectedVisibility: { ...prevState.selectedVisibility, [selection]: checked } })); }} selected={selections} options={columnVisibilityMenuItems} /> {[["cpu", _("CPU usage/load")], ["memory", _("Memory usage/swap")], ["disks", _("Disk I/O")], ["network", _("Network")]] .filter(itm => this.state.selectedVisibility[itm[0]]) .map(itm => (
{itm[1]} ))}
{this.state.selectedVisibility.cpu &&
<> { this.state.hours.length > 0 && { this.state.hours.map((time, i) => { const date_time = new Date(time); const boot_minutes = this.state.boots.filter(reboot => reboot.started.getDay() === date_time.getDay() && reboot.started.getYear() === date_time.getYear() && reboot.started.getHours() === date_time.getHours()) .map(reboot => reboot.started.getMinutes()); const showHeader = i == 0 || timeformat.date(time) != timeformat.date(this.state.hours[i - 1]); return ( {showHeader && } ); })} } {nodata_alert}
{ this.state.loading ? : }
); } } export const Application = () => { const [firewalldRequest, setFirewalldRequest] = useState(null); return ( cockpit.jump("/system")} className="pf-v6-c-breadcrumb__link">{_("Overview")} {_("Metrics and history")} { firewalldRequest && } ); };