This commit is contained in:
2024-11-24 14:53:42 +01:00
parent b1fd84a423
commit 3815ba1547
5 changed files with 203 additions and 0 deletions

129
app/netdata_DocRudi.py Executable file
View File

@@ -0,0 +1,129 @@
#!/usr/bin/env python3
# -*- coding: utf8 -*-
import json
from datetime import datetime
from urllib import request, parse, error
import plotly.graph_objs as go
import plotly.utils
from flask import Flask, render_template
from plotly.subplots import make_subplots
from waitress import serve
import os
import sys
import socket
netdata_host = os.environ.get('NETDATA_HOST', 'http://localhost:19999')
netdata_query_seconds = int(os.environ.get('NETDATA_QUERY_SECONDS', '200000'))
netdata_query_points = int(os.environ.get('NETDATA_QUERY_POINTS', '3000'))
print(type(netdata_query_points), file=sys.stderr)
site_refresh = int(os.environ.get('SITE_REFRESH', '0'))
server_host = socket.gethostname()
server_ip = socket.gethostbyname(server_host)
server_port = os.environ.get('SERVER_PORT', '19998')
app = Flask(__name__)
def get_docker_data(q_context, q_dimension):
netdata_url = f"{netdata_host}/api/v2/data"
params = {
'group_by': 'instance',
'scope_contexts': q_context,
'dimensions': q_dimension,
'format': 'json',
'after': f'-{netdata_query_seconds}',
'points': f'{netdata_query_points}',
'group': 'average'
}
url = f"{netdata_url}?{parse.urlencode(params)}"
with request.urlopen(url) as response:
data = json.loads(response.read().decode())
return data['result']
def process_label(label):
parts = label.split('@')[0].split('.')
if parts and parts[0].startswith('cgroup_'):
if parts[1] =='mem_usage':
return f'mem_{parts[0][7:]}'
else:
return f'{parts[1]}_{parts[0][7:]}'
return label
def create_plot():
result_data_cpu = get_docker_data('cgroup.cpu','*')
result_data_mem_usage = get_docker_data('cgroup.mem_usage', 'ram')
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.1,
subplot_titles=('CPU-Nutzung der Docker-Container', 'Mem-Usage der Docker-Container'))
def plot_data_function(data, row):
if 'labels' in data and 'data' in data:
labels = data['labels']
plot_data = data['data']
elif isinstance(data, list) and len(data) > 1:
labels = data[0]
plot_data = data[1:]
else:
print(f"Unerwartete Datenstruktur für Reihe {row}")
return
for i, label in enumerate(labels[1:], start=1): # Skip the first label (usually timestamp)
if 'slice_system-slice' in labels[i]:
continue
y_values = [round(float(row[i]), 2) if row[i] is not None else None for row in plot_data]
x_values = [datetime.fromtimestamp(row[0]) for row in plot_data]
processed_label = process_label(label)
trace = go.Scatter(
x=x_values,
y=y_values,
mode='lines',
name=processed_label
)
fig.add_trace(trace, row=row, col=1)
plot_data_function(result_data_cpu, row=1)
plot_data_function(result_data_mem_usage, row=2)
# Beschriftung für oberen Graphen wenn bei make_subplots shared_xaxes=false
# fig.update_xaxes(title_text="Zeit", tickformat='%Y-%m-%d %H:%M:%S', tickangle=0, row=1, col=1)
fig.update_xaxes(title_text="Zeit", tickformat='%Y-%m-%d\n%H:%M:%S', tickangle=0, row=2, col=1)
fig.update_yaxes(title_text="CPU-Nutzung (%)", tickformat='.2f', rangemode='tozero', row=1, col=1)
fig.update_yaxes(title_text="MemUsage (MiB)", tickformat='.0f', rangemode='tozero', row=2, col=1)
fig.update_layout(
height=1000,
legend_title="Container",
margin=dict(b=100),
showlegend=True,
paper_bgcolor='#a1bdd6',
plot_bgcolor='#687a8a'
)
return plotly.utils.PlotlyJSONEncoder().encode(fig)
@app.route('/')
def index():
plot = create_plot()
return render_template('index.html', plot=plot, netdata_host=netdata_host,
site_refresh=site_refresh)
def check_url(url, timeout=5):
try:
with request.urlopen(url, timeout=timeout):
print(f"Die Netdata-URL {url} ist erreichbar.", file=sys.stderr)
except error.URLError as e:
print(f"Fehler: Die URL {url} ist nicht erreichbar.", file=sys.stderr)
print(f"Fehlermeldung: {str(e)}", file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
# app.run(host='0.0.0.0', port=19998, debug=True)
check_url(netdata_host)
print(f"Dashboard started at http://{server_host}:{server_port} | http://{server_ip}:{server_port}", file=sys.stderr)
serve(app, host="0.0.0.0", port=server_port)

13
app/requirements.txt Normal file
View File

@@ -0,0 +1,13 @@
blinker==1.9.0
click==8.1.7
colorama==0.4.6
Flask==3.1.0
itsdangerous==2.2.0
Jinja2==3.1.4
MarkupSafe==3.0.2
packaging==24.2
plotly==5.24.1
tenacity==9.0.0
Werkzeug==3.1.3
waitress~=3.0.2

20
app/templates/index.html Normal file
View File

@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% if site_refresh > 0 %}
<meta http-equiv="refresh" content="{{ site_refresh }}">
{% endif %}
<title>Docker Container CPU-Nutzung</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body style="background-color:#64a0d6">
<h1>Docker Container CPU/MEM from netdata: <a href="{{ netdata_host }}" target="_blank">{{ netdata_host }}</a></h1>
<div id="plot"></div>
<script>
var graphs = {{ plot | safe }};
Plotly.plot('plot', graphs, {});
</script>
</body>
</html>