Debian Codegraph @

pkg/storaged/drive/smart-details.jsx

raw
1 /*
2 * Copyright (C) 2025 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7 import React from "react";
8
9 import { CardBody } from "@patternfly/react-core/dist/esm/components/Card/index.js";
10 import { DescriptionList } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js";
11 import { DropdownList } from "@patternfly/react-core/dist/esm/components/Dropdown";
12 import { ExclamationCircleIcon, ExclamationTriangleIcon } from "@patternfly/react-icons";
13 import { Flex } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js";
14 import { Icon } from "@patternfly/react-core/dist/esm/components/Icon/index.js";
15
16 import { format_temperature } from "../utils.js";
17 import { StorageCard, StorageDescription } from "../pages.jsx";
18 import { StorageBarMenu, StorageMenuItem } from "../storage-controls.jsx";
19
20 const _ = cockpit.gettext;
21
22 const selftestStatusDescription = {
23 // Shared values
24 success: _("Successful"),
25 aborted: _("Aborted"),
26 inprogress: _("In progress"),
27
28 // SATA special values
29 interrupted: _("Interrupted"),
30 fatal: _("Did not complete"),
31 error_unknown: _("Failed (Unknown)"),
32 error_electrical: _("Failed (Electrical)"),
33 error_servo: _("Failed (Servo)"),
34 error_read: _("Failed (Read)"),
35 error_handling: _("Failed (Damaged)"),
36
37 // NVMe special values
38 ctrl_reset: _("Aborted by a Controller Level Reset"),
39 ns_removed: _("Aborted due to a removal of a namespace from the namespace inventory"),
40 aborted_format: _("Aborted due to the processing of a Format NVM command"),
41 fatal_error: _("A fatal error occurred during the self-test operation"),
42 unknown_seg_fail: _("Completed with a segment that failed and the segment that failed is not known"),
43 known_seg_fail: _("Completed with one or more failed segments"),
44 aborted_unknown: _("Aborted for unknown reason"),
45 aborted_sanitize: _("Aborted due to a sanitize operation"),
46 };
47
48 // NVMe reports reasons why selftest failed
49 const nvmeCriticalWarning = {
50 spare: _("Spare capacity is below the threshold"),
51 temperature: _("Temperature outside of recommended thresholds"),
52 degraded: _("Degraded"),
53 readonly: _("All media is in read-only mode"),
54 volatile_mem: _("Volatile memory backup failed"),
55 pmr_readonly: _("Persistent memory has become read-only")
56 };
57
58 const SmartActions = ({ smart_info }) => {
59 const smartSelftestStatus = smart_info.SmartSelftestStatus;
60
61 const runSelfTest = (type) => {
62 smart_info.SmartSelftestStart(type, {});
63 };
64
65 const abortSelfTest = () => {
66 smart_info.SmartSelftestAbort({});
67 };
68
69 const testDisabled = smartSelftestStatus === "inprogress";
70
71 const actionItems = (
72 <DropdownList>
73 <StorageMenuItem isDisabled={testDisabled}
74 onClick={() => { runSelfTest('short') }}
75 >
76 {_("Run short test")}
77 </StorageMenuItem>
78 <StorageMenuItem isDisabled={testDisabled}
79 onClick={() => { runSelfTest('extended') }}
80 >
81 {_("Run extended test")}
82 </StorageMenuItem>
83 <StorageMenuItem isDisabled={!testDisabled}
84 onClick={() => { abortSelfTest() }}
85 >
86 {_("Abort test")}
87 </StorageMenuItem>
88 </DropdownList>
89 );
90
91 return (
92 <StorageBarMenu isKebab label={_("Actions")} menuItems={actionItems} />
93 );
94 };
95
96 export const isSmartOK = (drive_type, smart_info) => {
97 return (drive_type === "ata" && !smart_info.SmartFailing) ||
98 (drive_type === "nvme" && smart_info.SmartCriticalWarning.length === 0);
99 };
100
101 export const SmartCard = ({ card, smart_info, drive_type }) => {
102 const powerOnHours = (drive_type === "ata")
103 ? Math.floor(smart_info.SmartPowerOnSeconds / 3600)
104 : smart_info.SmartPowerOnHours;
105
106 const smartOK = isSmartOK(drive_type, smart_info);
107
108 const status = selftestStatusDescription[smart_info.SmartSelftestStatus] +
109 ((smart_info.SmartSelftestStatus === "inprogress" && smart_info.SmartSelftestPercentRemaining !== -1)
110 ? `, ${100 - smart_info.SmartSelftestPercentRemaining}%`
111 : "");
112
113 const assessment = (
114 <Flex spaceItems={{ default: 'spaceItemsXs' }}>
115 { !smartOK &&
116 <Icon status="danger">
117 <ExclamationCircleIcon />
118 </Icon>
119 }
120 { drive_type === "ata" && !smartOK &&
121 <span className="cockpit-disk-failing">{_("Disk is failing")}</span>
122 }
123 { drive_type === "nvme" && !smartOK &&
124 (<span className="cockpit-disk-failing">
125 {_("Disk is failing") + ": " + smart_info.SmartCriticalWarning.map(reason => nvmeCriticalWarning[reason]).join(", ")}
126 </span>)
127 }
128 { smartOK &&
129 <span>{_("Disk is OK")}</span>
130 }
131 { smart_info.SmartTemperature > 0
132 ? <span>({format_temperature(smart_info.SmartTemperature)})</span>
133 : null
134 }
135 </Flex>
136 );
137
138 return (
139 <StorageCard card={card} actions={<SmartActions smart_info={smart_info} />}>
140 <CardBody>
141 <DescriptionList isHorizontal horizontalTermWidthModifier={{ default: '20ch' }}>
142 <StorageDescription title={_("Assessment")}>
143 {assessment}
144 </StorageDescription>
145 <StorageDescription title={_("Power on hours")}
146 value={cockpit.format(_("$0 hours"), powerOnHours)}
147 />
148 <StorageDescription title={_("Self-test status")}
149 value={status}
150 />
151 {drive_type === "ata" && smart_info.SmartNumBadSectors > 0 &&
152 <StorageDescription title={_("Number of bad sectors")}>
153 <Flex flexWrap={{ default: "nowrap" }} spaceItems={{ default: "spaceItemsXs" }}>
154 <Icon status="warning">
155 <ExclamationTriangleIcon />
156 </Icon>
157 {smart_info.SmartNumBadSectors}
158 </Flex>
159 </StorageDescription>
160 }
161 {drive_type === "ata" && smart_info.SmartNumAttributesFailing > 0 &&
162 <StorageDescription title={_("Attributes failing")}>
163 <Flex flexWrap={{ default: "nowrap" }} spaceItems={{ default: "spaceItemsXs" }}>
164 <Icon status="warning">
165 <ExclamationTriangleIcon />
166 </Icon>
167 {smart_info.SmartNumAttributesFailing}
168 </Flex>
169 </StorageDescription>
170 }
171 </DescriptionList>
172 </CardBody>
173 </StorageCard>
174 );
175 };