|
| 1 | +/* |
| 2 | + * This file is part of Cockpit. |
| 3 | + * |
| 4 | + * Copyright (C) 2025 Red Hat, Inc. |
| 5 | + * |
| 6 | + * Cockpit is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU Lesser General Public License as published by |
| 8 | + * the Free Software Foundation; either version 2.1 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * Cockpit is distributed in the hope that it will be useful, but |
| 12 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with Cockpit; If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +// bump |
| 21 | +import cockpit from "cockpit"; |
| 22 | +import React, { useState } from "react"; |
| 23 | + |
| 24 | +import { CardBody } from "@patternfly/react-core/dist/esm/components/Card/index.js"; |
| 25 | +import { DescriptionList } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js"; |
| 26 | +import { Dropdown, DropdownItem, KebabToggle } from '@patternfly/react-core/dist/esm/deprecated/components/Dropdown/index.js'; |
| 27 | +import { ExclamationCircleIcon, ExclamationTriangleIcon } from "@patternfly/react-icons"; |
| 28 | +import { Flex } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 29 | +import { Icon } from "@patternfly/react-core/dist/esm/components/Icon/index.js"; |
| 30 | + |
| 31 | +import { format_temperature } from "../utils.js"; |
| 32 | +import { superuser } from "superuser.js"; |
| 33 | +import { StorageCard, StorageDescription } from "../pages.jsx"; |
| 34 | +import { useEvent } from "hooks.js"; |
| 35 | + |
| 36 | +const _ = cockpit.gettext; |
| 37 | + |
| 38 | +const selftestStatusDescription = { |
| 39 | + // Shared values |
| 40 | + success: _("Successful"), |
| 41 | + aborted: _("Aborted"), |
| 42 | + inprogress: _("In progress"), |
| 43 | + |
| 44 | + // SATA special values |
| 45 | + interrupted: _("Interrupted"), |
| 46 | + fatal: _("Did not complete"), |
| 47 | + error_unknown: _("Failed (Unknown)"), |
| 48 | + error_electrical: _("Failed (Electrical)"), |
| 49 | + error_servo: _("Failed (Servo)"), |
| 50 | + error_read: _("Failed (Read)"), |
| 51 | + error_handling: _("Failed (Damaged)"), |
| 52 | + |
| 53 | + // NVMe special values |
| 54 | + ctrl_reset: _("Aborted by a Controller Level Reset"), |
| 55 | + ns_removed: _("Aborted due to a removal of a namespace from the namespace inventory"), |
| 56 | + aborted_format: _("Aborted due to the processing of a Format NVM command"), |
| 57 | + fatal_error: _("A fatal error occurred during the self-test operation"), |
| 58 | + unknown_seg_fail: _("Completed with a segment that failed and the segment that failed is not known"), |
| 59 | + known_seg_fail: _("Completed with one or more failed segments"), |
| 60 | + aborted_unknown: _("Aborted for unknown reason"), |
| 61 | + aborted_sanitize: _("Aborted due to a sanitize operation"), |
| 62 | +}; |
| 63 | + |
| 64 | +// NVMe reports reasons why selftest failed |
| 65 | +const nvmeCriticalWarning = { |
| 66 | + spare: _("Spare capacity is below the threshold"), |
| 67 | + temperature: _("Temperature outside of recommended thresholds"), |
| 68 | + degraded: _("Degraded"), |
| 69 | + readonly: _("All media is in read-only mode"), |
| 70 | + volatile_mem: _("Volatile memory backup failed"), |
| 71 | + pmr_readonly: _("Persistent memory has become read-only") |
| 72 | +}; |
| 73 | + |
| 74 | +const SmartActions = ({ smart_info }) => { |
| 75 | + const [isKebabOpen, setKebabOpen] = useState(false); |
| 76 | + const smartSelftestStatus = smart_info.SmartSelftestStatus; |
| 77 | + |
| 78 | + const runSelfTest = (type) => { |
| 79 | + smart_info.SmartSelftestStart(type, {}); |
| 80 | + }; |
| 81 | + |
| 82 | + const abortSelfTest = () => { |
| 83 | + smart_info.SmartSelftestAbort({}); |
| 84 | + }; |
| 85 | + |
| 86 | + const testDisabled = !superuser.allowed || smartSelftestStatus === "inprogress"; |
| 87 | + |
| 88 | + const actions = [ |
| 89 | + <DropdownItem key="smart-short-test" |
| 90 | + isDisabled={testDisabled} |
| 91 | + onClick={() => { setKebabOpen(false); runSelfTest('short') }}> |
| 92 | + {_("Run short test")} |
| 93 | + </DropdownItem>, |
| 94 | + <DropdownItem key="smart-extended-test" |
| 95 | + isDisabled={testDisabled} |
| 96 | + onClick={() => { setKebabOpen(false); runSelfTest('extended') }}> |
| 97 | + {_("Run extended test")} |
| 98 | + </DropdownItem>, |
| 99 | + <DropdownItem key="abort-smart-test" |
| 100 | + isDisabled={testDisabled} |
| 101 | + onClick={() => { setKebabOpen(false); abortSelfTest() }}> |
| 102 | + {_("Abort test")} |
| 103 | + </DropdownItem>, |
| 104 | + ]; |
| 105 | + |
| 106 | + return ( |
| 107 | + <Dropdown toggle={<KebabToggle onToggle={(_, isOpen) => setKebabOpen(isOpen)} />} |
| 108 | + isPlain |
| 109 | + isOpen={isKebabOpen} |
| 110 | + position="right" |
| 111 | + id="smart-actions" |
| 112 | + dropdownItems={actions} |
| 113 | + /> |
| 114 | + ); |
| 115 | +}; |
| 116 | + |
| 117 | +export const isSmartOK = (drive_type, smart_info) => { |
| 118 | + return (drive_type === "ata" && !smart_info.SmartFailing) || |
| 119 | + (drive_type === "nvme" && smart_info.SmartCriticalWarning.length === 0); |
| 120 | +}; |
| 121 | + |
| 122 | +export const SmartCard = ({ card, smart_info, drive_type }) => { |
| 123 | + useEvent(superuser, "changed"); |
| 124 | + |
| 125 | + const powerOnHours = (drive_type === "ata") |
| 126 | + ? Math.floor(smart_info.SmartPowerOnSeconds / 3600) |
| 127 | + : smart_info.SmartPowerOnHours; |
| 128 | + |
| 129 | + const smartOK = isSmartOK(drive_type, smart_info); |
| 130 | + |
| 131 | + const status = selftestStatusDescription[smart_info.SmartSelftestStatus] + |
| 132 | + ((smart_info.SmartSelftestStatus === "inprogress" && smart_info.SmartSelftestPercentRemaining !== -1) |
| 133 | + ? `, ${100 - smart_info.SmartSelftestPercentRemaining}%` |
| 134 | + : ""); |
| 135 | + |
| 136 | + const assesment = ( |
| 137 | + <Flex spaceItems={{ default: 'spaceItemsXs' }}> |
| 138 | + { !smartOK && |
| 139 | + <Icon status="danger"> |
| 140 | + <ExclamationCircleIcon /> |
| 141 | + </Icon> |
| 142 | + } |
| 143 | + { drive_type === "ata" && !smartOK && |
| 144 | + <span className="cockpit-disk-failing">{_("Disk is failing")}</span> |
| 145 | + } |
| 146 | + { drive_type === "nvme" && !smartOK && |
| 147 | + (<span className="cockpit-disk-failing"> |
| 148 | + {_("Disk is failing") + ": " + smart_info.SmartCriticalWarning.map(reason => nvmeCriticalWarning[reason]).join(", ")} |
| 149 | + </span>) |
| 150 | + } |
| 151 | + { smartOK && |
| 152 | + <span>{_("Disk is OK")}</span> |
| 153 | + } |
| 154 | + { smart_info.SmartTemperature > 0 |
| 155 | + ? <span>({format_temperature(smart_info.SmartTemperature)})</span> |
| 156 | + : null |
| 157 | + } |
| 158 | + </Flex> |
| 159 | + ); |
| 160 | + |
| 161 | + return ( |
| 162 | + <StorageCard card={card} actions={<SmartActions smart_info={smart_info} />}> |
| 163 | + <CardBody> |
| 164 | + <DescriptionList isHorizontal horizontalTermWidthModifier={{ default: '20ch' }}> |
| 165 | + <StorageDescription title={_("Assessment")}> |
| 166 | + {assesment} |
| 167 | + </StorageDescription> |
| 168 | + <StorageDescription title={_("Power on hours")} |
| 169 | + value={cockpit.format(_("$0 hours"), powerOnHours)} |
| 170 | + /> |
| 171 | + <StorageDescription title={_("Self-test status")} |
| 172 | + value={status} |
| 173 | + /> |
| 174 | + {drive_type === "ata" && smart_info.SmartNumBadSectors > 0 && |
| 175 | + <StorageDescription title={_("Number of bad sectors")}> |
| 176 | + <Flex flexWrap={{ default: "nowrap" }} spaceItems={{ default: "spaceItemsXs" }}> |
| 177 | + <Icon status="warning"> |
| 178 | + <ExclamationTriangleIcon /> |
| 179 | + </Icon> |
| 180 | + {smart_info.SmartNumBadSectors} |
| 181 | + </Flex> |
| 182 | + </StorageDescription> |
| 183 | + } |
| 184 | + {drive_type === "ata" && smart_info.SmartNumAttributesFailing > 0 && |
| 185 | + <StorageDescription title={_("Attributes failing")}> |
| 186 | + <Flex flexWrap={{ default: "nowrap" }} spaceItems={{ default: "spaceItemsXs" }}> |
| 187 | + <Icon status="warning"> |
| 188 | + <ExclamationTriangleIcon /> |
| 189 | + </Icon> |
| 190 | + {smart_info.SmartNumAttributesFailing} |
| 191 | + </Flex> |
| 192 | + </StorageDescription> |
| 193 | + } |
| 194 | + </DescriptionList> |
| 195 | + </CardBody> |
| 196 | + </StorageCard> |
| 197 | + ); |
| 198 | +}; |
0 commit comments