Skip to content

Commit 6620c74

Browse files
Merge branch 'development' into alex_amdsmi
2 parents 9402695 + 54b4086 commit 6620c74

File tree

11 files changed

+374
-64
lines changed

11 files changed

+374
-64
lines changed

docs/PLUGIN_DOC.md

Lines changed: 135 additions & 29 deletions
Large diffs are not rendered by default.

docs/generate_plugin_doc_bundle.py

Lines changed: 98 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import pkgutil
3737
import sys
3838
from pathlib import Path
39-
from typing import Any, Iterable, List, Type
39+
from typing import Any, Iterable, List, Optional, Type
4040

4141
LINK_BASE_DEFAULT = "https://github.com/amd/node-scraper/blob/HEAD/"
4242
REL_ROOT_DEFAULT = "nodescraper/plugins/inband"
@@ -50,7 +50,7 @@ def get_attr(obj: Any, name: str, default: Any = None) -> Any:
5050
return default
5151

5252

53-
def _slice_from_rel_root(p: Path, rel_root: str | None) -> str | None:
53+
def _slice_from_rel_root(p: Path, rel_root: Optional[str]) -> Optional[str]:
5454
if not rel_root:
5555
return None
5656
parts = list(p.parts)
@@ -63,7 +63,7 @@ def _slice_from_rel_root(p: Path, rel_root: str | None) -> str | None:
6363
return None
6464

6565

66-
def setup_link(class_data, link_base: str, rel_root: str | None) -> str:
66+
def setup_link(class_data, link_base: str, rel_root: Optional[str]) -> str:
6767
try:
6868
file_location = Path(inspect.getfile(class_data)).resolve()
6969
except Exception:
@@ -80,7 +80,7 @@ def setup_link(class_data, link_base: str, rel_root: str | None) -> str:
8080
return base + rel_path
8181

8282

83-
def get_own_doc(cls: type) -> str | None:
83+
def get_own_doc(cls: type) -> Optional[str]:
8484
"""
8585
Return only the __doc__ defined in the class itself, ignore inheritance.
8686
"""
@@ -224,6 +224,57 @@ def add_cmd(s: Any):
224224
return cmds
225225

226226

227+
def extract_regexes_and_args_from_analyzer(
228+
analyzer_cls: type, args_cls: Optional[type]
229+
) -> List[str]:
230+
"""Extract regex patterns and analyzer args from analyzer class"""
231+
if not inspect.isclass(analyzer_cls):
232+
return []
233+
234+
output: List[str] = []
235+
236+
# Check for ERROR_REGEX class variable (used by RegexAnalyzer subclasses like DmesgAnalyzer)
237+
error_regex = get_attr(analyzer_cls, "ERROR_REGEX", None)
238+
if error_regex and isinstance(error_regex, list):
239+
output.append("**Built-in Regexes:**")
240+
for item in error_regex:
241+
# ErrorRegex objects have regex, message, event_category attributes
242+
if hasattr(item, "regex"):
243+
pattern = getattr(item.regex, "pattern", None)
244+
message = getattr(item, "message", "")
245+
if pattern:
246+
# Truncate long patterns
247+
pattern_str = pattern if len(pattern) < 50 else pattern[:47] + "..."
248+
output.append(f"- {message}: `{pattern_str}`")
249+
elif hasattr(item, "pattern"):
250+
pattern_str = item.pattern if len(item.pattern) < 50 else item.pattern[:47] + "..."
251+
output.append(f"- `{pattern_str}`")
252+
253+
# Check for other regex-related attributes
254+
for attr in dir(analyzer_cls):
255+
if "REGEX" in attr.upper() and not attr.startswith("_"):
256+
val = get_attr(analyzer_cls, attr, default=None)
257+
if val is None or attr == "ERROR_REGEX":
258+
continue
259+
260+
if hasattr(val, "pattern"):
261+
output.append(f"**{attr}**: `{val.pattern}`")
262+
elif isinstance(val, str):
263+
output.append(f"**{attr}**: `{val}`")
264+
265+
# Extract analyzer args if provided
266+
if inspect.isclass(args_cls):
267+
anns = get_attr(args_cls, "__annotations__", {}) or {}
268+
if anns:
269+
output.append("**Analyzer Args:**")
270+
for key, value in anns.items():
271+
# Format the type annotation
272+
type_str = str(value).replace("typing.", "")
273+
output.append(f"- `{key}`: {type_str}")
274+
275+
return output
276+
277+
227278
def md_header(text: str, level: int = 2) -> str:
228279
return f"{'#' * level} {text}\n\n"
229280

@@ -257,7 +308,20 @@ def class_vars_dump(cls: type, exclude: set) -> List[str]:
257308
continue
258309
if callable(val) or isinstance(val, (staticmethod, classmethod, property)):
259310
continue
260-
out.append(f"**{name}**: `{val}`")
311+
312+
# Format list values with each item on a new line
313+
if isinstance(val, list) and len(val) > 0:
314+
val_str = str(val)
315+
if len(val_str) > 200:
316+
formatted_items = []
317+
for item in val:
318+
formatted_items.append(f" {item}")
319+
formatted_list = "[\n" + ",\n".join(formatted_items) + "\n]"
320+
out.append(f"**{name}**: `{formatted_list}`")
321+
else:
322+
out.append(f"**{name}**: `{val}`")
323+
else:
324+
out.append(f"**{name}**: `{val}`")
261325
return out
262326

263327

@@ -279,14 +343,20 @@ def generate_plugin_table_rows(plugins: List[type]) -> List[List[str]]:
279343
seen.add(key)
280344
uniq.append(c)
281345
cmds = uniq
346+
347+
# Extract regexes and args from analyzer
348+
regex_and_args = []
349+
if inspect.isclass(an):
350+
regex_and_args = extract_regexes_and_args_from_analyzer(an, args)
351+
282352
rows.append(
283353
[
284-
f"{p.__module__}.{p.__name__}",
354+
p.__name__,
355+
"<br>".join(cmds).replace("|", "\\|") if cmds else "-",
356+
"<br>".join(regex_and_args).replace("|", "\\|") if regex_and_args else "-",
285357
link_anchor(dm, "model") if inspect.isclass(dm) else "-",
286358
link_anchor(col, "collector") if inspect.isclass(col) else "-",
287359
link_anchor(an, "analyzer") if inspect.isclass(an) else "-",
288-
link_anchor(args, "args") if inspect.isclass(args) else "-",
289-
"<br>".join(cmds) if cmds else "-",
290360
]
291361
)
292362
return rows
@@ -302,7 +372,7 @@ def render_table(headers: List[str], rows: List[List[str]]) -> str:
302372
return "".join(out)
303373

304374

305-
def render_collector_section(col: type, link_base: str, rel_root: str | None) -> str:
375+
def render_collector_section(col: type, link_base: str, rel_root: Optional[str]) -> str:
306376
hdr = md_header(f"Collector Class {col.__name__}", 2)
307377
desc = sanitize_doc(get_own_doc(col) or "")
308378
s = hdr
@@ -335,7 +405,7 @@ def render_collector_section(col: type, link_base: str, rel_root: str | None) ->
335405
return s
336406

337407

338-
def render_analyzer_section(an: type, link_base: str, rel_root: str | None) -> str:
408+
def render_analyzer_section(an: type, link_base: str, rel_root: Optional[str]) -> str:
339409
hdr = md_header(f"Data Analyzer Class {an.__name__}", 2)
340410
desc = sanitize_doc(get_own_doc(an) or "")
341411
s = hdr
@@ -350,10 +420,18 @@ def render_analyzer_section(an: type, link_base: str, rel_root: str | None) -> s
350420
if cv:
351421
s += md_header("Class Variables", 3) + md_list(cv)
352422

423+
# Add regex patterns if present (pass None for args_cls since we don't have context here)
424+
regex_info = extract_regexes_and_args_from_analyzer(an, None)
425+
if regex_info:
426+
s += md_header("Regex Patterns", 3)
427+
if len(regex_info) > 10:
428+
s += f"*{len(regex_info)} items defined*\n\n"
429+
s += md_list(regex_info)
430+
353431
return s
354432

355433

356-
def render_model_section(model: type, link_base: str, rel_root: str | None) -> str:
434+
def render_model_section(model: type, link_base: str, rel_root: Optional[str]) -> str:
357435
hdr = md_header(f"{model.__name__} Model", 2)
358436
desc = sanitize_doc(get_own_doc(model) or "")
359437
s = hdr
@@ -368,7 +446,7 @@ def render_model_section(model: type, link_base: str, rel_root: str | None) -> s
368446
return s
369447

370448

371-
def render_analyzer_args_section(args_cls: type, link_base: str, rel_root: str | None) -> str:
449+
def render_analyzer_args_section(args_cls: type, link_base: str, rel_root: Optional[str]) -> str:
372450
hdr = md_header(f"Analyzer Args Class {args_cls.__name__}", 2)
373451
desc = sanitize_doc(get_own_doc(args_cls) or "")
374452
s = hdr
@@ -418,7 +496,14 @@ def all_subclasses(cls: Type) -> set[type]:
418496
plugins.sort(key=lambda c: f"{c.__module__}.{c.__name__}".lower())
419497

420498
rows = generate_plugin_table_rows(plugins)
421-
headers = ["Plugin", "DataModel", "Collector", "Analyzer", "AnalyzerArgs", "Cmd(s)"]
499+
headers = [
500+
"Plugin",
501+
"Collection",
502+
"Analysis",
503+
"DataModel",
504+
"Collector",
505+
"Analyzer",
506+
]
422507

423508
collectors, analyzers, models, args_classes = [], [], [], []
424509
seen_c, seen_a, seen_m, seen_args = set(), set(), set(), set()

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)

0 commit comments

Comments
 (0)