From 5d2084fda4da7b85eab1284c341a40f97475b3bc Mon Sep 17 00:00:00 2001 From: KOMATA <20227709+HynoR@users.noreply.github.com> Date: Wed, 3 Dec 2025 14:14:35 +0800 Subject: [PATCH] feat: improve linux distribution information get logic (#11171) * feat: Add PrettyDistro field to dashboard information and update related services and frontend components * fix: Trim whitespace and parentheses from detected Linux distribution name in GetDistro method * fix: Correctly trim parentheses from detected Linux distribution name in GetDistro method * refactor: Simplify Linux distribution detection by removing unnecessary checks and consolidating logic --- agent/app/dto/dashboard.go | 2 + agent/app/service/dashboard.go | 2 + agent/utils/psutil/host.go | 55 ++++++++++++++++++++++++- frontend/src/api/interface/dashboard.ts | 1 + frontend/src/views/home/index.vue | 9 +++- 5 files changed, 66 insertions(+), 3 deletions(-) diff --git a/agent/app/dto/dashboard.go b/agent/app/dto/dashboard.go index fd419fba5..2fb4fd9d0 100644 --- a/agent/app/dto/dashboard.go +++ b/agent/app/dto/dashboard.go @@ -13,6 +13,7 @@ type DashboardBase struct { Platform string `json:"platform"` PlatformFamily string `json:"platformFamily"` PlatformVersion string `json:"platformVersion"` + PrettyDistro string `json:"prettyDistro"` KernelArch string `json:"kernelArch"` KernelVersion string `json:"kernelVersion"` VirtualizationSystem string `json:"virtualizationSystem"` @@ -49,6 +50,7 @@ type OsInfo struct { PlatformFamily string `json:"platformFamily"` KernelArch string `json:"kernelArch"` KernelVersion string `json:"kernelVersion"` + PrettyDistro string `json:"prettyDistro"` DiskSize int64 `json:"diskSize"` } diff --git a/agent/app/service/dashboard.go b/agent/app/service/dashboard.go index 1e67e7147..42e385bcb 100644 --- a/agent/app/service/dashboard.go +++ b/agent/app/service/dashboard.go @@ -88,6 +88,7 @@ func (u *DashboardService) LoadOsInfo() (*dto.OsInfo, error) { baseInfo.PlatformFamily = hostInfo.PlatformFamily baseInfo.KernelArch = hostInfo.KernelArch baseInfo.KernelVersion = hostInfo.KernelVersion + baseInfo.PrettyDistro = psutil.HOST.GetDistro() diskInfo, err := psutil.DISK.GetUsage(global.Dir.BaseDir, false) if err == nil { @@ -152,6 +153,7 @@ func (u *DashboardService) LoadBaseInfo(ioOption string, netOption string) (*dto Platform: hostInfo.Platform, PlatformFamily: hostInfo.PlatformFamily, PlatformVersion: hostInfo.PlatformVersion, + PrettyDistro: psutil.HOST.GetDistro(), KernelArch: hostInfo.KernelArch, KernelVersion: hostInfo.KernelVersion, VirtualizationSystem: string(ss), diff --git a/agent/utils/psutil/host.go b/agent/utils/psutil/host.go index 98e99f326..20ee730c8 100644 --- a/agent/utils/psutil/host.go +++ b/agent/utils/psutil/host.go @@ -1,6 +1,9 @@ package psutil import ( + "fmt" + "os" + "strings" "sync" "time" @@ -13,7 +16,8 @@ type HostInfoState struct { mu sync.RWMutex lastSampleTime time.Time - cachedInfo *host.InfoStat + cachedInfo *host.InfoStat + cachedDistro string } func (h *HostInfoState) GetHostInfo(forceRefresh bool) (*host.InfoStat, error) { @@ -36,3 +40,52 @@ func (h *HostInfoState) GetHostInfo(forceRefresh bool) (*host.InfoStat, error) { return hostInfo, nil } + +func (h *HostInfoState) GetDistro() string { + if h.cachedDistro == "" { + h.cachedDistro = detectLinuxDistro() + } + return h.cachedDistro +} + +func detectLinuxDistro() string { + distroFiles := []string{ + "/etc/os-release", + "/usr/lib/os-release", + } + + var targetFile string + for _, f := range distroFiles { + if _, err := os.Stat(f); err == nil { + targetFile = f + break + } + } + + if targetFile != "" { + data, err := os.ReadFile(targetFile) + if err == nil { + content := string(data) + for _, line := range strings.Split(content, "\n") { + idx := strings.Index(line, "=") + if idx == -1 { + continue + } + key := line[:idx] + if key == "PRETTY_NAME" { + d := strings.Trim(line[idx+1:], "\"") + if strings.Contains(d, "(") && strings.Contains(d, ")") { + d = d[:strings.LastIndex(d, "(")] + } + return strings.TrimSpace(d) + } + } + } + } + + if osInfo, err := host.Info(); err == nil { + return fmt.Sprintf("%s %s", osInfo.Platform, osInfo.PlatformVersion) + } + + return "Linux" +} diff --git a/frontend/src/api/interface/dashboard.ts b/frontend/src/api/interface/dashboard.ts index e8f41f20a..ea733e844 100644 --- a/frontend/src/api/interface/dashboard.ts +++ b/frontend/src/api/interface/dashboard.ts @@ -52,6 +52,7 @@ export namespace Dashboard { platform: string; platformFamily: string; platformVersion: string; + prettyDistro: string; kernelArch: string; kernelVersion: string; virtualizationSystem: string; diff --git a/frontend/src/views/home/index.vue b/frontend/src/views/home/index.vue index 8cae01349..0cce33776 100644 --- a/frontend/src/views/home/index.vue +++ b/frontend/src/views/home/index.vue @@ -203,7 +203,9 @@ {{ $t('home.platformVersion') }} {{ - baseInfo.platformVersion + baseInfo.prettyDistro + ? baseInfo.prettyDistro + : baseInfo.platformVersion ? baseInfo.platform + '-' + baseInfo.platformVersion : baseInfo.platform }} @@ -393,6 +395,7 @@ const baseInfo = ref({ platform: '', platformFamily: '', platformVersion: '', + prettyDistro: '', kernelArch: '', kernelVersion: '', virtualizationSystem: '', @@ -660,7 +663,9 @@ const handleCopy = () => { '\n' + i18n.global.t('home.platformVersion') + ': ' + - (baseInfo.value.platformVersion + (baseInfo.value.prettyDistro + ? baseInfo.value.prettyDistro + : baseInfo.value.platformVersion ? baseInfo.value.platform + '-' + baseInfo.value.platformVersion : baseInfo.value.platform) + '\n' +