forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskio.go
63 lines (53 loc) · 1.41 KB
/
diskio.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
// +build freebsd linux windows
package diskio
import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
"github.com/pkg/errors"
"github.com/shirou/gopsutil/disk"
)
func init() {
if err := mb.Registry.AddMetricSet("system", "diskio", New, parse.EmptyHostParser); err != nil {
panic(err)
}
}
// MetricSet for fetching system disk IO metrics.
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 disk IO metrics from the OS.
func (m *MetricSet) Fetch() ([]common.MapStr, error) {
stats, err := disk.IOCounters()
if err != nil {
return nil, errors.Wrap(err, "disk io counters")
}
events := make([]common.MapStr, 0, len(stats))
for _, counters := range stats {
event := common.MapStr{
"name": counters.Name,
"read": common.MapStr{
"count": counters.ReadCount,
"time": counters.ReadTime,
"bytes": counters.ReadBytes,
},
"write": common.MapStr{
"count": counters.WriteCount,
"time": counters.WriteTime,
"bytes": counters.WriteBytes,
},
"io": common.MapStr{
"time": counters.IoTime,
},
}
events = append(events, event)
if counters.SerialNumber != "" {
event["serial_number"] = counters.SerialNumber
}
}
return events, nil
}