Skip to content

Commit 622d7e4

Browse files
Merge branch 'development' into alex_summary_update
2 parents 9e660fe + 6061785 commit 622d7e4

File tree

7 files changed

+428
-41
lines changed

7 files changed

+428
-41
lines changed

nodescraper/plugins/inband/dmesg/dmesg_collector.py

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
# SOFTWARE.
2424
#
2525
###############################################################################
26-
import re
2726
from typing import Optional
2827

2928
from nodescraper.base import InBandDataCollector
3029
from nodescraper.connection.inband import TextFileArtifact
3130
from nodescraper.enums import EventCategory, EventPriority, ExecutionStatus, OSFamily
3231
from nodescraper.models import TaskResult
32+
from nodescraper.utils import nice_rotated_name, shell_quote
3333

3434
from .collector_args import DmesgCollectorArgs
3535
from .dmesgdata import DmesgData
@@ -48,43 +48,6 @@ class DmesgCollector(InBandDataCollector[DmesgData, DmesgCollectorArgs]):
4848
r"ls -1 /var/log/dmesg* 2>/dev/null | grep -E '^/var/log/dmesg(\.[0-9]+(\.gz)?)?$' || true"
4949
)
5050

51-
def _shell_quote(self, s: str) -> str:
52-
"""Single quote fix
53-
54-
Args:
55-
s (str): path to be converted
56-
57-
Returns:
58-
str: path to be returned
59-
"""
60-
return "'" + s.replace("'", "'\"'\"'") + "'"
61-
62-
def _nice_dmesg_name(self, path: str) -> str:
63-
"""Map path to filename
64-
65-
Args:
66-
path (str): file path
67-
68-
Returns:
69-
str: new local filename
70-
"""
71-
prefix = "rotated_"
72-
base = path.rstrip("/").rsplit("/", 1)[-1]
73-
74-
if base == "dmesg":
75-
return f"{prefix}dmesg_log.log"
76-
77-
m = re.fullmatch(r"dmesg\.(\d+)\.gz", base)
78-
if m:
79-
return f"{prefix}dmesg.{m.group(1)}.gz.log"
80-
81-
m = re.fullmatch(r"dmesg\.(\d+)", base)
82-
if m:
83-
return f"{prefix}dmesg.{m.group(1)}.log"
84-
85-
middle = base[:-3] if base.endswith(".gz") else base
86-
return f"{prefix}{middle}.log"
87-
8851
def _collect_dmesg_rotations(self):
8952
"""Collect dmesg logs"""
9053
list_res = self._run_sut_cmd(self.DMESG_LOGS_CMD, sudo=True)
@@ -100,12 +63,12 @@ def _collect_dmesg_rotations(self):
10063

10164
collected_logs, failed_logs = [], []
10265
for p in paths:
103-
qp = self._shell_quote(p)
66+
qp = shell_quote(p)
10467
if p.endswith(".gz"):
10568
cmd = f"gzip -dc {qp} 2>/dev/null || zcat {qp} 2>/dev/null"
10669
res = self._run_sut_cmd(cmd, sudo=True, log_artifact=False)
10770
if res.exit_code == 0 and res.stdout is not None:
108-
fname = self._nice_dmesg_name(p)
71+
fname = nice_rotated_name(p, "dmesg")
10972
self.logger.info("Collected dmesg log: %s", fname)
11073
self.result.artifacts.append(
11174
TextFileArtifact(filename=fname, contents=res.stdout)
@@ -121,7 +84,7 @@ def _collect_dmesg_rotations(self):
12184
cmd = f"cat {qp}"
12285
res = self._run_sut_cmd(cmd, sudo=True, log_artifact=False)
12386
if res.exit_code == 0 and res.stdout is not None:
124-
fname = self._nice_dmesg_name(p)
87+
fname = nice_rotated_name(p, "dmesg")
12588
self.logger.info("Collected dmesg log: %s", fname)
12689
self.result.artifacts.append(
12790
TextFileArtifact(filename=fname, contents=res.stdout)
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 .syslog_plugin import SyslogPlugin
27+
28+
__all__ = ["SyslogPlugin"]
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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.connection.inband.inband import TextFileArtifact
29+
from nodescraper.enums import EventCategory, EventPriority, OSFamily
30+
from nodescraper.models import TaskResult
31+
from nodescraper.utils import nice_rotated_name, shell_quote
32+
33+
from .syslogdata import SyslogData
34+
35+
36+
class SyslogCollector(InBandDataCollector[SyslogData, None]):
37+
"""Read syslog log"""
38+
39+
SUPPORTED_OS_FAMILY = {OSFamily.LINUX}
40+
41+
DATA_MODEL = SyslogData
42+
43+
SYSLOG_CMD = r"ls -1 /var/log/syslog* 2>/dev/null | grep -E '^/var/log/syslog(\.[0-9]+(\.gz)?)?$' || true"
44+
45+
def _collect_syslog_rotations(self) -> list[TextFileArtifact]:
46+
ret = []
47+
list_res = self._run_sut_cmd(self.SYSLOG_CMD, sudo=True)
48+
paths = [p.strip() for p in (list_res.stdout or "").splitlines() if p.strip()]
49+
if not paths:
50+
self._log_event(
51+
category=EventCategory.OS,
52+
description="No /var/log/syslog files found (including rotations).",
53+
data={"list_exit_code": list_res.exit_code},
54+
priority=EventPriority.WARNING,
55+
)
56+
return []
57+
58+
collected_logs, failed_logs = [], []
59+
collected = []
60+
for p in paths:
61+
qp = shell_quote(p)
62+
if p.endswith(".gz"):
63+
cmd = f"gzip -dc {qp} 2>/dev/null || zcat {qp} 2>/dev/null"
64+
res = self._run_sut_cmd(cmd, sudo=True, log_artifact=False)
65+
if res.exit_code == 0 and res.stdout is not None:
66+
fname = nice_rotated_name(p, "syslog")
67+
self.logger.info("Collected syslog log: %s", fname)
68+
collected.append(TextFileArtifact(filename=fname, contents=res.stdout))
69+
collected_logs.append(fname)
70+
else:
71+
failed_logs.append(p)
72+
else:
73+
cmd = f"cat {qp}"
74+
res = self._run_sut_cmd(cmd, sudo=True, log_artifact=False)
75+
if res.exit_code == 0 and res.stdout is not None:
76+
fname = nice_rotated_name(p, "syslog")
77+
self.logger.info("Collected syslog log: %s", fname)
78+
collected_logs.append(fname)
79+
collected.append(TextFileArtifact(filename=fname, contents=res.stdout))
80+
else:
81+
failed_logs.append(p)
82+
83+
if collected_logs:
84+
self._log_event(
85+
category=EventCategory.OS,
86+
description="Collected syslog rotated files",
87+
data={"collected": collected_logs},
88+
priority=EventPriority.INFO,
89+
)
90+
self.result.message = self.result.message or "syslog rotated files collected"
91+
92+
if failed_logs:
93+
self._log_event(
94+
category=EventCategory.OS,
95+
description="Some syslog files could not be collected.",
96+
data={"failed": failed_logs},
97+
priority=EventPriority.WARNING,
98+
)
99+
100+
if collected:
101+
ret = collected
102+
return ret
103+
104+
def collect_data(
105+
self,
106+
args=None,
107+
) -> tuple[TaskResult, SyslogData | None]:
108+
"""Collect syslog data from the system
109+
110+
Returns:
111+
tuple[TaskResult | None]: tuple containing the result of the task and the syslog data if available
112+
"""
113+
syslog_logs = self._collect_syslog_rotations()
114+
115+
if syslog_logs:
116+
syslog_data = SyslogData(syslog_logs=syslog_logs)
117+
self.result.message = "Syslog data collected"
118+
return self.result, syslog_data
119+
120+
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 .syslog_collector import SyslogCollector
29+
from .syslogdata import SyslogData
30+
31+
32+
class SyslogPlugin(InBandDataPlugin[SyslogData, None, None]):
33+
"""Plugin for collection of syslog data"""
34+
35+
DATA_MODEL = SyslogData
36+
37+
COLLECTOR = SyslogCollector
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.connection.inband.inband import TextFileArtifact
29+
from nodescraper.models import DataModel
30+
31+
32+
class SyslogData(DataModel):
33+
"""Data model for in band syslog logs"""
34+
35+
syslog_logs: list[TextFileArtifact] = []
36+
37+
def log_model(self, log_path: str):
38+
"""Log data model to a file
39+
40+
Args:
41+
log_path (str): log path
42+
"""
43+
for artifact in self.syslog_logs:
44+
log_name = os.path.join(log_path, artifact.filename)
45+
with open(log_name, "w", encoding="utf-8") as log_file:
46+
log_file.write(artifact.contents)

nodescraper/utils.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,35 @@ def bytes_to_human_readable(input_bytes: int) -> str:
169169

170170
gb = round(mb / 1000, 2)
171171
return f"{gb}GB"
172+
173+
174+
def shell_quote(s: str) -> str:
175+
"""Single quote fix
176+
177+
Args:
178+
s (str): path to be converted
179+
180+
Returns:
181+
str: path to be returned
182+
"""
183+
return "'" + s.replace("'", "'\"'\"'") + "'"
184+
185+
186+
def nice_rotated_name(path: str, stem: str, prefix: str = "rotated_") -> str:
187+
"""Map path to a new local filename, generalized for any stem."""
188+
base = path.rstrip("/").rsplit("/", 1)[-1]
189+
s = re.escape(stem)
190+
191+
if base == stem:
192+
return f"{prefix}{stem}.log"
193+
194+
m = re.fullmatch(rf"{s}\.(\d+)\.gz", base)
195+
if m:
196+
return f"{prefix}{stem}.{m.group(1)}.gz.log"
197+
198+
m = re.fullmatch(rf"{s}\.(\d+)", base)
199+
if m:
200+
return f"{prefix}{stem}.{m.group(1)}.log"
201+
202+
middle = base[:-3] if base.endswith(".gz") else base
203+
return f"{prefix}{middle}.log"

0 commit comments

Comments
 (0)