forked from bigpigeon/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
72 lines (60 loc) · 1.52 KB
/
memory.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
// +build darwin freebsd linux openbsd windows
// +build darwin freebsd linux openbsd windows
package memory
import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
"github.com/pkg/errors"
)
func init() {
if err := mb.Registry.AddMetricSet("system", "memory", New, parse.EmptyHostParser); err != nil {
panic(err)
}
}
// MetricSet for fetching system memory metrics.
type MetricSet struct {
mb.BaseMetricSet
}
// New is a mb.MetricSetFactory that returns a memory.MetricSet.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return &MetricSet{base}, nil
}
// Fetch fetches memory metrics from the OS.
func (m *MetricSet) Fetch() (event common.MapStr, err error) {
memStat, err := GetMemory()
if err != nil {
return nil, errors.Wrap(err, "memory")
}
AddMemPercentage(memStat)
swapStat, err := GetSwap()
if err != nil {
return nil, errors.Wrap(err, "swap")
}
AddSwapPercentage(swapStat)
memory := common.MapStr{
"total": memStat.Total,
"used": common.MapStr{
"bytes": memStat.Used,
"pct": memStat.UsedPercent,
},
"free": memStat.Free,
"actual": common.MapStr{
"free": memStat.ActualFree,
"used": common.MapStr{
"pct": memStat.ActualUsedPercent,
"bytes": memStat.ActualUsed,
},
},
}
swap := common.MapStr{
"total": swapStat.Total,
"used": common.MapStr{
"bytes": swapStat.Used,
"pct": swapStat.UsedPercent,
},
"free": swapStat.Free,
}
memory["swap"] = swap
return memory, nil
}