Skip to content

Commit c53a68d

Browse files
Merge pull request #38 from amd/alex_journal_log
New plugin: journal logs
2 parents 1bddedf + 3254994 commit c53a68d

File tree

6 files changed

+258
-0
lines changed

6 files changed

+258
-0
lines changed

nodescraper/connection/inband/inbandlocal.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def run_command(
5656
command,
5757
encoding="utf-8",
5858
shell=True,
59+
errors="replace",
5960
timeout=timeout,
6061
capture_output=True,
6162
check=False,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
###############################################################################
2+
#
3+
# MIT License
4+
#
5+
# Copyright (c) 2025 Advanced Micro Devices, Inc.
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
#
25+
###############################################################################
26+
from .journal_plugin import JournalPlugin
27+
28+
__all__ = ["JournalPlugin"]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
###############################################################################
2+
#
3+
# MIT License
4+
#
5+
# Copyright (c) 2025 Advanced Micro Devices, Inc.
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
#
25+
###############################################################################
26+
27+
from nodescraper.base import InBandDataCollector
28+
from nodescraper.enums import EventCategory, EventPriority, ExecutionStatus, OSFamily
29+
from nodescraper.models import TaskResult
30+
31+
from .journaldata import JournalData
32+
33+
34+
class JournalCollector(InBandDataCollector[JournalData, None]):
35+
"""Read journal log via journalctl."""
36+
37+
SUPPORTED_OS_FAMILY = {OSFamily.LINUX}
38+
DATA_MODEL = JournalData
39+
40+
def _read_with_journalctl(self):
41+
"""Read journal logs using journalctl
42+
43+
Returns:
44+
str|None: system journal read
45+
"""
46+
cmd = "journalctl --no-pager --system --output=short-iso"
47+
res = self._run_sut_cmd(cmd, sudo=True, log_artifact=False, strip=False)
48+
49+
if res.exit_code != 0:
50+
self._log_event(
51+
category=EventCategory.OS,
52+
description="Error reading journalctl",
53+
data={"command": res.command, "exit_code": res.exit_code},
54+
priority=EventPriority.ERROR,
55+
console_log=True,
56+
)
57+
self.result.message = "Could not read journalctl data"
58+
self.result.status = ExecutionStatus.ERROR
59+
return None
60+
61+
return res.stdout
62+
63+
def collect_data(self, args=None) -> tuple[TaskResult, JournalData | None]:
64+
"""Collect journal logs
65+
66+
Args:
67+
args (_type_, optional): Collection args. Defaults to None.
68+
69+
Returns:
70+
tuple[TaskResult, JournalData | None]: Tuple of results and data model or none.
71+
"""
72+
journal_log = self._read_with_journalctl()
73+
if journal_log:
74+
data = JournalData(journal_log=journal_log)
75+
self.result.message = self.result.message or "Journal data collected"
76+
return self.result, data
77+
return self.result, None
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
###############################################################################
2+
#
3+
# MIT License
4+
#
5+
# Copyright (c) 2025 Advanced Micro Devices, Inc.
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
#
25+
###############################################################################
26+
from nodescraper.base import InBandDataPlugin
27+
28+
from .journal_collector import JournalCollector
29+
from .journaldata import JournalData
30+
31+
32+
class JournalPlugin(InBandDataPlugin[JournalData, None, None]):
33+
"""Plugin for collection of journal data"""
34+
35+
DATA_MODEL = JournalData
36+
37+
COLLECTOR = JournalCollector
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
###############################################################################
2+
#
3+
# MIT License
4+
#
5+
# Copyright (c) 2025 Advanced Micro Devices, Inc.
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
#
25+
###############################################################################
26+
import os
27+
28+
from nodescraper.models import DataModel
29+
30+
31+
class JournalData(DataModel):
32+
"""Data model for journal logs"""
33+
34+
journal_log: str
35+
36+
def log_model(self, log_path: str):
37+
"""Log data model to a file
38+
39+
Args:
40+
log_path (str): log path
41+
"""
42+
log_name = os.path.join(log_path, "journal.log")
43+
with open(log_name, "w", encoding="utf-8") as log_filename:
44+
log_filename.write(self.journal_log)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
###############################################################################
2+
#
3+
# MIT License
4+
#
5+
# Copyright (c) 2025 Advanced Micro Devices, Inc.
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
#
25+
###############################################################################
26+
import types
27+
28+
from nodescraper.enums.systeminteraction import SystemInteractionLevel
29+
from nodescraper.plugins.inband.journal.journal_collector import JournalCollector
30+
from nodescraper.plugins.inband.journal.journaldata import JournalData
31+
32+
33+
class DummyRes:
34+
def __init__(self, command="", stdout="", exit_code=0, stderr=""):
35+
self.command = command
36+
self.stdout = stdout
37+
self.exit_code = exit_code
38+
self.stderr = stderr
39+
40+
41+
def get_collector(monkeypatch, run_map, system_info, conn_mock):
42+
c = JournalCollector(
43+
system_info=system_info,
44+
system_interaction_level=SystemInteractionLevel.INTERACTIVE,
45+
connection=conn_mock,
46+
)
47+
c.result = types.SimpleNamespace(artifacts=[], message=None)
48+
c._events = []
49+
50+
def _log_event(**kw):
51+
c._events.append(kw)
52+
53+
def _run_sut_cmd(cmd, *args, **kwargs):
54+
return run_map(cmd, *args, **kwargs)
55+
56+
monkeypatch.setattr(c, "_log_event", _log_event, raising=True)
57+
monkeypatch.setattr(c, "_run_sut_cmd", _run_sut_cmd, raising=True)
58+
59+
return c
60+
61+
62+
def test_collect_data_integration(monkeypatch, system_info, conn_mock):
63+
def run_map(cmd, **kwargs):
64+
return DummyRes(command=cmd, stdout='{"MESSAGE":"hello"}\n', exit_code=0)
65+
66+
c = get_collector(monkeypatch, run_map, system_info, conn_mock)
67+
68+
result, data = c.collect_data()
69+
assert isinstance(data, JournalData)
70+
71+
assert data.journal_log == '{"MESSAGE":"hello"}\n'

0 commit comments

Comments
 (0)