This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgenerate.py
executable file
·458 lines (372 loc) · 13.4 KB
/
generate.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#! /usr/bin/env python
"""
"""
from __future__ import unicode_literals
import os
import sys
import logging
import argparse
import json
import yaml
PROJ_ROOT = os.path.dirname(sys.argv[0])
OUTPUT_DIR = os.path.join(PROJ_ROOT, 'dashboards')
CONFIG_FILE = os.path.join(PROJ_ROOT, 'config.yml')
class ConfigObject(object):
inherits = None
def __init__(s, name, init_dict):
s.__dict__.update(init_dict)
s.name = name
def __unicode__(s):
return unicode(s.__dict__)
def converge(s, others):
"""converge templates
if ConfigObject has an inherits 'variable', fill in values from it.
others == all ConfigObject's type instances from configfile (e.g. for
converging dashboard templates, all dashboards found in config file
convergence is recursive ("template might use "inherits" variable
itself)
inherited attribute is set to inherits, and inherits is set to None, to
show, that template has already been instantiated and inherited
attribute keeps the original template name for other purposes
BEWARE OF LOOPS :-)
"""
if s.inherits is not None:
data = others[s.inherits].converge(others)
data.__dict__.update(s.__dict__)
data.inherited = data.inherits
data.inherits = None
return data
return s.__class__(s.name, s.__dict__)
def fill(s, attr, others):
"""fill in attribute
e.g. attr == 'rows', other == all rows in config file,
items in s.rows attribute gets replaced with appropriate objects
from others instantiating teplates on the way
"""
fill_list = []
for a in getattr(s, attr):
fill_list.append(others[a].converge(others))
result = s.__class__(s.name, s.__dict__)
setattr(result, attr, fill_list)
return result
def generate(s, context):
result = {}
for i in s.copy_items:
result[i] = getattr(s, i)
if hasattr(s, 'optional_copy_items'):
for i in s.optional_copy_items:
try:
result[i] = getattr(s, i)
except AttributeError:
continue
return result
class Dashboard(ConfigObject):
copy_items = (
'editable',
'hideControls',
'originalTitle',
'refresh',
'sharedCrosshair',
'style',
'tags',
'timezone',
'title',
)
optional_copy_items = ('folder',)
instantiate = True
title = 'Unnamed'
originalTitle = 'Unnamed'
tags = []
rows = []
dashboardLinks = []
templating = []
def generate(s, context):
"""TODO: I should check, that there are no "unused" items in the config
(that would probably be a typo)
"""
result = super(Dashboard, s).generate(context)
result['rows'] = [row.generate(context, s) for row in s.rows]
result['links'] = [link.generate(context) for link in s.dashboardLinks]
result['time'] = {
'from': s.time['from'],
'to': s.time['to'],
}
result['timepicker'] = {
'now': s.now,
'refresh_intervals': s.refresh_intervals,
'time_options': s.time_options,
}
result['templating'] = {
'list': [
template.generate(context, s) for template in s.templating],
}
result['annotations'] = {
'list': [],
}
result['schemaVersion'] = 7
result['version'] = 22
return result
class Row(ConfigObject):
"""TODO: check type: matches with definition to avoid certain hard to find
errors in config file
also inheritance will inherit different types :-/
etc...
FIXME: I presume row == panel
"""
_last_id = 0
seriesOverrides = []
aliasColors = {}
copy_items = (
'collapse',
'editable',
'height',
)
legend_items = (
'legend_alignAsTable',
'legend_avg',
'legend_current',
'legend_max',
'legend_min',
'legend_show',
'legend_total',
'legend_values',
)
grid_items = (
'grid_leftLogBase',
'grid_leftMax',
'grid_leftMin',
'grid_rightLogBase',
'grid_rightMax',
'grid_rightMin',
'grid_threshold1',
'grid_threshold1Color',
'grid_threshold2',
'grid_threshold2Color',
)
panel_copy_items = (
'aliasColors',
'bars',
'datasource',
'editable',
'error',
'fill',
'leftYAxisLabel',
'lines',
'lineWidth',
'nullPointMode',
'percentage',
'pointradius',
'points',
'renderer',
'rightYAxisLabel',
'seriesOverrides',
'span',
'stack',
'steppedLine',
'timeFrom',
'title',
'type',
)
@classmethod
def gen_id(cls):
cls._last_id += 1
return cls._last_id
def generate(s, context, parent_dashboard):
result = super(Row, s).generate(context)
result['title'] = 'Row'
result['showTitle'] = False
panel = {}
for item in s.panel_copy_items:
panel[item] = getattr(s, item)
panel['id'] = Row.gen_id()
panel['links'] = []
panel['timeShift'] = None
panel['tooltip'] = {
'shared': s.tooltip_shared,
'value_type': 'cumulative'}
panel['y_formats'] = s.y_formats
panel['legend'] = {}
lss = len('legend_')
for li in s.legend_items:
panel['legend'][li[lss:]] = getattr(s, li)
panel['grid'] = {}
gss = len('grid_')
for gi in s.grid_items:
panel['grid'][gi[gss:]] = getattr(s, gi)
# targets - a.k.a. graphs/expressions in a single row
assert len(s.targets) <= 26 # refId is a single letter (AFAIK)
panel['targets'] = []
target_cnt = 0
for target_data in s.targets:
target = {}
if 'legend_format' in target_data:
target['legendFormat'] = target_data['legend_format']
else:
target['legendFormat'] = s.legend_format
if 'intervalFactor' in target_data:
target['intervalFactor'] = target_data['intervalFactor']
else:
target['intervalFactor'] = s.intervalFactor
target['refId'] = chr(ord('A') + target_cnt)
try:
target['expr'] = target_data['expression'] % \
parent_dashboard.expvars
except KeyError:
print >>sys.stderr, "missing variable while trying to " \
"fill expr: %s" % target_data['expression']
print >>sys.stderr, "for dashboard %s" % parent_dashboard.title
sys.exit(1)
# workaround for double-\ in expressions
target['expr'] = target['expr'].replace('\\', '\\\\')
panel['targets'].append(target)
target_cnt += 1
result['panels'] = [panel]
return result
class Template(ConfigObject):
copy_items = (
'datasource',
'allValue',
'current',
'hide',
'includeAll',
'label',
'multi',
'options',
'refresh',
'sort',
'tagValuesQuery',
'tags',
'tagsQuery',
'type',
'useTags',
)
def generate(s, context, parent_dashboard):
result = super(Template, s).generate(context)
# Set template name and query.
result['name'] = s.name
result['query'] = 'label_values(%s, %s)' % (s.metric, s.label)
# This allows restricting values of the template by a regexp.
#
# When `templating_regexps` dict is defined on a dashboard and contains
# a key with the same name as the name of the template, use a regexp
# defined as an expvar with the same name as the value of the key.
# This allows overriding the regexp per dashboard.
if hasattr(parent_dashboard, 'templating_regexps') and \
s.name in parent_dashboard.templating_regexps:
result['regex'] = parent_dashboard.expvars[
parent_dashboard.templating_regexps[s.name]]
result['allValue'] = parent_dashboard.expvars[
parent_dashboard.templating_regexps
[s.name]].replace('\\', '\\\\')
return result
class DashboardLink(ConfigObject):
copy_items = ('icon', 'tags', 'targetBlank', 'type', 'url', 'title')
def generate(s, context):
return super(DashboardLink, s).generate(context)
class YamlConfigParser(object):
dashboards = {}
graphs = {}
rows = {}
templating = {}
dashboardLinks = {}
def __init__(s, config_file='./config.yml'):
s.config_file = config_file
logging.debug("YamlConfigParser: reading %s" % s.config_file)
with open(s.config_file, 'r') as f:
s.yaml = yaml.load(f)
def parse(s):
logging.debug("YamlConfigParser: parsing")
top_level_items = (
('dashboards', Dashboard, s.dashboards),
('rows', Row, s.rows),
('templating', Template, s.templating),
('dashboardLinks', DashboardLink, s.dashboardLinks),
)
for name, class_, store in top_level_items:
for item in s.yaml[name]:
store[item] = class_(item, s.yaml[name][item])
for dash in s.dashboards:
# FIXME: not nice
logging.debug("templatization & filling of %s dashboard" % dash)
s.dashboards[dash] = s.dashboards[dash].converge(s.dashboards)
# order of template instantiation and filling is not held (yaml
# returns dicts even if they are ordered in config file), sometimes
# we receive an already "filled" instance of dashboard when
# inherited, check for that eventuality and skip fill() in that
# case
if len(s.dashboards[dash].rows) and \
not isinstance(s.dashboards[dash].rows[0], ConfigObject):
s.dashboards[dash] = s.dashboards[dash].fill('rows', s.rows)
if len(s.dashboards[dash].templating) and \
not isinstance(s.dashboards[dash].templating[0], Template):
s.dashboards[dash] = s.dashboards[dash].fill(
'templating', s.templating)
if len(s.dashboards[dash].dashboardLinks) and \
not isinstance(
s.dashboards[dash].dashboardLinks[0],
ConfigObject):
s.dashboards[dash] = s.dashboards[dash].fill(
'dashboardLinks', s.dashboardLinks)
class DashboardGenerator(object):
def __init__(s, ycp):
s.ycp = ycp
def __iter__(s):
for dash_name, dash in s.ycp.dashboards.iteritems():
if dash.instantiate:
if hasattr(dash, 'folder'):
folder = dash.folder
delattr(dash, 'folder')
else:
folder = 'no_folder' # default folder
yield dash_name, folder, s.gen_dashboard(dash)
def gen_dashboard(s, d):
return json.dumps(d.generate(s.ycp.dashboards))
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose',
dest='verbose', action='store_true',
help='be a lil bit verbose')
parser.add_argument('-c', '--config-file',
dest='config_file',
default=CONFIG_FILE)
parser.add_argument('-d', '--dest-dir',
dest='dest_dir',
default=OUTPUT_DIR)
parser.add_argument('-n', '--noop',
dest='noop', action='store_true',
help="don't create json dashboard files")
return parser.parse_args()
def main():
logging.basicConfig(stream=sys.stderr)
args = parse_args()
if args.verbose:
logging.getLogger().setLevel(logging.DEBUG)
else:
logging.getLogger().setLevel(logging.ERROR)
ycp = YamlConfigParser(config_file=args.config_file)
ycp.parse()
dg = DashboardGenerator(ycp)
if not args.noop:
logging.debug('writing %s' % os.path.join(args.dest_dir, 'index'))
index_f = open(os.path.join(args.dest_dir, 'index'), 'w')
else:
logging.debug('would be writing %s' %
os.path.join(args.dest_dir, 'index'))
for dashboard_name, dashboard_folder, dashboard in dg:
out_fn = os.path.join(args.dest_dir,
dashboard_folder,
'%s.json' % dashboard_name)
if args.noop:
logging.debug('would be writing %s' % out_fn)
else:
logging.debug('writing %s' % out_fn)
if dashboard_folder and not os.path.exists(os.path.dirname(out_fn)):
os.makedirs(os.path.dirname(out_fn))
with open(out_fn, 'w') as f:
print >>f, dashboard
print >>index_f, os.path.join(dashboard_folder,
'%s.json' % dashboard_name)
if not args.noop:
index_f.close()
if __name__ == '__main__':
main()