]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/grafana.py
1 # -*- coding: utf-8 -*-
2 from __future__
import absolute_import
10 from .exceptions
import GrafanaError
11 from .settings
import Settings
14 logger
= logging
.getLogger('grafana')
17 class GrafanaRestClient(object):
20 def url_validation(method
, path
):
21 response
= requests
.request(
24 verify
=Settings
.GRAFANA_API_SSL_VERIFY
)
25 return response
.status_code
28 def push_dashboard(dashboard_obj
):
29 if not Settings
.GRAFANA_API_URL
:
30 raise GrafanaError("The Grafana API URL is not set")
31 if not Settings
.GRAFANA_API_URL
.startswith('http'):
32 raise GrafanaError("The Grafana API URL is invalid")
33 if not Settings
.GRAFANA_API_USERNAME
:
34 raise GrafanaError("The Grafana API username is not set")
35 if not Settings
.GRAFANA_API_PASSWORD
:
36 raise GrafanaError("The Grafana API password is not set")
37 url
= Settings
.GRAFANA_API_URL
.rstrip('/') + \
40 'Accept': 'application/json',
41 'Content-Type': 'application/json',
44 'dashboard': dashboard_obj
,
48 response
= requests
.post(
51 data
=json
.dumps(payload
),
52 auth
=(Settings
.GRAFANA_API_USERNAME
,
53 Settings
.GRAFANA_API_PASSWORD
),
54 verify
=Settings
.GRAFANA_API_SSL_VERIFY
56 except requests
.ConnectionError
:
57 raise GrafanaError("Could not connect to Grafana server")
58 response
.raise_for_status()
59 return response
.status_code
, response
.json()
62 class Retrier(object):
63 def __init__(self
, tries
, sleep
, func
, *args
, **kwargs
):
65 Wraps a function. An instance of this class may be called to call that
66 function, retrying if it raises an exception. Sleeps between retries,
67 eventually reraising the original exception when retries are exhausted.
68 Once the function returns a value, that value is returned.
70 :param tries: How many times to try, before reraising the exception
72 :param sleep: How many seconds to wait between tries
73 :type sleep: int|float
74 :param func: The function to execute
76 :param args: Any arguments to pass to the function
78 :param kwargs: Any keyword arguments to pass to the function
82 self
.tries
= int(tries
)
91 while self
.tried
< self
.tries
:
93 result
= self
.func(*self
.args
, **self
.kwargs
)
94 except Exception: # pylint: disable=broad-except
95 if self
.tried
== self
.tries
- 1:
99 time
.sleep(self
.sleep
)
104 def load_local_dashboards():
105 if os
.environ
.get('CEPH_DEV') == '1' or 'UNITTEST' in os
.environ
:
106 path
= os
.path
.abspath(os
.path
.join(
107 os
.path
.dirname(__file__
),
108 '../../../../monitoring/grafana/dashboards/'
111 path
= '/etc/grafana/dashboards/ceph-dashboard'
113 for item
in [p
for p
in os
.listdir(path
) if p
.endswith('.json')]:
114 db_path
= os
.path
.join(path
, item
)
115 with
open(db_path
) as f
:
116 dashboards
[item
] = json
.loads(f
.read())
120 def push_local_dashboards(tries
=1, sleep
=0):
122 dashboards
= load_local_dashboards()
123 except (EnvironmentError, ValueError):
124 logger
.exception("Failed to load local dashboard files")
129 grafana
= GrafanaRestClient()
130 for body
in dashboards
.values():
131 grafana
.push_dashboard(body
)
133 logger
.exception("Failed to push dashboards to Grafana")
135 retry
= Retrier(tries
, sleep
, push
)