Skip to content

Commit

Permalink
Igonore if redfish fails to logout with 401 as as response
Browse files Browse the repository at this point in the history
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: canonical#76
  • Loading branch information
gabrielcocenza committed Jul 17, 2024
1 parent 0ab3094 commit dd61061
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
13 changes: 11 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,16 @@ 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.error("Failed to logout redfish: %s", str(err))
if "code: 401" not in str(err):
raise

def get_sensor_data(self) -> Dict[str, List]:
"""Get sensor data.
Expand Down
26 changes: 26 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,28 @@ def test_discover_cache(self, mock_redfish_client):
username="",
password="",
)

@patch("prometheus_hardware_exporter.collectors.redfish.redfish_client")
def test_redfish_logout_401(self, mock_redfish_client):
"""Test that exception is not raised if logout fails with 401."""
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())

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

0 comments on commit dd61061

Please sign in to comment.