forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 9
/
fsstat.go
89 lines (72 loc) · 2.22 KB
/
fsstat.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
// +build darwin freebsd linux openbsd windows
package fsstat
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/elastic/beats/metricbeat/module/system/filesystem"
"github.com/pkg/errors"
)
var debugf = logp.MakeDebug("system-fsstat")
func init() {
if err := mb.Registry.AddMetricSet("system", "fsstat", New, parse.EmptyHostParser); err != nil {
panic(err)
}
}
// MetricSet for fetching a summary of filesystem stats.
type MetricSet struct {
mb.BaseMetricSet
config filesystem.Config
}
// New creates and returns a new instance of MetricSet.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
var config filesystem.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
// a single event containing aggregated data.
func (m *MetricSet) Fetch() (common.MapStr, error) {
fss, err := filesystem.GetFileSystemList()
if err != nil {
return nil, errors.Wrap(err, "filesystem list")
}
if len(m.config.IgnoreTypes) > 0 {
fss = filesystem.Filter(fss, filesystem.BuildTypeFilter(m.config.IgnoreTypes...))
}
// These values are optional and could also be calculated by Kibana
var totalFiles, totalSize, totalSizeFree, totalSizeUsed uint64
dict := map[string]bool{}
for _, fs := range fss {
stat, err := filesystem.GetFileSystemStat(fs)
if err != nil {
debugf("error fetching filesystem stats for '%s': %v", fs.DirName, err)
continue
}
logp.Debug("fsstat", "filesystem: %s total=%d, used=%d, free=%d", stat.Mount, stat.Total, stat.Used, stat.Free)
if _, ok := dict[stat.Mount]; ok {
// ignore filesystem with the same mounting point
continue
}
totalFiles += stat.Files
totalSize += stat.Total
totalSizeFree += stat.Free
totalSizeUsed += stat.Used
dict[stat.Mount] = true
}
return common.MapStr{
"total_size": common.MapStr{
"free": totalSizeFree,
"used": totalSizeUsed,
"total": totalSize,
},
"count": len(dict),
"total_files": totalFiles,
}, nil
}