2021-10-26 12:42:17 +00:00
|
|
|
import json
|
2021-12-14 08:25:49 +00:00
|
|
|
import re
|
2021-10-26 12:42:17 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Any, Dict, Tuple, Union
|
|
|
|
|
|
|
|
from termcolor import cprint
|
|
|
|
|
|
|
|
UNITS = ['ms', 's', 'm', 'h', 'd', 'w', 'y']
|
|
|
|
|
|
|
|
|
|
|
|
def resolve_time_and_unit(time: str) -> Union[Tuple[int, str], Tuple[None, None]]:
|
|
|
|
"""
|
|
|
|
Divide time with its unit and return a tuple like (10, 'm')
|
|
|
|
Return None if its and invalid prometheus time
|
|
|
|
Valid units are inside UNITS.
|
|
|
|
"""
|
|
|
|
if time[-1] in UNITS:
|
|
|
|
return int(time[:-1]), time[-1]
|
|
|
|
if time[-2:] in UNITS:
|
|
|
|
return int(time[:-2]), time[-2:]
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
def get_dashboards_data() -> Dict[str, Any]:
|
|
|
|
data: Dict[str, Any] = {'queries': {}, 'variables': {}, 'stats': {}}
|
2021-11-23 08:30:38 +00:00
|
|
|
for file in Path(__file__).parent.parent \
|
|
|
|
.joinpath('dashboards_out').glob('*.json'):
|
2021-10-26 12:42:17 +00:00
|
|
|
with open(file, 'r') as f:
|
|
|
|
dashboard_data = json.load(f)
|
|
|
|
data['stats'][str(file)] = {'total': 0, 'tested': 0}
|
|
|
|
add_dashboard_queries(data, dashboard_data, str(file))
|
|
|
|
add_dashboard_variables(data, dashboard_data)
|
2022-05-17 07:42:29 +00:00
|
|
|
add_default_dashboards_variables(data)
|
2021-10-26 12:42:17 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def add_dashboard_queries(data: Dict[str, Any], dashboard_data: Dict[str, Any], path: str) -> None:
|
|
|
|
"""
|
|
|
|
Grafana panels can have more than one target/query, in order to identify each
|
|
|
|
query in the panel we append the "legendFormat" of the target to the panel name.
|
|
|
|
format: panel_name-legendFormat
|
|
|
|
"""
|
|
|
|
if 'panels' not in dashboard_data:
|
|
|
|
return
|
2021-12-16 12:34:35 +00:00
|
|
|
error = 0
|
2021-10-26 12:42:17 +00:00
|
|
|
for panel in dashboard_data['panels']:
|
|
|
|
if (
|
|
|
|
'title' in panel
|
|
|
|
and 'targets' in panel
|
|
|
|
and len(panel['targets']) > 0
|
|
|
|
and 'expr' in panel['targets'][0]
|
|
|
|
):
|
|
|
|
for target in panel['targets']:
|
|
|
|
title = panel['title']
|
|
|
|
legend_format = target['legendFormat'] if 'legendFormat' in target else ""
|
2021-12-16 12:34:35 +00:00
|
|
|
query_id = f'{title}-{legend_format}'
|
2021-10-26 12:42:17 +00:00
|
|
|
if query_id in data['queries']:
|
|
|
|
# NOTE: If two or more panels have the same name and legend it
|
|
|
|
# might suggest a refactoring is needed or add something else
|
|
|
|
# to identify each query.
|
2022-01-03 11:52:09 +00:00
|
|
|
conflict_file = Path(data['queries'][query_id]['path']).name
|
|
|
|
file = Path(path).name
|
2021-12-16 12:34:35 +00:00
|
|
|
cprint((f'ERROR: Query in panel "{title}" with legend "{legend_format}"'
|
|
|
|
f' already exists. Conflict "{conflict_file}" '
|
|
|
|
f'with: "{file}"'), 'red')
|
|
|
|
error = 1
|
2021-10-26 12:42:17 +00:00
|
|
|
data['queries'][query_id] = {'query': target['expr'], 'path': path}
|
|
|
|
data['stats'][path]['total'] += 1
|
2021-12-16 12:34:35 +00:00
|
|
|
if error:
|
2021-12-17 07:58:58 +00:00
|
|
|
raise ValueError('Missing legend_format in queries, please add a proper value.')
|
2021-10-26 12:42:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_dashboard_variables(data: Dict[str, Any], dashboard_data: Dict[str, Any]) -> None:
|
|
|
|
if 'templating' not in dashboard_data or 'list' not in dashboard_data['templating']:
|
|
|
|
return
|
|
|
|
for variable in dashboard_data['templating']['list']:
|
|
|
|
if 'name' in variable:
|
|
|
|
data['variables'][variable['name']] = 'UNSET VARIABLE'
|
2021-12-14 08:25:49 +00:00
|
|
|
|
2022-05-17 07:42:29 +00:00
|
|
|
def add_default_dashboards_variables(data: Dict[str, Any]) -> None:
|
|
|
|
data['variables']['job'] = 'ceph'
|
|
|
|
data['variables']['job_haproxy'] = 'haproxy'
|
|
|
|
data['variables']['__rate_interval'] = '1m'
|
2021-12-14 08:25:49 +00:00
|
|
|
|
|
|
|
def replace_grafana_expr_variables(expr: str, variable: str, value: Any) -> str:
|
|
|
|
""" Replace grafana variables in expression with a value
|
|
|
|
|
2022-06-23 01:24:12 +00:00
|
|
|
It should match the whole word, 'osd' must not match with the 'osd' prefix in 'osd_hosts'
|
2021-12-14 08:25:49 +00:00
|
|
|
>>> replace_grafana_expr_variables('metric{name~="$osd_hosts|$other|$osd"}', \
|
|
|
|
'osd', 'replacement')
|
|
|
|
'metric{name~="$osd_hosts|$other|replacement"}'
|
|
|
|
|
|
|
|
>>> replace_grafana_expr_variables('metric{name~="$osd_hosts|$other|$osd"}', \
|
|
|
|
'other', 'replacement')
|
|
|
|
'metric{name~="$osd_hosts|replacement|$osd"}'
|
|
|
|
|
|
|
|
It replaces words with dollar prefix
|
|
|
|
>>> replace_grafana_expr_variables('metric{name~="no_dollar|$other|$osd"}', \
|
|
|
|
'no_dollar', 'replacement')
|
|
|
|
'metric{name~="no_dollar|$other|$osd"}'
|
2022-01-03 08:32:51 +00:00
|
|
|
|
|
|
|
It shouldn't replace the next char after the variable (positive lookahead test).
|
|
|
|
>>> replace_grafana_expr_variables('metric{name~="$osd"}', \
|
|
|
|
'osd', 'replacement')
|
|
|
|
'metric{name~="replacement"}'
|
2021-12-14 08:25:49 +00:00
|
|
|
"""
|
|
|
|
regex = fr'\${variable}(?=\W)'
|
2022-01-03 08:32:51 +00:00
|
|
|
new_expr = re.sub(regex, fr'{value}', expr)
|
2021-12-14 08:25:49 +00:00
|
|
|
return new_expr
|