Compare commits

..

2 Commits

Author SHA1 Message Date
b1db6212a0 apply ruff format 2026-01-30 15:15:42 +01:00
d8fc5cd8b8 mixe mixed up dataclass 2026-01-30 15:15:01 +01:00

View File

@@ -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
@@ -392,7 +403,8 @@ async def get_power_data(session, host: HostConfig):
# Start time measurement
start = time.monotonic()
# Get Root ressources
# Get root ressources
resources = await discover_redfish_resources(session, host)
if not resources or not resources.chassis:
logging.error("Could not discover any resources for %s", host.fqdn)
@@ -403,14 +415,13 @@ async def get_power_data(session, host: HostConfig):
host.mark_success()
UP_GAUGE.labels(host=host.fqdn).set(1)
chassis_url = resources.get("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:
@@ -431,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
@@ -449,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
@@ -465,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)
@@ -484,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: