@@ -87,6 +87,43 @@ def get_memory_stats():
87
87
88
88
return total_ram , used_ram , total_swap , used_swap
89
89
90
+ def get_disk_info ():
91
+ """Function to get disk usage statistics"""
92
+ result = subprocess .run (["df" , "-h" , "/" ], stdout = subprocess .PIPE , text = True )
93
+ lines = result .stdout .strip ().split ('\n ' )
94
+ parts = lines [1 ].split ()
95
+
96
+ return {
97
+ "Total Space" : parts [1 ],
98
+ "Used Space" : parts [2 ],
99
+ "Available Space" : parts [3 ],
100
+ "Usage Percentage" : parts [4 ]
101
+ }
102
+
103
+ def get_system_info ():
104
+ """Function to get system information"""
105
+ model_result = subprocess .run (["cat" , "/proc/device-tree/model" ], stdout = subprocess .PIPE , text = True )
106
+ kernel_result = subprocess .run (["uname" , "-r" ], stdout = subprocess .PIPE , text = True )
107
+ os_result = subprocess .run (["cat" , "/etc/os-release" ], stdout = subprocess .PIPE , text = True )
108
+
109
+ # Parse OS release info
110
+ os_info = {}
111
+ for line in os_result .stdout .strip ().split ('\n ' ):
112
+ if '=' in line :
113
+ key , value = line .split ('=' , 1 )
114
+ os_info [key ] = value .strip ('"' )
115
+
116
+ return {
117
+ "Model" : model_result .stdout .strip ().replace ('\x00 ' , '' ),
118
+ "Kernel Version" : kernel_result .stdout .strip (),
119
+ "OS" : os_info .get ('PRETTY_NAME' , 'Unknown' )
120
+ }
121
+
122
+ def get_uptime_info ():
123
+ """Function to get system uptime"""
124
+ result = subprocess .run (["uptime" , "-p" ], stdout = subprocess .PIPE , text = True )
125
+ return result .stdout .strip ()
126
+
90
127
@app .route ("/" )
91
128
@limiter .limit ("2 per 3 seconds" )
92
129
def index ():
@@ -103,7 +140,11 @@ def api():
103
140
"/api/time" : "Get current server time" ,
104
141
"/api/mem" : "Get memory statistics" ,
105
142
"/api/cpu" : "Get CPU usage" ,
143
+ "/api/disk" : "Get disk usage statistics" ,
144
+ "/api/uptime" : "Get system uptime" ,
145
+ "/api/system" : "Get system information" ,
106
146
"/api/shutdown" : "Authorize shutdown" ,
147
+ "/api/reboot" : "Authorize system reboot" ,
107
148
"/api/update" : "Authorize system update" ,
108
149
"/api/all" : "Get all system statistics"
109
150
},
@@ -138,6 +179,25 @@ def api_cpu():
138
179
"SoC Temperature" : temp
139
180
})
140
181
182
+ @app .route ("/api/disk" , methods = ['GET' ])
183
+ @limiter .limit ("5 per minute" )
184
+ def api_disk ():
185
+ """Return disk usage statistics as JSON"""
186
+ return jsonify (get_disk_info ())
187
+
188
+ @app .route ("/api/uptime" , methods = ['GET' ])
189
+ @limiter .limit ("5 per minute" )
190
+ def api_uptime ():
191
+ """Return system uptime as JSON"""
192
+ uptime = get_uptime_info ()
193
+ return jsonify ({"System Uptime" : uptime })
194
+
195
+ @app .route ("/api/system" , methods = ['GET' ])
196
+ @limiter .limit ("2 per minute" )
197
+ def api_system ():
198
+ """Return system information as JSON"""
199
+ return jsonify (get_system_info ())
200
+
141
201
@app .route ("/api/shutdown" , methods = ['POST' ])
142
202
@limiter .limit ("5 per hour" )
143
203
def api_shutdown ():
@@ -150,6 +210,16 @@ def api_shutdown():
150
210
return jsonify ({"message" : "System shutting down in 1 minute" }), 200
151
211
return jsonify ({"error" : "Unauthorized" }), 401
152
212
213
+ @app .route ("/api/reboot" , methods = ['POST' ])
214
+ @limiter .limit ("5 per hour" )
215
+ def api_reboot ():
216
+ """Authenticate using API key and reboot the system"""
217
+ api_key = request .headers .get ('x-api-key' )
218
+ if api_key == API_KEY :
219
+ r = subprocess .run (["reboot" ], stdout = subprocess .PIPE , text = True )
220
+ return jsonify ({"message" : "System rebooting now" }), 200
221
+ return jsonify ({"error" : "Unauthorized" }), 401
222
+
153
223
@app .route ("/api/update" , methods = ['POST' ])
154
224
@limiter .limit ("3 per hour" )
155
225
def api_update ():
@@ -168,11 +238,15 @@ def api_update():
168
238
@limiter .limit ("1 per second" )
169
239
def api_plain ():
170
240
"""Collect system statistics and return as JSON (original endpoint /api)"""
241
+ # Use existing functions for all metrics (just to DRY)
171
242
time = get_current_time ()
172
243
ipv4 = get_ipv4_addr ()
173
244
cpu = get_cpu_usage ()
174
245
temp = get_soc_temp ()
175
246
total_ram , used_ram , total_swap , used_swap = get_memory_stats ()
247
+ disk_info = get_disk_info ()
248
+ uptime = get_uptime_info ()
249
+ system_info = get_system_info ()
176
250
177
251
data = {
178
252
"Current Time" : time ,
@@ -182,7 +256,15 @@ def api_plain():
182
256
"Total RAM" : f"{ total_ram :.0f} MiB" ,
183
257
"Used RAM" : f"{ used_ram :.0f} " ,
184
258
"Total Swap" : f"{ total_swap :.0f} MiB" ,
185
- "Used Swap" : f"{ used_swap :.0f} "
259
+ "Used Swap" : f"{ used_swap :.0f} " ,
260
+ "System Uptime" : uptime ,
261
+ "Disk Total" : disk_info ["Total Space" ],
262
+ "Disk Used" : disk_info ["Used Space" ],
263
+ "Disk Available" : disk_info ["Available Space" ],
264
+ "Disk Usage" : disk_info ["Usage Percentage" ],
265
+ "System Model" : system_info ["Model" ],
266
+ "Kernel Version" : system_info ["Kernel Version" ],
267
+ "OS" : system_info ["OS" ]
186
268
}
187
269
188
270
return jsonify (data )
0 commit comments