Skip to content

Commit 398d01d

Browse files
committed
moved syslog files to datamodel
1 parent b7988dd commit 398d01d

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

nodescraper/plugins/inband/syslog/syslog_collector.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class SyslogCollector(InBandDataCollector[SyslogData, None]):
4242

4343
SYSLOG_CMD = r"ls -1 /var/log/syslog* 2>/dev/null | grep -E '^/var/log/syslog(\.[0-9]+(\.gz)?)?$' || true"
4444

45-
def _collect_syslog_rotations(self) -> list[str]:
45+
def _collect_syslog_rotations(self) -> list[TextFileArtifact]:
4646
ret = []
4747
list_res = self._run_sut_cmd(self.SYSLOG_CMD, sudo=True)
4848
paths = [p.strip() for p in (list_res.stdout or "").splitlines() if p.strip()]
@@ -56,6 +56,7 @@ def _collect_syslog_rotations(self) -> list[str]:
5656
return []
5757

5858
collected_logs, failed_logs = [], []
59+
collected = []
5960
for p in paths:
6061
qp = shell_quote(p)
6162
if p.endswith(".gz"):
@@ -64,9 +65,7 @@ def _collect_syslog_rotations(self) -> list[str]:
6465
if res.exit_code == 0 and res.stdout is not None:
6566
fname = nice_rotated_name(p, "syslog")
6667
self.logger.info("Collected syslog log: %s", fname)
67-
self.result.artifacts.append(
68-
TextFileArtifact(filename=fname, contents=res.stdout)
69-
)
68+
collected.append(TextFileArtifact(filename=fname, contents=res.stdout))
7069
collected_logs.append(fname)
7170
else:
7271
failed_logs.append(p)
@@ -76,10 +75,8 @@ def _collect_syslog_rotations(self) -> list[str]:
7675
if res.exit_code == 0 and res.stdout is not None:
7776
fname = nice_rotated_name(p, "syslog")
7877
self.logger.info("Collected syslog log: %s", fname)
79-
self.result.artifacts.append(
80-
TextFileArtifact(filename=fname, contents=res.stdout)
81-
)
8278
collected_logs.append(fname)
79+
collected.append(TextFileArtifact(filename=fname, contents=res.stdout))
8380
else:
8481
failed_logs.append(p)
8582

@@ -100,8 +97,8 @@ def _collect_syslog_rotations(self) -> list[str]:
10097
priority=EventPriority.WARNING,
10198
)
10299

103-
if collected_logs:
104-
ret = collected_logs
100+
if collected:
101+
ret = collected
105102
return ret
106103

107104
def collect_data(

nodescraper/plugins/inband/syslog/syslogdata.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,24 @@
2323
# SOFTWARE.
2424
#
2525
###############################################################################
26+
import os
27+
28+
from nodescraper.connection.inband.inband import TextFileArtifact
2629
from nodescraper.models import DataModel
2730

2831

2932
class SyslogData(DataModel):
3033
"""Data model for in band syslog logs"""
3134

32-
syslog_logs: list[str] = []
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)

test/unit/plugin/test_syslog_collector.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,10 @@ def run_map(cmd, **kwargs):
9191
c = get_collector(monkeypatch, run_map, system_info, conn_mock)
9292

9393
n = c._collect_syslog_rotations()
94-
assert n == [
95-
"rotated_syslog.log",
96-
"rotated_syslog.1.log",
97-
"rotated_syslog.2.gz.log",
98-
"rotated_syslog.10.gz.log",
99-
]
100-
101-
names = {a.filename for a in c.result.artifacts}
102-
assert names == {
103-
"rotated_syslog.log",
104-
"rotated_syslog.1.log",
105-
"rotated_syslog.2.gz.log",
106-
"rotated_syslog.10.gz.log",
107-
}
94+
assert n[0].filename == "rotated_syslog.log"
95+
assert n[1].filename == "rotated_syslog.1.log"
96+
assert n[2].filename == "rotated_syslog.2.gz.log"
97+
assert n[3].filename == "rotated_syslog.10.gz.log"
10898

10999
descs = [e["description"] for e in c._events]
110100
assert "Collected syslog rotated files" in descs
@@ -167,5 +157,5 @@ def run_map(cmd, **kwargs):
167157

168158
result, data = c.collect_data()
169159
assert isinstance(data, SyslogData)
170-
assert data.syslog_logs == ["rotated_syslog.log"]
160+
assert data.syslog_logs[0].filename == "rotated_syslog.log"
171161
assert c.result.message == "Syslog data collected"

0 commit comments

Comments
 (0)