Added RRD Tool plugins

This commit is contained in:
Donald Zou
2025-08-13 16:35:34 +08:00
parent 93baa505c7
commit 4d07845c7f
5 changed files with 70 additions and 2 deletions

View File

@@ -1447,6 +1447,8 @@ DashboardLogger: DashboardLogger = DashboardLogger()
InitWireguardConfigurationsList(startup=True) InitWireguardConfigurationsList(startup=True)
import plugins.rrd_data.main as rrd_data
with app.app_context(): with app.app_context():
DashboardClients: DashboardClients = DashboardClients(WireguardConfigurations) DashboardClients: DashboardClients = DashboardClients(WireguardConfigurations)
app.register_blueprint(createClientBlueprint(WireguardConfigurations, DashboardConfig, DashboardClients)) app.register_blueprint(createClientBlueprint(WireguardConfigurations, DashboardConfig, DashboardClients))
@@ -1457,8 +1459,11 @@ def startThreads():
scheduleJobThread = threading.Thread(target=peerJobScheduleBackgroundThread, daemon=True) scheduleJobThread = threading.Thread(target=peerJobScheduleBackgroundThread, daemon=True)
scheduleJobThread.start() scheduleJobThread.start()
t = threading.Thread(target=rrd_data.main, args=(WireguardConfigurations,), daemon=True)
t.start()
if __name__ == "__main__": if __name__ == "__main__":
startThreads() startThreads()
# logging.getLogger().addHandler(logging.StreamHandler()) # logging.getLogger().addHandler(logging.StreamHandler())
app.logger.addHandler(logging.StreamHandler()) app.logger.addHandler(logging.StreamHandler())
app.run(host=app_ip, debug=False, port=app_port) app.run(host=app_ip, debug=False, port=app_port)

View File

@@ -627,7 +627,7 @@ class WireguardConfiguration:
latestHandshake = latestHandshake.decode("UTF-8").split() latestHandshake = latestHandshake.decode("UTF-8").split()
count = 0 count = 0
now = datetime.now() now = datetime.now()
time_delta = timedelta(minutes=2) time_delta = timedelta(minutes=3)
with self.engine.begin() as conn: with self.engine.begin() as conn:
for _ in range(int(len(latestHandshake) / 2)): for _ in range(int(len(latestHandshake) / 2)):

View File

@@ -0,0 +1,12 @@
import rrdtool
rrdtool.graph(
'daily_wg0.png',
'--start', '-300',
'--end', 'now',
'--title', 'Daily Traffic',
'DEF:in=./plugins/rrd_data/wg0.rrd:in:AVERAGE',
'DEF:out=./plugins/rrd_data/wg0.rrd:out:AVERAGE',
'LINE1:in#00FF00:Incoming Traffic',
'LINE1:out#0000FF:Outgoing Traffic'
)

View File

@@ -0,0 +1,50 @@
"""
Update this to fit your need, currently it is 300 seconds
"""
__INTERVAL = 10
def main(WireguardConfigurations: dict = None):
import os.path
from time import sleep, time
import rrdtool
while True:
for c in WireguardConfigurations.keys():
rrd_path = f"./plugins/rrd_data/{c}.rrd"
if not os.path.exists(rrd_path):
print(f"Creating RRD for {c}")
rrdtool.create(
rrd_path,
'--step', str(__INTERVAL),
f'DS:in:COUNTER:{__INTERVAL * 2}:0:U',
f'DS:out:COUNTER:{__INTERVAL * 2}:0:U',
'RRA:AVERAGE:0.5:1:8640',
'RRA:AVERAGE:0.5:12:720',
'RRA:AVERAGE:0.5:288:365',
'RRA:AVERAGE:0.5:2016:52',
'RRA:AVERAGE:0.5:8640:1'
)
configuration = WireguardConfigurations[c]
current_time = int(time())
json_data = configuration.toJson()
receive_gb = json_data["DataUsage"]["Receive"]
sent_gb = json_data["DataUsage"]["Sent"]
receive_bytes = int(receive_gb * (1024 ** 3))
sent_bytes = int(sent_gb * (1024 ** 3))
print(f"{c}: Receive={receive_gb}GB ({receive_bytes} bytes), Sent={sent_gb}GB ({sent_bytes} bytes)")
update_string = f'{current_time}:{receive_bytes}:{sent_bytes}'
print(f"Updating {c} with: {update_string}")
try:
rrdtool.update(rrd_path, update_string)
print(f"Successfully updated {c}")
except Exception as e:
print(f"Error updating {c}: {e}")
sleep(__INTERVAL)

View File

@@ -0,0 +1 @@
rrdtool