-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos-info.go
103 lines (85 loc) · 2.51 KB
/
os-info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package lwhelper
import (
"bufio"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
)
type OSInfoS struct {
Name string `json:"name,omitempty"`
Vendor string `json:"vendor,omitempty"` // Linux Distro
Version string `json:"version,omitempty"`
Release string `json:"release,omitempty"`
Platform string `json:"platform,omitempty"`
Architecture string `json:"architecture,omitempty"`
SystemD string `json:"systemd,omitempty"`
}
var (
rePrettyName = regexp.MustCompile(`^PRETTY_NAME=(.*)$`)
reID = regexp.MustCompile(`^ID=(.*)$`)
reVersionID = regexp.MustCompile(`^VERSION_ID=(.*)$`)
reUbuntu = regexp.MustCompile(`[\( ]([\d\.]+)`)
reCentOS = regexp.MustCompile(`^CentOS( Linux)? release ([\d\.]+) `)
reRedHat = regexp.MustCompile(`[\( ]([\d\.]+)`)
)
func OSInfo() (osInfo OSInfoS) {
osInfo.Platform = runtime.GOOS
// This seems to be the best and most portable way to detect OS architecture (NOT kernel!)
if _, err := os.Stat("/lib64/ld-linux-x86-64.so.2"); err == nil {
osInfo.Architecture = "amd64"
} else if _, err := os.Stat("/lib/ld-linux.so.2"); err == nil {
osInfo.Architecture = "i386"
}
f, err := os.Open("/etc/os-release")
if err != nil {
return
}
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
if m := rePrettyName.FindStringSubmatch(s.Text()); m != nil {
osInfo.Name = strings.Trim(m[1], `"`)
} else if m := reID.FindStringSubmatch(s.Text()); m != nil {
osInfo.Vendor = strings.Trim(m[1], `"`)
} else if m := reVersionID.FindStringSubmatch(s.Text()); m != nil {
osInfo.Version = strings.Trim(m[1], `"`)
}
}
switch osInfo.Vendor {
case "debian":
osInfo.Release = ReadFile("/etc/debian_version")
case "ubuntu":
if m := reUbuntu.FindStringSubmatch(osInfo.Name); m != nil {
osInfo.Release = m[1]
}
case "centos":
if release := ReadFile("/etc/centos-release"); release != "" {
if m := reCentOS.FindStringSubmatch(release); m != nil {
osInfo.Release = m[2]
}
}
case "rhel":
if release := ReadFile("/etc/redhat-release"); release != "" {
if m := reRedHat.FindStringSubmatch(release); m != nil {
osInfo.Release = m[1]
}
}
if osInfo.Release == "" {
if m := reRedHat.FindStringSubmatch(osInfo.Name); m != nil {
osInfo.Release = m[1]
}
}
}
// ---
// systemd
buf, err := exec.Command("systemctl", "--version").CombinedOutput()
if err == nil {
s := strings.TrimSpace(string(buf))
s = strings.TrimPrefix(s, "systemd ")
s = strings.Fields(s)[0]
osInfo.SystemD = s
}
return
}