-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo.go
75 lines (69 loc) · 1.83 KB
/
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
package cloudinfo
import (
"net"
"os"
"strings"
)
type Info struct {
Provider string `json:"provider"` // provider name code, such as "aws", "gcp", etc
AccountId string `json:"account_id,omitempty"` // account id with provider
Architecture string `json:"architecture,omitempty"` // x86_64
PublicIP IPList `json:"public_ip,omitempty"`
PrivateIP IPList `json:"private_ip,omitempty"`
Name string `json:"name,omitempty"`
Hostname string `json:"hostname,omitempty"`
Image string `json:"image,omitempty"`
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Location LocationArray `json:"location,omitempty"` // structured location
DMI *DMI `json:"dmi,omitempty"`
}
// Sysinfo loads and return information available from the local machine (including DMI)
func Sysinfo() *Info {
info := sysinfo()
info.fix()
return info
}
func sysinfo() *Info {
dmi, _ := ReadDMI()
info := &Info{
Architecture: getArch(),
Provider: dmi.Cloud,
DMI: dmi,
}
iflist, _ := net.Interfaces()
for _, intf := range iflist {
addrs, _ := intf.Addrs()
for _, addr := range addrs {
switch a := addr.(type) {
case *net.IPNet:
if a.IP.IsLoopback() {
break
}
if a.IP.IsPrivate() || a.IP.IsLinkLocalUnicast() {
info.PrivateIP.addIP(a.IP)
break
}
info.PublicIP.addIP(a.IP)
}
}
}
return info
}
func (info *Info) fix() {
if info.Hostname == "" {
if h, err := os.Hostname(); err == nil {
info.Hostname = h
}
}
if info.Name == "" && info.Hostname != "" {
str := info.Hostname
if pos := strings.IndexByte(str, '.'); pos > 0 {
str = str[:pos]
}
info.Name = str
}
if info.ID == "" {
info.ID = info.DMI.ProductUUID
}
}