forked from rancher/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
os_c.go
executable file
·72 lines (62 loc) · 2.03 KB
/
os_c.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 hostInfo
import (
"fmt"
"github.com/pkg/errors"
"github.com/rancher/agent/model"
"github.com/rancher/agent/utilities/constants"
"github.com/rancher/agent/utilities/utils"
)
type OSCollector struct {
InfoData model.InfoData
}
func (o OSCollector) getDockerVersion(infoData model.InfoData, verbose bool) map[string]string {
data := map[string]string{}
versionData := infoData.Version
version := "unknown"
if verbose && versionData.Version != "" {
version = fmt.Sprintf("Docker version %v, build %v", versionData.Version, versionData.GitCommit)
} else if versionData.Version != "" {
version = utils.SemverTrunk(versionData.Version, 2)
}
data["dockerVersion"] = version
return data
}
func (o OSCollector) GetData() (map[string]interface{}, error) {
infoData := o.InfoData
data := map[string]interface{}{}
osData, err := o.getOS(infoData)
if err != nil {
return data, errors.Wrap(err, constants.OSGetDataError+"failed to get OS data")
}
for key, value := range o.getDockerVersion(infoData, true) {
data[key] = value
}
for key, value := range osData {
data[key] = value
}
return data, nil
}
func (o OSCollector) GetLabels(prefix string) (map[string]string, error) {
osData, err := o.getOS(o.InfoData)
if err != nil {
return map[string]string{}, errors.Wrap(err, constants.OSGetDataError+"failed to get OS data")
}
labels := map[string]string{
fmt.Sprintf("%s.%s", prefix, "docker_version"): o.getDockerVersion(o.InfoData, false)["dockerVersion"],
fmt.Sprintf("%s.%s", prefix, "linux_kernel_version"): utils.SemverTrunk(osData["kernelVersion"], 2),
}
return labels, nil
}
func (o OSCollector) KeyName() string {
return "osInfo"
}
func (o OSCollector) getOS(infoData model.InfoData) (map[string]string, error) {
data := map[string]string{}
data["operatingSystem"] = infoData.Info.OperatingSystem
kernelVersion, err := getKernelVersion()
if err != nil {
return map[string]string{}, errors.Wrap(err, constants.GetOSError+"failed to get kernel version")
}
data["kernelVersion"] = kernelVersion
return data, nil
}