-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsysinfo_linux.go
More file actions
90 lines (79 loc) · 1.88 KB
/
sysinfo_linux.go
File metadata and controls
90 lines (79 loc) · 1.88 KB
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
package sysinfo
import (
"io/ioutil"
"os/exec"
"strconv"
"strings"
)
func getPlatformSpecifics(info SystemInfo) SystemInfo {
info.Name, info.Version = linuxVersion()
info.Model = "UNKNOWN" // No easy way to find out system details on Linux
info.CPU = "UNKNOWN"
info.Memory = -1
out, err := exec.Command("cat", "/proc/cpuinfo").Output()
if err == nil {
for _, line := range strings.Split(string(out), "\n") {
fs := strings.Split(line, ":")
if len(fs) < 2 {
continue
}
k := strings.TrimSpace(fs[0])
v := strings.TrimSpace(fs[1])
if k == "model name" {
info.CPU = v
break
}
}
}
out, err = exec.Command("cat", "/proc/meminfo").Output()
if err == nil {
for _, line := range strings.Split(string(out), "\n") {
fs := strings.Split(line, ":")
if len(fs) < 2 {
continue
}
k := strings.TrimSpace(fs[0])
v := strings.TrimSpace(fs[1])
if k == "MemTotal" {
v = strings.ReplaceAll(v, " kB", "")
n, _ := strconv.ParseInt(v, 10, 64)
info.Memory = n * 1024
break
}
}
}
return info
}
// getLinuxVersion is a bit tedious as we try to figure out the vendor
// and the vendor version and there seem to be a lot of different
// variants for this.
func linuxVersion() (name string, version string) {
name = "UNKNOWN"
version = "UNKNOWN"
content, err := ioutil.ReadFile("/etc/alpine-release")
if err == nil {
name = "Alpine"
version = strings.TrimSpace(string(content[:]))
return name, version
}
// lsb_release is the fallback
out, err := exec.Command("lsb_release", "-a").Output()
if err != nil {
return name, version
}
for _, line := range strings.Split(string(out), "\n") {
fs := strings.Split(line, ":")
if len(fs) < 2 {
continue
}
k := strings.TrimSpace(fs[0])
v := strings.TrimSpace(fs[1])
switch k {
case "Distributor ID":
name = v
case "Release":
version = v
}
}
return name, version
}