diff --git a/prometheus_hardware_exporter/collectors/redfish.py b/prometheus_hardware_exporter/collectors/redfish.py index d9213f2..7b6d400 100644 --- a/prometheus_hardware_exporter/collectors/redfish.py +++ b/prometheus_hardware_exporter/collectors/redfish.py @@ -7,6 +7,7 @@ from cachetools.func import ttl_cache from redfish import redfish_client from redfish.rest.v1 import ( + BadRequestError, InvalidCredentialsError, RetriesExhaustedError, SessionCreationError, @@ -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. diff --git a/tests/unit/test_redfish.py b/tests/unit/test_redfish.py index 553f273..f69fcad 100644 --- a/tests/unit/test_redfish.py +++ b/tests/unit/test_redfish.py @@ -5,6 +5,7 @@ import redfish_utilities from parameterized import parameterized from redfish.rest.v1 import ( + BadRequestError, InvalidCredentialsError, RetriesExhaustedError, SessionCreationError, @@ -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())