Skip to content

Commit 441a9c0

Browse files
committed
refactor: Simplify Linux distribution detection by removing unnecessary checks and consolidating logic
1 parent 5548651 commit 441a9c0

File tree

1 file changed

+15
-52
lines changed

1 file changed

+15
-52
lines changed

agent/utils/psutil/host.go

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package psutil
22

33
import (
4+
"fmt"
45
"os"
56
"strings"
67
"sync"
@@ -42,11 +43,7 @@ func (h *HostInfoState) GetHostInfo(forceRefresh bool) (*host.InfoStat, error) {
4243

4344
func (h *HostInfoState) GetDistro() string {
4445
if h.cachedDistro == "" {
45-
d := detectLinuxDistro()
46-
if strings.Contains(d, "(") && strings.Contains(d, ")") {
47-
d = d[:strings.LastIndex(d, "(")]
48-
}
49-
h.cachedDistro = strings.TrimSpace(d)
46+
h.cachedDistro = detectLinuxDistro()
5047
}
5148
return h.cachedDistro
5249
}
@@ -55,10 +52,6 @@ func detectLinuxDistro() string {
5552
distroFiles := []string{
5653
"/etc/os-release",
5754
"/usr/lib/os-release",
58-
"/etc/lsb-release",
59-
"/etc/redhat-release",
60-
"/etc/debian_version",
61-
"/etc/issue",
6255
}
6356

6457
var targetFile string
@@ -73,56 +66,26 @@ func detectLinuxDistro() string {
7366
data, err := os.ReadFile(targetFile)
7467
if err == nil {
7568
content := string(data)
76-
switch targetFile {
77-
case "/etc/os-release", "/usr/lib/os-release":
78-
if v := findKeyValues(content, "PRETTY_NAME"); v["PRETTY_NAME"] != "" {
79-
return v["PRETTY_NAME"]
80-
}
81-
if v := findKeyValues(content, "NAME", "VERSION_ID"); v["NAME"] != "" && v["VERSION_ID"] != "" {
82-
return v["NAME"] + " " + v["VERSION_ID"]
83-
}
84-
case "/etc/lsb-release":
85-
if v := findKeyValues(content, "DISTRIB_DESCRIPTION"); v["DISTRIB_DESCRIPTION"] != "" {
86-
return v["DISTRIB_DESCRIPTION"]
69+
for _, line := range strings.Split(content, "\n") {
70+
idx := strings.Index(line, "=")
71+
if idx == -1 {
72+
continue
8773
}
88-
if v := findKeyValues(content, "DISTRIB_ID", "DISTRIB_RELEASE"); v["DISTRIB_ID"] != "" && v["DISTRIB_RELEASE"] != "" {
89-
return v["DISTRIB_ID"] + " " + v["DISTRIB_RELEASE"]
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)
9081
}
91-
case "/etc/redhat-release", "/etc/issue":
92-
return strings.TrimSpace(content)
93-
case "/etc/debian_version":
94-
return "Debian " + strings.TrimSpace(content)
9582
}
9683
}
9784
}
9885

99-
// gopsutil fallback
10086
if osInfo, err := host.Info(); err == nil {
101-
return osInfo.OS
87+
return fmt.Sprintf("%s %s", osInfo.Platform, osInfo.PlatformVersion)
10288
}
10389

104-
return "Unknown Linux"
105-
}
106-
107-
func findKeyValues(data string, keys ...string) map[string]string {
108-
result := make(map[string]string, len(keys))
109-
found := 0
110-
for _, line := range strings.Split(data, "\n") {
111-
idx := strings.Index(line, "=")
112-
if idx == -1 {
113-
continue
114-
}
115-
key := line[:idx]
116-
for _, k := range keys {
117-
if key == k {
118-
result[k] = strings.Trim(line[idx+1:], "\"")
119-
found++
120-
if found == len(keys) {
121-
return result
122-
}
123-
break
124-
}
125-
}
126-
}
127-
return result
90+
return "Linux"
12891
}

0 commit comments

Comments
 (0)