telegraf/plugins/inputs/system/disk.go

160 lines
3.6 KiB
Go
Raw Normal View History

2015-05-18 23:01:42 +00:00
package system
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"
2015-05-18 23:01:42 +00:00
)
type DiskStats struct {
ps 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, telegraf gather stats for all mountpoints.
## Setting mountpoints will restrict the stats to the specified mountpoints.
# mount_points = ["/"]
## Ignore some mountpoints by filesystem type. For example (dev)tmpfs (usually
## present on /run, /var/run, /dev/shm or /dev).
ignore_fs = ["tmpfs", "devtmpfs"]
`
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
}
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,
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 DiskIOStats struct {
ps PS
Devices []string
SkipSerialNumber bool
2015-05-18 23:01:42 +00:00
}
func (_ *DiskIOStats) Description() string {
return "Read metrics about disk IO by device"
}
var diskIoSampleConfig = `
## By default, telegraf will gather stats for all devices including
## disk partitions.
## Setting devices will restrict the stats to the specified devices.
# devices = ["sda", "sdb"]
2016-07-10 12:17:53 +00:00
## Uncomment the following line if you need disk serial numbers.
# skip_serial_number = false
`
func (_ *DiskIOStats) SampleConfig() string {
return diskIoSampleConfig
}
2015-05-18 23:01:42 +00:00
func (s *DiskIOStats) Gather(acc telegraf.Accumulator) error {
2015-05-18 23:01:42 +00:00
diskio, err := s.ps.DiskIO()
if err != nil {
return fmt.Errorf("error getting disk io info: %s", err)
}
var restrictDevices bool
devices := make(map[string]bool)
if len(s.Devices) != 0 {
restrictDevices = true
for _, dev := range s.Devices {
devices[dev] = true
}
}
2015-05-18 23:01:42 +00:00
for _, io := range diskio {
_, member := devices[io.Name]
if restrictDevices && !member {
continue
}
tags := map[string]string{}
tags["name"] = io.Name
if !s.SkipSerialNumber {
if len(io.SerialNumber) != 0 {
tags["serial"] = io.SerialNumber
} else {
tags["serial"] = "unknown"
}
2015-05-18 23:01:42 +00:00
}
2015-12-11 20:07:32 +00:00
fields := map[string]interface{}{
"reads": io.ReadCount,
"writes": io.WriteCount,
"read_bytes": io.ReadBytes,
"write_bytes": io.WriteBytes,
"read_time": io.ReadTime,
"write_time": io.WriteTime,
"io_time": io.IoTime,
"iops_in_progress": io.IopsInProgress,
2015-12-11 20:07:32 +00:00
}
acc.AddCounter("diskio", fields, tags)
2015-05-18 23:01:42 +00:00
}
return nil
}
func init() {
inputs.Add("disk", func() telegraf.Input {
2015-05-18 23:01:42 +00:00
return &DiskStats{ps: &systemPS{}}
})
inputs.Add("diskio", func() telegraf.Input {
2016-07-10 12:17:53 +00:00
return &DiskIOStats{ps: &systemPS{}, SkipSerialNumber: true}
2015-05-18 23:01:42 +00:00
})
}