Skip to content

Commit 107e162

Browse files
authored
Merge pull request ceph#53679 from rhcs-dashboard/alerts-v2-api
mgr/dashboard: start using alertmanager v2 Reviewed-by: Aashish Sharma <[email protected]>
2 parents 1abd6ea + 6f2d0b1 commit 107e162

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

src/pybind/mgr/dashboard/controllers/prometheus.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ def alert_proxy(self, method, path, params=None, payload=None):
4646
# type (str, str, dict, dict)
4747
user, password, cert_file = self.get_access_info('alertmanager')
4848
verify = cert_file.name if cert_file else Settings.ALERTMANAGER_API_SSL_VERIFY
49-
response = self._proxy(self._get_api_url(Settings.ALERTMANAGER_API_HOST),
49+
response = self._proxy(self._get_api_url(Settings.ALERTMANAGER_API_HOST, version='v2'),
5050
method, path, 'Alertmanager', params, payload,
51-
user=user, password=password, verify=verify)
51+
user=user, password=password, verify=verify, is_alertmanager=True)
5252
if cert_file:
5353
cert_file.close()
5454
os.unlink(cert_file.name)
@@ -81,15 +81,16 @@ def get_access_info(self, module_name):
8181

8282
return user, password, cert_file
8383

84-
def _get_api_url(self, host):
85-
return host.rstrip('/') + '/api/v1'
84+
def _get_api_url(self, host, version='v1'):
85+
return f'{host.rstrip("/")}/api/{version}'
8686

8787
def balancer_status(self):
8888
return ceph_service.CephService.send_command('mon', 'balancer status')
8989

9090
def _proxy(self, base_url, method, path, api_name, params=None, payload=None, verify=True,
91-
user=None, password=None):
91+
user=None, password=None, is_alertmanager=False):
9292
# type (str, str, str, str, dict, dict, bool)
93+
content = None
9394
try:
9495
from requests.auth import HTTPBasicAuth
9596
auth = HTTPBasicAuth(user, password) if user and password else None
@@ -102,11 +103,14 @@ def _proxy(self, base_url, method, path, api_name, params=None, payload=None, ve
102103
http_status_code=404,
103104
component='prometheus')
104105
try:
105-
content = json.loads(response.content, strict=False)
106+
if response.content:
107+
content = json.loads(response.content, strict=False)
106108
except json.JSONDecodeError as e:
107109
raise DashboardException(
108110
"Error parsing Prometheus Alertmanager response: {}".format(e.msg),
109111
component='prometheus')
112+
if is_alertmanager:
113+
return content
110114
balancer_status = self.balancer_status()
111115
if content['status'] == 'success': # pylint: disable=R1702
112116
alerts_info = []
@@ -146,6 +150,10 @@ def create_silence(self, **params):
146150
def delete_silence(self, s_id):
147151
return self.alert_proxy('DELETE', '/silence/' + s_id) if s_id else None
148152

153+
@RESTController.Collection(method='GET', path='/alertgroup')
154+
def get_alertgroup(self, **params):
155+
return self.alert_proxy('GET', '/alerts/groups', params)
156+
149157
@RESTController.Collection(method='GET', path='/prometheus_query_data')
150158
def get_prometeus_query_data(self, **params):
151159
params['query'] = params.pop('params')

src/pybind/mgr/dashboard/openapi.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10286,6 +10286,28 @@ paths:
1028610286
- jwt: []
1028710287
tags:
1028810288
- Prometheus
10289+
/api/prometheus/alertgroup:
10290+
get:
10291+
parameters: []
10292+
responses:
10293+
'200':
10294+
content:
10295+
application/vnd.ceph.api.v1.0+json:
10296+
type: object
10297+
description: OK
10298+
'400':
10299+
description: Operation exception. Please check the response body for details.
10300+
'401':
10301+
description: Unauthenticated access. Please login first.
10302+
'403':
10303+
description: Unauthorized access. Please check your permissions.
10304+
'500':
10305+
description: Unexpected error. Please check the response body for the stack
10306+
trace.
10307+
security:
10308+
- jwt: []
10309+
tags:
10310+
- Prometheus
1028910311
/api/prometheus/data:
1029010312
get:
1029110313
parameters: []

src/pybind/mgr/dashboard/tests/test_prometheus.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class PrometheusControllerTest(ControllerTestCase):
1414
alert_host = 'http://alertmanager:9093/mock'
15-
alert_host_api = alert_host + '/api/v1'
15+
alert_host_api = alert_host + '/api/v2'
1616

1717
prometheus_host = 'http://prometheus:9090/mock'
1818
prometheus_host_api = prometheus_host + '/api/v1'

0 commit comments

Comments
 (0)