Skip to content

Commit c0f1e9a

Browse files
committed
added log_model call for journal plugin + updated journaldata
1 parent f1f2f78 commit c0f1e9a

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

nodescraper/plugins/inband/journal/journal_collector.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
# SOFTWARE.
2424
#
2525
###############################################################################
26+
import io
27+
import json
28+
2629
from nodescraper.base import InBandDataCollector
2730
from nodescraper.enums import EventCategory, EventPriority, ExecutionStatus, OSFamily
2831
from nodescraper.models import TaskResult
@@ -42,12 +45,10 @@ def _read_with_journalctl(self):
4245
Returns:
4346
str|None: system journal read
4447
"""
45-
cmd = "journalctl --no-pager --system --all -o short-iso --output=json"
48+
cmd = "journalctl --no-pager --system --all --output=json"
4649
res = self._run_sut_cmd(cmd, sudo=True, log_artifact=False, strip=False)
4750

48-
if res.exit_code == 0:
49-
return res.stdout
50-
else:
51+
if res.exit_code != 0:
5152
self._log_event(
5253
category=EventCategory.OS,
5354
description="Error reading journalctl",
@@ -57,8 +58,20 @@ def _read_with_journalctl(self):
5758
)
5859
self.result.message = "Could not read journalctl data"
5960
self.result.status = ExecutionStatus.ERROR
61+
return None
62+
63+
raw = res.stdout
64+
text = (
65+
raw.decode("utf-8", errors="surrogateescape")
66+
if isinstance(raw, (bytes, bytearray))
67+
else raw
68+
)
69+
70+
lines = [ln for ln in (line.strip() for line in text.splitlines()) if ln.startswith("{")]
71+
array_like = "[" + ",".join(lines) + "]"
72+
entries: list[dict] = json.load(io.StringIO(array_like))
6073

61-
return None
74+
return entries
6275

6376
def collect_data(self, args=None) -> tuple[TaskResult, JournalData | None]:
6477
"""Collect journal lofs

nodescraper/plugins/inband/journal/journaldata.py

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

2831

2932
class JournalData(DataModel):
3033
"""Data model for journal logs"""
3134

32-
journal_log: str
35+
journal_log: list[dict]
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+
log_name = os.path.join(log_path, "journal.log")
44+
with open(log_name, "w", encoding="utf-8") as log_filename:
45+
for e in self.journal_log:
46+
log_filename.write(json.dumps(e, ensure_ascii=False, separators=(",", ":")))
47+
log_filename.write("\n")

test/unit/plugin/test_journal_collector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ def run_map(cmd, **kwargs):
6868
result, data = c.collect_data()
6969
assert isinstance(data, JournalData)
7070

71-
assert data.journal_log == '{"MESSAGE":"hello"}\n'
71+
assert data.journal_log == [{"MESSAGE": "hello"}]

0 commit comments

Comments
 (0)