forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk.go
113 lines (94 loc) · 2.47 KB
/
disk.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
102
103
104
105
106
107
108
109
110
111
112
113
package disk
import (
"fmt"
"strings"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/inputs/system"
)
type DiskStats struct {
ps system.PS
// Legacy support
LegacyMountPoints []string `toml:"mountpoints"`
MountPoints []string `toml:"mount_points"`
IgnoreFS []string `toml:"ignore_fs"`
}
func (ds *DiskStats) Description() string {
return "Read metrics about disk usage by mount point"
}
var diskSampleConfig = `
## By default stats will be gathered for all mount points.
## Set mount_points will restrict the stats to only the specified mount points.
# mount_points = ["/"]
## Ignore mount points by filesystem type.
ignore_fs = ["tmpfs", "devtmpfs", "devfs", "iso9660", "overlay", "aufs", "squashfs"]
`
func (ds *DiskStats) SampleConfig() string {
return diskSampleConfig
}
func (ds *DiskStats) Gather(acc telegraf.Accumulator) error {
// Legacy support:
if len(ds.LegacyMountPoints) != 0 {
ds.MountPoints = ds.LegacyMountPoints
}
disks, partitions, err := ds.ps.DiskUsage(ds.MountPoints, ds.IgnoreFS)
if err != nil {
return fmt.Errorf("error getting disk usage info: %s", err)
}
for i, du := range disks {
if du.Total == 0 {
// Skip dummy filesystem (procfs, cgroupfs, ...)
continue
}
mountOpts := parseOptions(partitions[i].Opts)
tags := map[string]string{
"path": du.Path,
"device": strings.Replace(partitions[i].Device, "/dev/", "", -1),
"fstype": du.Fstype,
"mode": mountOpts.Mode(),
}
var usedPercent float64
if du.Used+du.Free > 0 {
usedPercent = float64(du.Used) /
(float64(du.Used) + float64(du.Free)) * 100
}
fields := map[string]interface{}{
"total": du.Total,
"free": du.Free,
"used": du.Used,
"used_percent": usedPercent,
"inodes_total": du.InodesTotal,
"inodes_free": du.InodesFree,
"inodes_used": du.InodesUsed,
}
acc.AddGauge("disk", fields, tags)
}
return nil
}
type MountOptions []string
func (opts MountOptions) Mode() string {
if opts.exists("rw") {
return "rw"
} else if opts.exists("ro") {
return "ro"
} else {
return "unknown"
}
}
func (opts MountOptions) exists(opt string) bool {
for _, o := range opts {
if o == opt {
return true
}
}
return false
}
func parseOptions(opts string) MountOptions {
return strings.Split(opts, ",")
}
func init() {
ps := system.NewSystemPS()
inputs.Add("disk", func() telegraf.Input {
return &DiskStats{ps: ps}
})
}