Refactor: Container-Erkennung zentralisiert, SMART-Check auf LXC überspringen
Neues Modul services/platform_info.py prüft systemd-detect-virt einmalig beim Start (statt pro Request). SMART-Abfragen werden in Containern übersprungen, da /dev/sdX dort meist nicht verfügbar ist. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
"""
|
||||
Platform/Virtualization Detection
|
||||
Einmalige Prüfung via systemd-detect-virt beim Start.
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _detect_virt() -> str:
|
||||
try:
|
||||
r = subprocess.run(["/usr/bin/systemd-detect-virt"], capture_output=True, text=True, timeout=3)
|
||||
return r.stdout.strip()
|
||||
except Exception:
|
||||
return "none"
|
||||
|
||||
|
||||
VIRT_TYPE = _detect_virt()
|
||||
IS_CONTAINER = VIRT_TYPE not in ("none", "")
|
||||
IS_LXC = VIRT_TYPE == "lxc"
|
||||
|
||||
if IS_CONTAINER:
|
||||
logger.info(f"Running inside container (virt={VIRT_TYPE}) — hardware-specific checks disabled")
|
||||
@@ -10,6 +10,8 @@ import platform
|
||||
from typing import Dict, Any, Optional
|
||||
from datetime import datetime
|
||||
|
||||
from services.platform_info import IS_CONTAINER
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -297,16 +299,8 @@ def get_network_traffic() -> Dict[str, Any]:
|
||||
return {"error": str(e)}
|
||||
|
||||
|
||||
def _is_container() -> bool:
|
||||
try:
|
||||
r = subprocess.run(["/usr/bin/systemd-detect-virt"], capture_output=True, text=True, timeout=3)
|
||||
return r.returncode == 0 and r.stdout.strip() not in ("none", "")
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def get_disk_io() -> Dict[str, Any]:
|
||||
if _is_container():
|
||||
if IS_CONTAINER:
|
||||
return {"disks": []}
|
||||
try:
|
||||
with open("/proc/diskstats", "r") as f:
|
||||
|
||||
@@ -11,6 +11,8 @@ import re
|
||||
from typing import Dict, List, Any, Optional, Tuple
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from services.platform_info import IS_CONTAINER
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_TIMEOUT = 5
|
||||
@@ -189,6 +191,8 @@ def scrub_pool(pool_name: str) -> Dict[str, str]:
|
||||
|
||||
|
||||
def get_smart_info(disk: str) -> Dict[str, Any]:
|
||||
if IS_CONTAINER:
|
||||
return {"available": False, "reason": "container"}
|
||||
stdout, _, rc = _run(["smartctl", "-A", "-i", "--json", f"/dev/{disk}"])
|
||||
if not stdout:
|
||||
return {}
|
||||
|
||||
Reference in New Issue
Block a user