2323# SOFTWARE.
2424#
2525###############################################################################
26- import re
2726
2827from nodescraper .base import InBandDataCollector
2928from nodescraper .connection .inband .inband import TextFileArtifact
3029from nodescraper .enums import EventCategory , EventPriority , OSFamily
3130from nodescraper .models import TaskResult
31+ from nodescraper .utils import nice_rotated_name , shell_quote
3232
3333from .syslogdata import SyslogData
3434
@@ -42,36 +42,8 @@ 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 _shell_quote (self , s : str ) -> str :
46- """single-quote fix."""
47- return "'" + s .replace ("'" , "'\" '\" '" ) + "'"
48-
49- def _nice_syslog_name (self , path : str ) -> str :
50- """Map path to filename
51- Args:
52- path (str): file path
53- Returns:
54- str: new local filename
55- """
56- prefix = "rotated_"
57- base = path .rstrip ("/" ).rsplit ("/" , 1 )[- 1 ]
58-
59- if base == "syslog" :
60- return f"{ prefix } syslog.log"
61-
62- m = re .fullmatch (r"syslog\.(\d+)\.gz" , base )
63- if m :
64- return f"{ prefix } syslog.{ m .group (1 )} .gz.log"
65-
66- m = re .fullmatch (r"syslog\.(\d+)" , base )
67- if m :
68- return f"{ prefix } syslog.{ m .group (1 )} .log"
69-
70- middle = base [:- 3 ] if base .endswith (".gz" ) else base
71- return f"{ prefix } { middle } .log"
72-
73- def _collect_syslog_rotations (self ) -> int :
74- ret = 0
45+ def _collect_syslog_rotations (self ) -> list [str ]:
46+ ret = []
7547 list_res = self ._run_sut_cmd (self .SYSLOG_CMD , sudo = True )
7648 paths = [p .strip () for p in (list_res .stdout or "" ).splitlines () if p .strip ()]
7749 if not paths :
@@ -81,43 +53,35 @@ def _collect_syslog_rotations(self) -> int:
8153 data = {"list_exit_code" : list_res .exit_code },
8254 priority = EventPriority .WARNING ,
8355 )
84- return 0
56+ return []
8557
8658 collected_logs , failed_logs = [], []
8759 for p in paths :
88- qp = self . _shell_quote (p )
60+ qp = shell_quote (p )
8961 if p .endswith (".gz" ):
9062 cmd = f"gzip -dc { qp } 2>/dev/null || zcat { qp } 2>/dev/null"
9163 res = self ._run_sut_cmd (cmd , sudo = True , log_artifact = False )
9264 if res .exit_code == 0 and res .stdout is not None :
93- fname = self . _nice_syslog_name ( p )
65+ fname = nice_rotated_name ( p , "syslog" )
9466 self .logger .info ("Collected syslog log: %s" , fname )
9567 self .result .artifacts .append (
9668 TextFileArtifact (filename = fname , contents = res .stdout )
9769 )
98- collected_logs .append (
99- {"path" : p , "as" : fname , "bytes" : len (res .stdout .encode ("utf-8" , "ignore" ))}
100- )
70+ collected_logs .append (fname )
10171 else :
102- failed_logs .append (
103- {"path" : p , "exit_code" : res .exit_code , "stderr" : res .stderr , "cmd" : cmd }
104- )
72+ failed_logs .append (p )
10573 else :
10674 cmd = f"cat { qp } "
10775 res = self ._run_sut_cmd (cmd , sudo = True , log_artifact = False )
10876 if res .exit_code == 0 and res .stdout is not None :
109- fname = self . _nice_syslog_name ( p )
77+ fname = nice_rotated_name ( p , "syslog" )
11078 self .logger .info ("Collected syslog log: %s" , fname )
11179 self .result .artifacts .append (
11280 TextFileArtifact (filename = fname , contents = res .stdout )
11381 )
114- collected_logs .append (
115- {"path" : p , "as" : fname , "bytes" : len (res .stdout .encode ("utf-8" , "ignore" ))}
116- )
82+ collected_logs .append (fname )
11783 else :
118- failed_logs .append (
119- {"path" : p , "exit_code" : res .exit_code , "stderr" : res .stderr , "cmd" : cmd }
120- )
84+ failed_logs .append (p )
12185
12286 if collected_logs :
12387 self ._log_event (
@@ -137,7 +101,7 @@ def _collect_syslog_rotations(self) -> int:
137101 )
138102
139103 if collected_logs :
140- ret = len ( collected_logs )
104+ ret = collected_logs
141105 return ret
142106
143107 def collect_data (
0 commit comments