This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsstat.go
67 lines (55 loc) · 1.57 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
// +build darwin 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/topbeat/system"
"github.com/pkg/errors"
)
var debugf = logp.MakeDebug("system-fsstat")
func init() {
if err := mb.Registry.AddMetricSet("system", "fsstat", New); err != nil {
panic(err)
}
}
// MetricSet for fetching a summary of filesystem stats.
type MetricSet struct {
mb.BaseMetricSet
}
// New creates and returns a new instance of MetricSet.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return &MetricSet{
BaseMetricSet: base,
}, 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 := system.GetFileSystemList()
if err != nil {
return nil, errors.Wrap(err, "filesystem list")
}
// These values are optional and could also be calculated by Kibana
var totalFiles, totalSize, totalSizeFree, totalSizeUsed uint64
for _, fs := range fss {
fsStat, err := system.GetFileSystemStat(fs)
if err != nil {
debugf("error fetching filesystem stats for '%s': %v", fs.DirName, err)
continue
}
totalFiles += fsStat.Files
totalSize += fsStat.Total
totalSizeFree += fsStat.Free
totalSizeUsed += fsStat.Used
}
return common.MapStr{
"total_size": common.MapStr{
"free": totalSizeFree,
"used": totalSizeUsed,
"total": totalSize,
},
"count": len(fss),
"total_files": totalFiles,
}, nil
}