apply ruff format
This commit is contained in:
@@ -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,8 +56,8 @@ 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)
|
||||
@@ -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)
|
||||
@@ -411,7 +422,6 @@ async def get_power_data(session, host: HostConfig):
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user