1 # -*- coding: utf-8 -*-
4 from datetime
import datetime
8 from ..exceptions
import DashboardException
9 from ..security
import Scope
10 from ..settings
import Settings
11 from . import APIDoc
, APIRouter
, BaseController
, Endpoint
, RESTController
, Router
14 @Router('/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
='/', version
=None)
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
,
33 verify
=Settings
.PROMETHEUS_API_SSL_VERIFY
)
35 def alert_proxy(self
, method
, path
, params
=None, payload
=None):
36 # type (str, str, dict, dict)
37 return self
._proxy
(self
._get
_api
_url
(Settings
.ALERTMANAGER_API_HOST
),
38 method
, path
, 'Alertmanager', params
, payload
,
39 verify
=Settings
.ALERTMANAGER_API_SSL_VERIFY
)
41 def _get_api_url(self
, host
):
42 return host
.rstrip('/') + '/api/v1'
44 def _proxy(self
, base_url
, method
, path
, api_name
, params
=None, payload
=None, verify
=True):
45 # type (str, str, str, str, dict, dict, bool)
47 response
= requests
.request(method
, base_url
+ path
, params
=params
,
48 json
=payload
, verify
=verify
)
50 raise DashboardException(
51 "Could not reach {}'s API on {}".format(api_name
, base_url
),
53 component
='prometheus')
55 content
= json
.loads(response
.content
, strict
=False)
56 except json
.JSONDecodeError
as e
:
57 raise DashboardException(
58 "Error parsing Prometheus Alertmanager response: {}".format(e
.msg
),
59 component
='prometheus')
60 if content
['status'] == 'success':
62 return content
['data']
64 raise DashboardException(content
, http_status_code
=400, component
='prometheus')
67 @APIRouter('/prometheus', Scope
.PROMETHEUS
)
68 @APIDoc("Prometheus Management API", "Prometheus")
69 class Prometheus(PrometheusRESTController
):
70 def list(self
, **params
):
71 return self
.alert_proxy('GET', '/alerts', params
)
73 @RESTController.Collection(method
='GET')
74 def rules(self
, **params
):
75 return self
.prometheus_proxy('GET', '/rules', params
)
77 @RESTController.Collection(method
='GET', path
='/silences')
78 def get_silences(self
, **params
):
79 return self
.alert_proxy('GET', '/silences', params
)
81 @RESTController.Collection(method
='POST', path
='/silence', status
=201)
82 def create_silence(self
, **params
):
83 return self
.alert_proxy('POST', '/silences', payload
=params
)
85 @RESTController.Collection(method
='DELETE', path
='/silence/{s_id}', status
=204)
86 def delete_silence(self
, s_id
):
87 return self
.alert_proxy('DELETE', '/silence/' + s_id
) if s_id
else None
90 @APIRouter('/prometheus/notifications', Scope
.PROMETHEUS
)
91 @APIDoc("Prometheus Notifications Management API", "PrometheusNotifications")
92 class PrometheusNotifications(RESTController
):
94 def list(self
, **params
):
98 return PrometheusReceiver
.notifications
[-1:]
99 return PrometheusReceiver
.notifications
[int(f
) + 1:]
100 return PrometheusReceiver
.notifications