Skip to content

Commit 5d2084f

Browse files
authored
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
1 parent 8c32fa3 commit 5d2084f

File tree

5 files changed

+66
-3
lines changed

5 files changed

+66
-3
lines changed

agent/app/dto/dashboard.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type DashboardBase struct {
1313
Platform string `json:"platform"`
1414
PlatformFamily string `json:"platformFamily"`
1515
PlatformVersion string `json:"platformVersion"`
16+
PrettyDistro string `json:"prettyDistro"`
1617
KernelArch string `json:"kernelArch"`
1718
KernelVersion string `json:"kernelVersion"`
1819
VirtualizationSystem string `json:"virtualizationSystem"`
@@ -49,6 +50,7 @@ type OsInfo struct {
4950
PlatformFamily string `json:"platformFamily"`
5051
KernelArch string `json:"kernelArch"`
5152
KernelVersion string `json:"kernelVersion"`
53+
PrettyDistro string `json:"prettyDistro"`
5254

5355
DiskSize int64 `json:"diskSize"`
5456
}

agent/app/service/dashboard.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (u *DashboardService) LoadOsInfo() (*dto.OsInfo, error) {
8888
baseInfo.PlatformFamily = hostInfo.PlatformFamily
8989
baseInfo.KernelArch = hostInfo.KernelArch
9090
baseInfo.KernelVersion = hostInfo.KernelVersion
91+
baseInfo.PrettyDistro = psutil.HOST.GetDistro()
9192

9293
diskInfo, err := psutil.DISK.GetUsage(global.Dir.BaseDir, false)
9394
if err == nil {
@@ -152,6 +153,7 @@ func (u *DashboardService) LoadBaseInfo(ioOption string, netOption string) (*dto
152153
Platform: hostInfo.Platform,
153154
PlatformFamily: hostInfo.PlatformFamily,
154155
PlatformVersion: hostInfo.PlatformVersion,
156+
PrettyDistro: psutil.HOST.GetDistro(),
155157
KernelArch: hostInfo.KernelArch,
156158
KernelVersion: hostInfo.KernelVersion,
157159
VirtualizationSystem: string(ss),

agent/utils/psutil/host.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package psutil
22

33
import (
4+
"fmt"
5+
"os"
6+
"strings"
47
"sync"
58
"time"
69

@@ -13,7 +16,8 @@ type HostInfoState struct {
1316
mu sync.RWMutex
1417
lastSampleTime time.Time
1518

16-
cachedInfo *host.InfoStat
19+
cachedInfo *host.InfoStat
20+
cachedDistro string
1721
}
1822

1923
func (h *HostInfoState) GetHostInfo(forceRefresh bool) (*host.InfoStat, error) {
@@ -36,3 +40,52 @@ func (h *HostInfoState) GetHostInfo(forceRefresh bool) (*host.InfoStat, error) {
3640

3741
return hostInfo, nil
3842
}
43+
44+
func (h *HostInfoState) GetDistro() string {
45+
if h.cachedDistro == "" {
46+
h.cachedDistro = detectLinuxDistro()
47+
}
48+
return h.cachedDistro
49+
}
50+
51+
func detectLinuxDistro() string {
52+
distroFiles := []string{
53+
"/etc/os-release",
54+
"/usr/lib/os-release",
55+
}
56+
57+
var targetFile string
58+
for _, f := range distroFiles {
59+
if _, err := os.Stat(f); err == nil {
60+
targetFile = f
61+
break
62+
}
63+
}
64+
65+
if targetFile != "" {
66+
data, err := os.ReadFile(targetFile)
67+
if err == nil {
68+
content := string(data)
69+
for _, line := range strings.Split(content, "\n") {
70+
idx := strings.Index(line, "=")
71+
if idx == -1 {
72+
continue
73+
}
74+
key := line[:idx]
75+
if key == "PRETTY_NAME" {
76+
d := strings.Trim(line[idx+1:], "\"")
77+
if strings.Contains(d, "(") && strings.Contains(d, ")") {
78+
d = d[:strings.LastIndex(d, "(")]
79+
}
80+
return strings.TrimSpace(d)
81+
}
82+
}
83+
}
84+
}
85+
86+
if osInfo, err := host.Info(); err == nil {
87+
return fmt.Sprintf("%s %s", osInfo.Platform, osInfo.PlatformVersion)
88+
}
89+
90+
return "Linux"
91+
}

frontend/src/api/interface/dashboard.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export namespace Dashboard {
5252
platform: string;
5353
platformFamily: string;
5454
platformVersion: string;
55+
prettyDistro: string;
5556
kernelArch: string;
5657
kernelVersion: string;
5758
virtualizationSystem: string;

frontend/src/views/home/index.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@
203203
<span class="system-label">{{ $t('home.platformVersion') }}</span>
204204
</template>
205205
{{
206-
baseInfo.platformVersion
206+
baseInfo.prettyDistro
207+
? baseInfo.prettyDistro
208+
: baseInfo.platformVersion
207209
? baseInfo.platform + '-' + baseInfo.platformVersion
208210
: baseInfo.platform
209211
}}
@@ -393,6 +395,7 @@ const baseInfo = ref<Dashboard.BaseInfo>({
393395
platform: '',
394396
platformFamily: '',
395397
platformVersion: '',
398+
prettyDistro: '',
396399
kernelArch: '',
397400
kernelVersion: '',
398401
virtualizationSystem: '',
@@ -660,7 +663,9 @@ const handleCopy = () => {
660663
'\n' +
661664
i18n.global.t('home.platformVersion') +
662665
': ' +
663-
(baseInfo.value.platformVersion
666+
(baseInfo.value.prettyDistro
667+
? baseInfo.value.prettyDistro
668+
: baseInfo.value.platformVersion
664669
? baseInfo.value.platform + '-' + baseInfo.value.platformVersion
665670
: baseInfo.value.platform) +
666671
'\n' +

0 commit comments

Comments
 (0)