2020-04-06 01:00:33 +00:00
|
|
|
package system
|
2019-12-10 05:05:55 +00:00
|
|
|
|
|
|
|
import (
|
2022-11-21 23:01:14 +00:00
|
|
|
"context"
|
2019-12-10 05:05:55 +00:00
|
|
|
"runtime"
|
2021-01-10 01:22:39 +00:00
|
|
|
|
2022-11-21 23:01:14 +00:00
|
|
|
"github.com/acobaugh/osrelease"
|
|
|
|
"github.com/docker/docker/api/types"
|
2024-03-13 03:42:58 +00:00
|
|
|
"github.com/docker/docker/api/types/system"
|
2022-11-21 23:01:14 +00:00
|
|
|
"github.com/docker/docker/client"
|
2021-01-10 01:22:39 +00:00
|
|
|
"github.com/docker/docker/pkg/parsers/kernel"
|
2019-12-10 05:05:55 +00:00
|
|
|
)
|
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
type Information struct {
|
2022-11-21 23:01:14 +00:00
|
|
|
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 {
|
2019-12-10 05:05:55 +00:00
|
|
|
Architecture string `json:"architecture"`
|
2022-11-21 23:01:14 +00:00
|
|
|
CPUThreads int `json:"cpu_threads"`
|
|
|
|
MemoryBytes int64 `json:"memory_bytes"`
|
|
|
|
KernelVersion string `json:"kernel_version"`
|
2019-12-10 05:05:55 +00:00
|
|
|
OS string `json:"os"`
|
2022-11-21 23:01:14 +00:00
|
|
|
OSType string `json:"os_type"`
|
2019-12-10 05:05:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
func GetSystemInformation() (*Information, error) {
|
2019-12-10 05:05:55 +00:00
|
|
|
k, err := kernel.GetKernelVersion()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-21 23:01:14 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-03-13 03:42:58 +00:00
|
|
|
func GetDockerInfo(ctx context.Context) (types.Version, system.Info, error) {
|
2022-11-21 23:01:14 +00:00
|
|
|
// TODO: find a way to re-use the client from the docker environment.
|
|
|
|
c, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
|
|
if err != nil {
|
2024-03-13 03:42:58 +00:00
|
|
|
return types.Version{}, system.Info{}, err
|
2022-11-21 23:01:14 +00:00
|
|
|
}
|
2023-01-24 19:33:20 +00:00
|
|
|
defer c.Close()
|
2022-11-21 23:01:14 +00:00
|
|
|
|
|
|
|
dockerVersion, err := c.ServerVersion(ctx)
|
|
|
|
if err != nil {
|
2024-03-13 03:42:58 +00:00
|
|
|
return types.Version{}, system.Info{}, err
|
2022-11-21 23:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dockerInfo, err := c.Info(ctx)
|
|
|
|
if err != nil {
|
2024-03-13 03:42:58 +00:00
|
|
|
return types.Version{}, system.Info{}, err
|
2019-12-10 05:05:55 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 23:01:14 +00:00
|
|
|
return dockerVersion, dockerInfo, nil
|
2019-12-10 05:05:55 +00:00
|
|
|
}
|