forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raid.go
90 lines (73 loc) · 1.92 KB
/
raid.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
package raid
import (
"path/filepath"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/cfgwarn"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
"github.com/elastic/beats/metricbeat/module/system"
"github.com/elastic/procfs"
"github.com/pkg/errors"
)
func init() {
if err := mb.Registry.AddMetricSet("system", "raid", New, parse.EmptyHostParser); err != nil {
panic(err)
}
}
// MetricSet contains proc fs data.
type MetricSet struct {
mb.BaseMetricSet
fs procfs.FS
}
// New creates a new instance of the raid metricset.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
cfgwarn.Experimental("The system raid metricset is experimental")
systemModule, ok := base.Module().(*system.Module)
if !ok {
return nil, errors.New("unexpected module type")
}
// Additional configuration options
config := struct {
MountPoint string `config:"raid.mount_point"`
}{}
if err := base.Module().UnpackConfig(&config); err != nil {
return nil, err
}
if config.MountPoint == "" {
config.MountPoint = systemModule.HostFS
}
mountPoint := filepath.Join(config.MountPoint, procfs.DefaultMountPoint)
fs, err := procfs.NewFS(mountPoint)
if err != nil {
return nil, err
}
m := &MetricSet{
BaseMetricSet: base,
fs: fs,
}
return m, nil
}
// Fetch fetches one event for each device
func (m *MetricSet) Fetch() ([]common.MapStr, error) {
stats, err := m.fs.ParseMDStat()
if err != nil {
return nil, err
}
events := make([]common.MapStr, 0, len(stats))
for _, stat := range stats {
event := common.MapStr{
"name": stat.Name,
"activity_state": stat.ActivityState,
"disks": common.MapStr{
"active": stat.DisksActive,
"total": stat.DisksTotal,
},
"blocks": common.MapStr{
"synced": stat.BlocksSynced,
"total": stat.BlocksTotal,
},
}
events = append(events, event)
}
return events, nil
}