diff --git a/bundles/grafana/items.py b/bundles/grafana/items.py index 61e0fef..08ff625 100644 --- a/bundles/grafana/items.py +++ b/bundles/grafana/items.py @@ -6,6 +6,7 @@ from shlex import quote from copy import deepcopy import yaml import json +from itertools import count svc_systemd['grafana-server'] = { 'needs': [ @@ -93,30 +94,39 @@ for dashboard_id, monitored_node in enumerate(monitored_nodes, start=1): dashboard = deepcopy(dashboard_template) dashboard['id'] = dashboard_id dashboard['title'] = monitored_node.name + panel_id = count() + - 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'] = input_name - panel['gridPos']['y'] = (panel_id - 1) * panel['gridPos']['h'] + for row_id, row_name in enumerate(sorted(monitored_node.metadata.get('grafana_rows'))): + with open(repo.path.join([f'data/grafana/rows/{row_name}.py'])) as file: + row = eval(file.read()) - 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': f'{input_name}_{target_id}', - 'query': flux_template.render( - bucket=bucket, - host=monitored_node.name, - filters={ - 'host': monitored_node.name, - **target, - }, - ).strip() - }) + for panel_in_row, (panel_name, panel_config) in enumerate(row.items()): + panel = deepcopy(panel_template) + panel['id'] = next(panel_id) + panel['title'] = panel_name + panel['gridPos']['w'] = 24 // len(row) + panel['gridPos']['x'] = (24 // len(row)) * panel_in_row + panel['gridPos']['y'] = (row_id - 1) * panel['gridPos']['h'] - dashboard['panels'].append(panel) + if 'display_name' in panel_config: + panel['fieldConfig']['defaults']['displayName'] = '${'+panel_config['display_name']+'}' + + for query_name, query_config in panel_config['queries'].items(): + panel['targets'].append({ + 'refId': query_name, + 'query': flux_template.render( + bucket=bucket, + host=monitored_node.name, + filters={ + 'host': monitored_node.name, + **query_config['filters'], + }, + function=query_config.get('function', None), + ).strip() + }) + + dashboard['panels'].append(panel) files[f'/var/lib/grafana/dashboards/{monitored_node.name}.json'] = { 'content': json.dumps(dashboard, sort_keys=True, indent=4), diff --git a/bundles/telegraf/metadata.py b/bundles/telegraf/metadata.py index b5ff7a6..5d887ec 100644 --- a/bundles/telegraf/metadata.py +++ b/bundles/telegraf/metadata.py @@ -47,6 +47,10 @@ defaults = { }, }, }, + 'grafana_rows': [ + 'cpu', + 'disk_io', + ], } diff --git a/data/grafana/flux.mako b/data/grafana/flux.mako index ee51e81..92c2056 100644 --- a/data/grafana/flux.mako +++ b/data/grafana/flux.mako @@ -4,5 +4,9 @@ from(bucket: "${bucket}") <% values = values if isinstance(values, list) else [values] %>\ |> filter(fn: (r) => ${' or '.join(f'r["{key}"] == "{value}"' for value in values)}) % endfor +% if function == 'derivative': + |> derivative() +% else: |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false) +% endif |> yield(name: "mean") diff --git a/data/grafana/panel.py b/data/grafana/panel.py index 95590de..a5eea50 100644 --- a/data/grafana/panel.py +++ b/data/grafana/panel.py @@ -2,10 +2,10 @@ # 'id': 1, # 'title': 'TBD', "gridPos": { - "x": 0, +# "x": 0, # "y": 0, - "h": 5, - "w": 24 + "h": 8, +# "w": 24 }, 'type': 'timeseries', 'transformations': [], @@ -26,7 +26,7 @@ 'mode': 'normal', 'group': 'A' }, - 'axisPlacement': 'hidden', +# 'axisPlacement': 'hidden', 'axisLabel': '', 'scaleDistribution': { 'type': 'linear' @@ -67,7 +67,7 @@ }, 'options': { 'tooltip': { - 'mode': 'none', + 'mode': 'single', }, 'legend': { 'displayMode': 'list', diff --git a/data/grafana/panels/cpu.py b/data/grafana/panels/cpu.py deleted file mode 100644 index 3d6ca23..0000000 --- a/data/grafana/panels/cpu.py +++ /dev/null @@ -1,14 +0,0 @@ -{ - 'stacked': True, - 'targets': [ - { - '_measurement': 'cpu', - 'cpu': 'cpu-total', - '_field': [ - 'usage_iowait', - 'usage_system', - 'usage_user', - ], - }, - ], -} diff --git a/data/grafana/panels/disk.py b/data/grafana/panels/disk.py deleted file mode 100644 index 0967ef4..0000000 --- a/data/grafana/panels/disk.py +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/data/grafana/panels/diskio.py b/data/grafana/panels/diskio.py deleted file mode 100644 index 1ef5166..0000000 --- a/data/grafana/panels/diskio.py +++ /dev/null @@ -1,9 +0,0 @@ -{ - 'stacked': True, - 'targets': [ - { - '_measurement': 'diskio', - '_field': 'write_bytes', - }, - ], -} diff --git a/data/grafana/panels/kernel.py b/data/grafana/panels/kernel.py deleted file mode 100644 index 0967ef4..0000000 --- a/data/grafana/panels/kernel.py +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/data/grafana/panels/mem.py b/data/grafana/panels/mem.py deleted file mode 100644 index 0967ef4..0000000 --- a/data/grafana/panels/mem.py +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/data/grafana/panels/processes.py b/data/grafana/panels/processes.py deleted file mode 100644 index 0967ef4..0000000 --- a/data/grafana/panels/processes.py +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/data/grafana/panels/swap.py b/data/grafana/panels/swap.py deleted file mode 100644 index 0967ef4..0000000 --- a/data/grafana/panels/swap.py +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/data/grafana/panels/system.py b/data/grafana/panels/system.py deleted file mode 100644 index aa383d1..0000000 --- a/data/grafana/panels/system.py +++ /dev/null @@ -1,12 +0,0 @@ -{ - 'targets': [ - { - '_measurement': 'system', - '_field': [ - 'load1', - 'load5', - 'load15', - ], - }, - ], -} diff --git a/data/grafana/panels/zfs.py b/data/grafana/panels/zfs.py deleted file mode 100644 index 0967ef4..0000000 --- a/data/grafana/panels/zfs.py +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/data/grafana/rows/cpu.py b/data/grafana/rows/cpu.py new file mode 100644 index 0000000..140d471 --- /dev/null +++ b/data/grafana/rows/cpu.py @@ -0,0 +1,35 @@ +{ + 'usage': { + 'stacked': True, + 'queries': { + 'usage': { + 'filters': { + '_measurement': 'cpu', + 'cpu': 'cpu-total', + '_field': [ + 'usage_iowait', + 'usage_system', + 'usage_user', + ], + }, + 'function': 'mean', + }, + }, + }, + 'load': { + 'stacked': False, + 'queries': { + 'load': { + 'filters': { + '_measurement': 'system', + '_field': [ + 'load1', + 'load5', + 'load15', + ], + }, + 'function': 'mean', + }, + }, + }, +} diff --git a/data/grafana/rows/disk_io.py b/data/grafana/rows/disk_io.py new file mode 100644 index 0000000..257e739 --- /dev/null +++ b/data/grafana/rows/disk_io.py @@ -0,0 +1,32 @@ +{ + 'read': { + 'stacked': True, + 'queries': { + 'usage': { + 'filters': { + '_measurement': 'diskio', + '_field': [ + 'read_bytes', + ], + }, + 'function': 'derivative', + }, + }, + 'display_name': '__field.labels.name' + }, + 'write': { + 'stacked': False, + 'queries': { + 'load': { + 'filters': { + '_measurement': 'diskio', + '_field': [ + 'write_bytes', + ], + }, + 'function': 'derivative', + }, + }, + 'display_name': '__field.labels.name' + }, +}