diff --git a/python/redfish-api/redfish_exporter.py b/python/redfish-api/redfish_exporter.py index c710494..d9336f4 100644 --- a/python/redfish-api/redfish_exporter.py +++ b/python/redfish-api/redfish_exporter.py @@ -217,6 +217,28 @@ 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: + return + + try: + logout_url = f"https://{host.fqdn}/redfish/v1/SessionService/Sessions" + async with session.delete( + logout_url, + headers={"X-Auth-Token": host.session_token}, + ssl=False, + timeout=5, + ) as resp: + 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) + except Exception as e: + logging.warning("Error during logout for %s: %s", host.fqdn, e) + finally: + host.session_token = None + async def run_exporter(config, stop_event): """Main loop""" @@ -248,11 +270,15 @@ async def run_exporter(config, stop_event): # Connection pooling with aiohttp connector = aiohttp.TCPConnector(limit_per_host=5, limit=50, ttl_dns_cache=300) async with aiohttp.ClientSession(connector=connector) as session: + hosts = [HostConfig(**h) for h in config["hosts"]] while not stop_event.is_set(): tasks = [get_power_data(session, hc) for hc in host_objs] await asyncio.gather(*tasks) await process_request(interval) + # shutdown service + logging.info("Exporter stopping, logging out from Redfish sessions...") + await asyncio.gather(*(logout_host(session, h) for h in hosts if h.session_token)) logging.info("Exporter stopped cleanly.")