mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2025-10-11 04:16:17 +00:00
Update flask_server.py
This commit is contained in:
@@ -16,6 +16,7 @@ import time
|
|||||||
import socket
|
import socket
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
import re # Added for regex matching
|
import re # Added for regex matching
|
||||||
|
import select # Added for non-blocking read
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
CORS(app) # Enable CORS for Next.js frontend
|
CORS(app) # Enable CORS for Next.js frontend
|
||||||
@@ -1583,12 +1584,17 @@ def get_detailed_gpu_info(gpu):
|
|||||||
try:
|
try:
|
||||||
check_result = subprocess.run(['which', 'intel_gpu_top'], capture_output=True, timeout=1)
|
check_result = subprocess.run(['which', 'intel_gpu_top'], capture_output=True, timeout=1)
|
||||||
if check_result.returncode != 0:
|
if check_result.returncode != 0:
|
||||||
# Tool not found
|
|
||||||
detailed_info['has_monitoring_tool'] = False
|
detailed_info['has_monitoring_tool'] = False
|
||||||
print(f"[v0] intel_gpu_top not found for Intel GPU")
|
print(f"[v0] intel_gpu_top not found for Intel GPU")
|
||||||
return detailed_info
|
return detailed_info
|
||||||
else:
|
|
||||||
print(f"[v0] intel_gpu_top found for Intel GPU")
|
import os
|
||||||
|
if not os.path.exists('/dev/dri/card0'):
|
||||||
|
detailed_info['has_monitoring_tool'] = False
|
||||||
|
print(f"[v0] /dev/dri/card0 not accessible - intel_gpu_top cannot run")
|
||||||
|
return detailed_info
|
||||||
|
|
||||||
|
print(f"[v0] intel_gpu_top found and /dev/dri/card0 accessible")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[v0] Error checking for intel_gpu_top: {e}")
|
print(f"[v0] Error checking for intel_gpu_top: {e}")
|
||||||
detailed_info['has_monitoring_tool'] = False
|
detailed_info['has_monitoring_tool'] = False
|
||||||
@@ -1596,43 +1602,37 @@ def get_detailed_gpu_info(gpu):
|
|||||||
|
|
||||||
data_retrieved = False
|
data_retrieved = False
|
||||||
try:
|
try:
|
||||||
# Start intel_gpu_top process with JSON output to stdout
|
result = subprocess.run(
|
||||||
process = subprocess.Popen(
|
['intel_gpu_top', '-J', '-s', '500', '-o', '-'],
|
||||||
['intel_gpu_top', '-J', '-s', '1000', '-o', '-'],
|
capture_output=True,
|
||||||
stdout=subprocess.PIPE,
|
text=True,
|
||||||
stderr=subprocess.PIPE,
|
timeout=1.5 # Reduced from 2 seconds
|
||||||
text=True
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Wait for output with increased timeout
|
output = result.stdout.strip()
|
||||||
try:
|
|
||||||
stdout, stderr = process.communicate(timeout=15)
|
|
||||||
|
|
||||||
if stdout:
|
if output and result.returncode == 0:
|
||||||
output = stdout.strip()
|
print(f"[v0] intel_gpu_top output received ({len(output)} chars)")
|
||||||
print(f"[v0] intel_gpu_top output received: {output[:300]}...")
|
print(f"[v0] First 500 chars: {output[:500]}")
|
||||||
|
|
||||||
# Try to parse as JSON
|
# Try to parse as JSON
|
||||||
try:
|
try:
|
||||||
import json
|
# Find first complete JSON object
|
||||||
# The output may contain multiple JSON objects, get the first one
|
brace_count = 0
|
||||||
json_str = output
|
json_end = -1
|
||||||
# Find first complete JSON object
|
for i, char in enumerate(output):
|
||||||
brace_count = 0
|
if char == '{':
|
||||||
json_end = -1
|
brace_count += 1
|
||||||
for i, char in enumerate(output):
|
elif char == '}':
|
||||||
if char == '{':
|
brace_count -= 1
|
||||||
brace_count += 1
|
if brace_count == 0:
|
||||||
elif char == '}':
|
json_end = i + 1
|
||||||
brace_count -= 1
|
break
|
||||||
if brace_count == 0:
|
|
||||||
json_end = i + 1
|
|
||||||
break
|
|
||||||
|
|
||||||
if json_end > 0:
|
|
||||||
json_str = output[:json_end]
|
|
||||||
|
|
||||||
|
if json_end > 0:
|
||||||
|
json_str = output[:json_end]
|
||||||
json_data = json.loads(json_str)
|
json_data = json.loads(json_str)
|
||||||
|
print(f"[v0] Parsed JSON successfully with keys: {list(json_data.keys())}")
|
||||||
|
|
||||||
# Parse frequency data
|
# Parse frequency data
|
||||||
if 'frequency' in json_data:
|
if 'frequency' in json_data:
|
||||||
@@ -1681,63 +1681,52 @@ def get_detailed_gpu_info(gpu):
|
|||||||
detailed_info[key] = float(busy_value)
|
detailed_info[key] = float(busy_value)
|
||||||
data_retrieved = True
|
data_retrieved = True
|
||||||
|
|
||||||
print(f"[v0] Intel GPU parsed JSON data successfully: {detailed_info}")
|
print(f"[v0] Intel GPU data retrieved successfully")
|
||||||
|
|
||||||
except (json.JSONDecodeError, ValueError) as e:
|
except (json.JSONDecodeError, ValueError) as e:
|
||||||
print(f"[v0] JSON parsing failed: {e}, using text parsing")
|
print(f"[v0] JSON parsing failed: {e}")
|
||||||
|
# Fallback to text parsing
|
||||||
|
# Parse frequency: "0/ 0 MHz"
|
||||||
|
freq_match = re.search(r'(\d+)/\s*(\d+)\s*MHz', output)
|
||||||
|
if freq_match:
|
||||||
|
detailed_info['clock_graphics'] = f"{freq_match.group(1)} MHz"
|
||||||
|
detailed_info['clock_max'] = f"{freq_match.group(2)} MHz"
|
||||||
|
data_retrieved = True
|
||||||
|
|
||||||
# Fallback to text parsing if JSON fails
|
# Parse power: "0.00/ 7.23 W"
|
||||||
# Parse frequency: "0/ 0 MHz"
|
power_match = re.search(r'([\d.]+)/\s*([\d.]+)\s*W', output)
|
||||||
freq_match = re.search(r'(\d+)/\s*(\d+)\s*MHz', output)
|
if power_match:
|
||||||
if freq_match:
|
detailed_info['power_draw'] = f"{power_match.group(1)} W"
|
||||||
detailed_info['clock_graphics'] = f"{freq_match.group(1)} MHz"
|
detailed_info['power_limit'] = f"{power_match.group(2)} W"
|
||||||
detailed_info['clock_max'] = f"{freq_match.group(2)} MHz"
|
data_retrieved = True
|
||||||
|
|
||||||
|
# Parse RC6 (power saving state): "100% RC6"
|
||||||
|
rc6_match = re.search(r'(\d+)%\s*RC6', output)
|
||||||
|
if rc6_match:
|
||||||
|
detailed_info['power_state'] = f"RC6: {rc6_match.group(1)}%"
|
||||||
|
data_retrieved = True
|
||||||
|
|
||||||
|
# Parse engine utilization
|
||||||
|
engines = {
|
||||||
|
'Render/3D': 'engine_render',
|
||||||
|
'Blitter': 'engine_blitter',
|
||||||
|
'Video': 'engine_video',
|
||||||
|
'VideoEnhance': 'engine_video_enhance'
|
||||||
|
}
|
||||||
|
for engine_name, key in engines.items():
|
||||||
|
pattern = rf'{engine_name}\s+([\d.]+)%'
|
||||||
|
match = re.search(pattern, output)
|
||||||
|
if match:
|
||||||
|
detailed_info[key] = float(match.group(1))
|
||||||
data_retrieved = True
|
data_retrieved = True
|
||||||
|
|
||||||
# Parse power: "0.00/ 7.23 W"
|
# Parse IRQ rate
|
||||||
power_match = re.search(r'([\d.]+)/\s*([\d.]+)\s*W', output)
|
irq_match = re.search(r'(\d+)\s*irqs/s', output)
|
||||||
if power_match:
|
if irq_match:
|
||||||
detailed_info['power_draw'] = f"{power_match.group(1)} W"
|
detailed_info['irq_rate'] = int(irq_match.group(1))
|
||||||
detailed_info['power_limit'] = f"{power_match.group(2)} W"
|
data_retrieved = True
|
||||||
data_retrieved = True
|
else:
|
||||||
|
print(f"[v0] No output received from intel_gpu_top (return code: {result.returncode})")
|
||||||
# Parse RC6 (power saving state): "100% RC6"
|
|
||||||
rc6_match = re.search(r'(\d+)%\s*RC6', output)
|
|
||||||
if rc6_match:
|
|
||||||
detailed_info['power_state'] = f"RC6: {rc6_match.group(1)}%"
|
|
||||||
data_retrieved = True
|
|
||||||
|
|
||||||
# Parse engine utilization
|
|
||||||
engines = {
|
|
||||||
'Render/3D': 'engine_render',
|
|
||||||
'Blitter': 'engine_blitter',
|
|
||||||
'Video': 'engine_video',
|
|
||||||
'VideoEnhance': 'engine_video_enhance'
|
|
||||||
}
|
|
||||||
for engine_name, key in engines.items():
|
|
||||||
pattern = rf'{engine_name}\s+([\d.]+)%'
|
|
||||||
match = re.search(pattern, output)
|
|
||||||
if match:
|
|
||||||
detailed_info[key] = float(match.group(1))
|
|
||||||
data_retrieved = True
|
|
||||||
|
|
||||||
# Parse IRQ rate
|
|
||||||
irq_match = re.search(r'(\d+)\s*irqs/s', output)
|
|
||||||
if irq_match:
|
|
||||||
detailed_info['irq_rate'] = int(irq_match.group(1))
|
|
||||||
data_retrieved = True
|
|
||||||
|
|
||||||
except subprocess.TimeoutExpired:
|
|
||||||
print(f"[v0] intel_gpu_top process timed out after 15 seconds, terminating...")
|
|
||||||
process.terminate()
|
|
||||||
try:
|
|
||||||
process.wait(timeout=1)
|
|
||||||
except subprocess.TimeoutExpired:
|
|
||||||
process.kill()
|
|
||||||
process.wait()
|
|
||||||
detailed_info['has_monitoring_tool'] = False
|
|
||||||
print(f"[v0] intel_gpu_top timed out - marking tool as unavailable")
|
|
||||||
return detailed_info
|
|
||||||
|
|
||||||
if data_retrieved:
|
if data_retrieved:
|
||||||
detailed_info['has_monitoring_tool'] = True
|
detailed_info['has_monitoring_tool'] = True
|
||||||
@@ -1746,8 +1735,13 @@ def get_detailed_gpu_info(gpu):
|
|||||||
detailed_info['has_monitoring_tool'] = False
|
detailed_info['has_monitoring_tool'] = False
|
||||||
print(f"[v0] Intel GPU monitoring failed - no data retrieved")
|
print(f"[v0] Intel GPU monitoring failed - no data retrieved")
|
||||||
|
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
print(f"[v0] intel_gpu_top timed out after 1.5 seconds - marking tool as unavailable")
|
||||||
|
detailed_info['has_monitoring_tool'] = False
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[v0] Error getting Intel GPU details: {e}")
|
print(f"[v0] Error getting Intel GPU details: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
detailed_info['has_monitoring_tool'] = False
|
detailed_info['has_monitoring_tool'] = False
|
||||||
|
|
||||||
elif vendor == 'NVIDIA':
|
elif vendor == 'NVIDIA':
|
||||||
@@ -2876,4 +2870,7 @@ if __name__ == '__main__':
|
|||||||
print("Server will be accessible on all network interfaces (0.0.0.0:8008)")
|
print("Server will be accessible on all network interfaces (0.0.0.0:8008)")
|
||||||
print("API endpoints available at: /api/system, /api/storage, /api/network, /api/vms, /api/logs, /api/health, /api/hardware")
|
print("API endpoints available at: /api/system, /api/storage, /api/network, /api/vms, /api/logs, /api/health, /api/hardware")
|
||||||
|
|
||||||
|
app.run(host='0.0.0.0', port=8008, debug=False)
|
||||||
|
, /api/logs, /api/health, /api/hardware")
|
||||||
|
|
||||||
app.run(host='0.0.0.0', port=8008, debug=False)
|
app.run(host='0.0.0.0', port=8008, debug=False)
|
||||||
|
Reference in New Issue
Block a user