Skip to content

Commit 80b283b

Browse files
committed
fix for multi json return
1 parent fcee500 commit 80b283b

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

nodescraper/plugins/inband/amdsmi/amdsmi_collector.py

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,50 @@ def _run_amd_smi_dict(self, cmd: str) -> Optional[Union[dict, list[dict]]]:
183183
cmd_ret = self._run_amd_smi(cmd)
184184
if cmd_ret:
185185
try:
186+
# Try to parse as single JSON first
186187
return json.loads(cmd_ret)
187188
except json.JSONDecodeError as e:
188-
self._log_event(
189-
category=EventCategory.APPLICATION,
190-
description=f"Error parsing command: `{cmd}` json data",
191-
data={
192-
"cmd": cmd,
193-
"exception": get_exception_traceback(e),
194-
},
195-
priority=EventPriority.ERROR,
196-
console_log=True,
197-
)
198-
return None
189+
# try to extract and parse multiple JSON objects
190+
try:
191+
json_objects = []
192+
decoder = json.JSONDecoder()
193+
idx = 0
194+
cmd_ret_stripped = cmd_ret.strip()
195+
196+
while idx < len(cmd_ret_stripped):
197+
while idx < len(cmd_ret_stripped) and cmd_ret_stripped[idx].isspace():
198+
idx += 1
199+
200+
if idx >= len(cmd_ret_stripped):
201+
break
202+
203+
if cmd_ret_stripped[idx] not in ["{", "["]:
204+
break
205+
206+
try:
207+
obj, end_idx = decoder.raw_decode(cmd_ret_stripped, idx)
208+
json_objects.append(obj)
209+
idx += end_idx
210+
except json.JSONDecodeError:
211+
break
212+
213+
if json_objects:
214+
return json_objects if len(json_objects) > 1 else json_objects[0]
215+
else:
216+
raise
217+
218+
except Exception:
219+
self._log_event(
220+
category=EventCategory.APPLICATION,
221+
description=f"Error parsing command: `{cmd}` json data",
222+
data={
223+
"cmd": cmd,
224+
"exception": get_exception_traceback(e),
225+
},
226+
priority=EventPriority.ERROR,
227+
console_log=True,
228+
)
229+
return None
199230
return None
200231

201232
def _to_number(self, v: object) -> Optional[Union[int, float]]:

0 commit comments

Comments
 (0)