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

View file

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

View file

@ -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": "",

View file

@ -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")

View file

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

View file

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

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