-
-
Notifications
You must be signed in to change notification settings - Fork 285
/
system.go
144 lines (125 loc) · 3.23 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package system
import (
"context"
"runtime"
"github.com/acobaugh/osrelease"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/system"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/parsers/kernel"
)
type Information struct {
Version string `json:"version"`
Docker DockerInformation `json:"docker"`
System System `json:"system"`
}
type DockerInformation struct {
Version string `json:"version"`
Cgroups DockerCgroups `json:"cgroups"`
Containers DockerContainers `json:"containers"`
Storage DockerStorage `json:"storage"`
Runc DockerRunc `json:"runc"`
}
type DockerCgroups struct {
Driver string `json:"driver"`
Version string `json:"version"`
}
type DockerContainers struct {
Total int `json:"total"`
Running int `json:"running"`
Paused int `json:"paused"`
Stopped int `json:"stopped"`
}
type DockerStorage struct {
Driver string `json:"driver"`
Filesystem string `json:"filesystem"`
}
type DockerRunc struct {
Version string `json:"version"`
}
type System struct {
Architecture string `json:"architecture"`
CPUThreads int `json:"cpu_threads"`
MemoryBytes int64 `json:"memory_bytes"`
KernelVersion string `json:"kernel_version"`
OS string `json:"os"`
OSType string `json:"os_type"`
}
func GetSystemInformation() (*Information, error) {
k, err := kernel.GetKernelVersion()
if err != nil {
return nil, err
}
version, info, err := GetDockerInfo(context.Background())
if err != nil {
return nil, err
}
release, err := osrelease.Read()
if err != nil {
return nil, err
}
var os string
if release["PRETTY_NAME"] != "" {
os = release["PRETTY_NAME"]
} else if release["NAME"] != "" {
os = release["NAME"]
} else {
os = info.OperatingSystem
}
var filesystem string
for _, v := range info.DriverStatus {
if v[0] != "Backing Filesystem" {
continue
}
filesystem = v[1]
break
}
return &Information{
Version: Version,
Docker: DockerInformation{
Version: version.Version,
Cgroups: DockerCgroups{
Driver: info.CgroupDriver,
Version: info.CgroupVersion,
},
Containers: DockerContainers{
Total: info.Containers,
Running: info.ContainersRunning,
Paused: info.ContainersPaused,
Stopped: info.ContainersStopped,
},
Storage: DockerStorage{
Driver: info.Driver,
Filesystem: filesystem,
},
Runc: DockerRunc{
Version: info.RuncCommit.ID,
},
},
System: System{
Architecture: runtime.GOARCH,
CPUThreads: runtime.NumCPU(),
MemoryBytes: info.MemTotal,
KernelVersion: k.String(),
OS: os,
OSType: runtime.GOOS,
},
}, nil
}
func GetDockerInfo(ctx context.Context) (types.Version, system.Info, error) {
// TODO: find a way to re-use the client from the docker environment.
c, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return types.Version{}, system.Info{}, err
}
defer c.Close()
dockerVersion, err := c.ServerVersion(ctx)
if err != nil {
return types.Version{}, system.Info{}, err
}
dockerInfo, err := c.Info(ctx)
if err != nil {
return types.Version{}, system.Info{}, err
}
return dockerVersion, dockerInfo, nil
}