| 1 |
/* |
| 2 |
* Copyright (C) 2021 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
import cockpit from "cockpit"; |
| 6 |
import { journal } from "journal"; |
| 7 |
|
| 8 |
// Sometimes `journalctl` can be compiled without `PCRE2` which means |
| 9 |
// that `--grep` is not usable. Hide the search box in such cases. |
| 10 |
export function checkJournalctlGrep(setShowTextSearch) { |
| 11 |
cockpit.spawn(["journalctl", "--version"]) |
| 12 |
.then(m => { |
| 13 |
if (m.indexOf("-PCRE2") !== -1) { |
| 14 |
setShowTextSearch(false); |
| 15 |
} else { |
| 16 |
setShowTextSearch(true); |
| 17 |
} |
| 18 |
}); |
| 19 |
} |
| 20 |
|
| 21 |
// Build the journalctl query for the inline help popover |
| 22 |
export const getFilteredQuery = ({ match, options }) => { |
| 23 |
const cmd = journal.build_cmd(match, options); |
| 24 |
const filtered_cmd = cmd.filter(i => i !== "-q" && i !== "--output=json"); |
| 25 |
if (filtered_cmd[filtered_cmd.length - 1] == "--") |
| 26 |
filtered_cmd.pop(); |
| 27 |
|
| 28 |
return filtered_cmd.join(" "); |
| 29 |
}; |
| 30 |
|
| 31 |
export const getGrepFiltersFromOptions = ({ options }) => { |
| 32 |
const grep = options.grep || ""; |
| 33 |
let full_grep = ""; |
| 34 |
const match = []; |
| 35 |
|
| 36 |
// `prio` is a legacy name. Accept it, but don't generate it |
| 37 |
const prio_level = options.priority || options.prio || "err"; |
| 38 |
full_grep += "priority:" + prio_level + " "; |
| 39 |
|
| 40 |
if (options.service) { |
| 41 |
options.service.split(",").forEach(s => { |
| 42 |
if (!s.endsWith(".service")) |
| 43 |
s = s + ".service"; |
| 44 |
match.push('_SYSTEMD_UNIT=' + s, "+", "COREDUMP_UNIT=" + s, "+", "UNIT=" + s); |
| 45 |
}); |
| 46 |
full_grep += "service:" + options.service + " "; |
| 47 |
} else if (options["user-service"]) { |
| 48 |
options["user-service"].split(",").forEach(s => { |
| 49 |
if (!s.endsWith(".service")) |
| 50 |
s = s + ".service"; |
| 51 |
match.push('_SYSTEMD_USER_UNIT=' + s, "+", "COREDUMP_USER_UNIT=" + s, "+", "USER_UNIT=" + s); |
| 52 |
}); |
| 53 |
full_grep += "user-service:" + options["user-service"] + " "; |
| 54 |
} |
| 55 |
|
| 56 |
if (options.tag && options.tag !== "*") { |
| 57 |
match.push('SYSLOG_IDENTIFIER=' + options.tag); |
| 58 |
full_grep += "identifier:" + options.tag + " "; |
| 59 |
} |
| 60 |
|
| 61 |
if (options.boot) |
| 62 |
full_grep += "boot:" + options.boot + " "; |
| 63 |
|
| 64 |
if (options.since) |
| 65 |
full_grep += "since:" + options.since.replaceAll(" ", "\\ ") + " "; |
| 66 |
|
| 67 |
if (options.until) |
| 68 |
full_grep += "until:" + options.until.replaceAll(" ", "\\ ") + " "; |
| 69 |
|
| 70 |
// Other filters may be passed as well |
| 71 |
Object.keys(options).forEach(k => { |
| 72 |
if (k === k.toUpperCase() && options[k]) { |
| 73 |
options[k].split(",").forEach(v => match.push(k + "=" + v)); |
| 74 |
full_grep += k + '=' + options[k] + " "; |
| 75 |
} |
| 76 |
}); |
| 77 |
|
| 78 |
full_grep += grep; |
| 79 |
|
| 80 |
return [full_grep, match]; |
| 81 |
}; |
| 82 |
|
| 83 |
const split_search = (text) => { |
| 84 |
let last_i = 0; |
| 85 |
const words = []; |
| 86 |
|
| 87 |
// Add space so the following loop can always pick group on space (without trailing |
| 88 |
// space the last group would not be recognized and it would need a special check) |
| 89 |
if (text.length && text[text.length - 1] !== " ") |
| 90 |
text += " "; |
| 91 |
|
| 92 |
for (let i = 1; i < text.length; i++) { |
| 93 |
if (text[i] === " " && text[i - 1] !== "\\") { |
| 94 |
words.push(text.substring(last_i, i).replaceAll("\\ ", " ")); |
| 95 |
last_i = i + 1; |
| 96 |
} |
| 97 |
} |
| 98 |
return words; |
| 99 |
}; |
| 100 |
|
| 101 |
export const getOptionsFromTextInput = (value) => { |
| 102 |
const new_items = {}; |
| 103 |
const values = split_search(value) |
| 104 |
.filter(item => { |
| 105 |
let s = item.split("="); |
| 106 |
if (s.length === 2 && s[0] === s[0].toUpperCase()) { |
| 107 |
new_items[s[0]] = s[1]; |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
const well_know_keys = ["since", "until", "boot", "priority", "follow", "service", "identifier"]; |
| 112 |
const map_keys = (key) => { |
| 113 |
if (key === "identifier") |
| 114 |
return "tag"; |
| 115 |
if (key == "service") |
| 116 |
return "unit"; |
| 117 |
return key; |
| 118 |
}; |
| 119 |
s = item.split(/:(.*)/); |
| 120 |
if (s.length >= 2 && well_know_keys.includes(s[0])) { |
| 121 |
new_items[map_keys(s[0])] = s[1]; |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
return true; |
| 126 |
}); |
| 127 |
new_items.grep = values.join(" "); |
| 128 |
return new_items; |
| 129 |
}; |