Skip to content

Commit

Permalink
Protect against no date
Browse files Browse the repository at this point in the history
If for whatever reason hotsos cannot get the current date, perhaps
because sosreport didnt collect it, we now print a warning log
when we fail to create a search constraint as a result.
  • Loading branch information
dosaboy committed Jan 11, 2025
1 parent 48b6fd2 commit 6a0df95
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 18 deletions.
15 changes: 12 additions & 3 deletions hotsos/core/host_helpers/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def since_date(self):
and has the format "YEAR-MONTH-DAY". It does not specify a time.
"""
current = CLIHelper().date(format="--iso-8601")
if not current:
log.warning("could not determine since date for journalctl "
"command")
return None

ts = datetime.datetime.strptime(current, "%Y-%m-%d")
if HotSOSConfig.use_all_logs:
days = HotSOSConfig.max_logrotate_depth
Expand All @@ -64,12 +69,16 @@ def format_journalctl_cmd(self, **kwargs):

if kwargs.get("date"):
self.cmd = f"{self.cmd} --since {kwargs.get('date')}"
else:
elif self.since_date:
self.cmd = f"{self.cmd} --since {self.since_date}"


class JournalctlBinFileCmd(BinFileCmd, JournalctlBase):
""" Implements file-based journalctl command. """
""" Implements file-based journalctl command.
NOTE: this may suffer from incompatibility issues if the journal data was
created with a different version of systemd-journald.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.register_hook("pre-exec", self.preformat_sos_journalctl)
Expand All @@ -81,7 +90,7 @@ def preformat_sos_journalctl(self, **kwargs):

if kwargs.get("date"):
self.path = f"{self.path} --since {kwargs.get('date')}"
else:
elif self.since_date:
self.path = f"{self.path} --since {self.since_date}"


Expand Down
11 changes: 8 additions & 3 deletions hotsos/core/host_helpers/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

class CLIExecError(Exception):
""" Exception raised when execution of a command files. """
def __init__(self, return_value=None):
def __init__(self, message, return_value=None):
"""
@param message: information message about the exception
@param return_value: default return value that a command
should return if execution fails.
"""
self.message = message
self.return_value = return_value


Expand All @@ -31,9 +33,12 @@ def catch_exceptions_inner2(*args, **kwargs):
log.debug(msg)

if isinstance(exc, json.JSONDecodeError):
raise CLIExecError(return_value={}) from exc
raise CLIExecError(str(exc), return_value={}) from exc

raise CLIExecError(return_value=[]) from exc
if isinstance(exc, subprocess.CalledProcessError):
raise CLIExecError(exc.output, return_value={}) from exc

raise CLIExecError(str(exc), return_value=[]) from exc

return catch_exceptions_inner2

Expand Down
4 changes: 2 additions & 2 deletions hotsos/core/host_helpers/uptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def __init__(self):
def in_minutes(self):
""" Total uptime in minutes. """
if not self.subgroups:
log.info("uptime not available")
return None
log.warning("uptime not available")
return 0

if self.subgroups['hour']['value']:
expr = self.subgroups['hour']['expr']
Expand Down
12 changes: 10 additions & 2 deletions hotsos/core/plugins/kernel/kernlog/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
FileSearcher,
SearchConstraintSearchSince,
)
from hotsos.core.log import log

KERNLOG_TS = r'\[\s*\d+\.\d+\]'
KERNLOG_PREFIX = rf'(?:\S+\s+\d+\s+[\d:]+\s+\S+\s+\S+:\s+)?{KERNLOG_TS}'
Expand Down Expand Up @@ -85,8 +86,15 @@ def __iter__(self):
class KernLogBase():
""" Base class for kernlog analysis implementations. """
def __init__(self):
c = SearchConstraintSearchSince(ts_matcher_cls=CommonTimestampMatcher)
self.searcher = FileSearcher(constraint=c)
try:
constraint = SearchConstraintSearchSince(
ts_matcher_cls=CommonTimestampMatcher)
except ValueError as exc:
log.warning("failed to create global search constraint for "
"calltrace checker: %s", exc)
constraint = None

self.searcher = FileSearcher(constraint=constraint)
self.hostnet_helper = HostNetworkingHelper()
self.cli_helper = CLIHelper()

Expand Down
6 changes: 5 additions & 1 deletion hotsos/core/plugins/openvswitch/ovs.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,14 @@ def simple_search(cls):
""" # noqa
pattern = (r'([\d-]+)T([\d:]+)\.\d+Z.+\|bfd\(\S+\)\|INFO\|'
r'([a-z0-9-]+): BFD state change: (\S+)')
constraints = []
constraint = create_constraint(search_result_age_hours=24,
min_hours_since_last_boot=0)
if constraint:
constraints.append(constraint)

return SearchDef(pattern, tag=cls.unique_search_tag,
constraints=[constraint])
constraints=constraints)

@classmethod
def paths(cls):
Expand Down
17 changes: 11 additions & 6 deletions hotsos/core/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ def __init__(self, *args, **kwargs):

current_date = CLIHelper().date(format='+%Y-%m-%d %H:%M:%S')
if not current_date or not isinstance(current_date, str):
log.error("current date '%s' being provided to search "
"constraints is not valid.", current_date)
return
msg = (f"current date '{current_date}' being provided to search "
"constraints is not valid.")
raise ValueError(msg)

super().__init__(*args, current_date=current_date, **kwargs)

Expand Down Expand Up @@ -241,6 +241,11 @@ def create_constraint(search_result_age_hours=None,
hours = min(hours,
max(uptime_etime_hours - min_hours_since_last_boot, 0))

return SearchConstraintSearchSince(
ts_matcher_cls=CommonTimestampMatcher,
hours=hours)
try:
return SearchConstraintSearchSince(
ts_matcher_cls=CommonTimestampMatcher,
hours=hours)
except ValueError as exc:
log.warning("failed to create search constraint: %s", exc)

return None
7 changes: 6 additions & 1 deletion hotsos/core/ycheck/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,13 @@ class GlobalSearcher(contextlib.AbstractContextManager, UserDict):
"""

def __init__(self):
constraint = SearchConstraintSearchSince(
try:
constraint = SearchConstraintSearchSince(
ts_matcher_cls=CommonTimestampMatcher)
except ValueError as exc:
log.warning("failed to create global search constraint: %s", exc)
constraint = None

self._loaded_searches = []
self._results = None
self._searcher = FileSearcher(constraint=constraint)
Expand Down

0 comments on commit 6a0df95

Please sign in to comment.