diff --git a/bundles/grafana/items.py b/bundles/grafana/items.py index 3e37f67..61e0fef 100644 --- a/bundles/grafana/items.py +++ b/bundles/grafana/items.py @@ -83,31 +83,42 @@ with open(repo.path.join([f'data/grafana/flux.mako'])) as file: bucket = repo.get_node(node.metadata.get('grafana/influxdb_node')).metadata.get('influxdb/bucket') -for dashboard_id, (node_name, panels) in enumerate(node.metadata.get('grafana/dashboards').items(), start=1): +monitored_nodes = [ + other_node + for other_node in repo.nodes + if other_node.metadata.get('telegraf/influxdb_node', None) == node.metadata.get('grafana/influxdb_node') +] + +for dashboard_id, monitored_node in enumerate(monitored_nodes, start=1): dashboard = deepcopy(dashboard_template) dashboard['id'] = dashboard_id - dashboard['title'] = node_name + dashboard['title'] = monitored_node.name - for panel_id, (panel_name, panel_config) in enumerate(panels.items(), start=1): + for panel_id, input_name in enumerate(sorted(monitored_node.metadata.get('telegraf/config/inputs')), start=1): panel = deepcopy(panel_template) panel['id'] = panel_id - panel['title'] = panel_name + panel['title'] = input_name + panel['gridPos']['y'] = (panel_id - 1) * panel['gridPos']['h'] - for target_name, target_config in panel_config.items(): - print(target_name, target_config) + with open(repo.path.join([f'data/grafana/panels/{input_name}.py'])) as file: + panel_config = eval(file.read()) + + for target_id, target in enumerate(panel_config.get('targets', []), start=1): panel['targets'].append({ - 'refId': target_name, + 'refId': f'{input_name}_{target_id}', 'query': flux_template.render( bucket=bucket, - host=node_name, - field=target_name, - filters=target_config['filter'], + host=monitored_node.name, + filters={ + 'host': monitored_node.name, + **target, + }, ).strip() }) dashboard['panels'].append(panel) - files[f'/var/lib/grafana/dashboards/{node_name}.json'] = { + files[f'/var/lib/grafana/dashboards/{monitored_node.name}.json'] = { 'content': json.dumps(dashboard, sort_keys=True, indent=4), 'triggers': [ 'svc_systemd:grafana-server:restart', diff --git a/bundles/grafana/metadata.py b/bundles/grafana/metadata.py index 7a52a71..6e3cd28 100644 --- a/bundles/grafana/metadata.py +++ b/bundles/grafana/metadata.py @@ -31,7 +31,6 @@ defaults = { 'allow_signup': False, }, }, - 'dashboards': {}, 'datasources': {}, }, 'postgresql': { @@ -56,26 +55,6 @@ defaults = { } -@metadata_reactor.provides( - 'grafana/dashboards', -) -def dashboards(metadata): - dashboards = {} - - for monitored_node in repo.nodes: - if monitored_node.metadata.get('telegraf/influxdb_node', None) == metadata.get('grafana/influxdb_node'): - for telegraf_input in monitored_node.metadata.get('telegraf/config/inputs'): - with open(repo.path.join([f'data/grafana/panels/{telegraf_input}.py'])) as file: - dashboards.setdefault(monitored_node.name, {})[telegraf_input] = \ - eval(Template(file.read()).render(metadata=monitored_node.metadata)) - - return { - 'grafana': { - 'dashboards': dashboards, - } - } - - @metadata_reactor.provides( 'grafana/datasources', ) diff --git a/data/grafana/dashboard.py b/data/grafana/dashboard.py index 6a04af5..a5f6f03 100644 --- a/data/grafana/dashboard.py +++ b/data/grafana/dashboard.py @@ -25,37 +25,11 @@ "style": "dark", "tags": [], "templating": { - "list": [ - { - "allValue": None, - "current": { - "isNone": True, - "selected": False, - "text": "None", - "value": "" - }, - "datasource": None, - "definition": "", - "description": None, - "error": None, - "hide": 0, - "includeAll": False, - "label": None, - "multi": False, - "name": "query0", - "options": [], - "query": "", - "refresh": 1, - "regex": "", - "skipUrlSync": False, - "sort": 0, - "type": "query" - } - ] + "list": [], }, "time": { - "from": "2021-07-01T19:00:00.201Z", - "to": "2021-07-01T19:03:10.018Z" + "from": "now-6h", + "to": "now" }, "timepicker": {}, "timezone": "", diff --git a/data/grafana/flux.mako b/data/grafana/flux.mako index 45ac1e9..ee51e81 100644 --- a/data/grafana/flux.mako +++ b/data/grafana/flux.mako @@ -1,9 +1,8 @@ from(bucket: "${bucket}") |> range(start: v.timeRangeStart, stop: v.timeRangeStop) - |> filter(fn: (r) => r["host"] == "${host}") -% for key, value in filters.items(): - |> filter(fn: (r) => r["${key}"] == "${value}") +% for key, values in filters.items(): +<% values = values if isinstance(values, list) else [values] %>\ + |> filter(fn: (r) => ${' or '.join(f'r["{key}"] == "{value}"' for value in values)}) % endfor - |> filter(fn: (r) => r["_field"] == "${field}") |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false) |> yield(name: "mean") diff --git a/data/grafana/panel.py b/data/grafana/panel.py index ef354a5..95590de 100644 --- a/data/grafana/panel.py +++ b/data/grafana/panel.py @@ -1,6 +1,12 @@ { # 'id': 1, # 'title': 'TBD', + "gridPos": { + "x": 0, +# "y": 0, + "h": 5, + "w": 24 + }, 'type': 'timeseries', 'transformations': [], 'description': '', diff --git a/data/grafana/panels/cpu.py b/data/grafana/panels/cpu.py index 9f36d10..3d6ca23 100644 --- a/data/grafana/panels/cpu.py +++ b/data/grafana/panels/cpu.py @@ -1,20 +1,14 @@ { - 'usage_system': { - 'filter': { + 'stacked': True, + 'targets': [ + { '_measurement': 'cpu', 'cpu': 'cpu-total', + '_field': [ + 'usage_iowait', + 'usage_system', + 'usage_user', + ], }, - }, - 'usage_iowait': { - 'filter': { - '_measurement': 'cpu', - 'cpu': 'cpu-total', - }, - }, - 'usage_user': { - 'filter': { - '_measurement': 'cpu', - 'cpu': 'cpu-total', - }, - }, + ], } diff --git a/data/grafana/panels/diskio.py b/data/grafana/panels/diskio.py index 0967ef4..1ef5166 100644 --- a/data/grafana/panels/diskio.py +++ b/data/grafana/panels/diskio.py @@ -1 +1,9 @@ -{} +{ + 'stacked': True, + 'targets': [ + { + '_measurement': 'diskio', + '_field': 'write_bytes', + }, + ], +} diff --git a/data/grafana/panels/system.py b/data/grafana/panels/system.py index 0967ef4..aa383d1 100644 --- a/data/grafana/panels/system.py +++ b/data/grafana/panels/system.py @@ -1 +1,12 @@ -{} +{ + 'targets': [ + { + '_measurement': 'system', + '_field': [ + 'load1', + 'load5', + 'load15', + ], + }, + ], +}