-
Notifications
You must be signed in to change notification settings - Fork 204
/
hostParametersGetter.go
64 lines (52 loc) · 1.42 KB
/
hostParametersGetter.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
package hostParameters
import (
"fmt"
"sort"
"strings"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/mem"
)
type hostParametersGetter struct {
versionString string
}
// NewHostParameterGetter will create a structure that is able to get and format the host's relevant parameters
func NewHostParameterGetter(version string) *hostParametersGetter {
return &hostParametersGetter{
versionString: version,
}
}
// GetHostInfo is able to get all the known parameters of a host
func (hpg *hostParametersGetter) GetHostInfo() *HostInfo {
hi := &HostInfo{
AppVersion: hpg.versionString,
}
hpg.applyCpuInfo(hi)
hpg.applyMemInfo(hi)
return hi
}
func (hpg *hostParametersGetter) applyCpuInfo(hi *HostInfo) {
rawCpuInfo, err := cpu.Info()
if err != nil {
hi.CPUModel = fmt.Sprintf("[ERR:%s]", err)
return
}
if len(rawCpuInfo) == 0 {
hi.CPUModel = "[ERR:no logical cpus]"
return
}
hi.CPUNumLogical = len(rawCpuInfo)
hi.CPUModel = rawCpuInfo[0].ModelName
hi.CPUMaxFreqInMHz = int(rawCpuInfo[0].Mhz)
hi.CPUFlags = rawCpuInfo[0].Flags
sort.Slice(hi.CPUFlags, func(i, j int) bool {
return strings.Compare(hi.CPUFlags[i], hi.CPUFlags[j]) < 0
})
}
func (hpg *hostParametersGetter) applyMemInfo(hi *HostInfo) {
vms, err := mem.VirtualMemory()
if err != nil {
hi.MemorySize = fmt.Sprintf("[ERR:%s]", err)
}
hi.MemorySize = core.ConvertBytes(vms.Total)
}