-
Notifications
You must be signed in to change notification settings - Fork 3
/
configure_grafana.py
executable file
·244 lines (199 loc) · 7.81 KB
/
configure_grafana.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python3
# Copyright 2018 6WIND S.A.
import argparse
import json
import os
import sys
import time
import yaml
import requests
#------------------------------------------------------------------------------
def generate_dashboard(name, dashboards, datasource):
"""
Given the dashboard name, the dashboards part of the configuration, and the
default datasource, generate a dashboard. We use the skeleton.json file, and
fill it with given panels and template variables.
Rows have titles, panels, and optional repeat and collapse options.
Panels span can be configured. 12 = one lie.
A dashboard can inherit from another one, and be enabled or disabled, has
rows and optional templates.
"""
data = {}
resource_dir = os.path.join(os.path.dirname(__file__), 'resources')
skeleton_file = os.path.join(resource_dir, 'skeleton.json')
with open(skeleton_file) as skel:
data = json.load(skel)
dashboard = dashboards[name]
templates = dashboard.get('templating', [])
rows = dashboard.get('rows', [])
# If we inherit from a dashboard, initialize rows and templates with this
# dashboard.
if 'inherits' in dashboard:
master = dashboards[dashboard['inherits']]
rows = master.get('rows', []) + rows
templates = master.get('templating', []) + templates
data['title'] = dashboard['title']
if 'refresh' in dashboard:
data['refresh'] = dashboard['refresh']
if 'time-from' in dashboard:
data['time']['from'] = dashboard['time-from']
for template in templates:
template_data = {}
template_dir = os.path.join(resource_dir, 'templates')
template_file_path = os.path.join(template_dir,
template['file'] + '.json')
with open(template_file_path) as template_file:
template_data = json.load(template_file)
template_data['datasource'] = datasource
# Set template default values
if 'values' in template:
for value in template['values']:
template_data['current']['value'].append(value)
data['templating']['list'].append(template_data)
panelid = 1
for row in rows:
row_data = {
'collapse': row.get('collapse', False),
'height': 250,
'panels': [],
'repeat': row.get('repeat', None),
'showTitle': True if 'title' in row else False,
'title': row.get('title', None),
'titleSize': 'h5'
}
# Process panels
for panel in row['panels']:
# panel can contain the file name or a dict
if isinstance(panel, dict):
panel_name = list(panel.keys())[0]
else:
panel_name = panel
panel_data = {}
panel_dir = os.path.join(resource_dir, 'panels')
panel_file_path = os.path.join(panel_dir, panel_name + '.json')
with open(panel_file_path) as panel_file:
panel_data = json.load(panel_file)
# Renumber each panel to avoid having the same one
panel_data['id'] = panelid
panelid += 1
# Set datasource with default
panel_data['datasource'] = datasource
# Set span if found in panel
if isinstance(panel, dict):
if 'span' in panel[panel_name]:
panel_data['span'] = panel[panel_name]['span']
row_data['panels'].append(panel_data)
data['rows'].append(row_data)
return data
#------------------------------------------------------------------------------
def get_config(conf_file_path):
"""
Parse the yaml configuration file into a dict.
"""
with open(conf_file_path, 'r') as conf_file:
config = yaml.safe_load(conf_file)
return config
#------------------------------------------------------------------------------
def get_grafana_url(config):
"""
Given the configuration, return grafana url.
"""
return os.path.join('http://', '%s:%u' % (config['grafana']['host'],
config['grafana']['port']))
#------------------------------------------------------------------------------
def get_grafana_session(config):
"""
Connect to grafana and get a session. We try for 60 seconds.
"""
session = requests.Session()
tries = 30
url = get_grafana_url(config)
user = config['grafana']['user']
password = config['grafana']['password']
# Wait for grafana to be ready for 60 seconds
while tries:
try:
session.post(
os.path.join(url, 'login'),
data=json.dumps({
'user': user,
'email': '',
'password': password
}),
headers={
'content-type': 'application/json'
})
break
except requests.exceptions.ConnectionError as connection_error:
tries -= 1
time.sleep(2)
if tries == 0:
print(connection_error)
exit(1)
return session
#------------------------------------------------------------------------------
def upload_datasources(session, config):
"""
Upload datasources found in configuration to grafana.
"""
default_datasource = 'influxdb'
if 'datasources' in config and config['datasources']:
for datasource in config['datasources'].values():
if not datasource['enabled']:
continue
del datasource['enabled']
if datasource['isDefault']:
default_datasource = datasource['name']
datasources_post = session.post(
os.path.join(get_grafana_url(config), 'api', 'datasources'),
data=json.dumps(datasource),
headers={'content-type': 'application/json'}
)
print(datasources_post.text)
return default_datasource
#------------------------------------------------------------------------------
def upload_dashboards(session, config, default_datasource):
"""
Upload dashboards found in configuration to grafana.
"""
for name, dashboard in config['dashboards'].items():
if not dashboard['enabled']:
continue
data = generate_dashboard(name, config['dashboards'],
default_datasource)
dashboard_post = session.post(
os.path.join(get_grafana_url(config), 'api', 'dashboards', 'db'),
data=json.dumps({'dashboard': data}),
headers={'content-type': 'application/json'},
)
print(dashboard_post.text)
#------------------------------------------------------------------------------
def set_dashboard_theme(session, config):
"""
Set the theme (light/dark).
"""
theme = config['grafana'].get('theme')
if not theme:
return
config_patch = session.patch(
os.path.join(get_grafana_url(config), 'api', 'user', 'preferences'),
data=json.dumps({'theme': theme}),
headers={'content-type': 'application/json'},
)
#------------------------------------------------------------------------------
def main():
"""
Main.
"""
parser = argparse.ArgumentParser(description='Configure grafana')
parser.add_argument('configuration',
metavar='FILE',
help='Specify the configuration to load from confs directory')
args = parser.parse_args()
config = get_config(args.configuration)
session = get_grafana_session(config)
default_datasource = upload_datasources(session, config)
upload_dashboards(session, config, default_datasource)
set_dashboard_theme(session, config)
if __name__ == '__main__':
main()