Update flask_server.py

This commit is contained in:
MacRimi
2025-11-04 14:24:34 +01:00
parent 73a170a5f1
commit 65fd847251

View File

@@ -1202,136 +1202,96 @@ def get_pcie_link_speed(disk_name):
try: try:
# For NVMe drives, get PCIe information from sysfs # For NVMe drives, get PCIe information from sysfs
if disk_name.startswith('nvme'): if disk_name.startswith('nvme'):
# Extract controller number (nvme0, nvme1, etc.) controller = disk_name.split('n')[0] # nvme0n1 -> nvme0
controller = disk_name.rstrip('n0123456789p')
print(f"[v0] Getting PCIe info for {disk_name}, controller: {controller}")
# Path to PCIe device in sysfs # Path to PCIe device in sysfs
sys_path = f'/sys/class/nvme/{controller}/device' sys_path = f'/sys/class/nvme/{controller}/device'
print(f"[v0] Checking sys_path: {sys_path}, exists: {os.path.exists(sys_path)}")
if os.path.exists(sys_path): if os.path.exists(sys_path):
# Get current link speed
try: try:
with open(f'{sys_path}/current_link_speed', 'r') as f: pci_address = os.path.basename(os.readlink(sys_path))
speed_str = f.read().strip() print(f"[v0] PCI address for {disk_name}: {pci_address}")
# Format: "8.0 GT/s PCIe" or "16 GT/s"
if 'GT/s' in speed_str:
gt_s = float(speed_str.split()[0])
# Convert GT/s to PCIe generation
# PCIe 1.0 = 2.5 GT/s, 2.0 = 5 GT/s, 3.0 = 8 GT/s, 4.0 = 16 GT/s, 5.0 = 32 GT/s
if gt_s <= 2.5:
pcie_info['pcie_gen'] = '1.0'
elif gt_s <= 5.0:
pcie_info['pcie_gen'] = '2.0'
elif gt_s <= 8.0:
pcie_info['pcie_gen'] = '3.0'
elif gt_s <= 16.0:
pcie_info['pcie_gen'] = '4.0'
else:
pcie_info['pcie_gen'] = '5.0'
except:
pass
# Get current link width # Use lspci to get detailed PCIe information
try: result = subprocess.run(['lspci', '-vvv', '-s', pci_address],
with open(f'{sys_path}/current_link_width', 'r') as f: capture_output=True, text=True, timeout=5)
width = f.read().strip() if result.returncode == 0:
pcie_info['pcie_width'] = f'x{width}' print(f"[v0] lspci output for {pci_address}:")
except: for line in result.stdout.split('\n'):
pass # Look for "LnkSta:" line which shows current link status
if 'LnkSta:' in line:
print(f"[v0] Found LnkSta: {line}")
# Example: "LnkSta: Speed 8GT/s, Width x4"
if 'Speed' in line:
speed_match = re.search(r'Speed\s+([\d.]+)GT/s', line)
if speed_match:
gt_s = float(speed_match.group(1))
if gt_s <= 2.5:
pcie_info['pcie_gen'] = '1.0'
elif gt_s <= 5.0:
pcie_info['pcie_gen'] = '2.0'
elif gt_s <= 8.0:
pcie_info['pcie_gen'] = '3.0'
elif gt_s <= 16.0:
pcie_info['pcie_gen'] = '4.0'
else:
pcie_info['pcie_gen'] = '5.0'
print(f"[v0] Current PCIe gen: {pcie_info['pcie_gen']}")
# Get maximum link speed if 'Width' in line:
try: width_match = re.search(r'Width\s+x(\d+)', line)
with open(f'{sys_path}/max_link_speed', 'r') as f: if width_match:
speed_str = f.read().strip() pcie_info['pcie_width'] = f'x{width_match.group(1)}'
if 'GT/s' in speed_str: print(f"[v0] Current PCIe width: {pcie_info['pcie_width']}")
gt_s = float(speed_str.split()[0])
if gt_s <= 2.5:
pcie_info['pcie_max_gen'] = '1.0'
elif gt_s <= 5.0:
pcie_info['pcie_max_gen'] = '2.0'
elif gt_s <= 8.0:
pcie_info['pcie_max_gen'] = '3.0'
elif gt_s <= 16.0:
pcie_info['pcie_max_gen'] = '4.0'
else:
pcie_info['pcie_max_gen'] = '5.0'
except:
pass
# Get maximum link width # Look for "LnkCap:" line which shows maximum capabilities
try: elif 'LnkCap:' in line:
with open(f'{sys_path}/max_link_width', 'r') as f: print(f"[v0] Found LnkCap: {line}")
width = f.read().strip() if 'Speed' in line:
pcie_info['pcie_max_width'] = f'x{width}' speed_match = re.search(r'Speed\s+([\d.]+)GT/s', line)
except: if speed_match:
pass gt_s = float(speed_match.group(1))
if gt_s <= 2.5:
pcie_info['pcie_max_gen'] = '1.0'
elif gt_s <= 5.0:
pcie_info['pcie_max_gen'] = '2.0'
elif gt_s <= 8.0:
pcie_info['pcie_max_gen'] = '3.0'
elif gt_s <= 16.0:
pcie_info['pcie_max_gen'] = '4.0'
else:
pcie_info['pcie_max_gen'] = '5.0'
print(f"[v0] Max PCIe gen: {pcie_info['pcie_max_gen']}")
# Alternative method using lspci if sysfs doesn't work if 'Width' in line:
if not pcie_info['pcie_gen']: width_match = re.search(r'Width\s+x(\d+)', line)
try: if width_match:
# Get PCI address for this NVMe device pcie_info['pcie_max_width'] = f'x{width_match.group(1)}'
pci_address = os.path.basename(os.readlink(f'{sys_path}')) print(f"[v0] Max PCIe width: {pcie_info['pcie_max_width']}")
else:
# Use lspci to get detailed PCIe information print(f"[v0] lspci failed with return code: {result.returncode}")
result = subprocess.run(['lspci', '-vvv', '-s', pci_address], except Exception as e:
capture_output=True, text=True, timeout=5) print(f"[v0] Error getting PCIe info via lspci: {e}")
if result.returncode == 0: import traceback
for line in result.stdout.split('\n'): traceback.print_exc()
# Look for "LnkSta:" line which shows current link status else:
if 'LnkSta:' in line: print(f"[v0] sys_path does not exist: {sys_path}")
# Example: "LnkSta: Speed 8GT/s, Width x4" alt_sys_path = f'/sys/block/{disk_name}/device/device'
if 'Speed' in line: print(f"[v0] Trying alternative path: {alt_sys_path}, exists: {os.path.exists(alt_sys_path)}")
speed_match = re.search(r'Speed\s+([\d.]+)GT/s', line)
if speed_match:
gt_s = float(speed_match.group(1))
if gt_s <= 2.5:
pcie_info['pcie_gen'] = '1.0'
elif gt_s <= 5.0:
pcie_info['pcie_gen'] = '2.0'
elif gt_s <= 8.0:
pcie_info['pcie_gen'] = '3.0'
elif gt_s <= 16.0:
pcie_info['pcie_gen'] = '4.0'
else:
pcie_info['pcie_gen'] = '5.0'
if 'Width' in line:
width_match = re.search(r'Width\s+x(\d+)', line)
if width_match:
pcie_info['pcie_width'] = f'x{width_match.group(1)}'
# Look for "LnkCap:" line which shows maximum capabilities
elif 'LnkCap:' in line:
if 'Speed' in line:
speed_match = re.search(r'Speed\s+([\d.]+)GT/s', line)
if speed_match:
gt_s = float(speed_match.group(1))
if gt_s <= 2.5:
pcie_info['pcie_max_gen'] = '1.0'
elif gt_s <= 5.0:
pcie_info['pcie_max_gen'] = '2.0'
elif gt_s <= 8.0:
pcie_info['pcie_max_gen'] = '3.0'
elif gt_s <= 16.0:
pcie_info['pcie_max_gen'] = '4.0'
else:
pcie_info['pcie_max_gen'] = '5.0'
if 'Width' in line:
width_match = re.search(r'Width\s+x(\d+)', line)
if width_match:
pcie_info['pcie_max_width'] = f'x{width_match.group(1)}'
except Exception as e:
# print(f"[v0] Error getting PCIe info via lspci for {disk_name}: {e}")
pass
except Exception as e: except Exception as e:
# print(f"[v0] Error getting PCIe link speed for {disk_name}: {e}") print(f"[v0] Error in get_pcie_link_speed for {disk_name}: {e}")
pass import traceback
traceback.print_exc()
print(f"[v0] Final PCIe info for {disk_name}: {pcie_info}")
return pcie_info return pcie_info
# END OF ADDED FUNCTION # END OF CHANGES FOR get_pcie_link_speed
def get_smart_data(disk_name): def get_smart_data(disk_name):
"""Get SMART data for a specific disk - Enhanced with multiple device type attempts""" """Get SMART data for a specific disk - Enhanced with multiple device type attempts"""
@@ -3539,7 +3499,7 @@ def get_detailed_gpu_info(gpu):
mem_clock = clocks['GFX_MCLK'] mem_clock = clocks['GFX_MCLK']
if 'value' in mem_clock: if 'value' in mem_clock:
detailed_info['clock_memory'] = f"{mem_clock['value']} MHz" detailed_info['clock_memory'] = f"{mem_clock['value']} MHz"
# print(f"[v0] Memory Clock: {detailed_info['clock_memory']}", flush=True) # print(f"[v0] Memory Clock: {detailed_info['clock_memory']} MHz", flush=True)
pass pass
data_retrieved = True data_retrieved = True
@@ -4890,7 +4850,7 @@ def api_network_interface_metrics(interface_name):
for point in all_data: for point in all_data:
filtered_point = {'time': point.get('time')} filtered_point = {'time': point.get('time')}
# Add network fields if they exist # Add network fields if they exist
for key in ['netin', 'netout', 'diskread', 'diskwrite']: for key in ['netin', 'netout']:
if key in point: if key in point:
filtered_point[key] = point[key] filtered_point[key] = point[key]
rrd_data.append(filtered_point) rrd_data.append(filtered_point)
@@ -5802,10 +5762,6 @@ def api_prometheus():
mem_used_bytes = mem_used * 1024 * 1024 # Convert MiB to bytes mem_used_bytes = mem_used * 1024 * 1024 # Convert MiB to bytes
mem_total_bytes = mem_total * 1024 * 1024 mem_total_bytes = mem_total * 1024 * 1024
metrics.append(f'# HELP proxmox_gpu_memory_used_bytes GPU memory used in bytes')
metrics.append(f'# TYPE proxmox_gpu_memory_used_bytes gauge')
metrics.append(f'proxmox_gpu_memory_used_bytes{{node="{node}",gpu="{gpu_name}",vendor="{gpu_vendor}",slot="{gpu_slot}"}} {mem_used_bytes} {timestamp}')
metrics.append(f'# HELP proxmox_gpu_memory_total_bytes GPU memory total in bytes') metrics.append(f'# HELP proxmox_gpu_memory_total_bytes GPU memory total in bytes')
metrics.append(f'# TYPE proxmox_gpu_memory_total_bytes gauge') metrics.append(f'# TYPE proxmox_gpu_memory_total_bytes gauge')
metrics.append(f'proxmox_gpu_memory_total_bytes{{node="{node}",gpu="{gpu_name}",vendor="{gpu_vendor}",slot="{gpu_slot}"}} {mem_total_bytes} {timestamp}') metrics.append(f'proxmox_gpu_memory_total_bytes{{node="{node}",gpu="{gpu_name}",vendor="{gpu_vendor}",slot="{gpu_slot}"}} {mem_total_bytes} {timestamp}')