From 6f2d0b14e52885d97fdb48c2e5d7d71201494a8f Mon Sep 17 00:00:00 2001 From: Nizamudeen A Date: Tue, 26 Sep 2023 21:38:51 +0530 Subject: [PATCH] mgr/dashboard: start using alertmanager v2 I was looking into sorting the alerts and saw there is an api v2 for alertmanager which also has an endpoint like `alerts/groups` which might be something that is useful for us. Refer: https://github.com/prometheus/alertmanager/blob/main/api/v2/openapi.yaml Fixes: https://tracker.ceph.com/issues/65070 Signed-off-by: Nizamudeen A --- .../mgr/dashboard/controllers/prometheus.py | 20 ++++++++++++----- src/pybind/mgr/dashboard/openapi.yaml | 22 +++++++++++++++++++ .../mgr/dashboard/tests/test_prometheus.py | 2 +- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/pybind/mgr/dashboard/controllers/prometheus.py b/src/pybind/mgr/dashboard/controllers/prometheus.py index 7f5f193f9ab41..ffc6407b4a9f1 100644 --- a/src/pybind/mgr/dashboard/controllers/prometheus.py +++ b/src/pybind/mgr/dashboard/controllers/prometheus.py @@ -46,9 +46,9 @@ class PrometheusRESTController(RESTController): # type (str, str, dict, dict) user, password, cert_file = self.get_access_info('alertmanager') verify = cert_file.name if cert_file else Settings.ALERTMANAGER_API_SSL_VERIFY - response = self._proxy(self._get_api_url(Settings.ALERTMANAGER_API_HOST), + response = self._proxy(self._get_api_url(Settings.ALERTMANAGER_API_HOST, version='v2'), method, path, 'Alertmanager', params, payload, - user=user, password=password, verify=verify) + user=user, password=password, verify=verify, is_alertmanager=True) if cert_file: cert_file.close() os.unlink(cert_file.name) @@ -81,15 +81,16 @@ class PrometheusRESTController(RESTController): return user, password, cert_file - def _get_api_url(self, host): - return host.rstrip('/') + '/api/v1' + def _get_api_url(self, host, version='v1'): + return f'{host.rstrip("/")}/api/{version}' def balancer_status(self): return ceph_service.CephService.send_command('mon', 'balancer status') def _proxy(self, base_url, method, path, api_name, params=None, payload=None, verify=True, - user=None, password=None): + user=None, password=None, is_alertmanager=False): # type (str, str, str, str, dict, dict, bool) + content = None try: from requests.auth import HTTPBasicAuth auth = HTTPBasicAuth(user, password) if user and password else None @@ -102,11 +103,14 @@ class PrometheusRESTController(RESTController): http_status_code=404, component='prometheus') try: - content = json.loads(response.content, strict=False) + if response.content: + content = json.loads(response.content, strict=False) except json.JSONDecodeError as e: raise DashboardException( "Error parsing Prometheus Alertmanager response: {}".format(e.msg), component='prometheus') + if is_alertmanager: + return content balancer_status = self.balancer_status() if content['status'] == 'success': # pylint: disable=R1702 alerts_info = [] @@ -146,6 +150,10 @@ class Prometheus(PrometheusRESTController): def delete_silence(self, s_id): return self.alert_proxy('DELETE', '/silence/' + s_id) if s_id else None + @RESTController.Collection(method='GET', path='/alertgroup') + def get_alertgroup(self, **params): + return self.alert_proxy('GET', '/alerts/groups', params) + @RESTController.Collection(method='GET', path='/prometheus_query_data') def get_prometeus_query_data(self, **params): params['query'] = params.pop('params') diff --git a/src/pybind/mgr/dashboard/openapi.yaml b/src/pybind/mgr/dashboard/openapi.yaml index dbf6a3e299e54..c660ad38083b5 100644 --- a/src/pybind/mgr/dashboard/openapi.yaml +++ b/src/pybind/mgr/dashboard/openapi.yaml @@ -10295,6 +10295,28 @@ paths: - jwt: [] tags: - Prometheus + /api/prometheus/alertgroup: + get: + parameters: [] + responses: + '200': + content: + application/vnd.ceph.api.v1.0+json: + type: object + description: OK + '400': + description: Operation exception. Please check the response body for details. + '401': + description: Unauthenticated access. Please login first. + '403': + description: Unauthorized access. Please check your permissions. + '500': + description: Unexpected error. Please check the response body for the stack + trace. + security: + - jwt: [] + tags: + - Prometheus /api/prometheus/data: get: parameters: [] diff --git a/src/pybind/mgr/dashboard/tests/test_prometheus.py b/src/pybind/mgr/dashboard/tests/test_prometheus.py index 10aa8669ec0ee..7ff3e5dc16659 100644 --- a/src/pybind/mgr/dashboard/tests/test_prometheus.py +++ b/src/pybind/mgr/dashboard/tests/test_prometheus.py @@ -12,7 +12,7 @@ from ..tests import ControllerTestCase class PrometheusControllerTest(ControllerTestCase): alert_host = 'http://alertmanager:9093/mock' - alert_host_api = alert_host + '/api/v1' + alert_host_api = alert_host + '/api/v2' prometheus_host = 'http://prometheus:9090/mock' prometheus_host_api = prometheus_host + '/api/v1' -- 2.39.5