Skip to content

Commit

Permalink
refactor: Replace redfish discover method. (#38)
Browse files Browse the repository at this point in the history
* refactor: Replace redfish discover method.

redfish.discover_ssdp method is replaced with a simple login call to the
redfish service. If the RetriesExhaustedError exception is raised, we can
infer that the redfish service isn't present on the system.

Caching functionality of redfish discover result is maintained.

Imports redfish_client method directly instead of importing entire
redfish module.

Modifies unit tests for the new method.

Related: canonical/hardware-observer-operator#61
  • Loading branch information
dashmage authored Oct 6, 2023
1 parent 2bf88e3 commit 3bd88aa
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 66 deletions.
4 changes: 2 additions & 2 deletions prometheus_hardware_exporter/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ def __init__(self, config: Config) -> None:
super().__init__(config)
self.config = config
self.discover_redfish_services = RedfishHelper.get_cached_discover_method(
self.config.redfish_discover_cache_ttl
self.config.redfish_discover_cache_ttl,
)

@property
Expand Down Expand Up @@ -946,7 +946,7 @@ def fetch(self) -> List[Payload]:
"""Load redfish data."""
payloads: List[Payload] = []

service_status = self.discover_redfish_services()
service_status = self.discover_redfish_services(self.config.redfish_host)
payloads.append(Payload(name="redfish_service_available", value=float(service_status)))
if not service_status:
return payloads
Expand Down
42 changes: 30 additions & 12 deletions prometheus_hardware_exporter/collectors/redfish.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
from logging import getLogger
from typing import Any, Callable, Dict, List, Optional, Tuple

import redfish
import redfish_utilities
from cachetools.func import ttl_cache
from redfish.rest.v1 import HttpClient, RestResponse
from redfish import redfish_client
from redfish.rest.v1 import (
HttpClient,
InvalidCredentialsError,
RestResponse,
RetriesExhaustedError,
SessionCreationError,
)
from typing_extensions import Self

from prometheus_hardware_exporter.config import Config
Expand All @@ -26,15 +32,27 @@ def get_cached_discover_method(ttl: int) -> Callable:
"""

@ttl_cache(ttl=ttl)
def _discover() -> bool:
"""Return true if redfish services have been discovered."""
logger.info("Discovering redfish services...")
services = redfish.discover_ssdp()
if len(services) == 0:
logger.info("No redfish services discovered")
return False
logger.debug("Discovered redfish services: %s", services)
return True
def _discover(host: str) -> bool:
"""Return True if redfish service has been discovered."""
try:
redfish_obj = redfish_client(base_url=host, username="", password="")
redfish_obj.login(auth="session")
except RetriesExhaustedError:
# redfish not available
result = False
except (SessionCreationError, InvalidCredentialsError):
# redfish available, wrong credentials or unable to create a session
result = True
else:
# redfish available, login succeeded
result = True
redfish_obj.logout()

if result:
logger.debug("Redfish service available.")
else:
logger.debug("Redfish service not available.")
return result

return _discover

Expand All @@ -49,7 +67,7 @@ def __init__(self, config: Config) -> None:

def __enter__(self) -> Self:
"""Login to redfish while entering context manager."""
self.redfish_obj = redfish.redfish_client(
self.redfish_obj = redfish_client(
base_url=self.host,
username=self.username,
password=self.password,
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def test_105_perccli_physical_device_command_success(self):
]:
assert name in get_payloads

@patch("prometheus_hardware_exporter.collectors.redfish.redfish.redfish_client")
@patch("prometheus_hardware_exporter.collectors.redfish.redfish_client")
@patch(
"prometheus_hardware_exporter.collectors.redfish.RedfishHelper.get_cached_discover_method"
)
Expand All @@ -578,7 +578,7 @@ def _discover(*args, **kwargs):
mock_redfish_client.assert_not_called()

@patch("prometheus_hardware_exporter.collector.logger")
@patch("prometheus_hardware_exporter.collectors.redfish.redfish.redfish_client")
@patch("prometheus_hardware_exporter.collectors.redfish.redfish_client")
@patch(
"prometheus_hardware_exporter.collectors.redfish.RedfishHelper.get_cached_discover_method"
)
Expand Down Expand Up @@ -618,7 +618,7 @@ def _discover(*args, **kwargs):
@patch("prometheus_hardware_exporter.collector.logger")
@patch("prometheus_hardware_exporter.collectors.redfish.RedfishHelper.__exit__")
@patch("prometheus_hardware_exporter.collectors.redfish.RedfishHelper.__enter__")
@patch("prometheus_hardware_exporter.collectors.redfish.redfish.redfish_client")
@patch("prometheus_hardware_exporter.collectors.redfish.redfish_client")
@patch(
"prometheus_hardware_exporter.collectors.redfish.RedfishHelper.get_cached_discover_method"
)
Expand Down
Loading

0 comments on commit 3bd88aa

Please sign in to comment.