]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/controllers/prometheus.py
219adfa86f70bb8e2196d4e68062f5314a165fb7
1 # -*- coding: utf-8 -*-
2 from __future__
import absolute_import
4 from datetime
import datetime
8 from . import Controller
, ApiController
, BaseController
, RESTController
, Endpoint
9 from ..security
import Scope
10 from ..settings
import Settings
11 from ..exceptions
import DashboardException
14 @Controller('/api/prometheus_receiver', secure
=False)
15 class PrometheusReceiver(BaseController
):
17 The receiver is needed in order to receive alert notifications (reports)
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
)
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
)
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
)
39 def _get_api_url(self
, host
):
40 return host
.rstrip('/') + '/api/v1'
42 def _proxy(self
, base_url
, method
, path
, api_name
, params
=None, payload
=None):
43 # type (str, str, str, str, dict, dict)
45 response
= requests
.request(method
, base_url
+ path
, params
=params
, json
=payload
)
47 raise DashboardException(
48 "Could not reach {}'s API on {}".format(api_name
, base_url
),
50 component
='prometheus')
51 content
= json
.loads(response
.content
)
52 if content
['status'] == 'success':
54 return content
['data']
56 raise DashboardException(content
, http_status_code
=400, component
='prometheus')
59 @ApiController('/prometheus', Scope
.PROMETHEUS
)
60 class Prometheus(PrometheusRESTController
):
61 def list(self
, **params
):
62 return self
.alert_proxy('GET', '/alerts', params
)
64 @RESTController.Collection(method
='GET')
65 def rules(self
, **params
):
66 return self
.prometheus_proxy('GET', '/rules', params
)
68 @RESTController.Collection(method
='GET', path
='/silences')
69 def get_silences(self
, **params
):
70 return self
.alert_proxy('GET', '/silences', params
)
72 @RESTController.Collection(method
='POST', path
='/silence', status
=201)
73 def create_silence(self
, **params
):
74 return self
.alert_proxy('POST', '/silences', payload
=params
)
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
81 @ApiController('/prometheus/notifications', Scope
.PROMETHEUS
)
82 class PrometheusNotifications(RESTController
):
84 def list(self
, **params
):
88 return PrometheusReceiver
.notifications
[-1:]
89 return PrometheusReceiver
.notifications
[int(f
) + 1:]
90 return PrometheusReceiver
.notifications