This commit is contained in:
mwiegand 2021-07-04 22:02:25 +02:00
parent acc15e3456
commit 710b30792d
15 changed files with 111 additions and 67 deletions

View file

@ -6,6 +6,7 @@ from shlex import quote
from copy import deepcopy from copy import deepcopy
import yaml import yaml
import json import json
from itertools import count
svc_systemd['grafana-server'] = { svc_systemd['grafana-server'] = {
'needs': [ 'needs': [
@ -93,30 +94,39 @@ for dashboard_id, monitored_node in enumerate(monitored_nodes, start=1):
dashboard = deepcopy(dashboard_template) dashboard = deepcopy(dashboard_template)
dashboard['id'] = dashboard_id dashboard['id'] = dashboard_id
dashboard['title'] = monitored_node.name 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): for row_id, row_name in enumerate(sorted(monitored_node.metadata.get('grafana_rows'))):
panel = deepcopy(panel_template) with open(repo.path.join([f'data/grafana/rows/{row_name}.py'])) as file:
panel['id'] = panel_id row = eval(file.read())
panel['title'] = input_name
panel['gridPos']['y'] = (panel_id - 1) * panel['gridPos']['h']
with open(repo.path.join([f'data/grafana/panels/{input_name}.py'])) as file: for panel_in_row, (panel_name, panel_config) in enumerate(row.items()):
panel_config = eval(file.read()) panel = deepcopy(panel_template)
panel['id'] = next(panel_id)
for target_id, target in enumerate(panel_config.get('targets', []), start=1): panel['title'] = panel_name
panel['targets'].append({ panel['gridPos']['w'] = 24 // len(row)
'refId': f'{input_name}_{target_id}', panel['gridPos']['x'] = (24 // len(row)) * panel_in_row
'query': flux_template.render( panel['gridPos']['y'] = (row_id - 1) * panel['gridPos']['h']
bucket=bucket,
host=monitored_node.name,
filters={
'host': monitored_node.name,
**target,
},
).strip()
})
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'] = { files[f'/var/lib/grafana/dashboards/{monitored_node.name}.json'] = {
'content': json.dumps(dashboard, sort_keys=True, indent=4), 'content': json.dumps(dashboard, sort_keys=True, indent=4),

View file

@ -47,6 +47,10 @@ defaults = {
}, },
}, },
}, },
'grafana_rows': [
'cpu',
'disk_io',
],
} }

View file

@ -4,5 +4,9 @@ from(bucket: "${bucket}")
<% values = values if isinstance(values, list) else [values] %>\ <% values = values if isinstance(values, list) else [values] %>\
|> filter(fn: (r) => ${' or '.join(f'r["{key}"] == "{value}"' for value in values)}) |> filter(fn: (r) => ${' or '.join(f'r["{key}"] == "{value}"' for value in values)})
% endfor % endfor
% if function == 'derivative':
|> derivative()
% else:
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false) |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
% endif
|> yield(name: "mean") |> yield(name: "mean")

View file

@ -2,10 +2,10 @@
# 'id': 1, # 'id': 1,
# 'title': 'TBD', # 'title': 'TBD',
"gridPos": { "gridPos": {
"x": 0, # "x": 0,
# "y": 0, # "y": 0,
"h": 5, "h": 8,
"w": 24 # "w": 24
}, },
'type': 'timeseries', 'type': 'timeseries',
'transformations': [], 'transformations': [],
@ -26,7 +26,7 @@
'mode': 'normal', 'mode': 'normal',
'group': 'A' 'group': 'A'
}, },
'axisPlacement': 'hidden', # 'axisPlacement': 'hidden',
'axisLabel': '', 'axisLabel': '',
'scaleDistribution': { 'scaleDistribution': {
'type': 'linear' 'type': 'linear'
@ -67,7 +67,7 @@
}, },
'options': { 'options': {
'tooltip': { 'tooltip': {
'mode': 'none', 'mode': 'single',
}, },
'legend': { 'legend': {
'displayMode': 'list', 'displayMode': 'list',

View file

@ -1,14 +0,0 @@
{
'stacked': True,
'targets': [
{
'_measurement': 'cpu',
'cpu': 'cpu-total',
'_field': [
'usage_iowait',
'usage_system',
'usage_user',
],
},
],
}

View file

@ -1 +0,0 @@
{}

View file

@ -1,9 +0,0 @@
{
'stacked': True,
'targets': [
{
'_measurement': 'diskio',
'_field': 'write_bytes',
},
],
}

View file

@ -1 +0,0 @@
{}

View file

@ -1 +0,0 @@
{}

View file

@ -1 +0,0 @@
{}

View file

@ -1 +0,0 @@
{}

View file

@ -1,12 +0,0 @@
{
'targets': [
{
'_measurement': 'system',
'_field': [
'load1',
'load5',
'load15',
],
},
],
}

View file

@ -1 +0,0 @@
{}

35
data/grafana/rows/cpu.py Normal file
View file

@ -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',
},
},
},
}

View file

@ -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'
},
}