forked from TencentBlueKing/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
82 lines (67 loc) · 1.66 KB
/
helper.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
// +build darwin freebsd linux openbsd windows
package memory
import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/module/system"
sigar "github.com/elastic/gosigar"
)
type MemStat struct {
sigar.Mem
UsedPercent float64 `json:"used_p"`
ActualUsedPercent float64 `json:"actual_used_p"`
}
func GetMemory() (*MemStat, error) {
mem := sigar.Mem{}
err := mem.Get()
if err != nil {
return nil, err
}
return &MemStat{Mem: mem}, nil
}
func AddMemPercentage(m *MemStat) {
if m.Mem.Total == 0 {
return
}
perc := float64(m.Mem.Used) / float64(m.Mem.Total)
m.UsedPercent = system.Round(perc)
actualPerc := float64(m.Mem.ActualUsed) / float64(m.Mem.Total)
m.ActualUsedPercent = system.Round(actualPerc)
}
type SwapStat struct {
sigar.Swap
UsedPercent float64 `json:"used_p"`
}
func GetSwap() (*SwapStat, error) {
swap := sigar.Swap{}
err := swap.Get()
if err != nil {
return nil, err
}
return &SwapStat{Swap: swap}, nil
}
func GetMemoryEvent(memStat *MemStat) common.MapStr {
return common.MapStr{
"total": memStat.Total,
"used": memStat.Used,
"free": memStat.Free,
"actual_used": memStat.ActualUsed,
"actual_free": memStat.ActualFree,
"used_p": memStat.UsedPercent,
"actual_used_p": memStat.ActualUsedPercent,
}
}
func GetSwapEvent(swapStat *SwapStat) common.MapStr {
return common.MapStr{
"total": swapStat.Total,
"used": swapStat.Used,
"free": swapStat.Free,
"used_p": swapStat.UsedPercent,
}
}
func AddSwapPercentage(s *SwapStat) {
if s.Swap.Total == 0 {
return
}
perc := float64(s.Swap.Used) / float64(s.Swap.Total)
s.UsedPercent = system.Round(perc)
}