From 2d48e87893e008237f557775cb91690aba7ae263 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Thu, 13 Mar 2025 10:50:11 +0100 Subject: [PATCH] ntp graphs --- src/ntp_routes.py | 216 +++++++++++++--------------------------------- 1 file changed, 59 insertions(+), 157 deletions(-) diff --git a/src/ntp_routes.py b/src/ntp_routes.py index 622f5a5..4752277 100644 --- a/src/ntp_routes.py +++ b/src/ntp_routes.py @@ -1,8 +1,6 @@ from flask import Flask, session, g, render_template_string, Response from loguru import logger import json -import plotly.express as px -import plotly.graph_objects as po import matplotlib.pyplot as plt import matplotlib.dates as mdates from matplotlib.ticker import ScalarFormatter @@ -17,160 +15,9 @@ from app import oidc -@app.route('/ntpserver') -def ntpserver(): - try: - dbh = psycopg.connect() - engine = sqlalchemy.create_engine("postgresql+psycopg://", creator=lambda: dbh) - query = """ - select time_bucket('5 minutes', time) as bucket, - device, - avg(cast(values->'rootdisp'->>'value' as float)) as rootdisp, - max(cast(values->'stratum'->>'value' as int)) as stratum - from measurements - where time >= date_trunc('day', now()) AND time < date_trunc('day', now()) + '1 day'::interval and - application = 'TSM' and attributes->>'Label' = 'david' - group by bucket, device - order by bucket, device - """ - - df = pd.read_sql(query, con=engine) - - fig = po.Figure() - fig.add_trace(po.Scatter(x=df['bucket'], y=df['rootdisp'], mode='lines', name='Root Dispersion', yaxis='y1', line=dict(color='red'))) - fig.add_trace(po.Scatter(x=df['bucket'], y=df['stratum'], mode='lines', name='Stratum', yaxis='y2', line=dict(color='blue'))) - - fig.update_layout( - title='NTP Server Numbers', - - # Linke Y-Achse - yaxis=dict( - title='Root Dispersion', - ticksuffix=' ms' - ), - - # Rechte Y-Achse - yaxis2=dict( - title='Stratum', - overlaying='y', # Legt die zweite Y-Achse über die erste - side='right', # Setzt sie auf die rechte Seite - tickmode='linear', # Stellt sicher, dass die Ticks in festen Intervallen sind - dtick=1, # Zeigt nur ganzzahlige Ticks - ), - - legend=dict(x=0.05, y=1) # Position der Legende - ) - - graph_html_1 = fig.to_html(full_html=False, default_height='30%') - - query = """ - select time_bucket('5 minutes', time) as bucket, - device, - avg(cast(values->'time-req-pkts'->>'value' as float)) as packets - from measurements - where time >= date_trunc('day', now()) AND time < date_trunc('day', now()) + '1 day'::interval and - application = 'SNMP' and attributes->>'Label' = 'david' - group by bucket, device - order by bucket, device - """ - df = pd.read_sql(query, con=engine) - fig_2 = px.line(df, x='bucket', y='packets') - fig_2.update_layout( - xaxis_title="", - yaxis_title="", - yaxis_ticksuffix="p/s", - title=f"Time Requests" - ) - graph_html_2 = fig_2.to_html(full_html=False, default_height='30%') - - query = """ - select time_bucket('5 minutes', time) as bucket, - device, - avg(cast(values->'load1'->>'value' as float)) as loadaverage1min - from measurements - where time >= date_trunc('day', now()) AND time < date_trunc('day', now()) + '1 day'::interval and - application = 'SNMP' and attributes->>'Label' = 'david' - group by bucket, device - order by bucket, device - """ - df = pd.read_sql(query, con=engine) - fig_3 = px.line(df, x='bucket', y='loadaverage1min') - fig_3.update_layout( - xaxis_title="", - yaxis_title="", - title=f"CPU Load" - ) - graph_html_3 = fig_3.to_html(full_html=False, default_height='30%') - - return render_template_string(f""" - - - NTP Server Numbers - - - {graph_html_1} - {graph_html_2} - {graph_html_3} - - - """) - except Exception as e: - raise Exception(f"Error when querying NTP server values: {e}") - finally: - if dbh is not None: - dbh.close() - - -@app.route('/plot.png') -def plot_png(): - dbh = psycopg.connect() - engine = sqlalchemy.create_engine("postgresql+psycopg://", creator=lambda: dbh) - query = """ - select time_bucket('5 minutes', time) as bucket, - device, - avg(cast(values->'rootdisp'->>'value' as float)) as rootdisp, - max(cast(values->'stratum'->>'value' as int)) as stratum - from measurements - where time >= date_trunc('day', now()) AND time < date_trunc('day', now()) + '1 day'::interval and - application = 'SNMP' and attributes->>'Label' = 'harrison' - group by bucket, device - order by bucket, device - """ - df = pd.read_sql(query, con=engine) - df['rootdisp'] = df['rootdisp'] / 1e6 - - plot_date = df['bucket'].dt.date.iloc[0] if not df.empty else "Unknown Date" - - fig, ax1 = plt.subplots(figsize=(10,5)) - - ax1.plot(df['bucket'], df['rootdisp'], 'r-', label='Root Dispersion') - ax1.set_xlabel('Time') - ax1.set_ylabel('Root Dispersion (ms)', color='r') - ax1.tick_params(axis='y', labelcolor='r') - - ax2 = ax1.twinx() - ax2.plot(df['bucket'], df['stratum'], 'b-', label='Stratum') - ax2.set_ylabel('Stratum', color='b') - ax2.tick_params(axis='y', labelcolor='b') - ax2.set_yticks(range(int(df['stratum'].min()), int(df['stratum'].max()) + 1)) - - fig.suptitle(f"harrison - {plot_date}") - fig.tight_layout() - - ax1.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) - fig.autofmt_xdate(rotation=45) - - img_io = io.BytesIO() - plt.savefig(img_io, format='png') - img_io.seek(0) - plt.close(fig) - - return Response(img_io, mimetype='image/png') - - -@app.route('/plot2.png') -def plot2_png(): +@app.route('/ntp/stratum-rootdisp.png') +def stratum_rootdisp_png(): dbh = psycopg.connect() engine = sqlalchemy.create_engine("postgresql+psycopg://", creator=lambda: dbh) query = """ @@ -206,7 +53,7 @@ def plot2_png(): ax1.set_ylabel('Root Dispersion (ms)', color='r') ax1.tick_params(axis='y', labelcolor='r') - ax2.plot(device_df['bucket'], device_df['stratum'], 'b--', label='Stratum') + ax2.plot(device_df['bucket'], device_df['stratum'], 'b-', label='Stratum') ax2.set_ylabel('Stratum', color='b') ax2.tick_params(axis='y', labelcolor='b') ax2.set_yticks(range(int(device_df['stratum'].min()), int(device_df['stratum'].max()) + 1)) @@ -216,7 +63,62 @@ def plot2_png(): ax1.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) fig.autofmt_xdate(rotation=45) - fig.suptitle(f'NTP Server Comparison - {plot_date}') + fig.suptitle(f'Stratum and Root Dispersion - {plot_date}') + fig.tight_layout() + + img_io = io.BytesIO() + plt.savefig(img_io, format='png') + img_io.seek(0) + plt.close(fig) + + return Response(img_io, mimetype='image/png') + + +@app.route('/ntp/packets-load.png') +def packets_load_png(): + dbh = psycopg.connect() + engine = sqlalchemy.create_engine("postgresql+psycopg://", creator=lambda: dbh) + query = """ + select time_bucket('5 minutes', time) as bucket, + attributes->>'Label' as device, + avg(cast(values->'load1'->>'value' as float)) as load, + avg(cast(values->'processed-pkts'->>'value' as int)) as packets + from measurements + where time >= date_trunc('day', now()) AND time < date_trunc('day', now()) + '1 day'::interval and + application = 'SNMP' and attributes->>'Label' IN ('harrison', 'david') + group by bucket, attributes->>'Label' + order by bucket, attributes->>'Label' + """ + df = pd.read_sql(query, con=engine) + + + # Extract date for title + plot_date = df['bucket'].dt.date.iloc[0] if not df.empty else "Unknown Date" + + # Create figure with two side-by-side subplots + fig, axes = plt.subplots(1, 2, figsize=(15, 5), sharex=True) + + for i, device in enumerate(['harrison', 'david']): + ax1 = axes[i] + ax2 = ax1.twinx() + + device_df = df[df['device'] == device] + + ax1.plot(device_df['bucket'], device_df['load'], 'r-', label='CPU Load') + ax1.set_xlabel('Time') + ax1.set_ylabel('Load', color='r') + ax1.tick_params(axis='y', labelcolor='r') + + ax2.plot(device_df['bucket'], device_df['packets'], 'b-', label='Processed Packets') + ax2.set_ylabel('Packets', color='b') + ax2.tick_params(axis='y', labelcolor='b') + + ax1.set_title(f'{device.capitalize()}') + + ax1.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) + fig.autofmt_xdate(rotation=45) + + fig.suptitle(f'CPU Load and Processed Packets - {plot_date}') fig.tight_layout() img_io = io.BytesIO()