fix: Rename class to Tools for OpenWebUI compatibility
This commit is contained in:
@@ -8,7 +8,7 @@ Permet à Effi d'accéder aux métriques système de FolzoCorp, EffiServer et NA
|
||||
|
||||
1. **Admin Panel** → **Tools** → **Create Tool**
|
||||
2. **Name**: `Beszel System Monitor`
|
||||
3. **Description**: `Monitor system resources via Beszel. Usage: get_system_stats("folzocorp"), get_all_stats(), check_alerts("folzocorp"), get_gpu_status("folzocorp")`
|
||||
3. **Description**: `Monitor system resources via Beszel. Usage: get_all_stats(), get_system_stats("folzocorp"), check_alerts("folzocorp"), get_gpu_status("folzocorp"), can_run_comfyui()`
|
||||
4. **Coller le code ci-dessous**
|
||||
5. **Save and Activate**
|
||||
|
||||
@@ -24,17 +24,16 @@ Les variables d'environnement à configurer:
|
||||
```python
|
||||
import os
|
||||
import requests
|
||||
from typing import Optional, Dict, Any, List
|
||||
|
||||
|
||||
class BeszelClient:
|
||||
class Tools:
|
||||
def __init__(self):
|
||||
self.url = os.getenv("BESZEL_URL", "http://192.168.1.25:8090")
|
||||
self.username = os.getenv("BESZEL_USER", "admin")
|
||||
self.password = os.getenv("BESZEL_PASSWORD", "")
|
||||
self.token = None
|
||||
|
||||
def login(self) -> bool:
|
||||
def _login(self):
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{self.url}/api/auth/login",
|
||||
@@ -48,8 +47,8 @@ class BeszelClient:
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def get_systems(self) -> Optional[List[Dict]]:
|
||||
if not self.token and not self.login():
|
||||
def _get_systems(self):
|
||||
if not self.token and not self._login():
|
||||
return None
|
||||
try:
|
||||
headers = {"Authorization": f"Bearer {self.token}"}
|
||||
@@ -60,8 +59,8 @@ class BeszelClient:
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def get_system_stats(self, system_id: str) -> Optional[Dict[str, Any]]:
|
||||
if not self.token and not self.login():
|
||||
def _get_system_stats(self, system_id):
|
||||
if not self.token and not self._login():
|
||||
return None
|
||||
try:
|
||||
headers = {"Authorization": f"Bearer {self.token}"}
|
||||
@@ -76,7 +75,7 @@ class BeszelClient:
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def format_stats_text(self, stats: Dict, system_name: str = "System") -> str:
|
||||
def _format_stats_text(self, stats, system_name="System"):
|
||||
lines = []
|
||||
lines.append(f"=== {system_name} ===")
|
||||
lines.append(f"CPU: {stats.get('cpu', 0):.1f}%")
|
||||
@@ -119,7 +118,7 @@ class BeszelClient:
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
def check_thresholds(self, stats: Dict, thresholds: Optional[Dict] = None) -> List[str]:
|
||||
def _check_thresholds(self, stats, thresholds=None):
|
||||
if thresholds is None:
|
||||
thresholds = {'cpu': 80, 'memory': 90, 'vram': 90}
|
||||
|
||||
@@ -145,11 +144,9 @@ class BeszelClient:
|
||||
|
||||
return alerts
|
||||
|
||||
|
||||
def get_all_stats():
|
||||
def get_all_stats(self):
|
||||
"""Get stats from all monitored systems."""
|
||||
client = BeszelClient()
|
||||
systems = client.get_systems()
|
||||
systems = self._get_systems()
|
||||
|
||||
if not systems:
|
||||
return "[ERROR] Cannot connect to Beszel or no systems found"
|
||||
@@ -158,20 +155,18 @@ def get_all_stats():
|
||||
for sys_info in systems:
|
||||
sys_id = sys_info.get('id')
|
||||
sys_name = sys_info.get('name', sys_id)
|
||||
stats = client.get_system_stats(sys_id)
|
||||
stats = self._get_system_stats(sys_id)
|
||||
|
||||
if stats:
|
||||
results.append(client.format_stats_text(stats, sys_name))
|
||||
results.append(self._format_stats_text(stats, sys_name))
|
||||
else:
|
||||
results.append(f"=== {sys_name} ===\n[ERROR] Cannot get stats")
|
||||
|
||||
return "\n\n".join(results)
|
||||
|
||||
|
||||
def get_system_stats(system_name: str):
|
||||
def get_system_stats(self, system_name):
|
||||
"""Get stats for a specific system. Options: folzocorp, effiserver, synology-nas"""
|
||||
client = BeszelClient()
|
||||
systems = client.get_systems()
|
||||
systems = self._get_systems()
|
||||
|
||||
if not systems:
|
||||
return "[ERROR] Cannot connect to Beszel"
|
||||
@@ -187,19 +182,17 @@ def get_system_stats(system_name: str):
|
||||
|
||||
for sys_info in systems:
|
||||
if sys_info.get('id') == target_id or sys_info.get('name', '').lower() == system_name.lower():
|
||||
stats = client.get_system_stats(sys_info.get('id'))
|
||||
stats = self._get_system_stats(sys_info.get('id'))
|
||||
if stats:
|
||||
return client.format_stats_text(stats, sys_info.get('name', system_name))
|
||||
return self._format_stats_text(stats, sys_info.get('name', system_name))
|
||||
return f"[ERROR] Cannot get stats for {system_name}"
|
||||
|
||||
available = [s.get('name', s.get('id')) for s in systems]
|
||||
return f"[ERROR] System '{system_name}' not found. Available: {', '.join(available)}"
|
||||
|
||||
|
||||
def check_alerts(system_name: str = "all"):
|
||||
def check_alerts(self, system_name="all"):
|
||||
"""Check for resource alerts. Use 'all' or specific system name."""
|
||||
client = BeszelClient()
|
||||
systems = client.get_systems()
|
||||
systems = self._get_systems()
|
||||
|
||||
if not systems:
|
||||
return "[ERROR] Cannot connect to Beszel"
|
||||
@@ -213,9 +206,9 @@ def check_alerts(system_name: str = "all"):
|
||||
if system_name.lower() != 'all' and sys_name.lower() != system_name.lower():
|
||||
continue
|
||||
|
||||
stats = client.get_system_stats(sys_id)
|
||||
stats = self._get_system_stats(sys_id)
|
||||
if stats:
|
||||
system_alerts = client.check_thresholds(stats)
|
||||
system_alerts = self._check_thresholds(stats)
|
||||
if system_alerts:
|
||||
alerts.append(f"=== {sys_name} ===")
|
||||
alerts.extend(system_alerts)
|
||||
@@ -225,11 +218,9 @@ def check_alerts(system_name: str = "all"):
|
||||
|
||||
return "\n".join(alerts)
|
||||
|
||||
|
||||
def get_gpu_status(system_name: str = "folzocorp"):
|
||||
def get_gpu_status(self, system_name="folzocorp"):
|
||||
"""Get GPU status for a specific system. Most useful for folzocorp (AMD RX 7800 XT)."""
|
||||
client = BeszelClient()
|
||||
systems = client.get_systems()
|
||||
systems = self._get_systems()
|
||||
|
||||
if not systems:
|
||||
return "[ERROR] Cannot connect to Beszel"
|
||||
@@ -244,7 +235,7 @@ def get_gpu_status(system_name: str = "folzocorp"):
|
||||
available = [s.get('name', s.get('id')) for s in systems]
|
||||
return f"[ERROR] System '{system_name}' not found. Available: {', '.join(available)}"
|
||||
|
||||
stats = client.get_system_stats(target_id)
|
||||
stats = self._get_system_stats(target_id)
|
||||
if not stats:
|
||||
return f"[ERROR] Cannot get stats for {system_name}"
|
||||
|
||||
@@ -268,18 +259,16 @@ def get_gpu_status(system_name: str = "folzocorp"):
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def can_run_comfyui():
|
||||
def can_run_comfyui(self):
|
||||
"""Check if there's enough RAM and VRAM to run ComfyUI."""
|
||||
client = BeszelClient()
|
||||
|
||||
stats = client.get_system_stats("folzocorp")
|
||||
stats = self._get_system_stats("folzocorp")
|
||||
if not stats:
|
||||
return "[ERROR] Cannot get FolzoCorp stats"
|
||||
|
||||
memory_pct = stats.get('memory', 0)
|
||||
|
||||
vram_info = None
|
||||
vram_pct = 0
|
||||
if 'gpu' in stats and stats['gpu']:
|
||||
for gpu_name, gpu_data in stats['gpu'].items():
|
||||
vram_total = gpu_data.get('memory_total', 0) / (1024**3)
|
||||
@@ -288,15 +277,36 @@ def can_run_comfyui():
|
||||
vram_pct = (gpu_data.get('memory_used', 0) / gpu_data.get('memory_total', 1)) * 100
|
||||
vram_info = f"{gpu_name}: {vram_free_gb:.1f}GB free ({vram_pct:.1f}% used)"
|
||||
|
||||
can_run = memory_pct < 85 and (not vram_info or vram_pct < 85)
|
||||
can_run = memory_pct < 85 and vram_pct < 85
|
||||
|
||||
lines = ["=== ComfyUI Readiness Check ==="]
|
||||
lines.append(f"RAM: {100-memory_pct:.1f}% available ({memory_pct:.1f}% used)")
|
||||
if vram_info:
|
||||
lines.append(f"VRAM: {vram_info}")
|
||||
lines.append(f"\n结论: {'✅ Can run ComfyUI' if can_run else '⚠️ Resources low, may be slow'}")
|
||||
lines.append(f"\nConclusion: {'✅ Can run ComfyUI' if can_run else '⚠️ Resources low, may be slow'}")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def get_all_stats():
|
||||
tools = Tools()
|
||||
return tools.get_all_stats()
|
||||
|
||||
def get_system_stats(system_name):
|
||||
tools = Tools()
|
||||
return tools.get_system_stats(system_name)
|
||||
|
||||
def check_alerts(system_name="all"):
|
||||
tools = Tools()
|
||||
return tools.check_alerts(system_name)
|
||||
|
||||
def get_gpu_status(system_name="folzocorp"):
|
||||
tools = Tools()
|
||||
return tools.get_gpu_status(system_name)
|
||||
|
||||
def can_run_comfyui():
|
||||
tools = Tools()
|
||||
return tools.can_run_comfyui()
|
||||
```
|
||||
|
||||
## Usage Examples
|
||||
|
||||
Reference in New Issue
Block a user