apply ruff format

This commit is contained in:
2026-01-30 15:15:42 +01:00
parent d8fc5cd8b8
commit b1db6212a0

View File

@@ -22,22 +22,27 @@ from prometheus_client import (
@dataclass @dataclass
class RedfishResource: class RedfishResource:
"""Container for Redfish resource URLs.""" """Container for Redfish resource URLs."""
chassis: str | None = None chassis: str | None = None
systems: str | None = None systems: str | None = None
power: str | None = None power: str | None = None
session_service: str | None = None session_service: str | None = None
@dataclass @dataclass
class PowerMetrics: class PowerMetrics:
"""Container for power metrics.""" """Container for power metrics."""
voltage: float | None = None voltage: float | None = None
watts: float | None = None watts: float | None = None
amps: float | None = None amps: float | None = None
serial: str | None = None serial: str | None = None
@dataclass @dataclass
class RedfishSession: class RedfishSession:
"""Container for Redfish session data.""" """Container for Redfish session data."""
token: str | None = None token: str | None = None
loggout_url: str | None = None loggout_url: str | None = None
vendor: str | None = None vendor: str | None = None
@@ -146,7 +151,9 @@ async def login_hpe(session, host: HostConfig) -> bool:
payload = {"UserName": host.username, "Password": host.password} payload = {"UserName": host.username, "Password": host.password}
try: 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: if login_resp.status == 201:
host.session.token = login_resp.headers.get("X-Auth-Token") host.session.token = login_resp.headers.get("X-Auth-Token")
host.session.logout_url = login_resp.headers.get("Location") 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: if not host.session.vendor:
host.session.vendor = await probe_vendor(session, host) 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): for attempt in range(1, host.max_retries + 1):
try: try:
@@ -244,7 +253,9 @@ async def fetch_with_retry(session, host: HostConfig, url: str) -> dict | None:
return 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""" """Discover available Redfish resources and return relevant URLs"""
root_url = f"https://{host.fqdn}/redfish/v1/" root_url = f"https://{host.fqdn}/redfish/v1/"
data = await fetch_with_retry(session, host, root_url) data = await fetch_with_retry(session, host, root_url)
@@ -411,7 +422,6 @@ async def get_power_data(session, host: HostConfig):
UP_GAUGE.labels(host=host.fqdn).set(0) UP_GAUGE.labels(host=host.fqdn).set(0)
return return
for chassis_member in chassis_data.get("Members", []): for chassis_member in chassis_data.get("Members", []):
chassis_member_url = chassis_member.get("@odata.id") chassis_member_url = chassis_member.get("@odata.id")
if not chassis_member_url: if not chassis_member_url:
@@ -432,7 +442,9 @@ async def get_power_data(session, host: HostConfig):
continue continue
# Get Power ressource (fallback to "Power") # 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: if not power_resource_url:
continue continue
@@ -450,7 +462,9 @@ async def get_power_data(session, host: HostConfig):
continue continue
power_supplies_url = f"https://{host.fqdn}{power_supplies_url}" 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: if not power_supplies_data:
continue continue
@@ -466,7 +480,9 @@ async def get_power_data(session, host: HostConfig):
continue continue
# Process PowerSupplies object # 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: if metrics:
update_prometheus_metrics(host, metrics) update_prometheus_metrics(host, metrics)
@@ -485,10 +501,13 @@ async def get_power_data(session, host: HostConfig):
# Measure request and process latency # Measure request and process latency
REQUEST_LATENCY.labels(host=host.fqdn).observe(time.monotonic() - start) REQUEST_LATENCY.labels(host=host.fqdn).observe(time.monotonic() - start)
def update_prometheus_metrics(host: HostConfig, metrics: PowerMetrics): def update_prometheus_metrics(host: HostConfig, metrics: PowerMetrics):
"""Update Prometheus metrics with PowerMetrics data.""" """Update Prometheus metrics with PowerMetrics data."""
if metrics.voltage is not None and metrics.serial: 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: if metrics.watts is not None and metrics.serial:
WATTS_GAUGE.labels(host=host.fqdn, psu_serial=metrics.serial).set(metrics.watts) WATTS_GAUGE.labels(host=host.fqdn, psu_serial=metrics.serial).set(metrics.watts)
if metrics.amps is not None and metrics.serial: if metrics.amps is not None and metrics.serial: