This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgrafana_dash_gen.py
443 lines (360 loc) · 17.2 KB
/
grafana_dash_gen.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import time
import os
import urllib.request
import urllib.error
import json
import stringcase
import base64
class GrafanaDashGen():
grafana_url = "localhost:"+os.environ['GF_SERVER_HTTP_PORT']
api_key_file = os.environ['GF_PATHS_DATA'] + "/grafana_api_key"
history_file = os.environ['GF_PATHS_DATA'] + "/field_history.json"
reports_directory = "/usr/share/grafana/conf/provisioning/dashboards"
generated_tag = "generated"
dashboard = None
current_id = None
row = None
history = None
apikey = None
def __init__(self):
while self.check_api() == False:
print('Waiting for Grafana API')
time.sleep(5)
self.get_api_key()
def check_api(self):
# Check if Grafana is alive and healthy
# This is done by checking for an 'ok' string in the health endpoint response
# Returns boolean true/false
req = urllib.request.Request('http://' + self.grafana_url + '/api/health')
try:
res = urllib.request.urlopen(req, timeout=5).read()
data = json.loads(res.decode())
if data['database'] == 'ok':
return True
except (urllib.error.HTTPError, urllib.error.URLError):
pass
return False
def get_api_key(self):
# If there's an API key in the env var, we can use that
if "GF_API_KEY" in os.environ:
self.apikey = os.environ['GF_API_KEY']
elif os.path.isfile(self.api_key_file):
with open(self.api_key_file, 'r') as file:
self.apikey = file.read()
else:
# If there's no key, try to create one using the default credentials
# there may be an existing key, but we don't have the keyfile for it
# so let's check and delete any existing key first
req = urllib.request.Request('http://' + self.grafana_url + '/api/auth/keys')
base64string = base64.b64encode(bytes('%s:%s' % ('admin', 'admin'),'ascii'))
req.add_header("Authorization", "Basic %s" % base64string.decode('utf-8'))
try:
res = urllib.request.urlopen(req, timeout=5).read()
json_result = json.loads(res.decode())
# for each key returned by the API
for result in json_result:
if result['name'] == "dashboardapikey":
key = result['id']
# delete the key if it's a dashboard block created one.
req = urllib.request.Request('http://' + self.grafana_url + '/api/auth/keys/' + str(key), method='DELETE')
req.add_header("Authorization", "Basic %s" % base64string.decode('utf-8'))
res = urllib.request.urlopen(req, timeout=5)
except(urllib.error.HTTPError, urllib.error.URLError) as ex:
print('Failed to find keys with exception: ', ex)
api_key_request = {
'name': 'dashboardapikey',
'role': 'Admin'
}
api_key_request_json = json.dumps(api_key_request)
jsondatabytes = api_key_request_json.encode('utf-8')
req = urllib.request.Request('http://' + self.grafana_url + '/api/auth/keys', jsondatabytes)
req.add_header('Accept', 'application/json')
req.add_header('Content-Type', 'application/json; charset=utf-8')
req.add_header('Content-Length', len(jsondatabytes))
req.add_header("Authorization", "Basic %s" % base64string.decode('utf-8'))
try:
res = urllib.request.urlopen(req, timeout=5).read()
json_result = json.loads(res.decode())
if json_result['name'] != "dashboardapikey":
return False
except (urllib.error.HTTPError, urllib.error.URLError) as ex:
print('Could not generate API key (this is bad)')
print('Exception: ', ex)
return False
self.apikey = json_result['key']
# Store this for future use
with open(self.api_key_file, 'w') as self.api_key_file:
self.api_key_file.write(self.apikey)
def clear_history(self, data):
# clear the history for the specified measurement, allowing the dashboard to be added again
# note that this also clears the fields
self.load_history()
self.history.pop(data['name'], None)
self.write_history()
def sync_dashboard(self, data, quiet = False):
# data contains name and values[]['name']['type']
if not quiet:
print('Synchronizing dashboard for ' + data['name'])
self.load_history()
if data['name'] in self.history:
# we've already added this dashboard in the past so just need to add fields that aren't in history
# load the dashboard from grafana
if self.load_existing_dashboard(data['name']):
if not quiet:
print('Loaded existing dashboard: ' + data['name'])
else:
# we had the dashboard in history but it's not in grafana, so skip
if not quiet:
print('Could not find previously added dashboard ' + data['name'] + '. Skipping.')
return
else:
# this is a new measurement so create a new dashboard for it
print('Creating new dashboard: ' + data['name'])
self.create_new_dashboard(data['name'])
for fields in data['values']:
# check the history file to see if we have done this one already
# if we have we can skip it
fieldname = fields[0]
fieldtype = fields[1]
if fieldname in self.history[data['name']]:
if self.history[data['name']][fieldname] == 'added':
if not quiet:
print('Skipping known field: ' + fieldname)
continue
print('New field found: ' + fieldname + ' (' + fieldtype + ')')
self.add_field_to_dashboard(fieldname, fieldtype, data['name'])
self.add_to_history(fieldname, data['name'])
# store this in grafana
self.create_update_dashboard()
self.write_history()
def load_history(self):
# Load the history from the file on disk and parse it into the class instance
with open(self.history_file, 'a+') as history:
history.seek(0)
try:
self.history = json.load(history)
except json.decoder.JSONDecodeError:
self.history = {}
print('Creating new history log')
pass
def add_to_history(self, fieldname, measurement):
self.history.setdefault(measurement, {})
self.history[measurement].setdefault(fieldname, 'added')
def write_history(self):
# Write the history held in the class back to the file on disk for safe keeps
with open(self.history_file, 'w') as history:
json.dump(self.history, history)
def load_existing_dashboard(self,uid):
# Load the existing dashboard JSON from Grafana, ready to append new panels
req = urllib.request.Request('http://' + self.grafana_url + '/api/dashboards/uid/' + uid)
req.add_header('Authorization', 'Bearer ' + self.apikey)
req.add_header('Accept', 'application/json')
req.add_header('Content-Type', 'application/json; charset=utf-8')
try:
res = urllib.request.urlopen(req, timeout=5).read()
self.dashboard = json.loads(res.decode())['dashboard']
except (urllib.error.HTTPError, urllib.error.URLError):
return False
pass
# Here we need to find the highest ID existing in the dashboard in order to increment from there
self.current_id = self.get_highest_dashboard_id()
return True
def create_new_dashboard(self, measurement):
with open('templates/dashboard_template.json') as dashboardfile:
self.dashboard = json.load(dashboardfile)
self.current_id = 1
self.dashboard['title'] = stringcase.titlecase(measurement)
self.dashboard['uid'] = measurement
self.dashboard['tags'] = [self.generated_tag]
self.history[measurement] = {}
# With every new dashboard we add a summary panel first
self.add_summary_panel_to_dashboard(measurement)
def add_field_to_dashboard(self, fieldname, fieldtype, measurement):
# Take the existing dashboard JSON and inject the appropriate panel JSON
# We are interested in .panels[]
# Add the panel json to this array, being careful to increment the id
self.create_row(fieldname)
# we can take a guess at panel type not only based on field type but name too
# ie a timestamp might be a float but if it's called timestamp we could show a 'clock' panel or sth
if fieldtype == "float" or fieldtype == "integer":
self.add_panel_to_row("stat", fieldname, measurement)
self.add_panel_to_row("graph", fieldname, measurement)
elif fieldtype == "string" or fieldtype == "boolean":
self.add_panel_to_row("textstat", fieldname, measurement)
self.add_panel_to_row("table", fieldname, measurement)
self.dashboard['panels'].append(self.row)
self.row = None
def add_panel_to_row(self, paneltype, fieldname, measurement):
newpanel = self.load_panel(paneltype)
self.current_id = self.current_id + 1
newpanel['id'] = self.current_id
newpanel['title'] = self.generate_panel_name(fieldname)
if paneltype in ['gauge', 'graph', 'stat', 'table', 'textstat']:
newpanel['targets'][0]['measurement'] = measurement
newpanel['targets'][0]['alias'] = fieldname
newpanel['targets'][0]['select'][0][0]['params'][0] = fieldname
self.row['panels'].append(newpanel)
def add_summary_panel_to_dashboard(self, measurement):
self.create_row("Data summary", False)
newpanel = self.load_panel('summary')
self.current_id = self.current_id + 1
newpanel['id'] = self.current_id
newpanel['title'] = self.generate_panel_name(measurement)
newpanel['targets'][0]['measurement'] = measurement
self.dashboard['panels'].append(self.row)
self.dashboard['panels'].append(newpanel)
self.row = None
def create_row(self, title, collapsed = True):
row = self.load_panel('row')
self.current_id = self.current_id + 1
row['id'] = self.current_id
row['title'] = self.generate_panel_name(title)
if len(self.dashboard['panels']) > 0:
row['gridPos']['y'] = self.dashboard['panels'][-1]['gridPos']['y'] + 1
row['collapsed'] = collapsed
self.row = row
# Load the object for the specified fieldtype
def load_panel(self, paneltype):
with open('templates/' + paneltype + '_panel_template.json') as panelfile:
panel = json.load(panelfile)
return panel
def generate_panel_name(self, name):
return stringcase.titlecase(name)
def create_update_dashboard(self):
# Send this JSON to Grafana
# This could be a new dashboard (no ID), or updating an existing one (with ID)
grafana_dashboard = {
'dashboard': self.dashboard,
'folderId': 0,
'overwrite': True
}
dashboardjson = json.dumps(grafana_dashboard)
jsondatabytes = dashboardjson.encode('utf-8')
# Send this to grafana
# TODO load auth key automatically
req = urllib.request.Request('http://' + self.grafana_url + '/api/dashboards/db', jsondatabytes)
req.add_header('Authorization', 'Bearer ' + self.apikey)
req.add_header('Accept', 'application/json')
req.add_header('Content-Type', 'application/json; charset=utf-8')
req.add_header('Content-Length', len(jsondatabytes))
try:
res = urllib.request.urlopen(req, timeout=5)
return True
except (urllib.error.HTTPError, urllib.error.URLError):
pass
return False
def get_highest_dashboard_id(self):
id_values = self.get_recursively(self.dashboard, 'id')
return max(id_values)
def get_recursively(self, search_dict, field):
fields_found = []
for key, value in search_dict.items():
if key == field and isinstance(value, int):
fields_found.append(value)
elif isinstance(value, dict):
results = self.get_recursively(value, field)
for result in results:
if isinstance(result, int):
fields_found.append(result)
elif isinstance(value, list):
for item in value:
if isinstance(item, dict):
more_results = self.get_recursively(item, field)
for another_result in more_results:
if isinstance(another_result, int):
fields_found.append(another_result)
return fields_found
def get_provisioning_dash(self):
# Returns id of first json file in reports_diretory or
# id of default.json file if it exists, otherwise 0
found_file = "none"
list_dir = os.listdir(self.reports_directory)
list_dir = [f.lower() for f in list_dir]
list_dir = sorted(list_dir)
count = 0
for filename in list_dir:
if filename.endswith(".json"):
count = count + 1
if count == 1:
# This is the first json file
found_file = filename
if filename == "default.json":
found_file = filename
continue
return_id = 0
if found_file != "none":
# Get uid from file
found_file = open(self.reports_directory + "/" + found_file)
report_json = json.loads(found_file.read())
found_file.close()
found_file = report_json["uid"]
# Find matching uid in grafana and get id
req = urllib.request.Request('http://' + self.grafana_url + '/api/search?query=%')
res = urllib.request.urlopen(req, timeout=5).read()
data = json.loads(res.decode())
for result in data:
if result['uid'] == found_file:
return_id = result['id']
continue
return return_id
def get_lowest_dashboard_id(self):
# Returns minimum id of any generated dashboard
# based on dashboard having a tag (generated_tag)
req = urllib.request.Request('http://' + self.grafana_url + '/api/search?query=%')
res = urllib.request.urlopen(req, timeout=5).read()
data = json.loads(res.decode())
min_id = 0
ids = []
for result in data:
if self.generated_tag in result['tags']:
ids.append(result['id'])
if len(ids) > 0:
ids.sort
min_id = ids[0]
return min_id
def set_default_dashboard(self, id):
# Sets the default dashboard to provided id
default_dashboard = {
'homeDashboardId': id,
'theme': "",
'timezone': ""
}
defaultjson = json.dumps(default_dashboard)
jsondatabytes = defaultjson.encode('utf-8')
req = urllib.request.Request('http://' + self.grafana_url + '/api/user/preferences', jsondatabytes, method='PUT')
req.add_header('Authorization', 'Bearer ' + self.apikey)
req.add_header('Accept', 'application/json')
req.add_header('Content-Type', 'application/json; charset=utf-8')
req.add_header('Content-Length', len(jsondatabytes))
try:
res = urllib.request.urlopen(req, timeout=5)
return True
except (urllib.error.HTTPError, urllib.error.URLError):
print(urllib.error)
pass
return False
def get_current_default_dashboard(self):
# Returns current default dashboard ID
req = urllib.request.Request('http://' + self.grafana_url + '/api/user/preferences')
res = urllib.request.urlopen(req, timeout=5).read()
data = json.loads(res.decode())
dash = data["homeDashboardId"]
return dash
def default_dashboard(self):
# Routine to set the default dashboard based on opinionated criteria
x = True
default_dash = self.get_current_default_dashboard()
provisioned = self.get_provisioning_dash()
if provisioned != 0:
if provisioned != default_dash:
print("Found provisioned dashboard ID {0} and setting as default.".format(provisioned))
x = self.set_default_dashboard(provisioned)
else:
if default_dash == 0:
min_dash = self.get_lowest_dashboard_id()
if min_dash > 0 and min_dash != default_dash:
x = self.set_default_dashboard(min_dash)
print("Set default dashboard to ID {0}.".format(min_dash))
if x != True:
print("Could not set default dashboard.")
return x