ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
feat(system): NVIDIA GPU monitoring
Juan Denisd58fdd7da72b
diff --git a/backend/app/__init__.py b/backend/app/__init__.pyindex db4d7296..9923c473 100644--- a/backend/app/__init__.py+++ b/backend/app/__init__.py@@ -311,6 +311,10 @@ def create_app(config_name=None):from app.api.image_updates import image_updates_bpapp.register_blueprint(image_updates_bp, url_prefix='/api/v1/image-updates')+ # Register blueprints - GPU monitoring+ from app.api.gpu import gpu_bp+ app.register_blueprint(gpu_bp, url_prefix='/api/v1/gpu')+# Register blueprints - Nginx Advancedfrom app.api.nginx_advanced import nginx_advanced_bpapp.register_blueprint(nginx_advanced_bp, url_prefix='/api/v1/nginx/advanced')diff --git a/backend/app/api/gpu.py b/backend/app/api/gpu.pynew file mode 100644index 00000000..e4abb807--- /dev/null+++ b/backend/app/api/gpu.py@@ -0,0 +1,13 @@+from flask import Blueprint, jsonify+from flask_jwt_extended import jwt_required+from app.services.gpu_service import GpuService++gpu_bp = Blueprint('gpu', __name__)+++@gpu_bp.route('/', methods=['GET'])+@jwt_required()+def gpu_info():+ """Per-GPU stats (utilization, VRAM, temperature, power, fan, driver) plus+ the GPU compute processes, with best-effort container resolution."""+ return jsonify(GpuService.info())diff --git a/backend/app/services/gpu_service.py b/backend/app/services/gpu_service.pynew file mode 100644index 00000000..c23637bb--- /dev/null+++ b/backend/app/services/gpu_service.py@@ -0,0 +1,126 @@+"""NVIDIA GPU monitoring. Shells out to `nvidia-smi` (like the rest of the+service layer) and degrades gracefully to "no GPUs" when it isn't present."""+import logging+import re+import subprocess++logger = logging.getLogger(__name__)++# (key, caster) in the same order as GPU_QUERY below.+GPU_FIELDS = [+ ('index', int),+ ('name', str),+ ('utilization_gpu', float), # %+ ('memory_used', float), # MiB+ ('memory_total', float), # MiB+ ('temperature', float), # C+ ('power_draw', float), # W+ ('power_limit', float), # W+ ('fan_speed', float), # %+ ('driver_version', str),+]+GPU_QUERY = ('index,name,utilization.gpu,memory.used,memory.total,'+ 'temperature.gpu,power.draw,power.limit,fan.speed,driver_version')++_NA = ('', '[N/A]', 'N/A', '[Not Supported]', '[Unknown Error]')+++class GpuService:++ @staticmethod+ def _run(args, timeout=10):+ return subprocess.run(['nvidia-smi', *args], capture_output=True, text=True, timeout=timeout)++ @staticmethod+ def _coerce(value, caster):+ v = value.strip()+ if v in _NA:+ return None+ try:+ return caster(v)+ except (ValueError, TypeError):+ return v if caster is str else None++ @classmethod+ def available(cls):+ try:+ return cls._run(['--query-gpu=index', '--format=csv,noheader']).returncode == 0+ except Exception:+ return False++ @classmethod+ def list_gpus(cls):+ try:+ result = cls._run([f'--query-gpu={GPU_QUERY}', '--format=csv,noheader,nounits'])+ if result.returncode != 0:+ return []+ except Exception:+ return []++ gpus = []+ for line in result.stdout.strip().splitlines():+ if not line.strip():+ continue+ parts = [p.strip() for p in line.split(',')]+ if len(parts) < len(GPU_FIELDS):+ continue+ gpu = {key: cls._coerce(raw, caster)+ for (key, caster), raw in zip(GPU_FIELDS, parts)}+ if gpu.get('memory_total'):+ gpu['memory_percent'] = round(100.0 * (gpu.get('memory_used') or 0) / gpu['memory_total'], 1)+ gpus.append(gpu)+ return gpus++ @classmethod+ def _container_for_pid(cls, pid):+ """Best-effort: resolve a host PID to a Docker container name via its+ cgroup. Returns None when it can't be determined."""+ if not pid:+ return None+ try:+ with open(f'/proc/{pid}/cgroup', 'r') as fh:+ match = re.search(r'docker[-/]([0-9a-f]{64})', fh.read())+ if not match:+ return None+ cid = match.group(1)[:12]+ out = subprocess.run(['docker', 'inspect', '-f', '{{.Name}}', cid],+ capture_output=True, text=True, timeout=5)+ return out.stdout.strip().lstrip('/') if out.returncode == 0 else cid+ except Exception:+ return None++ @classmethod+ def processes(cls):+ try:+ result = cls._run(['--query-compute-apps=gpu_uuid,pid,process_name,used_memory',+ '--format=csv,noheader,nounits'])+ if result.returncode != 0:+ return []+ except Exception:+ return []++ procs = []+ for line in result.stdout.strip().splitlines():+ if not line.strip():+ continue+ parts = [p.strip() for p in line.split(',')]+ if len(parts) < 4:+ continue+ pid = cls._coerce(parts[1], int)+ procs.append({+ 'gpu_uuid': parts[0],+ 'pid': pid,+ 'process_name': parts[2],+ 'used_memory': cls._coerce(parts[3], float),+ 'container': cls._container_for_pid(pid),+ })+ return procs++ @classmethod+ def info(cls):+ gpus = cls.list_gpus()+ return {+ 'available': bool(gpus),+ 'gpus': gpus,+ 'processes': cls.processes() if gpus else [],+ }diff --git a/backend/tests/test_gpu.py b/backend/tests/test_gpu.pynew file mode 100644index 00000000..226c8b73--- /dev/null+++ b/backend/tests/test_gpu.py@@ -0,0 +1,56 @@+"""Tests for NVIDIA GPU monitoring (nvidia-smi parsing)."""+import types++from app.services.gpu_service import GpuService++SAMPLE = "0, NVIDIA GeForce RTX 3090, 15, 2048, 24576, 45, 120.50, 350.00, 30, 535.104.05\n"+++def _fake_run(stdout, rc=0):+ return lambda *a, **k: types.SimpleNamespace(returncode=rc, stdout=stdout, stderr='')+++class TestGpuService:+ def test_list_gpus_parses_a_row(self, monkeypatch):+ monkeypatch.setattr(GpuService, '_run', _fake_run(SAMPLE))+ gpus = GpuService.list_gpus()+ assert len(gpus) == 1+ g = gpus[0]+ assert g['index'] == 0+ assert g['name'] == 'NVIDIA GeForce RTX 3090'+ assert g['memory_total'] == 24576.0+ assert g['memory_percent'] == round(100 * 2048 / 24576, 1)+ assert g['driver_version'] == '535.104.05'++ def test_coerces_na_to_none(self, monkeypatch):+ line = "0, GPU, [N/A], 100, 1000, [N/A], 50, 100, [N/A], 535\n"+ monkeypatch.setattr(GpuService, '_run', _fake_run(line))+ g = GpuService.list_gpus()[0]+ assert g['utilization_gpu'] is None+ assert g['temperature'] is None+ assert g['fan_speed'] is None+ assert g['memory_percent'] == 10.0++ def test_available_false_on_nonzero_exit(self, monkeypatch):+ monkeypatch.setattr(GpuService, '_run', _fake_run('', rc=1))+ assert GpuService.available() is False++ def test_info_when_no_gpus(self, monkeypatch):+ monkeypatch.setattr(GpuService, '_run', _fake_run('', rc=1))+ info = GpuService.info()+ assert info['available'] is False+ assert info['gpus'] == [] and info['processes'] == []+++class TestGpuApi:+ def test_gpu_endpoint_returns_info(self, client, auth_headers, app, monkeypatch):+ monkeypatch.setattr(GpuService, 'list_gpus', lambda: [{'index': 0, 'name': 'RTX 3090'}])+ monkeypatch.setattr(GpuService, 'processes', lambda: [])+ resp = client.get('/api/v1/gpu/', headers=auth_headers)+ assert resp.status_code == 200+ body = resp.get_json()+ assert body['available'] is True+ assert len(body['gpus']) == 1++ def test_gpu_requires_auth(self, client, app):+ assert client.get('/api/v1/gpu/').status_code == 401