Skip to content

Commit

Permalink
Ignore if redfish fails to logout (#78)
Browse files Browse the repository at this point in the history
* Igonore if redfish fails to logout with 401 as as response

Redfish sometimes fails to logout after scraping the data and this
end up generating the metric redfish_collector_failed that trigger
prometheus alerts that can be noise.

This can be a BMC problem that might not be properly closing
sessions.

For more info see:
DMTF/python-redfish-library#160

Closes: #76
  • Loading branch information
gabrielcocenza authored Aug 1, 2024
1 parent 0ab3094 commit 5ddd4d2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
11 changes: 9 additions & 2 deletions prometheus_hardware_exporter/collectors/redfish.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from cachetools.func import ttl_cache
from redfish import redfish_client
from redfish.rest.v1 import (
BadRequestError,
InvalidCredentialsError,
RetriesExhaustedError,
SessionCreationError,
Expand Down Expand Up @@ -92,8 +93,14 @@ def login(self) -> None:

def logout(self) -> None:
"""Logout from redfish."""
self.redfish_obj.logout()
logger.debug("(service) Logged out from redfish service ...")
# redfish sometimes fails to logout and this end up giving false alerts
# See https://github.com/DMTF/python-redfish-library/issues/160 and
# https://github.com/canonical/prometheus-hardware-exporter/issues/76
try:
self.redfish_obj.logout()
logger.debug("(service) Logged out from redfish service ...")
except BadRequestError as err:
logger.warning("Failed to logout redfish: %s", str(err))

def get_sensor_data(self) -> Dict[str, List]:
"""Get sensor data.
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_redfish.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import redfish_utilities
from parameterized import parameterized
from redfish.rest.v1 import (
BadRequestError,
InvalidCredentialsError,
RetriesExhaustedError,
SessionCreationError,
Expand Down Expand Up @@ -1095,3 +1096,15 @@ def test_discover_cache(self, mock_redfish_client):
username="",
password="",
)

@patch("prometheus_hardware_exporter.collectors.redfish.redfish_client")
def test_redfish_logout_bad_request(self, mock_redfish_client):
"""Test that exception is not raised if redfish logout fails."""
mock_redfish_obj = Mock()
mock_redfish_obj.logout.side_effect = BadRequestError(
"Invalid session resource: /redfish/v1/SessionService/Sessions/132562, "
"return code: 401"
)
mock_redfish_client.return_value = mock_redfish_obj
with RedfishHelper(Mock()) as redfish_helper:
self.assertIsNone(redfish_helper.logout())

0 comments on commit 5ddd4d2

Please sign in to comment.