From b1db6212a0b9f3e0b0311c37aa7f4003d07d5dcb Mon Sep 17 00:00:00 2001 From: Marco Lucarelli Date: Fri, 30 Jan 2026 15:15:42 +0100 Subject: [PATCH] apply ruff format --- python/redfish-api/redfish_exporter_v9000.py | 45 ++++++++++++++------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/python/redfish-api/redfish_exporter_v9000.py b/python/redfish-api/redfish_exporter_v9000.py index 6b0d8eb..e38a4e1 100644 --- a/python/redfish-api/redfish_exporter_v9000.py +++ b/python/redfish-api/redfish_exporter_v9000.py @@ -22,22 +22,27 @@ from prometheus_client import ( @dataclass class RedfishResource: """Container for Redfish resource URLs.""" + chassis: str | None = None systems: str | None = None power: str | None = None session_service: str | None = None + @dataclass class PowerMetrics: """Container for power metrics.""" + voltage: float | None = None watts: float | None = None amps: float | None = None serial: str | None = None + @dataclass class RedfishSession: """Container for Redfish session data.""" + token: str | None = None loggout_url: str | None = None vendor: str | None = None @@ -51,12 +56,12 @@ class HostConfig: username: str password: str chassis: list[str] | None = None - max_retries: int = 3 # 3 retires - backoff: int = 2 # wait 2 seconds + max_retries: int = 3 # 3 retires + backoff: int = 2 # wait 2 seconds cool_down: int = 120 # seconds to wait after too many failures failures: int = 0 next_retry_time: float = field(default=0.0, init=False) - session: RedfishSession = field(default_factory=RedfishSession) + session: RedfishSession = field(default_factory=RedfishSession) # New attributes for Redfish stuff vendor: str | None = None @@ -146,7 +151,9 @@ async def login_hpe(session, host: HostConfig) -> bool: payload = {"UserName": host.username, "Password": host.password} try: - 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") host.session.logout_url = login_resp.headers.get("Location") @@ -177,7 +184,9 @@ async def fetch_with_retry(session, host: HostConfig, url: str) -> dict | None: if not host.session.vendor: host.session.vendor = await probe_vendor(session, host) - is_hpe = host.session.vendor and host.session.vendor.strip().upper().startswith("HPE") + is_hpe = host.session.vendor and host.session.vendor.strip().upper().startswith( + "HPE" + ) for attempt in range(1, host.max_retries + 1): try: @@ -244,7 +253,9 @@ async def fetch_with_retry(session, host: HostConfig, url: str) -> dict | None: return None -async def discover_redfish_resources(session, host: HostConfig) -> RedfishResource | None: +async def discover_redfish_resources( + session, host: HostConfig +) -> RedfishResource | None: """Discover available Redfish resources and return relevant URLs""" root_url = f"https://{host.fqdn}/redfish/v1/" data = await fetch_with_retry(session, host, root_url) @@ -261,7 +272,7 @@ async def discover_redfish_resources(session, host: HostConfig) -> RedfishResour if not resources.chassis: logging.error("No valid Chassis URL found for host %s", host.fqdn) return None - + return resources @@ -404,14 +415,13 @@ async def get_power_data(session, host: HostConfig): host.mark_success() UP_GAUGE.labels(host=host.fqdn).set(1) - chassis_url = f"https://{host.fqdn}{resources.chassis}" + chassis_url = f"https://{host.fqdn}{resources.chassis}" chassis_data = await fetch_with_retry(session, host, chassis_url) if not chassis_data: host.mark_failure() UP_GAUGE.labels(host=host.fqdn).set(0) return - for chassis_member in chassis_data.get("Members", []): chassis_member_url = chassis_member.get("@odata.id") if not chassis_member_url: @@ -432,7 +442,9 @@ async def get_power_data(session, host: HostConfig): continue # Get Power ressource (fallback to "Power") - power_resource_url, power_resource_type = get_power_resource_info(member_data, host.fqdn) + power_resource_url, power_resource_type = get_power_resource_info( + member_data, host.fqdn + ) if not power_resource_url: continue @@ -450,7 +462,9 @@ async def get_power_data(session, host: HostConfig): continue power_supplies_url = f"https://{host.fqdn}{power_supplies_url}" - power_supplies_data = await fetch_with_retry(session, host, power_supplies_url) + power_supplies_data = await fetch_with_retry( + session, host, power_supplies_url + ) if not power_supplies_data: continue @@ -466,7 +480,9 @@ async def get_power_data(session, host: HostConfig): continue # Process PowerSupplies object - metrics = await process_power_supply(session, host, psu_data, "PowerSubsystem") + metrics = await process_power_supply( + session, host, psu_data, "PowerSubsystem" + ) if metrics: update_prometheus_metrics(host, metrics) @@ -485,10 +501,13 @@ async def get_power_data(session, host: HostConfig): # Measure request and process latency REQUEST_LATENCY.labels(host=host.fqdn).observe(time.monotonic() - start) + def update_prometheus_metrics(host: HostConfig, metrics: PowerMetrics): """Update Prometheus metrics with PowerMetrics data.""" if metrics.voltage is not None and metrics.serial: - VOLTAGE_GAUGE.labels(host=host.fqdn, psu_serial=metrics.serial).set(metrics.voltage) + VOLTAGE_GAUGE.labels(host=host.fqdn, psu_serial=metrics.serial).set( + metrics.voltage + ) if metrics.watts is not None and metrics.serial: WATTS_GAUGE.labels(host=host.fqdn, psu_serial=metrics.serial).set(metrics.watts) if metrics.amps is not None and metrics.serial: