1 # -*- coding: utf-8 -*-
2 from __future__
import absolute_import
5 from datetime
import datetime
9 from ..exceptions
import DashboardException
10 from ..security
import Scope
11 from ..settings
import Settings
12 from . import ApiController
, BaseController
, Controller
, ControllerDoc
, Endpoint
, RESTController
15 @Controller('/api/prometheus_receiver', secure
=False)
16 class PrometheusReceiver(BaseController
):
18 The receiver is needed in order to receive alert notifications (reports)
22 @Endpoint('POST', path
='/', version
=None)
23 def fetch_alert(self
, **notification
):
24 notification
['notified'] = datetime
.now().isoformat()
25 notification
['id'] = str(len(self
.notifications
))
26 self
.notifications
.append(notification
)
29 class PrometheusRESTController(RESTController
):
30 def prometheus_proxy(self
, method
, path
, params
=None, payload
=None):
31 # type (str, str, dict, dict)
32 return self
._proxy
(self
._get
_api
_url
(Settings
.PROMETHEUS_API_HOST
),
33 method
, path
, 'Prometheus', params
, payload
,
34 verify
=Settings
.PROMETHEUS_API_SSL_VERIFY
)
36 def alert_proxy(self
, method
, path
, params
=None, payload
=None):
37 # type (str, str, dict, dict)
38 return self
._proxy
(self
._get
_api
_url
(Settings
.ALERTMANAGER_API_HOST
),
39 method
, path
, 'Alertmanager', params
, payload
,
40 verify
=Settings
.ALERTMANAGER_API_SSL_VERIFY
)
42 def _get_api_url(self
, host
):
43 return host
.rstrip('/') + '/api/v1'
45 def _proxy(self
, base_url
, method
, path
, api_name
, params
=None, payload
=None, verify
=True):
46 # type (str, str, str, str, dict, dict, bool)
48 response
= requests
.request(method
, base_url
+ path
, params
=params
,
49 json
=payload
, verify
=verify
)
51 raise DashboardException(
52 "Could not reach {}'s API on {}".format(api_name
, base_url
),
54 component
='prometheus')
55 content
= json
.loads(response
.content
)
56 if content
['status'] == 'success':
58 return content
['data']
60 raise DashboardException(content
, http_status_code
=400, component
='prometheus')
63 @ApiController('/prometheus', Scope
.PROMETHEUS
)
64 @ControllerDoc("Prometheus Management API", "Prometheus")
65 class Prometheus(PrometheusRESTController
):
66 def list(self
, **params
):
67 return self
.alert_proxy('GET', '/alerts', params
)
69 @RESTController.Collection(method
='GET')
70 def rules(self
, **params
):
71 return self
.prometheus_proxy('GET', '/rules', params
)
73 @RESTController.Collection(method
='GET', path
='/silences')
74 def get_silences(self
, **params
):
75 return self
.alert_proxy('GET', '/silences', params
)
77 @RESTController.Collection(method
='POST', path
='/silence', status
=201)
78 def create_silence(self
, **params
):
79 return self
.alert_proxy('POST', '/silences', payload
=params
)
81 @RESTController.Collection(method
='DELETE', path
='/silence/{s_id}', status
=204)
82 def delete_silence(self
, s_id
):
83 return self
.alert_proxy('DELETE', '/silence/' + s_id
) if s_id
else None
86 @ApiController('/prometheus/notifications', Scope
.PROMETHEUS
)
87 @ControllerDoc("Prometheus Notifications Management API", "PrometheusNotifications")
88 class PrometheusNotifications(RESTController
):
90 def list(self
, **params
):
94 return PrometheusReceiver
.notifications
[-1:]
95 return PrometheusReceiver
.notifications
[int(f
) + 1:]
96 return PrometheusReceiver
.notifications