From b1e5369a33340de5ff1db57197de92a931a1f68a Mon Sep 17 00:00:00 2001 From: Marco Lucarelli Date: Wed, 21 Jan 2026 08:35:27 +0100 Subject: [PATCH] apply ruff --- python/redfish-api/get_power_redfishapi.py | 4 +- python/redfish-api/redfish_exporter.py | 55 ++++++++++++++++------ 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/python/redfish-api/get_power_redfishapi.py b/python/redfish-api/get_power_redfishapi.py index 4a4577b..f855245 100644 --- a/python/redfish-api/get_power_redfishapi.py +++ b/python/redfish-api/get_power_redfishapi.py @@ -18,7 +18,7 @@ def get_power_data(host): vendor = data.get("Vendor", "") is_hpe = vendor.strip().upper().startswith("HPE") if is_hpe: - response = "" # just to be sure + response = "" # just to be sure login_url = f"https://{host}.mgmt.wtb1.ch.abainfra.net/redfish/v1/SessionService/Sessions" payload = {"UserName": username, "Password": password} response = requests.post(login_url, json=payload, verify=False, timeout=10) @@ -49,7 +49,7 @@ def get_power_data(host): watts_input = psu.get("PowerInputWatts") serial = psu.get("SerialNumber") print( - f"PSU {idx}, {serial}: {host}, {line_input_v} V, {watts_input} W, {round(watts_input/line_input_v,2)} A" + f"PSU {idx}, {serial}: {host}, {line_input_v} V, {watts_input} W, {round(watts_input / line_input_v, 2)} A" ) else: print(f"Error {response.status_code}: {response.text}") diff --git a/python/redfish-api/redfish_exporter.py b/python/redfish-api/redfish_exporter.py index 2a550ca..cf8d4f4 100644 --- a/python/redfish-api/redfish_exporter.py +++ b/python/redfish-api/redfish_exporter.py @@ -28,7 +28,9 @@ class HostConfig: # New attributes for Redfish stuff vendor: str | None = None session_token: str | None = None - session_logout: str | None = None # SessionLocation like /redfish/v1/SessionService/Sessions/marco.lucarelli%40abacus.ch00000000xxx/ + session_logout: str | None = ( + None # SessionLocation like /redfish/v1/SessionService/Sessions/marco.lucarelli%40abacus.ch00000000xxx/ + ) def should_skip(self) -> bool: """Check if host is still in cool-down window""" @@ -93,16 +95,20 @@ async def fetch_with_retry(session, host: HostConfig, url: str) -> dict | None: if not host.vendor: try: - async with session.get(f"https://{host.fqdn}/redfish/v1/", ssl=False, timeout=10) as resp: + async with session.get( + f"https://{host.fqdn}/redfish/v1/", ssl=False, timeout=10 + ) as resp: if resp.status == 200: data = await resp.json() host.vendor = data.get("Vendor", "") logging.debug("Detected vendor for %s: %s", host.fqdn, host.vendor) else: - logging.warning("Vendor probe failed on %s: HTTP %s", host.fqdn, resp.status) + logging.warning( + "Vendor probe failed on %s: HTTP %s", host.fqdn, resp.status + ) except Exception as e: logging.warning("Vendor probe failed for %s: %s", host.fqdn, e) - + is_hpe = host.vendor and host.vendor.strip().upper().startswith("HPE") for attempt in range(1, host.max_retries + 1): @@ -117,32 +123,50 @@ async def fetch_with_retry(session, host: HostConfig, url: str) -> dict | None: else: # Need to login and store new session token # HPE Redfish login - login_url = f"https://{host.fqdn}/redfish/v1/SessionService/Sessions" + login_url = ( + f"https://{host.fqdn}/redfish/v1/SessionService/Sessions" + ) payload = {"UserName": host.username, "Password": host.password} - async with session.post(login_url, json=payload, ssl=False, timeout=10) as login_resp: + async with session.post( + login_url, json=payload, ssl=False, timeout=10 + ) as login_resp: if login_resp.status == 201: - host.session_token = login_resp.headers.get("X-Auth-Token") # as response in header + host.session_token = login_resp.headers.get( + "X-Auth-Token" + ) # as response in header if not host.session_token: raise RuntimeError("No X-Auth-Token in login response") - host.session_logout = login_resp.headers.get("Location") # as response in header + host.session_logout = login_resp.headers.get( + "Location" + ) # as response in header if not host.session_logout: raise RuntimeError("No Location in login response") headers["X-Auth-Token"] = host.session_token logging.info("New session token obtained for %s", host.fqdn) else: - logging.warning("Login failed for %s: HTTP %s", host.fqdn, login_resp.status) + logging.warning( + "Login failed for %s: HTTP %s", + host.fqdn, + login_resp.status, + ) continue # retry login next attempt - async with session.get(url, headers=headers, ssl=False, timeout=10) as resp: + async with session.get( + url, headers=headers, ssl=False, timeout=10 + ) as resp: if resp.status == 200: host.mark_success() return await resp.json() elif resp.status in (401, 403): # Token expired or invalid, clear it and retry - logging.warning("Invalid token for %s, reauthenticating...", host.fqdn) + logging.warning( + "Invalid token for %s, reauthenticating...", host.fqdn + ) host.session_token = None continue - logging.warning("HTTP %s from %s (attempt %d)", resp.status, host.fqdn, attempt) + logging.warning( + "HTTP %s from %s (attempt %d)", resp.status, host.fqdn, attempt + ) else: # Default: BasicAuth, like Supermicro and so @@ -225,6 +249,7 @@ async def get_power_data(session, host: HostConfig): REQUEST_LATENCY.labels(host=host.fqdn).observe(time.monotonic() - start) + async def logout_host(session, host): """Clean logout for Redfish with session tokens""" if not host.session_token: @@ -232,7 +257,7 @@ async def logout_host(session, host): if not host.session_logout: return try: - logout_url = f"{host.session_logout}" # the full URL is here! + logout_url = f"{host.session_logout}" # the full URL is here! async with session.delete( logout_url, headers={"X-Auth-Token": host.session_token}, @@ -242,7 +267,9 @@ async def logout_host(session, host): if resp.status in (200, 204): logging.info("Logged out from %s", host.fqdn) else: - logging.warning("Logout failed for %s (HTTP %s)", host.fqdn, resp.status) + logging.warning( + "Logout failed for %s (HTTP %s)", host.fqdn, resp.status + ) except Exception as e: logging.warning("Error during logout for %s: %s", host.fqdn, e) finally: