Skip to content

Commit ed1be90

Browse files
Merge branch 'development' into alex_devenum
2 parents 7b06590 + 54b4086 commit ed1be90

File tree

9 files changed

+141
-22
lines changed

9 files changed

+141
-22
lines changed

docs/node-scraper-external/ext_nodescraper_plugins/sample/sample_collector.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from nodescraper.base import InBandDataCollector
24
from nodescraper.enums import ExecutionStatus
35
from nodescraper.models import TaskResult
@@ -9,7 +11,7 @@ class SampleCollector(InBandDataCollector[SampleDataModel, None]):
911

1012
DATA_MODEL = SampleDataModel
1113

12-
def collect_data(self, args=None) -> tuple[TaskResult, SampleDataModel | None]:
14+
def collect_data(self, args=None) -> tuple[TaskResult, Optional[SampleDataModel]]:
1315
sample_data = SampleDataModel(some_str="example123")
1416
self.result.message = "Collector ran successfully"
1517
self.result.status = ExecutionStatus.OK
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 typing import Optional
28+
29+
from nodescraper.models import CollectorArgs
30+
31+
32+
class JournalCollectorArgs(CollectorArgs):
33+
boot: Optional[int] = None

nodescraper/plugins/inband/journal/journal_collector.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,50 @@
2525
###############################################################################
2626
from typing import Optional
2727

28+
from pydantic import ValidationError
29+
2830
from nodescraper.base import InBandDataCollector
2931
from nodescraper.enums import EventCategory, EventPriority, ExecutionStatus, OSFamily
3032
from nodescraper.models import TaskResult
33+
from nodescraper.utils import get_exception_details
3134

35+
from .collector_args import JournalCollectorArgs
3236
from .journaldata import JournalData
3337

3438

35-
class JournalCollector(InBandDataCollector[JournalData, None]):
39+
class JournalCollector(InBandDataCollector[JournalData, JournalCollectorArgs]):
3640
"""Read journal log via journalctl."""
3741

3842
SUPPORTED_OS_FAMILY = {OSFamily.LINUX}
3943
DATA_MODEL = JournalData
4044
CMD = "journalctl --no-pager --system --output=short-iso"
4145

42-
def _read_with_journalctl(self):
46+
def _read_with_journalctl(self, args: Optional[JournalCollectorArgs] = None):
4347
"""Read journal logs using journalctl
4448
4549
Returns:
4650
str|None: system journal read
4751
"""
48-
res = self._run_sut_cmd(self.CMD, sudo=True, log_artifact=False, strip=False)
52+
53+
cmd = "journalctl --no-pager --system --output=short-iso"
54+
try:
55+
# safe check for args.boot
56+
if args is not None and getattr(args, "boot", None):
57+
cmd = f"journalctl --no-pager -b {args.boot} --system --output=short-iso"
58+
59+
res = self._run_sut_cmd(cmd, sudo=True, log_artifact=False, strip=False)
60+
61+
except ValidationError as val_err:
62+
self._log_event(
63+
category=EventCategory.OS,
64+
description="Exception while running journalctl",
65+
data=get_exception_details(val_err),
66+
priority=EventPriority.ERROR,
67+
console_log=True,
68+
)
69+
self.result.message = "Could not read journalctl data"
70+
self.result.status = ExecutionStatus.ERROR
71+
return None
4972

5073
if res.exit_code != 0:
5174
self._log_event(
@@ -61,16 +84,22 @@ def _read_with_journalctl(self):
6184

6285
return res.stdout
6386

64-
def collect_data(self, args=None) -> tuple[TaskResult, Optional[JournalData]]:
87+
def collect_data(
88+
self,
89+
args: Optional[JournalCollectorArgs] = None,
90+
) -> tuple[TaskResult, Optional[JournalData]]:
6591
"""Collect journal logs
6692
6793
Args:
6894
args (_type_, optional): Collection args. Defaults to None.
6995
7096
Returns:
71-
tuple[TaskResult, Optional[JournalData, None]]: Tuple of results and data model or none.
97+
tuple[TaskResult, Optional[JournalData]]: Tuple of results and data model or none.
7298
"""
73-
journal_log = self._read_with_journalctl()
99+
if args is None:
100+
args = JournalCollectorArgs()
101+
102+
journal_log = self._read_with_journalctl(args)
74103
if journal_log:
75104
data = JournalData(journal_log=journal_log)
76105
self.result.message = self.result.message or "Journal data collected"

nodescraper/plugins/inband/journal/journal_plugin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@
2525
###############################################################################
2626
from nodescraper.base import InBandDataPlugin
2727

28+
from .collector_args import JournalCollectorArgs
2829
from .journal_collector import JournalCollector
2930
from .journaldata import JournalData
3031

3132

32-
class JournalPlugin(InBandDataPlugin[JournalData, None, None]):
33+
class JournalPlugin(InBandDataPlugin[JournalData, JournalCollectorArgs, None]):
3334
"""Plugin for collection of journal data"""
3435

3536
DATA_MODEL = JournalData
3637

3738
COLLECTOR = JournalCollector
39+
40+
COLLECTOR_ARGS = JournalCollectorArgs

nodescraper/plugins/inband/kernel/analyzer_args.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ def build_from_model(cls, datamodel: KernelDataModel) -> "KernelAnalyzerArgs":
6161
Returns:
6262
KernelAnalyzerArgs: instance of analyzer args class
6363
"""
64-
return cls(exp_kernel=datamodel.kernel_version)
64+
return cls(exp_kernel=datamodel.kernel_info)

nodescraper/plugins/inband/kernel/kernel_collector.py

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# SOFTWARE.
2424
#
2525
###############################################################################
26+
import re
2627
from typing import Optional
2728

2829
from nodescraper.base import InBandDataCollector
@@ -37,7 +38,31 @@ class KernelCollector(InBandDataCollector[KernelDataModel, None]):
3738

3839
DATA_MODEL = KernelDataModel
3940
CMD_WINDOWS = "wmic os get Version /Value"
40-
CMD = "sh -c 'uname -r'"
41+
CMD = "sh -c 'uname -a'"
42+
43+
def _parse_kernel_version(self, uname_a: str) -> Optional[str]:
44+
"""Extract the kernel release from `uname -a` output.
45+
46+
Args:
47+
uname_a (str): The full output string from the `uname -a` command.
48+
49+
Returns:
50+
Optional[str]: The parsed kernel release (e.g., "5.13.0-30-generic")
51+
if found, otherwise None.
52+
"""
53+
if not uname_a:
54+
return None
55+
56+
result = uname_a.strip().split()
57+
if len(result) >= 3:
58+
return result[2]
59+
60+
# if some change in output look for a version-like string (e.g. 4.18.0-553.el8_10.x86_64)
61+
match = re.search(r"\d+\.\d+\.\d+[\w\-\.]*", uname_a)
62+
if match:
63+
return match.group(0)
64+
65+
return None
4166

4267
def collect_data(
4368
self,
@@ -51,16 +76,28 @@ def collect_data(
5176
"""
5277

5378
kernel = None
79+
kernel_info = None
80+
5481
if self.system_info.os_family == OSFamily.WINDOWS:
5582
res = self._run_sut_cmd(self.CMD_WINDOWS)
5683
if res.exit_code == 0:
84+
kernel_info = res.stdout
5785
kernel = [line for line in res.stdout.splitlines() if "Version=" in line][0].split(
5886
"="
5987
)[1]
6088
else:
6189
res = self._run_sut_cmd(self.CMD)
6290
if res.exit_code == 0:
63-
kernel = res.stdout
91+
kernel_info = res.stdout
92+
kernel = self._parse_kernel_version(kernel_info)
93+
if not kernel:
94+
self._log_event(
95+
category=EventCategory.OS,
96+
description="Could not extract kernel version from 'uname -a'",
97+
data={"command": res.command, "exit_code": res.exit_code},
98+
priority=EventPriority.ERROR,
99+
console_log=True,
100+
)
64101

65102
if res.exit_code != 0:
66103
self._log_event(
@@ -71,8 +108,9 @@ def collect_data(
71108
console_log=True,
72109
)
73110

74-
if kernel:
75-
kernel_data = KernelDataModel(kernel_version=kernel)
111+
if kernel_info and kernel:
112+
113+
kernel_data = KernelDataModel(kernel_info=kernel_info, kernel_version=kernel)
76114
self._log_event(
77115
category="KERNEL_READ",
78116
description="Kernel version read",
@@ -82,6 +120,10 @@ def collect_data(
82120
else:
83121
kernel_data = None
84122

85-
self.result.message = f"Kernel: {kernel}" if kernel else "Kernel not found"
86-
self.result.status = ExecutionStatus.OK if kernel else ExecutionStatus.ERROR
123+
self.result.message = (
124+
"Kernel not found"
125+
if not kernel_info
126+
else f"Kernel info: {kernel_info} | Kernel version: {kernel if kernel else 'Kernel version not found'}"
127+
)
128+
self.result.status = ExecutionStatus.OK if kernel_info else ExecutionStatus.ERROR
87129
return self.result, kernel_data

nodescraper/plugins/inband/kernel/kerneldata.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
# SOFTWARE.
2424
#
2525
###############################################################################
26+
2627
from nodescraper.models import DataModel
2728

2829

2930
class KernelDataModel(DataModel):
31+
kernel_info: str
3032
kernel_version: str

test/unit/plugin/test_kernel_analyzer.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535

3636
@pytest.fixture
3737
def model_obj():
38-
return KernelDataModel(kernel_version="5.13.0-30-generic")
38+
return KernelDataModel(
39+
kernel_info="Linux MockSystem 5.13.0-30-generic #1 XYZ Day Month 10 15:19:13 EDT 2024 x86_64 x86_64 x86_64 GNU/Linux",
40+
kernel_version="5.13.0-30-generic",
41+
)
3942

4043

4144
@pytest.fixture
@@ -118,14 +121,14 @@ def test_invalid_kernel_config(system_info, model_obj, config):
118121

119122

120123
def test_match_regex(system_info, model_obj):
121-
args = KernelAnalyzerArgs(exp_kernel=[r"5.13.\d-\d+-[\w]+"], regex_match=True)
124+
args = KernelAnalyzerArgs(exp_kernel=[r".*5\.13\.\d+-\d+-[\w-]+.*"], regex_match=True)
122125
analyzer = KernelAnalyzer(system_info)
123126
result = analyzer.analyze_data(model_obj, args)
124127
assert result.status == ExecutionStatus.OK
125128

126129

127130
def test_mismatch_regex(system_info, model_obj):
128-
args = KernelAnalyzerArgs(exp_kernel=[r"4.3.\d-\d+-[\w]+"], regex_match=True)
131+
args = KernelAnalyzerArgs(exp_kernel=[r".*4\.13\.\d+-\d+-[\w-]+.*"], regex_match=True)
129132
analyzer = KernelAnalyzer(system_info)
130133
result = analyzer.analyze_data(model_obj, args)
131134

test/unit/plugin/test_kernel_collector.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,27 @@ def test_run_windows(collector, conn_mock):
5353

5454
result, data = collector.collect_data()
5555

56-
assert data == KernelDataModel(kernel_version="10.0.19041.1237")
56+
assert data == KernelDataModel(
57+
kernel_info="Version=10.0.19041.1237", kernel_version="10.0.19041.1237"
58+
)
5759
assert result.status == ExecutionStatus.OK
5860

5961

6062
def test_run_linux(collector, conn_mock):
6163
collector.system_info.os_family = OSFamily.LINUX
6264
conn_mock.run_command.return_value = CommandArtifact(
6365
exit_code=0,
64-
stdout="5.4.0-88-generic",
66+
stdout="Linux MockSystem 5.13.0-30-generic #1 XYZ Day Month 10 15:19:13 EDT 2024 x86_64 x86_64 x86_64 GNU/Linux",
6567
stderr="",
66-
command="sh -c 'uname -r'",
68+
command="sh -c 'uname -a'",
6769
)
6870

6971
result, data = collector.collect_data()
7072

71-
assert data == KernelDataModel(kernel_version="5.4.0-88-generic")
73+
assert data == KernelDataModel(
74+
kernel_info="Linux MockSystem 5.13.0-30-generic #1 XYZ Day Month 10 15:19:13 EDT 2024 x86_64 x86_64 x86_64 GNU/Linux",
75+
kernel_version="5.13.0-30-generic",
76+
)
7277
assert result.status == ExecutionStatus.OK
7378

7479

0 commit comments

Comments
 (0)