2025-09-28 19:40:23 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
ProxMenux Flask Server
|
|
|
|
Provides REST API endpoints for Proxmox monitoring data
|
|
|
|
Runs on port 8008 and serves system metrics, storage info, network stats, etc.
|
|
|
|
Also serves the Next.js dashboard as static files
|
|
|
|
"""
|
|
|
|
|
|
|
|
from flask import Flask, jsonify, request, send_from_directory, send_file
|
|
|
|
from flask_cors import CORS
|
|
|
|
import psutil
|
|
|
|
import subprocess
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import socket
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
CORS(app) # Enable CORS for Next.js frontend
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def serve_dashboard():
|
2025-09-29 18:07:30 +02:00
|
|
|
"""Serve the main dashboard page from Next.js build"""
|
2025-09-28 19:40:23 +02:00
|
|
|
try:
|
2025-09-29 19:19:35 +02:00
|
|
|
# Detectar si estamos ejecutándose desde AppImage
|
|
|
|
appimage_root = os.environ.get('APPDIR')
|
|
|
|
if not appimage_root:
|
|
|
|
# Fallback: intentar detectar desde la ubicación del script
|
|
|
|
base_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
appimage_root = os.path.dirname(base_dir) # Subir un nivel desde usr/bin/
|
2025-09-29 18:58:53 +02:00
|
|
|
|
2025-09-29 18:07:30 +02:00
|
|
|
index_paths = [
|
2025-09-29 19:19:35 +02:00
|
|
|
os.path.join(appimage_root, 'web', 'index.html'), # Ruta principal para AppImage
|
|
|
|
os.path.join(appimage_root, 'usr', 'web', 'index.html'), # Fallback con usr/
|
2025-09-29 18:58:53 +02:00
|
|
|
os.path.join(appimage_root, 'web', 'out', 'index.html'), # Fallback si está en subcarpeta
|
2025-09-29 19:19:35 +02:00
|
|
|
os.path.join(appimage_root, 'usr', 'web', 'out', 'index.html'), # Fallback con usr/out/
|
2025-09-29 18:07:30 +02:00
|
|
|
]
|
|
|
|
|
2025-09-29 18:58:53 +02:00
|
|
|
print(f"[v0] Flask server looking for index.html in:")
|
|
|
|
for path in index_paths:
|
|
|
|
abs_path = os.path.abspath(path)
|
|
|
|
exists = os.path.exists(abs_path)
|
|
|
|
print(f"[v0] {abs_path} - {'EXISTS' if exists else 'NOT FOUND'}")
|
|
|
|
if exists:
|
|
|
|
print(f"[v0] Found index.html, serving from: {abs_path}")
|
|
|
|
return send_file(abs_path)
|
2025-09-29 18:07:30 +02:00
|
|
|
|
2025-09-29 18:58:53 +02:00
|
|
|
# If no Next.js build found, return error message with actual paths checked
|
|
|
|
actual_paths = [os.path.abspath(path) for path in index_paths]
|
|
|
|
return f'''
|
2025-09-29 18:07:30 +02:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head><title>ProxMenux Monitor - Build Error</title></head>
|
|
|
|
<body style="font-family: Arial; padding: 2rem; background: #0a0a0a; color: #fff;">
|
|
|
|
<h1>🚨 ProxMenux Monitor - Build Error</h1>
|
|
|
|
<p>Next.js application not found. The AppImage may not have been built correctly.</p>
|
|
|
|
<p>Expected paths checked:</p>
|
2025-09-29 18:58:53 +02:00
|
|
|
<ul>{''.join([f'<li>{path}</li>' for path in actual_paths])}</ul>
|
2025-09-29 18:07:30 +02:00
|
|
|
<p>API endpoints are still available:</p>
|
|
|
|
<ul>
|
|
|
|
<li><a href="/api/system" style="color: #4f46e5;">/api/system</a></li>
|
|
|
|
<li><a href="/api/system-info" style="color: #4f46e5;">/api/system-info</a></li>
|
|
|
|
<li><a href="/api/storage" style="color: #4f46e5;">/api/storage</a></li>
|
|
|
|
<li><a href="/api/network" style="color: #4f46e5;">/api/network</a></li>
|
|
|
|
<li><a href="/api/vms" style="color: #4f46e5;">/api/vms</a></li>
|
|
|
|
<li><a href="/api/health" style="color: #4f46e5;">/api/health</a></li>
|
|
|
|
</ul>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
''', 500
|
2025-09-28 19:40:23 +02:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error serving dashboard: {e}")
|
2025-09-29 18:07:30 +02:00
|
|
|
return jsonify({'error': f'Dashboard not available: {str(e)}'}), 500
|
2025-09-28 19:40:23 +02:00
|
|
|
|
|
|
|
@app.route('/manifest.json')
|
|
|
|
def serve_manifest():
|
|
|
|
"""Serve PWA manifest"""
|
2025-09-29 18:07:30 +02:00
|
|
|
try:
|
|
|
|
manifest_paths = [
|
|
|
|
os.path.join(os.path.dirname(__file__), '..', 'web', 'public', 'manifest.json'),
|
|
|
|
os.path.join(os.path.dirname(__file__), '..', 'public', 'manifest.json')
|
|
|
|
]
|
|
|
|
|
|
|
|
for manifest_path in manifest_paths:
|
|
|
|
if os.path.exists(manifest_path):
|
|
|
|
return send_file(manifest_path)
|
|
|
|
|
|
|
|
# Return default manifest if not found
|
|
|
|
return jsonify({
|
|
|
|
"name": "ProxMenux Monitor",
|
|
|
|
"short_name": "ProxMenux",
|
|
|
|
"description": "Proxmox System Monitoring Dashboard",
|
|
|
|
"start_url": "/",
|
|
|
|
"display": "standalone",
|
|
|
|
"background_color": "#0a0a0a",
|
|
|
|
"theme_color": "#4f46e5",
|
|
|
|
"icons": [
|
|
|
|
{
|
|
|
|
"src": "/images/proxmenux-logo.png",
|
|
|
|
"sizes": "256x256",
|
|
|
|
"type": "image/png"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error serving manifest: {e}")
|
|
|
|
return jsonify({}), 404
|
2025-09-28 19:40:23 +02:00
|
|
|
|
|
|
|
@app.route('/sw.js')
|
|
|
|
def serve_sw():
|
|
|
|
"""Serve service worker"""
|
|
|
|
return '''
|
|
|
|
const CACHE_NAME = 'proxmenux-v1';
|
|
|
|
const urlsToCache = [
|
|
|
|
'/',
|
|
|
|
'/api/system',
|
|
|
|
'/api/storage',
|
|
|
|
'/api/network',
|
|
|
|
'/api/health'
|
|
|
|
];
|
|
|
|
|
|
|
|
self.addEventListener('install', event => {
|
|
|
|
event.waitUntil(
|
|
|
|
caches.open(CACHE_NAME)
|
|
|
|
.then(cache => cache.addAll(urlsToCache))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener('fetch', event => {
|
|
|
|
event.respondWith(
|
|
|
|
caches.match(event.request)
|
|
|
|
.then(response => response || fetch(event.request))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
''', 200, {'Content-Type': 'application/javascript'}
|
|
|
|
|
2025-09-29 18:07:30 +02:00
|
|
|
@app.route('/_next/<path:filename>')
|
|
|
|
def serve_next_static(filename):
|
|
|
|
"""Serve Next.js static files"""
|
|
|
|
try:
|
2025-09-29 19:19:35 +02:00
|
|
|
appimage_root = os.environ.get('APPDIR')
|
|
|
|
if not appimage_root:
|
|
|
|
base_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
appimage_root = os.path.dirname(base_dir)
|
2025-09-29 18:58:53 +02:00
|
|
|
|
2025-09-29 18:16:01 +02:00
|
|
|
static_paths = [
|
2025-09-29 18:58:53 +02:00
|
|
|
os.path.join(appimage_root, 'web', '_next'), # Ruta principal
|
2025-09-29 19:19:35 +02:00
|
|
|
os.path.join(appimage_root, 'usr', 'web', '_next'), # Fallback con usr/
|
|
|
|
os.path.join(appimage_root, 'web', 'out', '_next'), # Fallback con out/
|
|
|
|
os.path.join(appimage_root, 'usr', 'web', 'out', '_next'), # Fallback con usr/out/
|
2025-09-29 18:16:01 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
for static_dir in static_paths:
|
|
|
|
file_path = os.path.join(static_dir, filename)
|
|
|
|
if os.path.exists(file_path):
|
|
|
|
return send_file(file_path)
|
2025-09-29 18:07:30 +02:00
|
|
|
return '', 404
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error serving Next.js static file {filename}: {e}")
|
|
|
|
return '', 404
|
|
|
|
|
2025-09-28 19:40:23 +02:00
|
|
|
@app.route('/<path:filename>')
|
|
|
|
def serve_static_files(filename):
|
|
|
|
"""Serve static files (icons, etc.)"""
|
|
|
|
try:
|
2025-09-29 19:19:35 +02:00
|
|
|
appimage_root = os.environ.get('APPDIR')
|
|
|
|
if not appimage_root:
|
|
|
|
base_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
appimage_root = os.path.dirname(base_dir)
|
2025-09-29 18:58:53 +02:00
|
|
|
|
2025-09-29 18:07:30 +02:00
|
|
|
public_paths = [
|
2025-09-29 18:58:53 +02:00
|
|
|
os.path.join(appimage_root, 'web'), # Raíz web para exportación estática
|
2025-09-29 19:19:35 +02:00
|
|
|
os.path.join(appimage_root, 'usr', 'web'), # Fallback con usr/
|
|
|
|
os.path.join(appimage_root, 'web', 'out'), # Fallback con out/
|
|
|
|
os.path.join(appimage_root, 'usr', 'web', 'out'), # Fallback con usr/out/
|
2025-09-29 18:07:30 +02:00
|
|
|
]
|
2025-09-28 19:40:23 +02:00
|
|
|
|
2025-09-29 18:07:30 +02:00
|
|
|
for public_dir in public_paths:
|
|
|
|
file_path = os.path.join(public_dir, filename)
|
|
|
|
if os.path.exists(file_path):
|
|
|
|
return send_from_directory(public_dir, filename)
|
2025-09-28 19:40:23 +02:00
|
|
|
|
|
|
|
return '', 404
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error serving static file {filename}: {e}")
|
|
|
|
return '', 404
|
|
|
|
|
|
|
|
@app.route('/images/<path:filename>')
|
|
|
|
def serve_images(filename):
|
|
|
|
"""Serve image files"""
|
|
|
|
try:
|
2025-10-01 17:10:37 +02:00
|
|
|
appimage_root = os.environ.get('APPDIR')
|
|
|
|
if not appimage_root:
|
|
|
|
base_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
appimage_root = os.path.dirname(base_dir)
|
|
|
|
|
2025-09-29 18:07:30 +02:00
|
|
|
image_paths = [
|
2025-10-01 17:10:37 +02:00
|
|
|
os.path.join(appimage_root, 'web', 'images'), # Ruta principal para exportación estática
|
|
|
|
os.path.join(appimage_root, 'usr', 'web', 'images'), # Fallback con usr/
|
|
|
|
os.path.join(appimage_root, 'web', 'public', 'images'), # Ruta con public/
|
|
|
|
os.path.join(appimage_root, 'usr', 'web', 'public', 'images'), # Fallback usr/public/
|
|
|
|
os.path.join(appimage_root, 'public', 'images'), # Ruta directa a public
|
|
|
|
os.path.join(appimage_root, 'usr', 'public', 'images'), # Fallback usr/public
|
2025-09-29 18:07:30 +02:00
|
|
|
]
|
|
|
|
|
2025-10-01 17:10:37 +02:00
|
|
|
print(f"[v0] Looking for image: {filename}")
|
2025-09-29 18:07:30 +02:00
|
|
|
for image_dir in image_paths:
|
|
|
|
file_path = os.path.join(image_dir, filename)
|
2025-10-01 17:10:37 +02:00
|
|
|
abs_path = os.path.abspath(file_path)
|
|
|
|
exists = os.path.exists(abs_path)
|
|
|
|
print(f"[v0] Checking: {abs_path} - {'FOUND' if exists else 'NOT FOUND'}")
|
|
|
|
if exists:
|
|
|
|
print(f"[v0] Serving image from: {abs_path}")
|
2025-09-29 18:07:30 +02:00
|
|
|
return send_from_directory(image_dir, filename)
|
2025-10-01 17:10:37 +02:00
|
|
|
|
|
|
|
print(f"[v0] Image not found: {filename}")
|
2025-09-29 18:07:30 +02:00
|
|
|
return '', 404
|
2025-09-28 19:40:23 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(f"Error serving image {filename}: {e}")
|
|
|
|
return '', 404
|
|
|
|
|
|
|
|
def get_system_info():
|
|
|
|
"""Get basic system information"""
|
|
|
|
try:
|
|
|
|
# CPU usage
|
|
|
|
cpu_percent = psutil.cpu_percent(interval=1)
|
|
|
|
|
|
|
|
# Memory usage
|
|
|
|
memory = psutil.virtual_memory()
|
|
|
|
|
|
|
|
temp = 0
|
|
|
|
try:
|
|
|
|
if hasattr(psutil, "sensors_temperatures"):
|
|
|
|
temps = psutil.sensors_temperatures()
|
|
|
|
if temps:
|
2025-09-29 18:31:09 +02:00
|
|
|
# Priority order for temperature sensors
|
|
|
|
sensor_priority = ['coretemp', 'cpu_thermal', 'acpi', 'thermal_zone']
|
|
|
|
for sensor_name in sensor_priority:
|
|
|
|
if sensor_name in temps and temps[sensor_name]:
|
|
|
|
temp = temps[sensor_name][0].current
|
2025-09-28 19:40:23 +02:00
|
|
|
break
|
2025-09-29 18:31:09 +02:00
|
|
|
|
|
|
|
# If no priority sensor found, use first available
|
|
|
|
if temp == 0:
|
|
|
|
for name, entries in temps.items():
|
|
|
|
if entries:
|
|
|
|
temp = entries[0].current
|
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error reading temperature sensors: {e}")
|
|
|
|
temp = 0 # Use 0 to indicate no temperature available
|
2025-09-28 19:40:23 +02:00
|
|
|
|
|
|
|
# Uptime
|
|
|
|
boot_time = psutil.boot_time()
|
|
|
|
uptime_seconds = time.time() - boot_time
|
|
|
|
uptime_str = str(timedelta(seconds=int(uptime_seconds)))
|
|
|
|
|
|
|
|
# Load average
|
2025-09-29 18:31:09 +02:00
|
|
|
load_avg = os.getloadavg() if hasattr(os, 'getloadavg') else [0, 0, 0]
|
2025-09-28 19:40:23 +02:00
|
|
|
|
|
|
|
hostname = socket.gethostname()
|
|
|
|
node_id = f"pve-{hostname}"
|
|
|
|
|
|
|
|
# Try to get Proxmox node info if available
|
|
|
|
try:
|
|
|
|
result = subprocess.run(['pvesh', 'get', '/nodes', '--output-format', 'json'],
|
|
|
|
capture_output=True, text=True, timeout=5)
|
|
|
|
if result.returncode == 0:
|
|
|
|
nodes = json.loads(result.stdout)
|
|
|
|
if nodes and len(nodes) > 0:
|
|
|
|
node_id = nodes[0].get('node', node_id)
|
2025-09-29 18:31:09 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(f"Note: pvesh not available or failed: {e}")
|
2025-09-28 19:40:23 +02:00
|
|
|
pass # Use default if pvesh not available
|
|
|
|
|
|
|
|
return {
|
|
|
|
'cpu_usage': round(cpu_percent, 1),
|
|
|
|
'memory_usage': round(memory.percent, 1),
|
|
|
|
'memory_total': round(memory.total / (1024**3), 1), # GB
|
|
|
|
'memory_used': round(memory.used / (1024**3), 1), # GB
|
|
|
|
'temperature': temp,
|
|
|
|
'uptime': uptime_str,
|
|
|
|
'load_average': list(load_avg),
|
|
|
|
'hostname': hostname,
|
|
|
|
'node_id': node_id,
|
|
|
|
'timestamp': datetime.now().isoformat()
|
|
|
|
}
|
|
|
|
except Exception as e:
|
2025-09-29 18:31:09 +02:00
|
|
|
print(f"Critical error getting system info: {e}")
|
2025-10-01 17:27:05 +02:00
|
|
|
return {
|
|
|
|
'error': f'Unable to access system information: {str(e)}',
|
|
|
|
'timestamp': datetime.now().isoformat()
|
|
|
|
}
|
2025-09-28 19:40:23 +02:00
|
|
|
|
|
|
|
def get_storage_info():
|
|
|
|
"""Get storage and disk information"""
|
|
|
|
try:
|
|
|
|
storage_data = {
|
|
|
|
'total': 0,
|
|
|
|
'used': 0,
|
|
|
|
'available': 0,
|
|
|
|
'disks': []
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get disk usage for root partition
|
|
|
|
disk_usage = psutil.disk_usage('/')
|
|
|
|
storage_data['total'] = round(disk_usage.total / (1024**3), 1) # GB
|
|
|
|
storage_data['used'] = round(disk_usage.used / (1024**3), 1) # GB
|
|
|
|
storage_data['available'] = round(disk_usage.free / (1024**3), 1) # GB
|
|
|
|
|
|
|
|
# Get individual disk information
|
|
|
|
disk_partitions = psutil.disk_partitions()
|
|
|
|
for partition in disk_partitions:
|
|
|
|
try:
|
|
|
|
partition_usage = psutil.disk_usage(partition.mountpoint)
|
2025-09-29 18:31:09 +02:00
|
|
|
|
2025-10-01 17:27:05 +02:00
|
|
|
disk_temp = 0
|
2025-09-29 18:31:09 +02:00
|
|
|
try:
|
|
|
|
# Try to get disk temperature from sensors
|
|
|
|
if hasattr(psutil, "sensors_temperatures"):
|
|
|
|
temps = psutil.sensors_temperatures()
|
|
|
|
if temps:
|
|
|
|
for name, entries in temps.items():
|
|
|
|
if 'disk' in name.lower() or 'hdd' in name.lower() or 'sda' in name.lower():
|
|
|
|
if entries:
|
|
|
|
disk_temp = entries[0].current
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2025-09-28 19:40:23 +02:00
|
|
|
disk_info = {
|
|
|
|
'name': partition.device,
|
|
|
|
'mountpoint': partition.mountpoint,
|
|
|
|
'fstype': partition.fstype,
|
|
|
|
'total': round(partition_usage.total / (1024**3), 1),
|
|
|
|
'used': round(partition_usage.used / (1024**3), 1),
|
|
|
|
'available': round(partition_usage.free / (1024**3), 1),
|
|
|
|
'usage_percent': round((partition_usage.used / partition_usage.total) * 100, 1),
|
2025-10-01 17:27:05 +02:00
|
|
|
'health': 'unknown', # Would need SMART data for real health
|
2025-09-29 18:31:09 +02:00
|
|
|
'temperature': disk_temp
|
2025-09-28 19:40:23 +02:00
|
|
|
}
|
|
|
|
storage_data['disks'].append(disk_info)
|
|
|
|
except PermissionError:
|
2025-09-29 18:31:09 +02:00
|
|
|
print(f"Permission denied accessing {partition.mountpoint}")
|
2025-09-28 19:40:23 +02:00
|
|
|
continue
|
2025-09-29 18:31:09 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(f"Error accessing partition {partition.device}: {e}")
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not storage_data['disks'] and storage_data['total'] == 0:
|
|
|
|
return {
|
2025-10-01 17:27:05 +02:00
|
|
|
'error': 'No storage data available - unable to access disk information',
|
|
|
|
'total': 0,
|
|
|
|
'used': 0,
|
|
|
|
'available': 0,
|
|
|
|
'disks': []
|
2025-09-29 18:31:09 +02:00
|
|
|
}
|
2025-09-28 19:40:23 +02:00
|
|
|
|
|
|
|
return storage_data
|
2025-09-29 18:31:09 +02:00
|
|
|
|
2025-09-28 19:40:23 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(f"Error getting storage info: {e}")
|
2025-10-01 17:27:05 +02:00
|
|
|
return {
|
|
|
|
'error': f'Unable to access storage information: {str(e)}',
|
|
|
|
'total': 0,
|
|
|
|
'used': 0,
|
|
|
|
'available': 0,
|
|
|
|
'disks': []
|
|
|
|
}
|
2025-09-28 19:40:23 +02:00
|
|
|
|
|
|
|
def get_network_info():
|
|
|
|
"""Get network interface information"""
|
|
|
|
try:
|
|
|
|
network_data = {
|
|
|
|
'interfaces': [],
|
|
|
|
'traffic': {'incoming': 0, 'outgoing': 0}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get network interfaces
|
|
|
|
net_if_addrs = psutil.net_if_addrs()
|
|
|
|
net_if_stats = psutil.net_if_stats()
|
|
|
|
|
|
|
|
for interface_name, interface_addresses in net_if_addrs.items():
|
|
|
|
if interface_name == 'lo': # Skip loopback
|
|
|
|
continue
|
|
|
|
|
|
|
|
interface_info = {
|
|
|
|
'name': interface_name,
|
|
|
|
'status': 'up' if net_if_stats[interface_name].isup else 'down',
|
|
|
|
'addresses': []
|
|
|
|
}
|
|
|
|
|
|
|
|
for address in interface_addresses:
|
|
|
|
if address.family == 2: # IPv4
|
|
|
|
interface_info['addresses'].append({
|
|
|
|
'ip': address.address,
|
|
|
|
'netmask': address.netmask
|
|
|
|
})
|
|
|
|
|
|
|
|
network_data['interfaces'].append(interface_info)
|
|
|
|
|
|
|
|
# Get network I/O statistics
|
|
|
|
net_io = psutil.net_io_counters()
|
|
|
|
network_data['traffic'] = {
|
|
|
|
'bytes_sent': net_io.bytes_sent,
|
|
|
|
'bytes_recv': net_io.bytes_recv,
|
|
|
|
'packets_sent': net_io.packets_sent,
|
|
|
|
'packets_recv': net_io.packets_recv
|
|
|
|
}
|
|
|
|
|
|
|
|
return network_data
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error getting network info: {e}")
|
|
|
|
return {
|
2025-10-01 17:27:05 +02:00
|
|
|
'error': f'Unable to access network information: {str(e)}',
|
|
|
|
'interfaces': [],
|
|
|
|
'traffic': {'bytes_sent': 0, 'bytes_recv': 0, 'packets_sent': 0, 'packets_recv': 0}
|
2025-09-28 19:40:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def get_proxmox_vms():
|
|
|
|
"""Get Proxmox VM information (requires pvesh command)"""
|
|
|
|
try:
|
|
|
|
# Try to get VM list using pvesh command
|
|
|
|
result = subprocess.run(['pvesh', 'get', '/nodes/localhost/qemu', '--output-format', 'json'],
|
|
|
|
capture_output=True, text=True, timeout=10)
|
|
|
|
|
|
|
|
if result.returncode == 0:
|
|
|
|
vms = json.loads(result.stdout)
|
|
|
|
return vms
|
|
|
|
else:
|
2025-10-01 17:27:05 +02:00
|
|
|
return {
|
|
|
|
'error': 'pvesh command not available or failed - Proxmox API not accessible',
|
|
|
|
'vms': []
|
|
|
|
}
|
2025-09-28 19:40:23 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(f"Error getting VM info: {e}")
|
2025-10-01 17:27:05 +02:00
|
|
|
return {
|
|
|
|
'error': f'Unable to access VM information: {str(e)}',
|
|
|
|
'vms': []
|
|
|
|
}
|
2025-09-28 19:40:23 +02:00
|
|
|
|
|
|
|
@app.route('/api/system', methods=['GET'])
|
|
|
|
def api_system():
|
|
|
|
"""Get system information"""
|
|
|
|
return jsonify(get_system_info())
|
|
|
|
|
|
|
|
@app.route('/api/storage', methods=['GET'])
|
|
|
|
def api_storage():
|
|
|
|
"""Get storage information"""
|
|
|
|
return jsonify(get_storage_info())
|
|
|
|
|
|
|
|
@app.route('/api/network', methods=['GET'])
|
|
|
|
def api_network():
|
|
|
|
"""Get network information"""
|
|
|
|
return jsonify(get_network_info())
|
|
|
|
|
|
|
|
@app.route('/api/vms', methods=['GET'])
|
|
|
|
def api_vms():
|
|
|
|
"""Get virtual machine information"""
|
|
|
|
return jsonify(get_proxmox_vms())
|
|
|
|
|
|
|
|
@app.route('/api/logs', methods=['GET'])
|
|
|
|
def api_logs():
|
|
|
|
"""Get system logs"""
|
|
|
|
try:
|
|
|
|
# Get recent system logs
|
|
|
|
result = subprocess.run(['journalctl', '-n', '100', '--output', 'json'],
|
|
|
|
capture_output=True, text=True, timeout=10)
|
|
|
|
|
|
|
|
if result.returncode == 0:
|
|
|
|
logs = []
|
|
|
|
for line in result.stdout.strip().split('\n'):
|
|
|
|
if line:
|
|
|
|
try:
|
|
|
|
log_entry = json.loads(line)
|
|
|
|
logs.append({
|
|
|
|
'timestamp': log_entry.get('__REALTIME_TIMESTAMP', ''),
|
|
|
|
'level': log_entry.get('PRIORITY', '6'),
|
|
|
|
'service': log_entry.get('_SYSTEMD_UNIT', 'system'),
|
|
|
|
'message': log_entry.get('MESSAGE', ''),
|
|
|
|
'source': 'journalctl'
|
|
|
|
})
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
continue
|
|
|
|
return jsonify(logs)
|
|
|
|
else:
|
2025-10-01 17:27:05 +02:00
|
|
|
return jsonify({
|
|
|
|
'error': 'journalctl not available or failed',
|
|
|
|
'logs': []
|
|
|
|
})
|
2025-09-28 19:40:23 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(f"Error getting logs: {e}")
|
2025-10-01 17:27:05 +02:00
|
|
|
return jsonify({
|
|
|
|
'error': f'Unable to access system logs: {str(e)}',
|
|
|
|
'logs': []
|
|
|
|
})
|
2025-09-28 19:40:23 +02:00
|
|
|
|
|
|
|
@app.route('/api/health', methods=['GET'])
|
|
|
|
def api_health():
|
|
|
|
"""Health check endpoint"""
|
|
|
|
return jsonify({
|
|
|
|
'status': 'healthy',
|
|
|
|
'timestamp': datetime.now().isoformat(),
|
|
|
|
'version': '1.0.0'
|
|
|
|
})
|
|
|
|
|
|
|
|
@app.route('/api/system-info', methods=['GET'])
|
|
|
|
def api_system_info():
|
|
|
|
"""Get system and node information for dashboard header"""
|
|
|
|
try:
|
|
|
|
hostname = socket.gethostname()
|
|
|
|
node_id = f"pve-{hostname}"
|
2025-10-01 17:27:05 +02:00
|
|
|
pve_version = None
|
2025-09-28 19:40:23 +02:00
|
|
|
|
2025-10-01 17:27:05 +02:00
|
|
|
# Try to get Proxmox version
|
2025-09-28 19:40:23 +02:00
|
|
|
try:
|
|
|
|
result = subprocess.run(['pveversion'], capture_output=True, text=True, timeout=5)
|
|
|
|
if result.returncode == 0:
|
|
|
|
pve_version = result.stdout.strip().split('\n')[0]
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Try to get node info from Proxmox API
|
|
|
|
try:
|
|
|
|
result = subprocess.run(['pvesh', 'get', '/nodes', '--output-format', 'json'],
|
|
|
|
capture_output=True, text=True, timeout=5)
|
|
|
|
if result.returncode == 0:
|
|
|
|
nodes = json.loads(result.stdout)
|
|
|
|
if nodes and len(nodes) > 0:
|
|
|
|
node_info = nodes[0]
|
|
|
|
node_id = node_info.get('node', node_id)
|
|
|
|
hostname = node_info.get('node', hostname)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2025-10-01 17:27:05 +02:00
|
|
|
response = {
|
2025-09-28 19:40:23 +02:00
|
|
|
'hostname': hostname,
|
|
|
|
'node_id': node_id,
|
|
|
|
'status': 'online',
|
|
|
|
'timestamp': datetime.now().isoformat()
|
2025-10-01 17:27:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if pve_version:
|
|
|
|
response['pve_version'] = pve_version
|
|
|
|
else:
|
|
|
|
response['error'] = 'Proxmox version not available - pveversion command not found'
|
|
|
|
|
|
|
|
return jsonify(response)
|
2025-09-28 19:40:23 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(f"Error getting system info: {e}")
|
|
|
|
return jsonify({
|
2025-10-01 17:27:05 +02:00
|
|
|
'error': f'Unable to access system information: {str(e)}',
|
|
|
|
'hostname': socket.gethostname(),
|
|
|
|
'status': 'error',
|
2025-09-28 19:40:23 +02:00
|
|
|
'timestamp': datetime.now().isoformat()
|
|
|
|
})
|
|
|
|
|
|
|
|
@app.route('/api/info', methods=['GET'])
|
|
|
|
def api_info():
|
|
|
|
"""Root endpoint with API information"""
|
|
|
|
return jsonify({
|
|
|
|
'name': 'ProxMenux Monitor API',
|
|
|
|
'version': '1.0.0',
|
|
|
|
'endpoints': [
|
|
|
|
'/api/system',
|
|
|
|
'/api/system-info',
|
|
|
|
'/api/storage',
|
|
|
|
'/api/network',
|
|
|
|
'/api/vms',
|
|
|
|
'/api/logs',
|
|
|
|
'/api/health'
|
|
|
|
]
|
|
|
|
})
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2025-10-01 17:27:05 +02:00
|
|
|
print("Starting ProxMenux Flask Server on port 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")
|
2025-09-28 19:40:23 +02:00
|
|
|
|
|
|
|
app.run(host='0.0.0.0', port=8008, debug=False)
|