2015-05-18 23:01:42 +00:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-10-24 23:22:31 +00:00
|
|
|
"log"
|
2016-07-04 22:48:37 +00:00
|
|
|
"regexp"
|
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"
|
2015-05-18 23:01:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DiskStats struct {
|
|
|
|
ps 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 = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## By default, telegraf gather stats for all mountpoints.
|
|
|
|
## Setting mountpoints will restrict the stats to the specified mountpoints.
|
2016-01-20 17:42:55 +00:00
|
|
|
# mount_points = ["/"]
|
2016-02-22 17:29:10 +00:00
|
|
|
|
2016-02-22 21:43:00 +00:00
|
|
|
## Ignore some mountpoints by filesystem type. For example (dev)tmpfs (usually
|
|
|
|
## present on /run, /var/run, /dev/shm or /dev).
|
2017-01-13 14:19:57 +00:00
|
|
|
ignore_fs = ["tmpfs", "devtmpfs", "devfs"]
|
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
|
|
|
|
}
|
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,
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type DiskIOStats struct {
|
|
|
|
ps PS
|
2015-11-05 01:21:42 +00:00
|
|
|
|
|
|
|
Devices []string
|
2016-07-04 22:48:37 +00:00
|
|
|
DeviceTags []string
|
|
|
|
NameTemplates []string
|
2015-11-05 01:21:42 +00:00
|
|
|
SkipSerialNumber bool
|
2016-07-04 22:48:37 +00:00
|
|
|
|
|
|
|
infoCache map[string]diskInfoCache
|
2015-05-18 23:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *DiskIOStats) Description() string {
|
|
|
|
return "Read metrics about disk IO by device"
|
|
|
|
}
|
|
|
|
|
2015-11-05 01:21:42 +00:00
|
|
|
var diskIoSampleConfig = `
|
2016-02-22 21:43:00 +00:00
|
|
|
## By default, telegraf will gather stats for all devices including
|
|
|
|
## disk partitions.
|
|
|
|
## Setting devices will restrict the stats to the specified devices.
|
2016-01-27 18:09:14 +00:00
|
|
|
# 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
|
2016-07-04 22:48:37 +00:00
|
|
|
#
|
|
|
|
## On systems which support it, device metadata can be added in the form of
|
|
|
|
## tags.
|
|
|
|
## Currently only Linux is supported via udev properties. You can view
|
|
|
|
## available properties for a device by running:
|
|
|
|
## 'udevadm info -q property -n /dev/sda'
|
|
|
|
# device_tags = ["ID_FS_TYPE", "ID_FS_USAGE"]
|
|
|
|
#
|
|
|
|
## Using the same metadata source as device_tags, you can also customize the
|
|
|
|
## name of the device via templates.
|
|
|
|
## The 'name_templates' parameter is a list of templates to try and apply to
|
|
|
|
## the device. The template may contain variables in the form of '$PROPERTY' or
|
|
|
|
## '${PROPERTY}'. The first template which does not contain any variables not
|
|
|
|
## present for the device is used as the device name tag.
|
|
|
|
## The typical use case is for LVM volumes, to get the VG/LV name instead of
|
|
|
|
## the near-meaningless DM-0 name.
|
|
|
|
# name_templates = ["$ID_FS_LABEL","$DM_VG_NAME/$DM_LV_NAME"]
|
2015-11-05 01:21:42 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (_ *DiskIOStats) SampleConfig() string {
|
|
|
|
return diskIoSampleConfig
|
|
|
|
}
|
2015-05-18 23:01:42 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (s *DiskIOStats) Gather(acc telegraf.Accumulator) error {
|
2017-04-11 18:41:09 +00:00
|
|
|
diskio, err := s.ps.DiskIO(s.Devices)
|
2015-05-18 23:01:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting disk io info: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, io := range diskio {
|
2015-07-03 14:11:52 +00:00
|
|
|
tags := map[string]string{}
|
2016-07-04 22:48:37 +00:00
|
|
|
tags["name"] = s.diskName(io.Name)
|
|
|
|
for t, v := range s.diskTags(io.Name) {
|
|
|
|
tags[t] = v
|
|
|
|
}
|
2015-12-02 19:44:16 +00:00
|
|
|
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{}{
|
2016-11-16 13:16:16 +00:00
|
|
|
"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,
|
2017-08-11 18:49:42 +00:00
|
|
|
"weighted_io_time": io.WeightedIO,
|
2016-11-16 13:16:16 +00:00
|
|
|
"iops_in_progress": io.IopsInProgress,
|
2015-12-11 20:07:32 +00:00
|
|
|
}
|
2016-08-31 16:27:37 +00:00
|
|
|
acc.AddCounter("diskio", fields, tags)
|
2015-05-18 23:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-04 22:48:37 +00:00
|
|
|
var varRegex = regexp.MustCompile(`\$(?:\w+|\{\w+\})`)
|
|
|
|
|
|
|
|
func (s *DiskIOStats) diskName(devName string) string {
|
2017-10-24 23:22:31 +00:00
|
|
|
if len(s.NameTemplates) == 0 {
|
2016-07-04 22:48:37 +00:00
|
|
|
return devName
|
|
|
|
}
|
2017-10-24 23:22:31 +00:00
|
|
|
|
|
|
|
di, err := s.diskInfo(devName)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("W! Error gathering disk info: %s", err)
|
2016-07-04 22:48:37 +00:00
|
|
|
return devName
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, nt := range s.NameTemplates {
|
|
|
|
miss := false
|
|
|
|
name := varRegex.ReplaceAllStringFunc(nt, func(sub string) string {
|
|
|
|
sub = sub[1:] // strip leading '$'
|
|
|
|
if sub[0] == '{' {
|
|
|
|
sub = sub[1 : len(sub)-1] // strip leading & trailing '{' '}'
|
|
|
|
}
|
|
|
|
if v, ok := di[sub]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
miss = true
|
|
|
|
return ""
|
|
|
|
})
|
|
|
|
|
|
|
|
if !miss {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return devName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DiskIOStats) diskTags(devName string) map[string]string {
|
2017-10-24 23:22:31 +00:00
|
|
|
if len(s.DeviceTags) == 0 {
|
2016-07-04 22:48:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-10-24 23:22:31 +00:00
|
|
|
|
|
|
|
di, err := s.diskInfo(devName)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("W! Error gathering disk info: %s", err)
|
2016-07-04 22:48:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tags := map[string]string{}
|
|
|
|
for _, dt := range s.DeviceTags {
|
|
|
|
if v, ok := di[dt]; ok {
|
|
|
|
tags[dt] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags
|
|
|
|
}
|
|
|
|
|
2015-05-18 23:01:42 +00:00
|
|
|
func init() {
|
2017-04-18 18:42:58 +00:00
|
|
|
ps := 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
|
|
|
})
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("diskio", func() telegraf.Input {
|
2017-04-18 18:42:58 +00:00
|
|
|
return &DiskIOStats{ps: ps, SkipSerialNumber: true}
|
2015-05-18 23:01:42 +00:00
|
|
|
})
|
|
|
|
}
|