From d1735e3481fbe1b4444f7174966f2130e57e6878 Mon Sep 17 00:00:00 2001 From: Akizon77 Date: Thu, 28 Aug 2025 12:36:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=98=A0=E5=B0=84=E8=A1=A8?= =?UTF-8?q?=E7=AE=80=E5=8C=96=E4=BE=9B=E5=BA=94=E5=95=86=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- monitoring/unit/virtualization.go | 48 +++++++++++++++---------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/monitoring/unit/virtualization.go b/monitoring/unit/virtualization.go index 9c738d3..a653477 100644 --- a/monitoring/unit/virtualization.go +++ b/monitoring/unit/virtualization.go @@ -1,7 +1,6 @@ package monitoring import ( - "fmt" "os" "os/exec" "regexp" @@ -41,31 +40,30 @@ func detectByCPUID() string { return "none" } vendor := strings.ToLower(cpuid.CPU.HypervisorVendorString) - switch { - case vendor == "kvm" || strings.Contains(vendor, "kvm"): - return "kvm" - case vendor == "microsoft" || strings.Contains(vendor, "hyper-v") || strings.Contains(vendor, "msvm") || strings.Contains(vendor, "mshyperv"): - return "microsoft" // systemd uses "microsoft" for Hyper-V/WSL - case strings.Contains(vendor, "vmware"): - return "vmware" - case strings.Contains(vendor, "xen"): - return "xen" - case strings.Contains(vendor, "bhyve"): - return "bhyve" - case strings.Contains(vendor, "qemu"): - return "qemu" - case strings.Contains(vendor, "parallels"): - return "parallels" - case strings.Contains(vendor, "oracle") || strings.Contains(vendor, "virtualbox") || strings.Contains(vendor, "vbox"): - return "oracle" // systemd reports "oracle" for VirtualBox - case strings.Contains(vendor, "acrn"): - return "acrn" - default: - if vendor != "" { - return fmt.Sprintf("hypervisor:%s", vendor) - } - return "virtualized" + + vendorMap := map[string][]string{ + "kvm": {"kvm"}, + "microsoft": {"microsoft", "hyper-v", "msvm", "mshyperv"}, + "vmware": {"vmware"}, + "xen": {"xen"}, + "bhyve": {"bhyve"}, + "qemu": {"qemu"}, + "parallels": {"parallels"}, + "oracle": {"oracle", "virtualbox", "vbox"}, + "acrn": {"acrn"}, } + + for name, keys := range vendorMap { + for _, key := range keys { + if vendor == key || strings.Contains(vendor, key) { + return name + } + } + } + if vendor != "" { + return vendor + } + return "virtualized" } // detectContainer attempts to detect common Linux container environments when systemd isn't available.