def make_data_sources(stats_sources): datasources_dir = path.join(get_provisioning_dir(), 'datasources') os.makedirs(datasources_dir, exist_ok=True) template = get_data_source_template() for i, stats_source in enumerate(stats_sources): auth_required = stats_source.requires_auth() user = '' password = '' if auth_required: user = stats_source.basic_auth_user() password = stats_source.basic_auth_password() data_source_name = stats_source.short_name() replacement_map = { 'data-source-name': data_source_name, 'data-source-host': stats_source.host(), 'data-source-port': str(stats_source.port()), 'data-source-path': stats_source.stats_url_path(), 'data-source-basic-auth': str(auth_required), 'data-source-basic-auth-user': user, 'data-source-basic-auth-password': password } filename = 'ds-{}.yaml'.format(data_source_name).replace(':', '_') fullname = path.join(datasources_dir, filename) with open(fullname, 'w') as file: file.write(templating.replace(template, replacement_map))
def maybe_expand_templating(dashboard, template_params): dashboard_template = dashboard.get('templating') logging.debug('dashboard_template:{}'.format(dashboard_template)) if dashboard_template: templating_list = dashboard_template.get('list') logging.debug('templating_list:{}'.format(templating_list)) for element in templating_list: for template_param in template_params: if template_param['type'] == 'bucket' and \ element['type'] == 'custom': options = element['options'] option_template = options.pop() option_string = json.dumps(option_template) logging.debug('options:{}'.format(options)) logging.debug('option_template:{}'.format(option_template)) logging.debug('option_string:{}'.format(option_string)) logging.debug('template_params:{}'.format(template_params)) for idx, value in enumerate(template_param['values']): option = templating.replace(option_string, {'bucket': value}) option_json = json.loads(option) if idx == 0: option_json['selected'] = True element['current'] = option_json options.append(option_json)
def make_dashboards_yaml(): os.makedirs(get_dashboards_dir(), exist_ok=True) with open(path.join(util.get_root_dir(), 'dashboards.yaml'), 'r') as file: replacements = {'absolute-path-to-cwd': os.path.abspath('.')} contents = templating.replace(file.read(), replacements) with open(path.join(get_dashboards_dir(), 'dashboards.yaml'), 'w') as file_to_write: file_to_write.write(contents)
def make_dashboard_part(part_meta, template_params, sub_part_function=None): base_part = part_meta['_base'] part_template = get_template(base_part) part_template = metaify_template_string(part_template, part_meta) template_params_to_expand = [ p for p in template_params if templating.find_parameter(part_template, p['type']) >= 0 ] combinations = get_all_param_value_combinations(template_params_to_expand) result = [] logging.debug('part_template:{}'.format(part_template)) logging.debug('template_params:{}'.format(template_params)) logging.debug('combinations:{}'.format(combinations)) if combinations: for combination in combinations: replacements = {} sub_template_params = template_params[:] for param in combination: param_type = param['type'] param_value = param['value'] replacements[param_type] = param_value idx = util.index(sub_template_params, lambda x: x['type'] == param_type) sub_template_params[idx] = { 'type': param_type, 'values': [param_value] } logging.debug('replacements:{}'.format(replacements)) logging.debug('sub_template_params:{}'.format(sub_template_params)) part_string = templating.replace(part_template, replacements) part = json.loads(part_string) if sub_part_function: sub_part_function(part, part_meta, sub_template_params) if part not in result: result.append(part) else: part_string = templating.replace(part_template, {}) part = json.loads(part_string) if sub_part_function: sub_part_function(part, part_meta, template_params) if part not in result: result.append(part) return result
def make_custom_ini(grafana_http_port): os.makedirs(PROMTIMER_DIR, exist_ok=True) replacements = { 'absolute-path-to-cwd': os.path.abspath('.'), 'grafana-http-port': str(grafana_http_port) } template = get_custom_ini_template() contents = templating.replace(template, replacements) with open(path.join(PROMTIMER_DIR, 'custom.ini'), 'w') as file: file.write(contents)
def make_data_sources(data_sources_names, base_port): datasources_dir = path.join(get_provisioning_dir(), 'datasources') os.makedirs(datasources_dir, exist_ok=True) template = get_data_source_template() for i, data_source_name in enumerate(data_sources_names): data_source_name = data_sources_names[i] replacement_map = { 'data-source-name': data_source_name, 'data-source-port': str(base_port + i) } filename = path.join(datasources_dir, 'ds-{}.yaml'.format(data_source_name)) with open(filename, 'w') as file: file.write(templating.replace(template, replacement_map))
def make_dashboard(dashboard_meta, template_params, min_time_string, max_time_string, refresh): replacements = { 'dashboard-from-time': min_time_string, 'dashboard-to-time': max_time_string, 'dashboard-refresh': refresh } template_string = get_template(dashboard_meta['_base']) template_string = metaify_template_string(template_string, dashboard_meta) dashboard_string = templating.replace(template_string, replacements) logging.debug( 'make_dashboard: dashboard_string:{}'.format(dashboard_string)) dashboard = json.loads(dashboard_string) maybe_expand_templating(dashboard, template_params) template_params = maybe_substitute_templating_variables( dashboard, template_params) logging.debug('make_dashboard: title:{}, template_params {}'.format( dashboard_meta['title'], template_params)) panel_id = 0 current_y = 0 panel_row_width = 0 panel_row_height = 0 panels = make_panels(dashboard_meta['_panels'], template_params) for i, panel in enumerate(panels): panel = panels[i] if panel['gridPos']['w'] > (24 - panel_row_width): # If the next panel won't fit, move it to the next row of panels current_y = current_y + panel_row_height panel_row_height = 0 panel_row_width = 0 if panel['gridPos']['h'] > panel_row_height: # If the current panel is the tallest, update row height panel_row_height = panel['gridPos']['h'] panel['gridPos']['x'] = panel_row_width panel['gridPos']['y'] = current_y panel_row_width += panel['gridPos']['w'] panel['id'] = panel_id panel_id += 1 dashboard['panels'].append(panel) return dashboard
def make_dashboard(dashboard_meta, template_params, min_time, max_time): replacements = {'dashboard-from-time': min_time.isoformat(), 'dashboard-to-time': max_time.isoformat()} template_string = get_template(dashboard_meta['_base']) template_string = metaify_template_string(template_string, dashboard_meta) dashboard_string = templating.replace(template_string, replacements) logging.debug('make_dashboard: dashboard_string:{}'.format(dashboard_string)) dashboard = json.loads(dashboard_string) maybe_expand_templating(dashboard, template_params) template_params = maybe_substitute_templating_variables(dashboard, template_params) logging.debug('make_dashboard: title:{}, template_params {}'.format( dashboard_meta['title'], template_params)) panel_id = 0 panels = make_panels(dashboard_meta['_panels'], template_params) for i, panel in enumerate(panels): panel = panels[i] panel['gridPos']['w'] = 12 panel['gridPos']['h'] = 12 panel['gridPos']['x'] = (i % 2) * 12 panel['gridPos']['y'] = int(i / 2) * 12 panel['id'] = panel_id panel_id += 1 dashboard['panels'].append(panel) return dashboard