]>
Commit | Line | Data |
---|---|---|
11fdf7f2 | 1 | # -*- coding: utf-8 -*- |
11fdf7f2 | 2 | |
11fdf7f2 | 3 | import json |
f67539c2 TL |
4 | from datetime import datetime |
5 | ||
11fdf7f2 TL |
6 | import requests |
7 | ||
f67539c2 | 8 | from ..exceptions import DashboardException |
11fdf7f2 TL |
9 | from ..security import Scope |
10 | from ..settings import Settings | |
a4b75251 | 11 | from . import APIDoc, APIRouter, BaseController, Endpoint, RESTController, Router |
11fdf7f2 TL |
12 | |
13 | ||
a4b75251 | 14 | @Router('/api/prometheus_receiver', secure=False) |
11fdf7f2 | 15 | class PrometheusReceiver(BaseController): |
9f95a23c TL |
16 | """ |
17 | The receiver is needed in order to receive alert notifications (reports) | |
18 | """ | |
11fdf7f2 TL |
19 | notifications = [] |
20 | ||
b3b6e05e | 21 | @Endpoint('POST', path='/', version=None) |
11fdf7f2 TL |
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 | ||
494da23a TL |
28 | class PrometheusRESTController(RESTController): |
29 | def prometheus_proxy(self, method, path, params=None, payload=None): | |
9f95a23c | 30 | # type (str, str, dict, dict) |
494da23a | 31 | return self._proxy(self._get_api_url(Settings.PROMETHEUS_API_HOST), |
cd265ab1 TL |
32 | method, path, 'Prometheus', params, payload, |
33 | verify=Settings.PROMETHEUS_API_SSL_VERIFY) | |
494da23a TL |
34 | |
35 | def alert_proxy(self, method, path, params=None, payload=None): | |
9f95a23c | 36 | # type (str, str, dict, dict) |
494da23a | 37 | return self._proxy(self._get_api_url(Settings.ALERTMANAGER_API_HOST), |
cd265ab1 TL |
38 | method, path, 'Alertmanager', params, payload, |
39 | verify=Settings.ALERTMANAGER_API_SSL_VERIFY) | |
11fdf7f2 | 40 | |
494da23a TL |
41 | def _get_api_url(self, host): |
42 | return host.rstrip('/') + '/api/v1' | |
11fdf7f2 | 43 | |
cd265ab1 TL |
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) | |
494da23a | 46 | try: |
cd265ab1 TL |
47 | response = requests.request(method, base_url + path, params=params, |
48 | json=payload, verify=verify) | |
494da23a | 49 | except Exception: |
9f95a23c TL |
50 | raise DashboardException( |
51 | "Could not reach {}'s API on {}".format(api_name, base_url), | |
52 | http_status_code=404, | |
53 | component='prometheus') | |
2a845540 TL |
54 | try: |
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') | |
494da23a TL |
60 | if content['status'] == 'success': |
61 | if 'data' in content: | |
62 | return content['data'] | |
63 | return content | |
64 | raise DashboardException(content, http_status_code=400, component='prometheus') | |
11fdf7f2 | 65 | |
494da23a | 66 | |
a4b75251 TL |
67 | @APIRouter('/prometheus', Scope.PROMETHEUS) |
68 | @APIDoc("Prometheus Management API", "Prometheus") | |
494da23a | 69 | class Prometheus(PrometheusRESTController): |
11fdf7f2 | 70 | def list(self, **params): |
494da23a TL |
71 | return self.alert_proxy('GET', '/alerts', params) |
72 | ||
73 | @RESTController.Collection(method='GET') | |
74 | def rules(self, **params): | |
9f95a23c | 75 | return self.prometheus_proxy('GET', '/rules', params) |
494da23a TL |
76 | |
77 | @RESTController.Collection(method='GET', path='/silences') | |
78 | def get_silences(self, **params): | |
79 | return self.alert_proxy('GET', '/silences', params) | |
80 | ||
81 | @RESTController.Collection(method='POST', path='/silence', status=201) | |
82 | def create_silence(self, **params): | |
83 | return self.alert_proxy('POST', '/silences', payload=params) | |
84 | ||
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 | |
11fdf7f2 TL |
88 | |
89 | ||
a4b75251 TL |
90 | @APIRouter('/prometheus/notifications', Scope.PROMETHEUS) |
91 | @APIDoc("Prometheus Notifications Management API", "PrometheusNotifications") | |
11fdf7f2 TL |
92 | class PrometheusNotifications(RESTController): |
93 | ||
94 | def list(self, **params): | |
95 | if 'from' in params: | |
96 | f = params['from'] | |
97 | if f == 'last': | |
98 | return PrometheusReceiver.notifications[-1:] | |
99 | return PrometheusReceiver.notifications[int(f) + 1:] | |
100 | return PrometheusReceiver.notifications |