Filter disk stats by filesystem type, for Linux.
If we don't want to record the stats of NFS, tmpfs, /proc and similar then specify: fstypes = ["ext4", "xfs"] In the config file.
This commit is contained in:
parent
6eb4bdcf0e
commit
f1af559f11
plugins/system
|
@ -8,16 +8,25 @@ import (
|
|||
|
||||
type DiskStats struct {
|
||||
ps PS
|
||||
Fstypes []string
|
||||
}
|
||||
|
||||
func (_ *DiskStats) Description() string {
|
||||
return "Read metrics about disk usage by mount point"
|
||||
}
|
||||
|
||||
func (_ *DiskStats) SampleConfig() string { return "" }
|
||||
var diskSampleConfig = `
|
||||
# By default, telegraf gathers stats from any mounted partition, including
|
||||
# NFS shares, /proc, tmpfs.
|
||||
# fstypes = ["xfs", "ext4", ...]
|
||||
`
|
||||
|
||||
func (_ *DiskStats) SampleConfig() string {
|
||||
return diskSampleConfig
|
||||
}
|
||||
|
||||
func (s *DiskStats) Gather(acc plugins.Accumulator) error {
|
||||
disks, err := s.ps.DiskUsage()
|
||||
disks, err := s.ps.DiskUsage(s.Fstypes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting disk usage info: %s", err)
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ type DockerContainerStat struct {
|
|||
type PS interface {
|
||||
LoadAvg() (*load.LoadAvgStat, error)
|
||||
CPUTimes() ([]cpu.CPUTimesStat, error)
|
||||
DiskUsage() ([]*disk.DiskUsageStat, error)
|
||||
DiskUsage(fstypes []string) ([]*disk.DiskUsageStat, error)
|
||||
NetIO() ([]net.NetIOCountersStat, error)
|
||||
DiskIO() (map[string]disk.DiskIOCountersStat, error)
|
||||
VMStat() (*mem.VirtualMemoryStat, error)
|
||||
|
@ -53,8 +53,8 @@ func (s *systemPS) CPUTimes() ([]cpu.CPUTimesStat, error) {
|
|||
return cpu.CPUTimes(true)
|
||||
}
|
||||
|
||||
func (s *systemPS) DiskUsage() ([]*disk.DiskUsageStat, error) {
|
||||
parts, err := disk.DiskPartitions(true)
|
||||
func (s *systemPS) DiskUsage(fstypes []string) ([]*disk.DiskUsageStat, error) {
|
||||
parts, err := disk.DiskPartitions(fstypes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ const (
|
|||
|
||||
// Get disk partitions.
|
||||
// should use setmntent(3) but this implement use /etc/mtab file
|
||||
func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
|
||||
func DiskPartitions(fstypes []string) ([]DiskPartitionStat, error) {
|
||||
|
||||
filename := "/etc/mtab"
|
||||
lines, err := common.ReadLines(filename)
|
||||
|
@ -25,17 +25,25 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
set := make(map[string]struct{}, len(fstypes))
|
||||
for _, s := range fstypes {
|
||||
set[s] = struct{}{}
|
||||
}
|
||||
|
||||
ret := make([]DiskPartitionStat, 0, len(lines))
|
||||
|
||||
for _, line := range lines {
|
||||
fields := strings.Fields(line)
|
||||
d := DiskPartitionStat{
|
||||
Device: fields[0],
|
||||
Mountpoint: fields[1],
|
||||
Fstype: fields[2],
|
||||
Opts: fields[3],
|
||||
_, ok := set[fields[2]]
|
||||
if ok || len(fstypes) == 0 {
|
||||
d := DiskPartitionStat{
|
||||
Device: fields[0],
|
||||
Mountpoint: fields[1],
|
||||
Fstype: fields[2],
|
||||
Opts: fields[3],
|
||||
}
|
||||
ret = append(ret, d)
|
||||
}
|
||||
ret = append(ret, d)
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
|
|
Loading…
Reference in New Issue