| 1 |
/* |
| 2 |
* Copyright (C) 2025 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import React, { useState } from 'react'; |
| 7 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 8 |
import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 9 |
import { Icon } from "@patternfly/react-core/dist/esm/components/Icon/index.js"; |
| 10 |
import { ExclamationTriangleIcon } from "@patternfly/react-icons"; |
| 11 |
|
| 12 |
import cockpit from "cockpit"; |
| 13 |
|
| 14 |
import { useInit } from 'hooks'; |
| 15 |
|
| 16 |
const _ = cockpit.gettext; |
| 17 |
|
| 18 |
export const UncleanShutdownStatus = () => { |
| 19 |
const [uncleanShutdownId, setUncleanShutdownId] = useState(null); |
| 20 |
const [uncleanShutdownStatusVisible, setUncleanShutdownStatusVisible] = useState(false); |
| 21 |
|
| 22 |
useInit(() => { |
| 23 |
cockpit.spawn( |
| 24 |
["last", "--system", "--limit=2", "--time-format=iso", "shutdown", "reboot"], |
| 25 |
{ environ: ["LC_ALL=C"], err: "message", } |
| 26 |
).then((data) => { |
| 27 |
const previous_boot = data.split("\n")[1]; |
| 28 |
if (previous_boot === undefined) |
| 29 |
return; |
| 30 |
|
| 31 |
// "crash" on wtmpdb, "still running" on util-linux |
| 32 |
if (!previous_boot.includes("crash") && !previous_boot.includes("still running")) |
| 33 |
return; |
| 34 |
|
| 35 |
const lines = previous_boot.split(/ +/); |
| 36 |
const started = new Date(lines[4]); |
| 37 |
if (isNaN(started)) { |
| 38 |
console.warn("cannot parse start date of last line", previous_boot); |
| 39 |
} else { |
| 40 |
const epoch = started.getTime().toString(); |
| 41 |
setUncleanShutdownId(epoch); |
| 42 |
setUncleanShutdownStatusVisible(epoch != cockpit.sessionStorage.getItem("dismissed-unclean-shutdown-id")); |
| 43 |
} |
| 44 |
}); |
| 45 |
}); |
| 46 |
|
| 47 |
if (!uncleanShutdownId || !uncleanShutdownStatusVisible) { |
| 48 |
return null; |
| 49 |
} |
| 50 |
|
| 51 |
function hideAlert() { |
| 52 |
setUncleanShutdownStatusVisible(false); |
| 53 |
cockpit.sessionStorage.setItem('dismissed-unclean-shutdown-id', uncleanShutdownId); |
| 54 |
} |
| 55 |
|
| 56 |
return ( |
| 57 |
<li id="unclean-shutdown-status"> |
| 58 |
<Flex flexWrap={{ default: 'nowrap' }}> |
| 59 |
<FlexItem> |
| 60 |
<Icon status="danger"> |
| 61 |
<ExclamationTriangleIcon /> |
| 62 |
</Icon> |
| 63 |
</FlexItem> |
| 64 |
<div> |
| 65 |
<div className="pf-v6-u-text-break-word pf-v6-u-text-color-status-danger"> |
| 66 |
{_("Unclean shutdown")} |
| 67 |
</div> |
| 68 |
<Flex> |
| 69 |
<Button variant="link" isInline |
| 70 |
className="pf-v6-u-font-size-sm" |
| 71 |
onClick={() => cockpit.jump("/system/logs#/?boot=-1")}> |
| 72 |
{_("View logs")} |
| 73 |
</Button> |
| 74 |
<Button variant="link" isInline |
| 75 |
className="pf-v6-u-font-size-sm" |
| 76 |
onClick={() => hideAlert()} |
| 77 |
id="unclean-shutdown-status-dismiss"> |
| 78 |
{_("Dismiss")} |
| 79 |
</Button> |
| 80 |
</Flex> |
| 81 |
</div> |
| 82 |
</Flex> |
| 83 |
</li> |
| 84 |
); |
| 85 |
}; |