forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskio.go
109 lines (94 loc) · 2.66 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// +build darwin,cgo 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
statistics *DiskIOStat
}
// New is a mb.MetricSetFactory that returns a new MetricSet.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
ms := &MetricSet{
BaseMetricSet: base,
statistics: NewDiskIOStat(),
}
return ms, 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")
}
// open a sampling means sample the current cpu counter
m.statistics.OpenSampling()
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,
},
}
extraMetrics, err := m.statistics.CalIOStatistics(counters)
if err == nil {
event["iostat"] = common.MapStr{
"read": common.MapStr{
"request": common.MapStr{
"merges_per_sec": extraMetrics.ReadRequestMergeCountPerSec,
"per_sec": extraMetrics.ReadRequestCountPerSec,
},
"per_sec": common.MapStr{
"bytes": extraMetrics.ReadBytesPerSec,
},
},
"write": common.MapStr{
"request": common.MapStr{
"merges_per_sec": extraMetrics.WriteRequestMergeCountPerSec,
"per_sec": extraMetrics.WriteRequestCountPerSec,
},
"per_sec": common.MapStr{
"bytes": extraMetrics.WriteBytesPerSec,
},
},
"queue": common.MapStr{
"avg_size": extraMetrics.AvgQueueSize,
},
"request": common.MapStr{
"avg_size": extraMetrics.AvgRequestSize,
},
"await": extraMetrics.AvgAwaitTime,
"service_time": extraMetrics.AvgServiceTime,
"busy": extraMetrics.BusyPct,
}
}
events = append(events, event)
if counters.SerialNumber != "" {
event["serial_number"] = counters.SerialNumber
}
}
// open a sampling means store the last cpu counter
m.statistics.CloseSampling()
return events, nil
}