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
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),

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] %>\
|> 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")

View file

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

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