telegraf/plugins/inputs/disk/disk.go

114 lines
2.4 KiB
Go
Raw Normal View History

package disk
2015-05-18 23:01:42 +00:00
import (
"fmt"
"strings"
2015-05-18 23:01:42 +00:00
"github.com/influxdata/telegraf"
2016-01-20 18:57:35 +00:00
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/inputs/system"
2015-05-18 23:01:42 +00:00
)
type DiskStats struct {
ps system.PS
// Legacy support
Mountpoints []string
MountPoints []string
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"
}
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", "overlay", "aufs", "squashfs"]
`
func (_ *DiskStats) SampleConfig() string {
return diskSampleConfig
}
2015-05-18 23:01:42 +00:00
func (s *DiskStats) Gather(acc telegraf.Accumulator) error {
// Legacy support:
if len(s.Mountpoints) != 0 {
s.MountPoints = s.Mountpoints
2015-05-18 23:01:42 +00:00
}
disks, partitions, err := s.ps.DiskUsage(s.MountPoints, s.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)
2015-05-18 23:01:42 +00:00
tags := map[string]string{
"path": du.Path,
"device": strings.Replace(partitions[i].Device, "/dev/", "", -1),
"fstype": du.Fstype,
"mode": mountOpts.Mode(),
2015-05-18 23:01:42 +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,
"used": du.Used,
"used_percent": used_percent,
2015-12-11 20:07:32 +00:00
"inodes_total": du.InodesTotal,
"inodes_free": du.InodesFree,
"inodes_used": du.InodesUsed,
2015-12-11 20:07:32 +00:00
}
acc.AddGauge("disk", fields, tags)
2015-05-18 23:01:42 +00:00
}
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, ",")
}
2015-05-18 23:01:42 +00:00
func init() {
ps := system.NewSystemPS()
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
})
}