forked from cloudradar-monitoring/cagent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
system.go
129 lines (112 loc) · 2.58 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
package cagent
import (
"context"
"errors"
"net"
"os"
"runtime"
"strings"
"time"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
log "github.com/sirupsen/logrus"
"github.com/cloudradar-monitoring/cagent/pkg/common"
"github.com/cloudradar-monitoring/cagent/pkg/osinfo"
)
var (
cpuInfoTimeout = 10 * time.Second
hostInfoTimeout = 10 * time.Second
)
func Uname() (string, error) {
return osinfo.GetOsName()
}
func (ca *Cagent) HostInfoResults() (common.MeasurementsMap, error) {
res := common.MeasurementsMap{}
if len(ca.Config.SystemFields) == 0 {
return nil, nil
}
ctx, cancel := context.WithTimeout(context.Background(), hostInfoTimeout)
defer cancel()
info, err := host.InfoWithContext(ctx)
var errs []string
if err != nil {
log.Errorf("[SYSTEM] Failed to read host info: %s", err.Error())
errs = append(errs, err.Error())
}
for _, field := range ca.Config.SystemFields {
switch strings.ToLower(field) {
case "os_kernel":
if info != nil {
res[field] = info.OS
} else {
res[field] = nil
}
case "os_family":
if info != nil {
res[field] = info.PlatformFamily
} else {
res[field] = nil
}
case "uname":
uname, err := Uname()
if err != nil {
log.Errorf("[SYSTEM] Failed to read host uname: %s", err.Error())
errs = append(errs, err.Error())
res[field] = nil
} else {
res[field] = uname
}
case "fqdn":
res[field] = getFQDN()
case "cpu_model":
cpuName, err := getProcessorModelName()
if err != nil {
log.Errorf("[SYSTEM] Failed to read cpu info: %s", err.Error())
errs = append(errs, err.Error())
res[field] = nil
continue
}
res[field] = cpuName
case "os_arch":
res[field] = runtime.GOARCH
case "memory_total_b":
memStat, err := mem.VirtualMemory()
if err != nil {
log.Errorf("[SYSTEM] Failed to read mem info: %s", err.Error())
errs = append(errs, err.Error())
res[field] = nil
continue
}
res[field] = memStat.Total
}
}
if len(errs) == 0 {
return res, nil
}
return res, errors.New("SYSTEM: " + strings.Join(errs, "; "))
}
func getFQDN() string {
hostname, err := os.Hostname()
if err != nil {
return "unknown"
}
addrs, err := net.LookupIP(hostname)
if err != nil {
return hostname
}
for _, addr := range addrs {
if ipv4 := addr.To4(); ipv4 != nil {
ip, err := ipv4.MarshalText()
if err != nil {
return hostname
}
hosts, err := net.LookupAddr(string(ip))
if err != nil || len(hosts) == 0 {
return hostname
}
fqdn := hosts[0]
return strings.TrimSuffix(fqdn, ".")
}
}
return hostname
}