This commit is contained in:
mwiegand 2021-07-04 19:39:58 +02:00
parent 84ecb460df
commit acc15e3456
8 changed files with 64 additions and 82 deletions

View file

@ -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') 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 = deepcopy(dashboard_template)
dashboard['id'] = dashboard_id 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 = deepcopy(panel_template)
panel['id'] = panel_id 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(): with open(repo.path.join([f'data/grafana/panels/{input_name}.py'])) as file:
print(target_name, target_config) panel_config = eval(file.read())
for target_id, target in enumerate(panel_config.get('targets', []), start=1):
panel['targets'].append({ panel['targets'].append({
'refId': target_name, 'refId': f'{input_name}_{target_id}',
'query': flux_template.render( 'query': flux_template.render(
bucket=bucket, bucket=bucket,
host=node_name, host=monitored_node.name,
field=target_name, filters={
filters=target_config['filter'], 'host': monitored_node.name,
**target,
},
).strip() ).strip()
}) })
dashboard['panels'].append(panel) 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), 'content': json.dumps(dashboard, sort_keys=True, indent=4),
'triggers': [ 'triggers': [
'svc_systemd:grafana-server:restart', 'svc_systemd:grafana-server:restart',

View file

@ -31,7 +31,6 @@ defaults = {
'allow_signup': False, 'allow_signup': False,
}, },
}, },
'dashboards': {},
'datasources': {}, 'datasources': {},
}, },
'postgresql': { '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( @metadata_reactor.provides(
'grafana/datasources', 'grafana/datasources',
) )

View file

@ -25,37 +25,11 @@
"style": "dark", "style": "dark",
"tags": [], "tags": [],
"templating": { "templating": {
"list": [ "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"
}
]
}, },
"time": { "time": {
"from": "2021-07-01T19:00:00.201Z", "from": "now-6h",
"to": "2021-07-01T19:03:10.018Z" "to": "now"
}, },
"timepicker": {}, "timepicker": {},
"timezone": "", "timezone": "",

View file

@ -1,9 +1,8 @@
from(bucket: "${bucket}") from(bucket: "${bucket}")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop) |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["host"] == "${host}") % for key, values in filters.items():
% for key, value in filters.items(): <% values = values if isinstance(values, list) else [values] %>\
|> filter(fn: (r) => r["${key}"] == "${value}") |> filter(fn: (r) => ${' or '.join(f'r["{key}"] == "{value}"' for value in values)})
% endfor % endfor
|> filter(fn: (r) => r["_field"] == "${field}")
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false) |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: "mean") |> yield(name: "mean")

View file

@ -1,6 +1,12 @@
{ {
# 'id': 1, # 'id': 1,
# 'title': 'TBD', # 'title': 'TBD',
"gridPos": {
"x": 0,
# "y": 0,
"h": 5,
"w": 24
},
'type': 'timeseries', 'type': 'timeseries',
'transformations': [], 'transformations': [],
'description': '', 'description': '',

View file

@ -1,20 +1,14 @@
{ {
'usage_system': { 'stacked': True,
'filter': { 'targets': [
{
'_measurement': 'cpu', '_measurement': 'cpu',
'cpu': 'cpu-total', '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',
},
},
} }

View file

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

View file

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