forked from cloudradar/frontman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostinfo.go
100 lines (90 loc) · 2.25 KB
/
hostinfo.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
package frontman
import (
"context"
"fmt"
"os"
"runtime"
"strings"
"time"
"github.com/pkg/errors"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
"github.com/sirupsen/logrus"
)
// HostInfoResults fetches information about the host itself which can be
// send to the hub alongside measurements.
func (fm *Frontman) HostInfoResults() (MeasurementsMap, error) {
res := MeasurementsMap{}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
info, err := host.InfoWithContext(ctx)
errs := []string{}
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
err = fmt.Errorf("timeout exceeded")
}
logrus.Errorf("[SYSTEM] Failed to read host info: %s", err.Error())
errs = append(errs, err.Error())
}
for _, field := range fm.Config.HostInfo {
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 {
logrus.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":
cpuInfo, err := cpu.Info()
if err != nil {
logrus.Errorf("[SYSTEM] Failed to read cpu info: %s", err.Error())
errs = append(errs, err.Error())
res[field] = nil
continue
}
res[field] = cpuInfo[0].ModelName
case "os_arch":
res[field] = runtime.GOARCH
case "memory_total_b":
memStat, err := mem.VirtualMemory()
if err != nil {
logrus.Errorf("[SYSTEM] Failed to read mem info: %s", err.Error())
errs = append(errs, err.Error())
res[field] = nil
continue
}
res[field] = memStat.Total
case "hostname":
name, err := os.Hostname()
if err != nil {
logrus.Errorf("[SYSTEM] Failed to read hostname: %s", err.Error())
errs = append(errs, err.Error())
res[field] = nil
continue
}
res[field] = name
}
}
if len(errs) == 0 {
return res, nil
}
return res, errors.New("SYSTEM: " + strings.Join(errs, "; "))
}