-
Notifications
You must be signed in to change notification settings - Fork 10
/
gohai.go
86 lines (70 loc) · 1.99 KB
/
gohai.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package gohai
import (
"net"
"sync"
"github.com/DataDog/gohai/cpu"
"github.com/DataDog/gohai/filesystem"
"github.com/DataDog/gohai/memory"
"github.com/DataDog/gohai/network"
"github.com/DataDog/gohai/platform"
"github.com/n9e/n9e-agentd/pkg/config"
"k8s.io/klog/v2"
)
var (
// we can use this a hint that docker is running in host mode and it's safe to use detect
docker0Detected = false
)
// GetPayload builds a payload of every metadata collected with gohai except processes metadata.
func GetPayload() *Payload {
return &Payload{
Gohai: getGohaiInfo(),
}
}
func getGohaiInfo() *gohai {
res := new(gohai)
cpuPayload, err := new(cpu.Cpu).Collect()
if err == nil {
res.CPU = cpuPayload
} else {
klog.Errorf("Failed to retrieve cpu metadata: %s", err)
}
fileSystemPayload, err := new(filesystem.FileSystem).Collect()
if err == nil {
res.FileSystem = fileSystemPayload
} else {
klog.Errorf("Failed to retrieve filesystem metadata: %s", err)
}
memoryPayload, err := new(memory.Memory).Collect()
if err == nil {
res.Memory = memoryPayload
} else {
klog.Errorf("Failed to retrieve memory metadata: %s", err)
}
if !config.IsContainerized() || detectDocker0() {
networkPayload, err := new(network.Network).Collect()
if err == nil {
res.Network = networkPayload
} else {
klog.Errorf("Failed to retrieve network metadata: %s", err)
}
}
platformPayload, err := new(platform.Platform).Collect()
if err == nil {
res.Platform = platformPayload
} else {
klog.Errorf("Failed to retrieve platform metadata: %s", err)
}
return res
}
var docker0Detector sync.Once
func detectDocker0() bool {
docker0Detector.Do(func() {
iface, _ := net.InterfaceByName("docker0")
docker0Detected = iface != nil
})
return docker0Detected
}