]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/controllers/prometheus.py
219adfa86f70bb8e2196d4e68062f5314a165fb7
[ceph.git] / ceph / src / pybind / mgr / dashboard / controllers / prometheus.py
1 # -*- coding: utf-8 -*-
2 from __future__ import absolute_import
3
4 from datetime import datetime
5 import json
6 import requests
7
8 from . import Controller, ApiController, BaseController, RESTController, Endpoint
9 from ..security import Scope
10 from ..settings import Settings
11 from ..exceptions import DashboardException
12
13
14 @Controller('/api/prometheus_receiver', secure=False)
15 class PrometheusReceiver(BaseController):
16 """
17 The receiver is needed in order to receive alert notifications (reports)
18 """
19 notifications = []
20
21 @Endpoint('POST', path='/')
22 def fetch_alert(self, **notification):
23 notification['notified'] = datetime.now().isoformat()
24 notification['id'] = str(len(self.notifications))
25 self.notifications.append(notification)
26
27
28 class PrometheusRESTController(RESTController):
29 def prometheus_proxy(self, method, path, params=None, payload=None):
30 # type (str, str, dict, dict)
31 return self._proxy(self._get_api_url(Settings.PROMETHEUS_API_HOST),
32 method, path, 'Prometheus', params, payload)
33
34 def alert_proxy(self, method, path, params=None, payload=None):
35 # type (str, str, dict, dict)
36 return self._proxy(self._get_api_url(Settings.ALERTMANAGER_API_HOST),
37 method, path, 'Alertmanager', params, payload)
38
39 def _get_api_url(self, host):
40 return host.rstrip('/') + '/api/v1'
41
42 def _proxy(self, base_url, method, path, api_name, params=None, payload=None):
43 # type (str, str, str, str, dict, dict)
44 try:
45 response = requests.request(method, base_url + path, params=params, json=payload)
46 except Exception:
47 raise DashboardException(
48 "Could not reach {}'s API on {}".format(api_name, base_url),
49 http_status_code=404,
50 component='prometheus')
51 content = json.loads(response.content)
52 if content['status'] == 'success':
53 if 'data' in content:
54 return content['data']
55 return content
56 raise DashboardException(content, http_status_code=400, component='prometheus')
57
58
59 @ApiController('/prometheus', Scope.PROMETHEUS)
60 class Prometheus(PrometheusRESTController):
61 def list(self, **params):
62 return self.alert_proxy('GET', '/alerts', params)
63
64 @RESTController.Collection(method='GET')
65 def rules(self, **params):
66 return self.prometheus_proxy('GET', '/rules', params)
67
68 @RESTController.Collection(method='GET', path='/silences')
69 def get_silences(self, **params):
70 return self.alert_proxy('GET', '/silences', params)
71
72 @RESTController.Collection(method='POST', path='/silence', status=201)
73 def create_silence(self, **params):
74 return self.alert_proxy('POST', '/silences', payload=params)
75
76 @RESTController.Collection(method='DELETE', path='/silence/{s_id}', status=204)
77 def delete_silence(self, s_id):
78 return self.alert_proxy('DELETE', '/silence/' + s_id) if s_id else None
79
80
81 @ApiController('/prometheus/notifications', Scope.PROMETHEUS)
82 class PrometheusNotifications(RESTController):
83
84 def list(self, **params):
85 if 'from' in params:
86 f = params['from']
87 if f == 'last':
88 return PrometheusReceiver.notifications[-1:]
89 return PrometheusReceiver.notifications[int(f) + 1:]
90 return PrometheusReceiver.notifications