From feaada0a578635e1038cd0e1d72fea41fb7b1723 Mon Sep 17 00:00:00 2001 From: spiritlhl <103393591+spiritLHLS@users.noreply.github.com> Date: Sun, 5 May 2024 02:02:51 +0000 Subject: [PATCH] update --- README.md | 2 +- system/host.go | 70 ++++++++++++++++++++++++++++++++++- system/utils/utils_linux.go | 28 ++++++++++++++ system/utils/utils_windows.go | 5 +++ 4 files changed, 103 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d508d1f..2633152 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# basics \ No newline at end of file +# basics diff --git a/system/host.go b/system/host.go index 3a33ba8..cd585cb 100644 --- a/system/host.go +++ b/system/host.go @@ -3,6 +3,8 @@ package system import ( "context" "fmt" + "os" + "os/exec" "runtime" "time" @@ -11,6 +13,52 @@ import ( "github.com/shirou/gopsutil/host" ) +func getVmTypeFromSDV(path string) string { + cmd := exec.Command(fmt.Sprintf("%s/systemd-detect-virt", path)) + output, err := cmd.Output() + if err == nil { + switch string(output) { + case "kvm": + return "KVM" + case "xen": + return "Xen Hypervisor" + case "microsoft": + return "Microsoft Hyper-V" + case "vmware": + return "VMware" + case "oracle": + return "Oracle VirtualBox" + case "parallels": + return "Parallels" + case "qemu": + return "QEMU" + case "amazon": + return "Amazon Virtualization" + case "docker": + return "Docker" + case "openvz": + return "OpenVZ (Virutozzo)" + case "lxc": + return "LXC" + case "lxc-libvirt": + return "LXC (Based on libvirt)" + case "uml": + return "User-mode Linux" + case "systemd-nspawn": + return "Systemd nspawn" + case "bochs": + return "BOCHS" + case "rkt": + return "RKT" + case "zvm": + return "S390 Z/VM" + case "none": + return "Dedicated" + } + } + return "" +} + func getHostInfo() (string, string, string, string, string, string, string, string, error) { var Platform, Kernal, Arch, VmType, NatType, CurrentTimeZone string var cachedBootTime time.Time @@ -26,7 +74,27 @@ func getHostInfo() (string, string, string, string, string, string, string, stri Platform = hi.Platform + " " + hi.PlatformVersion Arch = hi.KernelArch // 查询虚拟化类型 - VmType = hi.VirtualizationSystem + if runtime.GOOS != "windows" { + path, exit := utils.GetPATH("systemd-detect-virt") + if exit { + VmType = getVmTypeFromSDV(path) + } + if VmType == "" { + _, err := os.Stat("/.dockerenv") + if os.IsExist(err) { + VmType = "Docker" + } + _, err = os.Stat("/dev/lxss") + if os.IsExist(err) { + VmType = "Windows Subsystem for Linux" + } + if VmType == "" { + VmType = "Dedicated (No visible signage)" + } + } + } else { + VmType = hi.VirtualizationSystem + } // 系统运行时长查询 cachedBootTime = time.Unix(int64(hi.BootTime), 0) } diff --git a/system/utils/utils_linux.go b/system/utils/utils_linux.go index fe75079..5389eb7 100644 --- a/system/utils/utils_linux.go +++ b/system/utils/utils_linux.go @@ -2,6 +2,7 @@ package utils import ( "bytes" + "os" "os/exec" "strings" ) @@ -68,3 +69,30 @@ func GetTimeZone() string { } return CurrentTimeZone } + +// GetPATH 检测本机的PATH环境是否含有对应的命令 +func GetPATH(key string) (string,bool) { + // 指定要搜索的目录列表 + dirs := []string{"/usr/local/bin", "/usr/local/sbin", "/usr/bin", "/usr/sbin", "/sbin", "/bin", "/snap/bin"} + // 循环遍历每个目录 + for _, dir := range dirs { + // 检查目录是否存在 + _, err := os.Stat(dir) + if os.IsNotExist(err) { + continue + } + cmd := exec.Command("ls", dir) + output, err := cmd.Output() + if err != nil { + continue + } + files := strings.Split(string(output), "\n") + for _, file := range files { + if file == key { + return dir, true + } + } + } + return "", false +} + diff --git a/system/utils/utils_windows.go b/system/utils/utils_windows.go index 145ecab..aef3015 100644 --- a/system/utils/utils_windows.go +++ b/system/utils/utils_windows.go @@ -130,3 +130,8 @@ func GetTimeZone() string { return "" } } + +// GetPATH 检测本机的PATH环境是否含有对应的命令 +func GetPATH(key string) (string, bool) { + return "", false +}