v0.0.3: 修复CPU/内存显示 - 列表和仪表盘显示使用量/总量格式,修复详情页CPU数据获取bug
This commit is contained in:
@@ -8,6 +8,7 @@ import os
|
||||
|
||||
from app.libvirt_conn import conn_pool
|
||||
from app.utils import parse_vm_info, generate_vm_xml
|
||||
from app.routers.monitor import get_vm_runtime_stats
|
||||
import libvirt
|
||||
|
||||
router = APIRouter()
|
||||
@@ -45,6 +46,12 @@ async def list_vms(host_id: str = Query("local"), include_ip: bool = False):
|
||||
for dom in domains:
|
||||
try:
|
||||
vm_info = parse_vm_info(dom, host_id, include_ip=include_ip)
|
||||
# 添加运行时统计(CPU使用率、内存使用量)
|
||||
cache_key = f"{host_id}_{vm_info['name']}"
|
||||
runtime = get_vm_runtime_stats(dom, cache_key)
|
||||
vm_info["cpu_percent"] = runtime["cpu_percent"]
|
||||
vm_info["memory_rss_mb"] = runtime["memory"].get("rss_mb", 0)
|
||||
vm_info["memory_usage_percent"] = runtime["memory"].get("usage_percent", 0)
|
||||
vms.append(vm_info)
|
||||
except Exception as e:
|
||||
vms.append({
|
||||
@@ -71,6 +78,12 @@ async def list_all_vms(include_ip: bool = False):
|
||||
vm_info = parse_vm_info(dom, host.id, include_ip=include_ip)
|
||||
vm_info["host_id"] = host.id
|
||||
vm_info["host_name"] = host.name
|
||||
# 添加运行时统计
|
||||
cache_key = f"{host.id}_{vm_info['name']}"
|
||||
runtime = get_vm_runtime_stats(dom, cache_key)
|
||||
vm_info["cpu_percent"] = runtime["cpu_percent"]
|
||||
vm_info["memory_rss_mb"] = runtime["memory"].get("rss_mb", 0)
|
||||
vm_info["memory_usage_percent"] = runtime["memory"].get("usage_percent", 0)
|
||||
all_vms.append(vm_info)
|
||||
except Exception:
|
||||
all_vms.append({
|
||||
@@ -100,8 +113,9 @@ async def get_vm_detail(name: str, host_id: str = Query("local")):
|
||||
# 运行中的虚拟机获取更多动态信息
|
||||
if info["state"] == "running":
|
||||
try:
|
||||
_, _, cpu_time, _ = dom.info()
|
||||
info["cpu_time_ns"] = cpu_time
|
||||
# dom.info(): [state, maxMem, memory, nrVirtCpu, cpuTime]
|
||||
dom_info = dom.info()
|
||||
info["cpu_time_ns"] = dom_info[4] # cpuTime 在索引4
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user