11from flask import Flask , render_template
22import os
3- import psutil , datetime
3+ import psutil
4+ import datetime
45
56app = Flask (__name__ )
67
78def get_established_connections ():
89 connection = psutil .net_connections ()
9-
1010 ipv4_dict = {}
1111 ipv6_dict = {}
1212
@@ -19,6 +19,15 @@ def get_established_connections():
1919
2020 return ipv4_dict , ipv6_dict
2121
22+ def change_up_time_format (uptime ):
23+ uptime_seconds = uptime .total_seconds ()
24+ days = int (uptime_seconds // (24 * 3600 ))
25+ uptime_seconds %= (24 * 3600 )
26+ hours = int (uptime_seconds // 3600 )
27+ uptime_seconds %= 3600
28+ minutes = int (uptime_seconds // 60 )
29+ return f"{ days } days, { hours } hours, { minutes } minutes"
30+
2231def get_system_info ():
2332 info = {
2433 'username' : os .getlogin (),
@@ -32,19 +41,59 @@ def get_system_info():
3241 'network_received' : round (psutil .net_io_counters ().bytes_recv / (1024 ** 2 ), 2 ), # In MB
3342 'process_count' : len (psutil .pids ()),
3443 'swap_memory' : psutil .swap_memory ().percent ,
35- 'uptime' : datetime .datetime .now () - datetime .datetime .fromtimestamp (psutil .boot_time ())
44+ 'uptime' : change_up_time_format ( datetime .datetime .now () - datetime .datetime .fromtimestamp (psutil .boot_time () ))
3645 }
3746
3847 ipv4_conn , ipv6_conn = get_established_connections ()
3948 info ['ipv4_connections' ] = ipv4_conn
4049 info ['ipv6_connections' ] = ipv6_conn
4150 return info
4251
43-
4452@app .route ('/' )
4553def dashboard ():
4654 system_info = get_system_info ()
4755 return render_template ('dashboard.html' , system_info = system_info )
4856
57+ @app .route ('/cpu_usage' )
58+ def cpu_usage ():
59+ # Detailed CPU usage stats
60+ cpu_usage = psutil .cpu_percent (interval = 1 , percpu = True )
61+ return render_template ('cpu_usage.html' , cpu_usage = cpu_usage )
62+
63+ @app .route ('/memory_usage' )
64+ def memory_usage ():
65+ memory_info = {
66+ 'memory_percent' : psutil .virtual_memory ().percent ,
67+ 'memory_available' : round (psutil .virtual_memory ().available / (1024 ** 3 ), 2 ), # In GB
68+ 'memory_used' : round (psutil .virtual_memory ().used / (1024 ** 3 ), 2 ) # In GB
69+ }
70+ return render_template ('memory_usage.html' , memory_info = memory_info )
71+
72+ @app .route ('/disk_usage' )
73+ def disk_usage ():
74+ disk_info = {
75+ 'disk_usage' : psutil .disk_usage ('/' ).percent ,
76+ 'disk_total' : round (psutil .disk_usage ('/' ).total / (1024 ** 3 ), 2 ), # In GB
77+ 'disk_used' : round (psutil .disk_usage ('/' ).used / (1024 ** 3 ), 2 ), # In GB
78+ 'disk_free' : round (psutil .disk_usage ('/' ).free / (1024 ** 3 ), 2 ) # In GB
79+ }
80+ return render_template ('disk_usage.html' , disk_info = disk_info )
81+
82+ @app .route ('/network_stats' )
83+ def network_stats ():
84+ net_io = psutil .net_io_counters ()
85+ network_info = {
86+ 'network_sent' : round (net_io .bytes_sent / (1024 ** 2 ), 2 ), # In MB
87+ 'network_received' : round (net_io .bytes_recv / (1024 ** 2 ), 2 ) # In MB
88+ }
89+ return render_template ('network_stats.html' , network_info = network_info )
90+
91+ @app .route ('/system_health' )
92+ def system_health ():
93+ # Reuse system_info function for summary
94+ system_info = get_system_info ()
95+ return render_template ('system_health.html' , system_info = system_info )
96+
97+
4998if __name__ == '__main__' :
5099 app .run (host = '0.0.0.0' , port = 5000 , debug = True )
0 commit comments