mirror of
https://github.com/donaldzou/WGDashboard.git
synced 2025-10-02 23:36:17 +00:00
Added RRD Tool plugins
This commit is contained in:
@@ -1447,6 +1447,8 @@ DashboardLogger: DashboardLogger = DashboardLogger()
|
||||
|
||||
InitWireguardConfigurationsList(startup=True)
|
||||
|
||||
import plugins.rrd_data.main as rrd_data
|
||||
|
||||
with app.app_context():
|
||||
DashboardClients: DashboardClients = DashboardClients(WireguardConfigurations)
|
||||
app.register_blueprint(createClientBlueprint(WireguardConfigurations, DashboardConfig, DashboardClients))
|
||||
@@ -1456,9 +1458,12 @@ def startThreads():
|
||||
bgThread.start()
|
||||
scheduleJobThread = threading.Thread(target=peerJobScheduleBackgroundThread, daemon=True)
|
||||
scheduleJobThread.start()
|
||||
|
||||
t = threading.Thread(target=rrd_data.main, args=(WireguardConfigurations,), daemon=True)
|
||||
t.start()
|
||||
|
||||
if __name__ == "__main__":
|
||||
startThreads()
|
||||
# logging.getLogger().addHandler(logging.StreamHandler())
|
||||
# logging.getLogger().addHandler(logging.StreamHandler())
|
||||
app.logger.addHandler(logging.StreamHandler())
|
||||
app.run(host=app_ip, debug=False, port=app_port)
|
@@ -627,7 +627,7 @@ class WireguardConfiguration:
|
||||
latestHandshake = latestHandshake.decode("UTF-8").split()
|
||||
count = 0
|
||||
now = datetime.now()
|
||||
time_delta = timedelta(minutes=2)
|
||||
time_delta = timedelta(minutes=3)
|
||||
|
||||
with self.engine.begin() as conn:
|
||||
for _ in range(int(len(latestHandshake) / 2)):
|
||||
|
12
src/plugins/rrd_data/daily_graph.py
Normal file
12
src/plugins/rrd_data/daily_graph.py
Normal 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'
|
||||
)
|
50
src/plugins/rrd_data/main.py
Normal file
50
src/plugins/rrd_data/main.py
Normal 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)
|
||||
|
1
src/plugins/rrd_data/requirements.txt
Normal file
1
src/plugins/rrd_data/requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
rrdtool
|
Reference in New Issue
Block a user