forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.go
101 lines (84 loc) · 2.21 KB
/
helper.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
// +build darwin freebsd linux openbsd windows
package filesystem
import (
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/module/system"
sigar "github.com/elastic/gosigar"
)
type FileSystemStat struct {
sigar.FileSystemUsage
DevName string `json:"device_name"`
Mount string `json:"mount_point"`
UsedPercent float64 `json:"used_p"`
ctime time.Time
}
func GetFileSystemList() ([]sigar.FileSystem, error) {
fss := sigar.FileSystemList{}
err := fss.Get()
if err != nil {
return nil, err
}
return fss.List, nil
}
func GetFileSystemStat(fs sigar.FileSystem) (*FileSystemStat, error) {
stat := sigar.FileSystemUsage{}
if err := stat.Get(fs.DirName); err != nil {
return nil, err
}
filesystem := FileSystemStat{
FileSystemUsage: stat,
DevName: fs.DevName,
Mount: fs.DirName,
}
return &filesystem, nil
}
func AddFileSystemUsedPercentage(f *FileSystemStat) {
if f.Total == 0 {
return
}
perc := float64(f.Used) / float64(f.Total)
f.UsedPercent = system.Round(perc, .5, 4)
}
func CollectFileSystemStats(fss []sigar.FileSystem) []common.MapStr {
events := make([]common.MapStr, 0, len(fss))
for _, fs := range fss {
fsStat, err := GetFileSystemStat(fs)
if err != nil {
logp.Debug("system", "Skip filesystem %d: %v", fsStat, err)
continue
}
AddFileSystemUsedPercentage(fsStat)
event := common.MapStr{
"@timestamp": common.Time(time.Now()),
"type": "filesystem",
"fs": GetFilesystemEvent(fsStat),
}
events = append(events, event)
}
return events
}
func GetFilesystemEvent(fsStat *FileSystemStat) common.MapStr {
return common.MapStr{
"device_name": fsStat.DevName,
"mount_point": fsStat.Mount,
"total": fsStat.Total,
"free": fsStat.Free,
"available": fsStat.Avail,
"files": fsStat.Files,
"free_files": fsStat.FreeFiles,
"used": common.MapStr{
"pct": fsStat.UsedPercent,
"bytes": fsStat.Used,
},
}
}
func GetFileSystemStats() ([]common.MapStr, error) {
fss, err := GetFileSystemList()
if err != nil {
logp.Warn("Getting filesystem list: %v", err)
return nil, err
}
return CollectFileSystemStats(fss), nil
}