Skip to content

Commit a7eea9e

Browse files
committed
readded local _warning function to shorten code
1 parent 1dd4bdc commit a7eea9e

File tree

1 file changed

+27
-57
lines changed

1 file changed

+27
-57
lines changed

nodescraper/plugins/inband/device_enumeration/device_enumeration_collector.py

Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from typing import Optional
2727

2828
from nodescraper.base import InBandDataCollector
29-
from nodescraper.connection.inband.inband import TextFileArtifact
29+
from nodescraper.connection.inband.inband import CommandArtifact, TextFileArtifact
3030
from nodescraper.enums import EventCategory, EventPriority, ExecutionStatus, OSFamily
3131
from nodescraper.models import TaskResult
3232

@@ -51,6 +51,23 @@ class DeviceEnumerationCollector(InBandDataCollector[DeviceEnumerationDataModel,
5151
'powershell -Command "(Get-VMHostPartitionableGpu | Measure-Object).Count"'
5252
)
5353

54+
def _warning(
55+
self,
56+
description: str,
57+
command: CommandArtifact,
58+
category: EventCategory = EventCategory.PLATFORM,
59+
):
60+
self._log_event(
61+
category=category,
62+
description=description,
63+
data={
64+
"command": command.command,
65+
"exit_code": command.exit_code,
66+
"stderr": command.stderr,
67+
},
68+
priority=EventPriority.WARNING,
69+
)
70+
5471
def collect_data(self, args=None) -> tuple[TaskResult, Optional[DeviceEnumerationDataModel]]:
5572
"""
5673
Read CPU and GPU count
@@ -87,15 +104,9 @@ def collect_data(self, args=None) -> tuple[TaskResult, Optional[DeviceEnumeratio
87104
device_enum.cpu_count = int(line.split(":")[1].strip())
88105
break
89106
except (ValueError, IndexError):
90-
self._log_event(
91-
category=EventCategory.PLATFORM,
107+
self._warning(
92108
description="Cannot parse CPU count from lscpu output",
93-
data={
94-
"command": lscpu_res.command,
95-
"exit_code": lscpu_res.exit_code,
96-
"stderr": lscpu_res.stderr,
97-
},
98-
priority=EventPriority.WARNING,
109+
command=lscpu_res,
99110
)
100111
device_enum.lscpu_output = lscpu_res.stdout
101112
self._log_event(
@@ -104,57 +115,25 @@ def collect_data(self, args=None) -> tuple[TaskResult, Optional[DeviceEnumeratio
104115
priority=EventPriority.INFO,
105116
)
106117
else:
107-
self._log_event(
108-
category=EventCategory.PLATFORM,
109-
description="Cannot collect lscpu output",
110-
data={
111-
"command": lscpu_res.command,
112-
"exit_code": lscpu_res.exit_code,
113-
"stderr": lscpu_res.stderr,
114-
},
115-
priority=EventPriority.WARNING,
116-
)
118+
self._warning(description="Cannot collect lscpu output", command=lscpu_res)
117119
else:
118120
if cpu_count_res.exit_code == 0:
119121
device_enum.cpu_count = int(cpu_count_res.stdout)
120122
else:
121-
self._log_event(
122-
category=EventCategory.PLATFORM,
123-
description="Cannot determine CPU count",
124-
data={
125-
"command": cpu_count_res.command,
126-
"exit_code": cpu_count_res.exit_code,
127-
"stderr": cpu_count_res.stderr,
128-
},
129-
priority=EventPriority.WARNING,
130-
)
123+
self._warning(description="Cannot determine CPU count", command=cpu_count_res)
131124

132125
if gpu_count_res.exit_code == 0:
133126
device_enum.gpu_count = int(gpu_count_res.stdout)
134127
else:
135-
self._log_event(
136-
category=EventCategory.PLATFORM,
137-
description="Cannot determine GPU count",
138-
data={
139-
"command": gpu_count_res.command,
140-
"exit_code": gpu_count_res.exit_code,
141-
"stderr": gpu_count_res.stderr,
142-
},
143-
priority=EventPriority.WARNING,
144-
)
128+
self._warning(description="Cannot determine GPU count", command=gpu_count_res)
145129

146130
if vf_count_res.exit_code == 0:
147131
device_enum.vf_count = int(vf_count_res.stdout)
148132
else:
149-
self._log_event(
150-
category=EventCategory.SW_DRIVER,
133+
self._warning(
151134
description="Cannot determine VF count",
152-
data={
153-
"command": vf_count_res.command,
154-
"exit_code": vf_count_res.exit_code,
155-
"stderr": vf_count_res.stderr,
156-
},
157-
priority=EventPriority.WARNING,
135+
command=vf_count_res,
136+
category=EventCategory.SW_DRIVER,
158137
)
159138

160139
# Collect lshw output on Linux
@@ -170,16 +149,7 @@ def collect_data(self, args=None) -> tuple[TaskResult, Optional[DeviceEnumeratio
170149
priority=EventPriority.INFO,
171150
)
172151
else:
173-
self._log_event(
174-
category=EventCategory.PLATFORM,
175-
description="Cannot collect lshw output",
176-
data={
177-
"command": lshw_res.command,
178-
"exit_code": lshw_res.exit_code,
179-
"stderr": lshw_res.stderr,
180-
},
181-
priority=EventPriority.WARNING,
182-
)
152+
self._warning(description="Cannot collect lshw output", command=lshw_res)
183153

184154
if device_enum.cpu_count or device_enum.gpu_count or device_enum.vf_count:
185155
log_data = device_enum.model_dump(

0 commit comments

Comments
 (0)