forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 9
/
load.go
55 lines (45 loc) · 1.17 KB
/
load.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
// +build darwin freebsd linux openbsd
package load
import (
"github.com/pkg/errors"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
"github.com/elastic/beats/metricbeat/module/system"
)
func init() {
if err := mb.Registry.AddMetricSet("system", "load", New, parse.EmptyHostParser); err != nil {
panic(err)
}
}
// MetricSet for fetching system CPU load metrics.
type MetricSet struct {
mb.BaseMetricSet
}
// New returns a new load MetricSet.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return &MetricSet{
BaseMetricSet: base,
}, nil
}
// Fetch fetches system load metrics.
func (m *MetricSet) Fetch() (common.MapStr, error) {
load, err := system.Load()
if err != nil {
return nil, errors.Wrap(err, "failed to get CPU load values")
}
avgs := load.Averages()
normAvgs := load.NormalizedAverages()
event := common.MapStr{
"cores": system.NumCPU,
"1": avgs.OneMinute,
"5": avgs.FiveMinute,
"15": avgs.FifteenMinute,
"norm": common.MapStr{
"1": normAvgs.OneMinute,
"5": normAvgs.FiveMinute,
"15": normAvgs.FifteenMinute,
},
}
return event, nil
}