2022-05-24 21:49:47 +08:00
|
|
|
//go:generate ../../../tools/readme_config_includer/generator
|
2018-07-12 07:43:49 +08:00
|
|
|
package disk
|
2015-05-19 07:01:42 +08:00
|
|
|
|
|
|
|
|
import (
|
2022-05-24 21:49:47 +08:00
|
|
|
_ "embed"
|
2015-05-19 07:01:42 +08:00
|
|
|
"fmt"
|
2016-12-06 01:42:36 +08:00
|
|
|
"strings"
|
2015-05-19 07:01:42 +08:00
|
|
|
|
2016-01-28 05:21:36 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-21 02:57:35 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2018-07-12 07:43:49 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/system"
|
2023-02-22 22:55:55 +08:00
|
|
|
"github.com/shirou/gopsutil/v3/disk"
|
2015-05-19 07:01:42 +08:00
|
|
|
)
|
|
|
|
|
|
2022-05-24 21:49:47 +08:00
|
|
|
//go:embed sample.conf
|
|
|
|
|
var sampleConfig string
|
|
|
|
|
|
2015-05-19 07:01:42 +08:00
|
|
|
type DiskStats struct {
|
2018-07-12 07:43:49 +08:00
|
|
|
ps system.PS
|
2015-10-08 05:42:11 +08:00
|
|
|
|
2022-05-10 23:59:57 +08:00
|
|
|
LegacyMountPoints []string `toml:"mountpoints" deprecated:"0.10.2;2.0.0;use 'mount_points' instead"`
|
2016-01-21 01:42:55 +08:00
|
|
|
|
2022-05-13 05:16:19 +08:00
|
|
|
MountPoints []string `toml:"mount_points"`
|
|
|
|
|
IgnoreFS []string `toml:"ignore_fs"`
|
|
|
|
|
IgnoreMountOpts []string `toml:"ignore_mount_opts"`
|
2022-01-05 23:45:03 +08:00
|
|
|
|
|
|
|
|
Log telegraf.Logger `toml:"-"`
|
2015-05-19 07:01:42 +08:00
|
|
|
}
|
|
|
|
|
|
2022-05-24 21:49:47 +08:00
|
|
|
func (*DiskStats) SampleConfig() string {
|
|
|
|
|
return sampleConfig
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 23:45:03 +08:00
|
|
|
func (ds *DiskStats) Init() error {
|
2016-01-21 01:42:55 +08:00
|
|
|
// Legacy support:
|
2021-06-21 23:07:52 +08:00
|
|
|
if len(ds.LegacyMountPoints) != 0 {
|
|
|
|
|
ds.MountPoints = ds.LegacyMountPoints
|
2015-05-19 07:01:42 +08:00
|
|
|
}
|
|
|
|
|
|
2022-01-05 23:45:03 +08:00
|
|
|
ps := system.NewSystemPS()
|
|
|
|
|
ps.Log = ds.Log
|
|
|
|
|
ds.ps = ps
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ds *DiskStats) Gather(acc telegraf.Accumulator) error {
|
2022-05-13 05:16:19 +08:00
|
|
|
disks, partitions, err := ds.ps.DiskUsage(ds.MountPoints, ds.IgnoreMountOpts, ds.IgnoreFS)
|
2016-01-21 01:42:55 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("error getting disk usage info: %s", err)
|
2015-10-08 05:42:11 +08:00
|
|
|
}
|
2016-12-06 01:42:36 +08:00
|
|
|
for i, du := range disks {
|
2016-02-23 01:29:10 +08:00
|
|
|
if du.Total == 0 {
|
|
|
|
|
// Skip dummy filesystem (procfs, cgroupfs, ...)
|
|
|
|
|
continue
|
|
|
|
|
}
|
2023-02-22 22:55:55 +08:00
|
|
|
|
|
|
|
|
device := partitions[i].Device
|
2021-11-19 00:33:47 +08:00
|
|
|
mountOpts := MountOptions(partitions[i].Opts)
|
2015-05-19 07:01:42 +08:00
|
|
|
tags := map[string]string{
|
2015-08-11 03:43:15 +08:00
|
|
|
"path": du.Path,
|
2023-02-22 22:55:55 +08:00
|
|
|
"device": strings.ReplaceAll(device, "/dev/", ""),
|
2015-08-11 03:43:15 +08:00
|
|
|
"fstype": du.Fstype,
|
2017-09-07 05:28:11 +08:00
|
|
|
"mode": mountOpts.Mode(),
|
2015-05-19 07:01:42 +08:00
|
|
|
}
|
2023-02-22 22:55:55 +08:00
|
|
|
|
|
|
|
|
label, err := disk.Label(strings.TrimPrefix(device, "/dev/"))
|
|
|
|
|
if err == nil && label != "" {
|
|
|
|
|
tags["label"] = label
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 07:19:50 +08:00
|
|
|
var usedPercent float64
|
2016-01-26 08:34:09 +08:00
|
|
|
if du.Used+du.Free > 0 {
|
2021-02-17 07:19:50 +08:00
|
|
|
usedPercent = float64(du.Used) /
|
2016-01-26 08:34:09 +08:00
|
|
|
(float64(du.Used) + float64(du.Free)) * 100
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-12 04:07:32 +08:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
|
"total": du.Total,
|
|
|
|
|
"free": du.Free,
|
2016-01-26 08:34:09 +08:00
|
|
|
"used": du.Used,
|
2021-02-17 07:19:50 +08:00
|
|
|
"used_percent": usedPercent,
|
2015-12-12 04:07:32 +08:00
|
|
|
"inodes_total": du.InodesTotal,
|
|
|
|
|
"inodes_free": du.InodesFree,
|
2016-01-26 08:34:09 +08:00
|
|
|
"inodes_used": du.InodesUsed,
|
2015-12-12 04:07:32 +08:00
|
|
|
}
|
2016-09-01 00:27:37 +08:00
|
|
|
acc.AddGauge("disk", fields, tags)
|
2015-05-19 07:01:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 05:28:11 +08: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
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-19 07:01:42 +08:00
|
|
|
func init() {
|
2016-01-28 05:21:36 +08:00
|
|
|
inputs.Add("disk", func() telegraf.Input {
|
2022-01-05 23:45:03 +08:00
|
|
|
return &DiskStats{}
|
2015-05-19 07:01:42 +08:00
|
|
|
})
|
|
|
|
|
}
|