2018-07-11 23:43:49 +00:00
|
|
|
package disk
|
2015-05-18 23:01:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-12-05 17:42:36 +00:00
|
|
|
"strings"
|
2015-05-18 23:01:42 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2018-07-11 23:43:49 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/system"
|
2015-05-18 23:01:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DiskStats struct {
|
2018-07-11 23:43:49 +00:00
|
|
|
ps system.PS
|
2015-10-07 21:42:11 +00:00
|
|
|
|
2016-01-20 17:42:55 +00:00
|
|
|
// Legacy support
|
2015-10-07 21:42:11 +00:00
|
|
|
Mountpoints []string
|
2016-01-20 17:42:55 +00:00
|
|
|
|
|
|
|
MountPoints []string
|
2016-02-22 17:29:10 +00:00
|
|
|
IgnoreFS []string `toml:"ignore_fs"`
|
2015-05-18 23:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *DiskStats) Description() string {
|
|
|
|
return "Read metrics about disk usage by mount point"
|
|
|
|
}
|
|
|
|
|
2015-10-07 21:42:11 +00:00
|
|
|
var diskSampleConfig = `
|
2018-01-17 23:12:05 +00:00
|
|
|
## By default stats will be gathered for all mount points.
|
|
|
|
## Set mount_points will restrict the stats to only the specified mount points.
|
2016-01-20 17:42:55 +00:00
|
|
|
# mount_points = ["/"]
|
2016-02-22 17:29:10 +00:00
|
|
|
|
2018-01-17 23:12:05 +00:00
|
|
|
## Ignore mount points by filesystem type.
|
2018-06-06 20:44:26 +00:00
|
|
|
ignore_fs = ["tmpfs", "devtmpfs", "devfs", "overlay", "aufs", "squashfs"]
|
2015-10-08 21:17:04 +00:00
|
|
|
`
|
2015-10-07 21:42:11 +00:00
|
|
|
|
2015-10-08 21:17:04 +00:00
|
|
|
func (_ *DiskStats) SampleConfig() string {
|
|
|
|
return diskSampleConfig
|
2015-10-07 21:42:11 +00:00
|
|
|
}
|
2015-05-18 23:01:42 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (s *DiskStats) Gather(acc telegraf.Accumulator) error {
|
2016-01-20 17:42:55 +00:00
|
|
|
// Legacy support:
|
|
|
|
if len(s.Mountpoints) != 0 {
|
|
|
|
s.MountPoints = s.Mountpoints
|
2015-05-18 23:01:42 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 17:42:36 +00:00
|
|
|
disks, partitions, err := s.ps.DiskUsage(s.MountPoints, s.IgnoreFS)
|
2016-01-20 17:42:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting disk usage info: %s", err)
|
2015-10-07 21:42:11 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 17:42:36 +00:00
|
|
|
for i, du := range disks {
|
2016-02-22 17:29:10 +00:00
|
|
|
if du.Total == 0 {
|
|
|
|
// Skip dummy filesystem (procfs, cgroupfs, ...)
|
|
|
|
continue
|
|
|
|
}
|
2017-09-06 21:28:11 +00:00
|
|
|
mountOpts := parseOptions(partitions[i].Opts)
|
2015-05-18 23:01:42 +00:00
|
|
|
tags := map[string]string{
|
2015-08-10 19:43:15 +00:00
|
|
|
"path": du.Path,
|
2016-12-05 17:42:36 +00:00
|
|
|
"device": strings.Replace(partitions[i].Device, "/dev/", "", -1),
|
2015-08-10 19:43:15 +00:00
|
|
|
"fstype": du.Fstype,
|
2017-09-06 21:28:11 +00:00
|
|
|
"mode": mountOpts.Mode(),
|
2015-05-18 23:01:42 +00:00
|
|
|
}
|
2016-01-26 00:34:09 +00:00
|
|
|
var used_percent float64
|
|
|
|
if du.Used+du.Free > 0 {
|
|
|
|
used_percent = float64(du.Used) /
|
|
|
|
(float64(du.Used) + float64(du.Free)) * 100
|
|
|
|
}
|
|
|
|
|
2015-12-11 20:07:32 +00:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"total": du.Total,
|
|
|
|
"free": du.Free,
|
2016-01-26 00:34:09 +00:00
|
|
|
"used": du.Used,
|
|
|
|
"used_percent": used_percent,
|
2015-12-11 20:07:32 +00:00
|
|
|
"inodes_total": du.InodesTotal,
|
|
|
|
"inodes_free": du.InodesFree,
|
2016-01-26 00:34:09 +00:00
|
|
|
"inodes_used": du.InodesUsed,
|
2015-12-11 20:07:32 +00:00
|
|
|
}
|
2016-08-31 16:27:37 +00:00
|
|
|
acc.AddGauge("disk", fields, tags)
|
2015-05-18 23:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-06 21:28:11 +00:00
|
|
|
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, ",")
|
|
|
|
}
|
|
|
|
|
2015-05-18 23:01:42 +00:00
|
|
|
func init() {
|
2018-07-11 23:43:49 +00:00
|
|
|
ps := system.NewSystemPS()
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("disk", func() telegraf.Input {
|
2017-04-18 18:42:58 +00:00
|
|
|
return &DiskStats{ps: ps}
|
2015-05-18 23:01:42 +00:00
|
|
|
})
|
|
|
|
}
|