categraf/inputs/disk/disk.go

110 lines
2.1 KiB
Go
Raw Normal View History

2022-04-15 13:41:02 +08:00
package disk
2022-04-15 13:58:46 +08:00
import (
"log"
"strings"
2022-04-17 12:54:32 +08:00
"flashcat.cloud/categraf/config"
2022-04-15 13:58:46 +08:00
"flashcat.cloud/categraf/inputs"
"flashcat.cloud/categraf/inputs/system"
"flashcat.cloud/categraf/pkg/choice"
2022-07-25 19:27:14 +08:00
"flashcat.cloud/categraf/types"
2022-04-15 13:58:46 +08:00
)
type DiskStats struct {
2022-04-16 16:52:23 +08:00
ps system.PS
2022-04-15 13:58:46 +08:00
2022-07-25 19:27:14 +08:00
config.PluginConfig
MountPoints []string `toml:"mount_points"`
IgnoreFS []string `toml:"ignore_fs"`
IgnoreMountPoints []string `toml:"ignore_mount_points"`
2022-04-15 13:58:46 +08:00
}
func init() {
ps := system.NewSystemPS()
2022-07-25 19:27:14 +08:00
inputs.Add("disk", func() inputs.Input {
2022-04-15 13:58:46 +08:00
return &DiskStats{
2022-04-16 16:52:23 +08:00
ps: ps,
2022-04-15 13:58:46 +08:00
}
})
}
2022-07-24 00:18:00 +08:00
// just placeholder
func (s *DiskStats) GetInstances() []inputs.Instance {
return nil
}
2022-04-16 16:52:23 +08:00
func (s *DiskStats) Init() error {
return nil
2022-04-15 18:19:15 +08:00
}
2022-04-18 17:35:16 +08:00
func (s *DiskStats) Drop() {
}
2022-07-25 19:27:14 +08:00
func (s *DiskStats) Gather(slist *types.SampleList) {
2022-04-15 13:58:46 +08:00
disks, partitions, err := s.ps.DiskUsage(s.MountPoints, s.IgnoreFS)
if err != nil {
log.Println("E! failed to get disk usage:", err)
2022-04-25 15:34:15 +08:00
return
2022-04-15 13:58:46 +08:00
}
for i, du := range disks {
if du.Total == 0 {
// Skip dummy filesystem (procfs, cgroupfs, ...)
continue
}
if len(s.IgnoreMountPoints) > 0 {
if choice.Contains(du.Path, s.IgnoreMountPoints) {
continue
}
}
2022-04-15 13:58:46 +08:00
mountOpts := MountOptions(partitions[i].Opts)
tags := map[string]string{
"path": du.Path,
"device": strings.Replace(partitions[i].Device, "/dev/", "", -1),
"fstype": du.Fstype,
"mode": mountOpts.Mode(),
}
var usedPercent float64
if du.Used+du.Free > 0 {
usedPercent = float64(du.Used) /
(float64(du.Used) + float64(du.Free)) * 100
}
fields := map[string]interface{}{
"total": du.Total,
"free": du.Free,
"used": du.Used,
"used_percent": usedPercent,
"inodes_total": du.InodesTotal,
"inodes_free": du.InodesFree,
"inodes_used": du.InodesUsed,
}
2022-07-25 19:27:14 +08:00
slist.PushSamples("disk", fields, tags)
2022-04-15 13:58:46 +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
}