| 1 |
/* |
| 2 |
* Copyright (C) 2020 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
import { journal } from "journal"; |
| 8 |
import { superuser } from "superuser"; |
| 9 |
|
| 10 |
import React from 'react'; |
| 11 |
import { Alert, AlertActionCloseButton } from "@patternfly/react-core/dist/esm/components/Alert/index.js"; |
| 12 |
import { EmptyStatePanel } from "cockpit-components-empty-state.jsx"; |
| 13 |
import { JournalOutput } from "cockpit-components-logs-panel.jsx"; |
| 14 |
import { ExclamationCircleIcon } from '@patternfly/react-icons'; |
| 15 |
|
| 16 |
import { getGrepFiltersFromOptions, getFilteredQuery } from "./logsHelpers.js"; |
| 17 |
|
| 18 |
// We open a couple of long-running channels with { superuser: "try" }, |
| 19 |
// so we need to reload the page if the access level changes. |
| 20 |
superuser.reload_page_on_change(); |
| 21 |
|
| 22 |
const _ = cockpit.gettext; |
| 23 |
// Stop stream when entries > QUERY_MORE after clicking 'Load earlier entries' button |
| 24 |
const QUERY_MORE = 1000; |
| 25 |
// Stop stream when entries > QUERY_COUNT |
| 26 |
const QUERY_COUNT = 5000; |
| 27 |
|
| 28 |
export class JournalBox extends React.Component { |
| 29 |
constructor(props) { |
| 30 |
super(props); |
| 31 |
this.state = { |
| 32 |
cursor: undefined, |
| 33 |
loading: true, |
| 34 |
logs: [], |
| 35 |
streamFinished: false, |
| 36 |
didntReachStart: true, |
| 37 |
}; |
| 38 |
|
| 39 |
this.appendEntries = this.appendEntries.bind(this); |
| 40 |
this.followingProcs = []; |
| 41 |
this.loadServiceFilters = this.loadServiceFilters.bind(this); |
| 42 |
this.prependEntries = this.prependEntries.bind(this); |
| 43 |
this.procs = []; |
| 44 |
this.updateQuery = this.updateQuery.bind(this); |
| 45 |
this.queryError = this.queryError.bind(this); |
| 46 |
|
| 47 |
this.options = cockpit.location.options; |
| 48 |
} |
| 49 |
|
| 50 |
componentDidMount() { |
| 51 |
cockpit.addEventListener("locationchanged", this.updateQuery); |
| 52 |
this.updateQuery(); |
| 53 |
} |
| 54 |
|
| 55 |
componentDidUpdate(prevProps) { |
| 56 |
if (prevProps.dataFollowing != this.props.dataFollowing) { |
| 57 |
if (this.props.dataFollowing) { |
| 58 |
const cursor = document.querySelector(".cockpit-logline"); |
| 59 |
if (cursor) |
| 60 |
this.follow(cursor.getAttribute("data-cursor")); |
| 61 |
else |
| 62 |
this.follow(); |
| 63 |
} else { |
| 64 |
this.stopFollowing(); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
updateQuery() { |
| 70 |
this.stop(); |
| 71 |
|
| 72 |
this.options = cockpit.location.options; |
| 73 |
this.match = getGrepFiltersFromOptions({ options: this.options })[1]; |
| 74 |
const { dataFollowing, defaultSince, updateIdentifiersList, setFilteredQuery } = this.props; |
| 75 |
const { priority, grep, boot, since, until } = this.options; |
| 76 |
let last = dataFollowing ? null : 1; |
| 77 |
let count = 0; |
| 78 |
let oldest = null; |
| 79 |
const all = boot === undefined && since === undefined && until === undefined; |
| 80 |
|
| 81 |
this.out = new JournalOutput(this.options); |
| 82 |
this.renderer = journal.renderer(this.out); |
| 83 |
|
| 84 |
const tags_match = []; |
| 85 |
this.match.forEach(field => { |
| 86 |
if (!field.startsWith("SYSLOG_IDENTIFIER")) |
| 87 |
tags_match.push(field); |
| 88 |
}); |
| 89 |
|
| 90 |
const journalctlOptions = { |
| 91 |
boot, |
| 92 |
follow: false, /* follow: Show only the most recent journal entries, and continuously print new entries as they are appended to the journal. */ |
| 93 |
grep, |
| 94 |
priority, |
| 95 |
reverse: true, /* reverse: Reverse output so that the newest entries are displayed first */ |
| 96 |
since: since || defaultSince, |
| 97 |
until |
| 98 |
}; |
| 99 |
|
| 100 |
setFilteredQuery(getFilteredQuery({ match: this.match, options: journalctlOptions })); |
| 101 |
|
| 102 |
if (updateIdentifiersList) |
| 103 |
this.loadServiceFilters(tags_match, journalctlOptions); |
| 104 |
|
| 105 |
this.setState({ loading: true, didntReachStart: false, streamFinished: false, logs: [] }); |
| 106 |
|
| 107 |
const promise = journal.journalctl(this.match, journalctlOptions) |
| 108 |
.fail(this.queryError) |
| 109 |
.stream(entries => { |
| 110 |
if (!last) { |
| 111 |
last = entries[0].__CURSOR; |
| 112 |
this.follow(last); |
| 113 |
} |
| 114 |
count += entries.length; |
| 115 |
this.appendEntries(entries); |
| 116 |
oldest = entries[entries.length - 1].__CURSOR; |
| 117 |
if (count >= QUERY_COUNT) { |
| 118 |
this.setState({ didntReachStart: true, cursor: oldest }); |
| 119 |
promise.stop(); |
| 120 |
} |
| 121 |
}) |
| 122 |
.done(() => { |
| 123 |
this.setState({ streamFinished: true }); |
| 124 |
|
| 125 |
if (!last && !promise.stopped) { |
| 126 |
const journalctlOptions = { |
| 127 |
boot, |
| 128 |
count: 0, |
| 129 |
follow: true, |
| 130 |
grep, |
| 131 |
priority, |
| 132 |
since, |
| 133 |
until, |
| 134 |
}; |
| 135 |
this.followingProcs.push(journal.journalctl(this.match, journalctlOptions) |
| 136 |
.fail(this.queryError) |
| 137 |
.stream(entries => { |
| 138 |
this.prependEntries(entries); |
| 139 |
})); |
| 140 |
} |
| 141 |
if (!all) |
| 142 |
this.setState({ didntReachStart: true, cursor: oldest }); |
| 143 |
}) |
| 144 |
.always(() => this.setState({ loading: false })); |
| 145 |
this.procs.push(promise); |
| 146 |
} |
| 147 |
|
| 148 |
queryError(error) { |
| 149 |
this.setState({ error: cockpit.message(error) }); |
| 150 |
} |
| 151 |
|
| 152 |
prependEntries(entries) { |
| 153 |
for (let i = 0; i < entries.length; i++) { |
| 154 |
const serviceTag = entries[i].SYSLOG_IDENTIFIER; |
| 155 |
this.renderer.prepend(entries[i]); |
| 156 |
// Only update if the service is not yet known |
| 157 |
if (serviceTag && !this.props.currentIdentifiers?.includes(serviceTag)) |
| 158 |
// Due to asynchronous nature it needs to be checked whether this |
| 159 |
// identifier is not yet defined. The previous check could be omitted |
| 160 |
// and only this one used but let's try to trigger as few updates as possible |
| 161 |
this.props.setCurrentIdentifiers(identifiers => { |
| 162 |
if (!identifiers.includes(serviceTag)) |
| 163 |
return [...identifiers, serviceTag]; |
| 164 |
return identifiers; |
| 165 |
}); |
| 166 |
} |
| 167 |
this.renderer.prepend_flush(); |
| 168 |
|
| 169 |
this.setState({ logs: this.out.logs, loading: false }); |
| 170 |
} |
| 171 |
|
| 172 |
appendEntries(entries) { |
| 173 |
for (let i = 0; i < entries.length; i++) |
| 174 |
this.renderer.append(entries[i]); |
| 175 |
this.renderer.append_flush(); |
| 176 |
|
| 177 |
this.setState({ logs: this.out.logs, loading: false }); |
| 178 |
} |
| 179 |
|
| 180 |
follow(cursor) { |
| 181 |
const { priority, until, grep } = this.options; |
| 182 |
|
| 183 |
const journalctlOptions = { |
| 184 |
count: 0, |
| 185 |
cursor: cursor || null, |
| 186 |
follow: true, |
| 187 |
grep, |
| 188 |
priority, |
| 189 |
until, |
| 190 |
}; |
| 191 |
this.followingProcs.push(journal.journalctl(this.match, journalctlOptions) |
| 192 |
.fail(this.queryError) |
| 193 |
.stream(entries => { |
| 194 |
if (entries[0].__CURSOR == cursor) |
| 195 |
entries.shift(); |
| 196 |
this.prependEntries(entries); |
| 197 |
})); |
| 198 |
} |
| 199 |
|
| 200 |
loadServiceFilters(match, options) { |
| 201 |
// Ideally this would use `--output cat --output-fields SYSLOG_IDENTIFIER` and do |
| 202 |
// without `sh -ec`, grep, sort, replaceAll and all of those ugly stuff |
| 203 |
// For that we however need newer systemd that includes https://github.com/systemd/systemd/issues/13937 |
| 204 |
const currentServices = new Set(); |
| 205 |
const service_options = Object.assign({ output: "verbose" }, options); |
| 206 |
let cmd = journal.build_cmd(match, service_options); |
| 207 |
|
| 208 |
// cribbed from Python's shlex.quote() |
| 209 |
cmd = cmd.map(i => `'` + i.replaceAll(`'`, `'"'"'`) + `'`).join(" "); |
| 210 |
cmd = "set -o pipefail; " + cmd + " | grep SYSLOG_IDENTIFIER= | sort -u"; |
| 211 |
cockpit.spawn(["/bin/bash", "-ec", cmd], { superuser: "try", err: "message" }) |
| 212 |
.then(entries => { |
| 213 |
entries.split("\n").forEach(entry => { |
| 214 |
if (entry) |
| 215 |
currentServices.add(entry.substring(entry.indexOf('=') + 1)); |
| 216 |
}); |
| 217 |
}) |
| 218 |
.catch(e => { |
| 219 |
// grep returns `1` when nothing to match, but in that case message is empty |
| 220 |
if (e.message) |
| 221 |
console.log("Failed to load services:", e.message); |
| 222 |
}) |
| 223 |
.finally(() => { |
| 224 |
this.props.setCurrentIdentifiers(Array.from(currentServices)); |
| 225 |
}); |
| 226 |
} |
| 227 |
|
| 228 |
stop() { |
| 229 |
this.procs.forEach(proc => proc.stop()); |
| 230 |
this.followingProcs.forEach(proc => proc.stop()); |
| 231 |
} |
| 232 |
|
| 233 |
stopFollowing() { |
| 234 |
this.followingProcs.forEach(proc => proc.stop()); |
| 235 |
} |
| 236 |
|
| 237 |
render() { |
| 238 |
const { priority, grep } = this.options; |
| 239 |
const noLogs = !this.state.logs.length; |
| 240 |
let error = null; |
| 241 |
if (this.state.error) |
| 242 |
error = ( |
| 243 |
<Alert variant="danger" |
| 244 |
isInline |
| 245 |
actionClose={<AlertActionCloseButton onClose={() => this.setState({ error: undefined })} />} |
| 246 |
title={_("Failed to fetch logs")}> |
| 247 |
{this.state.error} |
| 248 |
</Alert> |
| 249 |
); |
| 250 |
|
| 251 |
if (!this.state.logs.length && this.state.loading) |
| 252 |
return ( |
| 253 |
<> |
| 254 |
{error} |
| 255 |
<EmptyStatePanel loading title={_("Loading...")} /> |
| 256 |
</> |
| 257 |
); |
| 258 |
|
| 259 |
/* Journalctl command stream finished and there are not more entries to query */ |
| 260 |
if (!this.state.logs.length && !this.state.didntReachStart && this.state.streamFinished) { |
| 261 |
return ( |
| 262 |
<div id="start-box" className="journal-start"> |
| 263 |
{error} |
| 264 |
<EmptyStatePanel action={_("Clear all filters")} |
| 265 |
icon={ExclamationCircleIcon} |
| 266 |
actionVariant="link" |
| 267 |
onAction={() => cockpit.location.go('/')} |
| 268 |
paragraph={_("Can not find any logs using the current combination of filters.")} |
| 269 |
title={_("No logs found")} |
| 270 |
/> |
| 271 |
</div> |
| 272 |
); |
| 273 |
} |
| 274 |
const loadEarlier = ( |
| 275 |
/* Show 'Load earlier entries' button if we didn't reach start yet */ |
| 276 |
this.state.didntReachStart |
| 277 |
? <EmptyStatePanel action={_("Load earlier entries")} |
| 278 |
icon={noLogs ? ExclamationCircleIcon : undefined} |
| 279 |
isActionInProgress={this.state.loading} |
| 280 |
onAction={() => { |
| 281 |
let count = 0; |
| 282 |
this.setState({ loading: true }); |
| 283 |
|
| 284 |
const journalctlOptions = { |
| 285 |
cursor: this.state.cursor, |
| 286 |
follow: false, |
| 287 |
grep, |
| 288 |
priority, |
| 289 |
reverse: true, |
| 290 |
}; |
| 291 |
this.setState({ didntReachStart: false }); |
| 292 |
const promise = journal.journalctl(this.match, journalctlOptions) |
| 293 |
.fail(this.queryError) |
| 294 |
.stream(entries => { |
| 295 |
if (entries[0].__CURSOR == this.state.cursor) |
| 296 |
entries.shift(); |
| 297 |
count += entries.length; |
| 298 |
this.appendEntries(entries); |
| 299 |
if (count >= QUERY_MORE) { |
| 300 |
const stopped = entries[entries.length - 1].__CURSOR; |
| 301 |
this.setState({ didntReachStart: true, cursor: stopped, loading: false }); |
| 302 |
promise.stop(); |
| 303 |
} |
| 304 |
}) |
| 305 |
.done(() => { |
| 306 |
this.setState({ streamFinished: true, loading: false }); |
| 307 |
}); |
| 308 |
this.procs.push(promise); |
| 309 |
}} |
| 310 |
paragraph={noLogs ? _("You may try to load older entries.") : ""} |
| 311 |
title={noLogs ? _("No logs found") : ""} |
| 312 |
loading={this.state.loading} /> |
| 313 |
: null |
| 314 |
); |
| 315 |
|
| 316 |
return ( |
| 317 |
<> |
| 318 |
{error} |
| 319 |
{this.state.logs.length |
| 320 |
? <div id="journal-logs" className="panel panel-default cockpit-log-panel" role="table"> |
| 321 |
{this.state.logs} |
| 322 |
</div> |
| 323 |
: null} |
| 324 |
<div id="start-box" className="journal-start"> |
| 325 |
{loadEarlier} |
| 326 |
</div> |
| 327 |
</> |
| 328 |
); |
| 329 |
} |
| 330 |
} |