-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
filesystem.go
65 lines (53 loc) · 1.56 KB
/
filesystem.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
// +build darwin freebsd linux openbsd windows
package filesystem
import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
"github.com/pkg/errors"
)
var debugf = logp.MakeDebug("system.filesystem")
func init() {
if err := mb.Registry.AddMetricSet("system", "filesystem", New, parse.EmptyHostParser); err != nil {
panic(err)
}
}
// MetricSet for fetching filesystem metrics.
type MetricSet struct {
mb.BaseMetricSet
config Config
}
// New creates and returns a new instance of MetricSet.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
var config Config
if err := base.Module().UnpackConfig(&config); err != nil {
return nil, err
}
return &MetricSet{
BaseMetricSet: base,
config: config,
}, nil
}
// Fetch fetches filesystem metrics for all mounted filesystems and returns
// an event for each mount point.
func (m *MetricSet) Fetch() ([]common.MapStr, error) {
fss, err := GetFileSystemList()
if err != nil {
return nil, errors.Wrap(err, "filesystem list")
}
if len(m.config.IgnoreTypes) > 0 {
fss = Filter(fss, BuildTypeFilter(m.config.IgnoreTypes...))
}
filesSystems := make([]common.MapStr, 0, len(fss))
for _, fs := range fss {
fsStat, err := GetFileSystemStat(fs)
if err != nil {
debugf("error getting filesystem stats for '%s': %v", fs.DirName, err)
continue
}
AddFileSystemUsedPercentage(fsStat)
filesSystems = append(filesSystems, GetFilesystemEvent(fsStat))
}
return filesSystems, nil
}