From 3afee58045fb0af42724255fb7fbf53516620359 Mon Sep 17 00:00:00 2001 From: Edward Hope-Morley Date: Sat, 11 Jan 2025 13:24:48 +0000 Subject: [PATCH] Use journald for kernlogs if kern.log not found On some hosts or sosreports kern.log may be missing so we fall back to journald for calltrace checking if this is the case. --- hotsos/core/host_helpers/cli/cli.py | 11 ++++++++++- .../core/plugins/kernel/kernlog/calltrace.py | 18 +++++++++++++++--- hotsos/core/plugins/kernel/kernlog/common.py | 11 +++++++---- hotsos/core/plugins/kernel/kernlog/events.py | 18 +++++++++++++++--- 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/hotsos/core/host_helpers/cli/cli.py b/hotsos/core/host_helpers/cli/cli.py index 93f8536a5..4925b0757 100644 --- a/hotsos/core/host_helpers/cli/cli.py +++ b/hotsos/core/host_helpers/cli/cli.py @@ -64,6 +64,9 @@ def __init__(self, *args, **kwargs): def format_journalctl_cmd(self, **kwargs): """ Add optional extras to journalctl command. """ + if kwargs.get("opts"): + self.cmd = f"{self.cmd} {kwargs.get('opts')}" + if kwargs.get("unit"): self.cmd = f"{self.cmd} --unit {kwargs.get('unit')}" @@ -84,7 +87,13 @@ def __init__(self, *args, **kwargs): self.register_hook("pre-exec", self.preformat_sos_journalctl) def preformat_sos_journalctl(self, **kwargs): - self.path = f"journalctl -oshort-iso -D {self.path}" + default_opts = '-oshort-iso' + if kwargs.get("opts"): + self.path = (f"journalctl {default_opts} {kwargs.get('opts')} " + f"-D {self.path}") + else: + self.path = f"journalctl {default_opts} -D {self.path}" + if kwargs.get("unit"): self.path = f"{self.path} --unit {kwargs.get('unit')}" diff --git a/hotsos/core/plugins/kernel/kernlog/calltrace.py b/hotsos/core/plugins/kernel/kernlog/calltrace.py index 7a2f6d7f4..5d3b0a60e 100644 --- a/hotsos/core/plugins/kernel/kernlog/calltrace.py +++ b/hotsos/core/plugins/kernel/kernlog/calltrace.py @@ -1,11 +1,14 @@ import abc +import os import re +from hotsos.core.config import HotSOSConfig from hotsos.core.log import log from hotsos.core.search import ( SearchDef, SequenceSearchDef, ) +from hotsos.core.host_helpers.cli import CLIHelperFile from hotsos.core.plugins.kernel.kernlog.common import ( CallTraceHeuristicBase, CallTraceStateBase, @@ -483,10 +486,19 @@ def __init__(self, *args, **kwargs): self.run() def run(self): - for tracetype in self.tracetypes: - self.searcher.add(tracetype.searchdef, self.path) + path = self.path + with CLIHelperFile() as cli: + if not os.path.exists(path): + path = cli.journalctl(opts='-k') + else: + if HotSOSConfig.use_all_logs: + path = f"{path}*" + + for tracetype in self.tracetypes: + self.searcher.add(tracetype.searchdef, path) + + self.results = self.searcher.run() - self.results = self.searcher.run() for tracetype in self.tracetypes: if isinstance(tracetype.searchdef, SequenceSearchDef): results = self.results.find_sequence_sections( diff --git a/hotsos/core/plugins/kernel/kernlog/common.py b/hotsos/core/plugins/kernel/kernlog/common.py index 36ed15d49..205311c41 100644 --- a/hotsos/core/plugins/kernel/kernlog/common.py +++ b/hotsos/core/plugins/kernel/kernlog/common.py @@ -100,8 +100,11 @@ def __init__(self): @property def path(self): - path = os.path.join(HotSOSConfig.data_root, 'var/log/kern.log') - if HotSOSConfig.use_all_logs: - return f"{path}*" + """ + Return path we want to search. By default we search kern.log but that + may not exist e.g. sosreport does not collect it if systemd-journald is + running. In that case we need to fallback to using the journal. - return path + @return: path + """ + return os.path.join(HotSOSConfig.data_root, 'var/log/kern.log') diff --git a/hotsos/core/plugins/kernel/kernlog/events.py b/hotsos/core/plugins/kernel/kernlog/events.py index 62991d93d..c428d6c5b 100644 --- a/hotsos/core/plugins/kernel/kernlog/events.py +++ b/hotsos/core/plugins/kernel/kernlog/events.py @@ -1,4 +1,8 @@ +import os + +from hotsos.core.config import HotSOSConfig from hotsos.core.log import log +from hotsos.core.host_helpers.cli import CLIHelperFile from hotsos.core.plugins.kernel.kernlog.common import KernLogBase from hotsos.core.search import SearchDef @@ -7,10 +11,18 @@ class KernLogEvents(KernLogBase): """ Kern log events info. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - for event in [self.over_mtu_dropped_packets_search_def]: - self.searcher.add(event, self.path) + path = self.path + with CLIHelperFile() as cli: + if not os.path.exists(path): + path = cli.journalctl(opts='-k') + else: + if HotSOSConfig.use_all_logs: + path = f"{path}*" + + for event in [self.over_mtu_dropped_packets_search_def]: + self.searcher.add(event, path) - self.results = self.searcher.run() + self.results = self.searcher.run() @property def over_mtu_dropped_packets_search_def(self):