Skip to content

Commit

Permalink
Use journald for kernlogs if kern.log not found
Browse files Browse the repository at this point in the history
On some hosts or sosreports kern.log may be missing so we
fall back to journald for calltrace checking if this is the
case.
  • Loading branch information
dosaboy committed Jan 11, 2025
1 parent 6a0df95 commit d1d49f4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
11 changes: 10 additions & 1 deletion hotsos/core/host_helpers/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')}"

Expand All @@ -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')}"

Expand Down
6 changes: 2 additions & 4 deletions hotsos/core/plugins/kernel/kernlog/calltrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,8 @@ def __init__(self, *args, **kwargs):
self.run()

def run(self):
for tracetype in self.tracetypes:
self.searcher.add(tracetype.searchdef, self.path)

self.results = self.searcher.run()
self.results = self.perform_search([t.searchdef
for t in self.tracetypes])
for tracetype in self.tracetypes:
if isinstance(tracetype.searchdef, SequenceSearchDef):
results = self.results.find_sequence_sections(
Expand Down
42 changes: 32 additions & 10 deletions hotsos/core/plugins/kernel/kernlog/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import os

from hotsos.core.config import HotSOSConfig
from hotsos.core.host_helpers import CLIHelper, HostNetworkingHelper
from hotsos.core.host_helpers import (
CLIHelper,
CLIHelperFile,
HostNetworkingHelper,
)
from hotsos.core.search import (
CommonTimestampMatcher,
FileSearcher,
Expand Down Expand Up @@ -86,6 +90,21 @@ def __iter__(self):
class KernLogBase():
""" Base class for kernlog analysis implementations. """
def __init__(self):
self.hostnet_helper = HostNetworkingHelper()
self.cli_helper = CLIHelper()

@property
def path(self):
"""
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 os.path.join(HotSOSConfig.data_root, 'var/log/kern.log')

def perform_search(self, searchdefs):
try:
constraint = SearchConstraintSearchSince(
ts_matcher_cls=CommonTimestampMatcher)
Expand All @@ -94,14 +113,17 @@ def __init__(self):
"calltrace checker: %s", exc)
constraint = None

self.searcher = FileSearcher(constraint=constraint)
self.hostnet_helper = HostNetworkingHelper()
self.cli_helper = CLIHelper()
searcher = FileSearcher(constraint=constraint)
with CLIHelperFile() as cli:
if not os.path.exists(self.path):
path = cli.journalctl(opts='-k')
else:
if HotSOSConfig.use_all_logs:
path = f"{self.path}*"
else:
path = self.path

@property
def path(self):
path = os.path.join(HotSOSConfig.data_root, 'var/log/kern.log')
if HotSOSConfig.use_all_logs:
return f"{path}*"
for sd in searchdefs:
searcher.add(sd, path)

return path
return searcher.run()
6 changes: 2 additions & 4 deletions hotsos/core/plugins/kernel/kernlog/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ 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)

self.results = self.searcher.run()
self.results = self.perform_search([
self.over_mtu_dropped_packets_search_def])

@property
def over_mtu_dropped_packets_search_def(self):
Expand Down

0 comments on commit d1d49f4

Please sign in to comment.