-
Notifications
You must be signed in to change notification settings - Fork 1
New plugin: journal logs #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d2fd50f
added collector, needs utest
alexandraBara f216ace
Merge branch 'development' into alex_journal_log
alexandraBara 1d37038
Merge branch 'development' into alex_journal_log
alexandraBara 5da215b
typo fix
alexandraBara ed246f1
typo fix
alexandraBara 96c1e80
added init file
alexandraBara 2920d4c
added docstring
alexandraBara d6e620d
Merge branch 'development' into alex_journal_log
alexandraBara ee5fba3
fixed mypy
alexandraBara c553201
Merge branch 'alex_syslog' into alex_journal_log
alexandraBara bca73c0
reading journal logs with journalctl only
alexandraBara 28e4319
updated failure case
alexandraBara 195544b
Merge branch 'development' into alex_journal_log
alexandraBara f1f2f78
Merge branch 'development' into alex_journal_log
alexandraBara c0f1e9a
added log_model call for journal plugin + updated journaldata
alexandraBara 1b1fc56
iso
alexandraBara 0d9625c
decode fix
alexandraBara 3254994
removed extra decode
alexandraBara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
############################################################################### | ||
# | ||
# MIT License | ||
# | ||
# Copyright (c) 2025 Advanced Micro Devices, Inc. | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
# | ||
############################################################################### | ||
from .journal_plugin import JournalPlugin | ||
|
||
__all__ = ["JournalPlugin"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
############################################################################### | ||
# | ||
# MIT License | ||
# | ||
# Copyright (c) 2025 Advanced Micro Devices, Inc. | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
# | ||
############################################################################### | ||
import base64 | ||
|
||
from nodescraper.base import InBandDataCollector | ||
from nodescraper.enums import EventCategory, EventPriority, ExecutionStatus, OSFamily | ||
from nodescraper.models import TaskResult | ||
|
||
from .journaldata import JournalData | ||
|
||
|
||
class JournalCollector(InBandDataCollector[JournalData, None]): | ||
"""Read journal log via journalctl.""" | ||
|
||
SUPPORTED_OS_FAMILY = {OSFamily.LINUX} | ||
DATA_MODEL = JournalData | ||
|
||
def _read_with_journalctl(self): | ||
"""Read journal logs using journalctl | ||
|
||
Returns: | ||
str|None: system journal read | ||
""" | ||
cmd = "journalctl --no-pager --system --all --output=short-iso 2>&1 | base64 -w0" | ||
res = self._run_sut_cmd(cmd, sudo=True, log_artifact=False, strip=False) | ||
|
||
if res.exit_code != 0: | ||
self._log_event( | ||
category=EventCategory.OS, | ||
description="Error reading journalctl", | ||
data={"command": res.command, "exit_code": res.exit_code}, | ||
priority=EventPriority.ERROR, | ||
console_log=True, | ||
) | ||
self.result.message = "Could not read journalctl data" | ||
self.result.status = ExecutionStatus.ERROR | ||
return None | ||
|
||
if isinstance(res.stdout, (bytes, bytearray)): | ||
b64 = ( | ||
res.stdout if isinstance(res.stdout, str) else res.stdout.decode("ascii", "ignore") | ||
) | ||
raw = base64.b64decode("".join(b64.split())) | ||
text = raw.decode("utf-8", errors="replace") | ||
else: | ||
text = res.stdout | ||
|
||
return text | ||
|
||
def collect_data(self, args=None) -> tuple[TaskResult, JournalData | None]: | ||
"""Collect journal logs | ||
|
||
Args: | ||
args (_type_, optional): Collection args. Defaults to None. | ||
|
||
Returns: | ||
tuple[TaskResult, JournalData | None]: Tuple of results and data model or none. | ||
""" | ||
journal_log = self._read_with_journalctl() | ||
if journal_log: | ||
data = JournalData(journal_log=journal_log) | ||
self.result.message = self.result.message or "Journal data collected" | ||
return self.result, data | ||
return self.result, None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
############################################################################### | ||
# | ||
# MIT License | ||
# | ||
# Copyright (c) 2025 Advanced Micro Devices, Inc. | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
# | ||
############################################################################### | ||
from nodescraper.base import InBandDataPlugin | ||
|
||
from .journal_collector import JournalCollector | ||
from .journaldata import JournalData | ||
|
||
|
||
class JournalPlugin(InBandDataPlugin[JournalData, None, None]): | ||
"""Plugin for collection of journal data""" | ||
|
||
DATA_MODEL = JournalData | ||
|
||
COLLECTOR = JournalCollector |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
############################################################################### | ||
# | ||
# MIT License | ||
# | ||
# Copyright (c) 2025 Advanced Micro Devices, Inc. | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
# | ||
############################################################################### | ||
import os | ||
|
||
from nodescraper.models import DataModel | ||
|
||
|
||
class JournalData(DataModel): | ||
"""Data model for journal logs""" | ||
|
||
journal_log: str | ||
|
||
def log_model(self, log_path: str): | ||
"""Log data model to a file | ||
|
||
Args: | ||
log_path (str): log path | ||
""" | ||
log_name = os.path.join(log_path, "journal.log") | ||
with open(log_name, "w", encoding="utf-8") as log_filename: | ||
log_filename.write(self.journal_log) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
############################################################################### | ||
# | ||
# MIT License | ||
# | ||
# Copyright (c) 2025 Advanced Micro Devices, Inc. | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
# | ||
############################################################################### | ||
import types | ||
|
||
from nodescraper.enums.systeminteraction import SystemInteractionLevel | ||
from nodescraper.plugins.inband.journal.journal_collector import JournalCollector | ||
from nodescraper.plugins.inband.journal.journaldata import JournalData | ||
|
||
|
||
class DummyRes: | ||
def __init__(self, command="", stdout="", exit_code=0, stderr=""): | ||
self.command = command | ||
self.stdout = stdout | ||
self.exit_code = exit_code | ||
self.stderr = stderr | ||
|
||
|
||
def get_collector(monkeypatch, run_map, system_info, conn_mock): | ||
c = JournalCollector( | ||
system_info=system_info, | ||
system_interaction_level=SystemInteractionLevel.INTERACTIVE, | ||
connection=conn_mock, | ||
) | ||
c.result = types.SimpleNamespace(artifacts=[], message=None) | ||
c._events = [] | ||
|
||
def _log_event(**kw): | ||
c._events.append(kw) | ||
|
||
def _run_sut_cmd(cmd, *args, **kwargs): | ||
return run_map(cmd, *args, **kwargs) | ||
|
||
monkeypatch.setattr(c, "_log_event", _log_event, raising=True) | ||
monkeypatch.setattr(c, "_run_sut_cmd", _run_sut_cmd, raising=True) | ||
|
||
return c | ||
|
||
|
||
def test_collect_data_integration(monkeypatch, system_info, conn_mock): | ||
def run_map(cmd, **kwargs): | ||
return DummyRes(command=cmd, stdout='{"MESSAGE":"hello"}\n', exit_code=0) | ||
|
||
c = get_collector(monkeypatch, run_map, system_info, conn_mock) | ||
|
||
result, data = c.collect_data() | ||
assert isinstance(data, JournalData) | ||
|
||
assert data.journal_log == '{"MESSAGE":"hello"}\n' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.