]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: start using alertmanager v2 53679/head
authorNizamudeen A <nia@redhat.com>
Tue, 26 Sep 2023 16:08:51 +0000 (21:38 +0530)
committerNizamudeen A <nia@redhat.com>
Tue, 26 Mar 2024 17:38:40 +0000 (23:08 +0530)
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 <nia@redhat.com>
src/pybind/mgr/dashboard/controllers/prometheus.py
src/pybind/mgr/dashboard/openapi.yaml
src/pybind/mgr/dashboard/tests/test_prometheus.py

index 7f5f193f9ab41b67861ddbae853175d795e17578..ffc6407b4a9f1963cfbbe978668f28d1d9a5eb04 100644 (file)
@@ -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')
index dbf6a3e299e54d742eed15e2e35e41f39c445b31..c660ad38083b5946c0beda16ed70d006503359e9 100644 (file)
@@ -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: []
index 10aa8669ec0ee84dd7bb0eae0b61a9d3828d51c9..7ff3e5dc166599b05367b082706f091748387bc0 100644 (file)
@@ -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'