forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetricset.go
42 lines (34 loc) · 965 Bytes
/
metricset.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
// +build darwin linux openbsd windows
package uptime
import (
"github.com/pkg/errors"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
sigar "github.com/elastic/gosigar"
)
func init() {
if err := mb.Registry.AddMetricSet("system", "uptime", New, parse.EmptyHostParser); err != nil {
panic(err)
}
}
// MetricSet for fetching an OS uptime metric.
type MetricSet struct {
mb.BaseMetricSet
}
// New is a mb.MetricSetFactory that returns a new MetricSet.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return &MetricSet{base}, nil
}
// Fetch fetches the uptime metric from the OS.
func (m *MetricSet) Fetch() (common.MapStr, error) {
var uptime sigar.Uptime
if err := uptime.Get(); err != nil {
return nil, errors.Wrap(err, "failed to get uptime")
}
return common.MapStr{
"duration": common.MapStr{
"ms": int64(uptime.Length * 1000),
},
}, nil
}