From 73152001339e1640dbde94d415a1d1c7a4dcaecf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Wed, 6 May 2026 19:48:04 -0300 Subject: [PATCH] Replace pyghmi.util.webclient with aiohmi.util.webclient --- misc/cfg-dhcp-redfish-bmcs-by-switch.py | 64 +++++++++++-------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/misc/cfg-dhcp-redfish-bmcs-by-switch.py b/misc/cfg-dhcp-redfish-bmcs-by-switch.py index a0249641..7b9fe422 100755 --- a/misc/cfg-dhcp-redfish-bmcs-by-switch.py +++ b/misc/cfg-dhcp-redfish-bmcs-by-switch.py @@ -30,32 +30,23 @@ # recommend, but hopefully can be useful reference material +import asyncio import sys sys.path.append('/opt/confluent/lib/python') -import concurrent.futures +import aiohmi.util.webclient as webclient import confluent.client as cli -import gzip -import io import json import os import struct import subprocess -import pyghmi.util.webclient as webclient import time bmcsbyuuid = {} -def checkfish(addr, mac): - wc = webclient.SecureHTTPConnection(addr, 443, verifycallback=lambda x: True) - wc.connect() - wc.request('GET', '/redfish/v1') - rsp = wc.getresponse() - body = rsp.read() - if body[:2] == b'\x1f\x8b': - body = gzip.GzipFile(fileobj=io.BytesIO(body)).read() - try: - body = json.loads(body) - except json.decoder.JSONDecodeError: +async def checkfish(addr, mac): + wc = webclient.WebConnection(addr, 443, verifycallback=lambda x: True) + body = await wc.grab_json_response('/redfish/v1') + if not body: return None uuid = body.get('UUID', None) if not uuid: @@ -70,6 +61,27 @@ def checkfish(addr, mac): return (uuid, mac, addr) +async def probe_bmcs(mactonode, mactoips): + tasks = [] + macs = [] + for mac in sorted(mactonode): + tasks.append(checkfish(mactoips[mac], mac)) + macs.append(mac) + results = await asyncio.gather(*tasks, return_exceptions=True) + for mac, result in zip(macs, results): + if isinstance(result, Exception): + sys.stderr.write('Failed to probe {}: {}\n'.format( + mactoips[mac], result)) + continue + if result is None: + continue + uuid, mac, addr = result + if uuid in bmcsbyuuid: + bmcsbyuuid[uuid]['bmcs'][mac] = addr + else: + bmcsbyuuid[uuid] = {'bmcs': {mac: addr}} + + if __name__ == '__main__': with open('/var/lib/dhcpd/dhcpd.leases', 'r') as leasefile: leases = leasefile.read() @@ -90,7 +102,7 @@ if __name__ == '__main__': currip = None inlease = False # warm up arp tables and fdb - pings = {} + pings = {} for mac in mactoips: pings[mac] = subprocess.Popen(['ping', '-c', '1', mactoips[mac]], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) for mac in pings: @@ -110,25 +122,7 @@ if __name__ == '__main__': for inf in macinfo: if inf.get('possiblenode', None): mactonode[mac] = inf['possiblenode'] - with concurrent.futures.ThreadPoolExecutor() as executor: - futures = {} - for mac in sorted(mactonode): - futures[executor.submit(checkfish, mactoips[mac], mac)] = mac - for future in concurrent.futures.as_completed(futures): - mac = futures[future] - try: - result = future.result() - except Exception as e: - sys.stderr.write('Failed to probe {}: {}\n'.format( - mactoips[mac], e)) - continue - if result is None: - continue - uuid, mac, addr = result - if uuid in bmcsbyuuid: - bmcsbyuuid[uuid]['bmcs'][mac] = addr - else: - bmcsbyuuid[uuid] = {'bmcs': {mac: addr}} + asyncio.run(probe_bmcs(mactonode, mactoips)) for uuid in sorted(bmcsbyuuid): macd = bmcsbyuuid[uuid]['bmcs'] macs = sorted(macd)