Skip to content

Commit fa94d8c

Browse files
committed
fixed ls cmd
1 parent ef68b40 commit fa94d8c

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

nodescraper/plugins/inband/dmesg/dmesg_collector.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,29 @@ class DmesgCollector(InBandDataCollector[DmesgData, None]):
4343
DMESG_CMD = "dmesg --time-format iso -x"
4444

4545
DMESG_LOGS_CMD = (
46-
r"ls -1 /var/log/dmesg /var/log/dmesg.1 /var/log/dmesg.[0-9]*.gz 2>/dev/null || true"
46+
r"ls -1 /var/log/dmesg* 2>/dev/null | grep -E '^/var/log/dmesg(\.[0-9]+(\.gz)?)?$' || true"
4747
)
4848

4949
def _shell_quote(self, s: str) -> str:
5050
"""single-quote fix."""
5151
return "'" + s.replace("'", "'\"'\"'") + "'"
5252

5353
def _nice_dmesg_name(self, path: str) -> str:
54-
"""Map path to filename"""
55-
if path.endswith("/dmesg"):
54+
"""Map path to filename."""
55+
base = path.rstrip("/").rsplit("/", 1)[-1]
56+
if base == "dmesg":
5657
return "dmesg.log"
57-
if path.endswith("/dmesg.1"):
58-
return "dmesg.1.log"
59-
m = re.search(r"/dmesg\.(\d+)\.gz$", path)
58+
m = re.fullmatch(r"dmesg\.(\d+)\.gz", base)
6059
if m:
6160
return f"dmesg.{m.group(1)}.gz.log"
62-
base = path.rsplit("/", 1)[-1]
63-
return base.replace(".gz", "") + ".log"
61+
m = re.fullmatch(r"dmesg\.(\d+)", base)
62+
if m:
63+
return f"dmesg.{m.group(1)}.log"
64+
65+
return (base[:-3] if base.endswith(".gz") else base) + ".log"
6466

6567
def _collect_dmesg_rotations(self) -> int:
66-
list_res = self._run_sut_cmd(
67-
r"ls -1 /var/log/dmesg /var/log/dmesg.1 /var/log/dmesg.[0-9]*.gz 2>/dev/null || true",
68-
sudo=True,
69-
)
68+
list_res = self._run_sut_cmd(self.DMESG_LOGS_CMD, sudo=True)
7069
paths = [p.strip() for p in (list_res.stdout or "").splitlines() if p.strip()]
7170
if not paths:
7271
self._log_event(

test/unit/plugin/test_dmesg_collector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def get_collector(monkeypatch, run_map, system_info, conn_mock):
159159
system_interaction_level=SystemInteractionLevel.INTERACTIVE,
160160
connection=conn_mock,
161161
)
162-
c.result = types.SimpleNamespace(artifacts=[], message=None) # minimal fields we use
162+
c.result = types.SimpleNamespace(artifacts=[], message=None)
163163
c._events = []
164164

165165
def _log_event(**kw):

0 commit comments

Comments
 (0)