forked from sensu/sensu-go
/
system.go
72 lines (55 loc) · 1.57 KB
/
system.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
// Package system provides information about the system of the current
// process. System information is used for Agent (and potentially
// Backend) Entity context.
package system
import (
"runtime"
"github.com/sensu/sensu-go/types"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/net"
)
const defaultHostname = "unidentified-hostname"
// Info describes the local system, hostname, OS, platform, platform
// family, platform version, and network interfaces.
func Info() (types.System, error) {
info, err := host.Info()
if err != nil {
return types.System{}, err
}
system := types.System{
Arch: runtime.GOARCH,
Hostname: info.Hostname,
OS: info.OS,
Platform: info.Platform,
PlatformFamily: info.PlatformFamily,
PlatformVersion: info.PlatformVersion,
}
if system.Hostname == "" {
system.Hostname = defaultHostname
}
network, err := NetworkInfo()
if err == nil {
system.Network = network
}
return system, nil
}
// NetworkInfo describes the local network interfaces, including their
// names (e.g. eth0), MACs (if available), and addresses.
func NetworkInfo() (types.Network, error) {
interfaces, err := net.Interfaces()
network := types.Network{}
if err != nil {
return network, err
}
for _, i := range interfaces {
nInterface := types.NetworkInterface{
Name: i.Name,
MAC: i.HardwareAddr,
}
for _, address := range i.Addrs {
nInterface.Addresses = append(nInterface.Addresses, address.Addr)
}
network.Interfaces = append(network.Interfaces, nInterface)
}
return network, nil
}