From e454d36b5e8a05a4b982d6af326843ccb64e120c Mon Sep 17 00:00:00 2001 From: Paramadon Date: Mon, 23 Sep 2024 19:13:05 -0400 Subject: [PATCH] making it optional to have memory_swap --- plugins/inputs/procstat/README.md | 5 ++++ plugins/inputs/procstat/process.go | 17 ++++++++++++ plugins/inputs/procstat/procstat.go | 33 +++++++++++++++++++----- plugins/inputs/procstat/procstat_test.go | 20 ++++++++++++-- 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/plugins/inputs/procstat/README.md b/plugins/inputs/procstat/README.md index 60d213cd0c50d..a25fc86c7e354 100644 --- a/plugins/inputs/procstat/README.md +++ b/plugins/inputs/procstat/README.md @@ -57,11 +57,16 @@ Processes can be selected for monitoring using one of several methods: ## when processes have a short lifetime. # pid_tag = false + ## Properties to collect + ## Available options are "cpu", "limits", "memory", "mmap" + # properties = ["cpu", "limits", "memory", "mmap"] + ## Method to use when finding process IDs. Can be one of 'pgrep', or ## 'native'. The pgrep finder calls the pgrep executable in the PATH while ## the native finder performs the search directly in a manor dependent on the ## platform. Default is 'pgrep' # pid_finder = "pgrep" + ``` ### Windows support diff --git a/plugins/inputs/procstat/process.go b/plugins/inputs/procstat/process.go index afc22d39b2e8e..139202212e9b6 100644 --- a/plugins/inputs/procstat/process.go +++ b/plugins/inputs/procstat/process.go @@ -2,6 +2,8 @@ package procstat import ( "fmt" + "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/metric" "time" "github.com/shirou/gopsutil/v3/cpu" @@ -16,6 +18,7 @@ type Process interface { MemoryInfo() (*process.MemoryInfoStat, error) Name() (string, error) MemoryMaps(bool) (*[]process.MemoryMapsStat, error) + Metric(string, *collectionConfig) telegraf.Metric Cmdline() (string, error) NumCtxSwitches() (*process.NumCtxSwitchesStat, error) NumFDs() (int32, error) @@ -76,3 +79,17 @@ func (p *Proc) Percent(_ time.Duration) (float64, error) { } return cpuPerc, err } + +func (p *Proc) Metric(prefix string, cfg *collectionConfig) telegraf.Metric { + if prefix != "" { + prefix += "_" + } + + fields := make(map[string]interface{}) + + if cfg.features["mmap"] { + collectMemmap(p, prefix, fields) + } + + return metric.New("procstat", p.tags, fields, time.Time{}) +} diff --git a/plugins/inputs/procstat/procstat.go b/plugins/inputs/procstat/procstat.go index 4d6a261ddfd42..52ef595a562c1 100644 --- a/plugins/inputs/procstat/procstat.go +++ b/plugins/inputs/procstat/procstat.go @@ -23,6 +23,12 @@ var ( type PID int32 +type collectionConfig struct { + solarisMode bool + tagging map[string]bool + features map[string]bool +} + type Procstat struct { PidFinder string `toml:"pid_finder"` PidFile string `toml:"pid_file"` @@ -38,14 +44,16 @@ type Procstat struct { PidTag bool WinService string `toml:"win_service"` Mode string + Properties []string `toml:"properties"` - solarisMode bool - - finder PIDFinder - + cfg collectionConfig + oldMode bool + solarisMode bool + finder PIDFinder createPIDFinder func() (PIDFinder, error) procs map[PID]Process - createProcess func(PID) (Process, error) + + createProcess func(PID) (Process, error) } var sampleConfig = ` @@ -562,12 +570,25 @@ func (p *Procstat) Init() error { if strings.ToLower(p.Mode) == "solaris" { p.solarisMode = true } + // Convert collection properties + p.cfg.features = make(map[string]bool, len(p.Properties)) + for _, prop := range p.Properties { + switch prop { + case "cpu", "limits", "memory", "mmap": //"cpu", "limits", "memory" not needed (might be useful for the future) + default: + return fmt.Errorf("invalid 'properties' setting %q", prop) + } + p.cfg.features[prop] = true + } return nil } func init() { inputs.Add("procstat", func() telegraf.Input { - return &Procstat{} + return &Procstat{ + Properties: []string{"mmap"}, + createProcess: NewProc, + } }) } diff --git a/plugins/inputs/procstat/procstat_test.go b/plugins/inputs/procstat/procstat_test.go index 7951dcfe886ec..db53018ca5610 100644 --- a/plugins/inputs/procstat/procstat_test.go +++ b/plugins/inputs/procstat/procstat_test.go @@ -2,6 +2,7 @@ package procstat import ( "fmt" + "github.com/influxdata/telegraf" "os" "os/exec" "path/filepath" @@ -100,8 +101,9 @@ type testProc struct { tags map[string]string } -func newTestProc(_ PID) (Process, error) { +func newTestProc(pid PID) (Process, error) { proc := &testProc{ + pid: pid, tags: make(map[string]string), } return proc, nil @@ -134,7 +136,9 @@ func (p *testProc) MemoryInfo() (*process.MemoryInfoStat, error) { func (p *testProc) MemoryMaps(bool) (*[]process.MemoryMapsStat, error) { return &[]process.MemoryMapsStat{}, nil } - +func (p *testProc) Metric(prefix string, cfg *collectionConfig) telegraf.Metric { + return nil +} func (p *testProc) Name() (string, error) { return "test_proc", nil } @@ -183,6 +187,7 @@ func TestGather_CreateProcessErrorOk(t *testing.T) { p := Procstat{ Exe: exe, + Properties: []string{"mmap"}, createPIDFinder: pidFinder([]PID{pid}), createProcess: func(PID) (Process, error) { return nil, fmt.Errorf("createProcess error") @@ -209,6 +214,7 @@ func TestGather_ProcessName(t *testing.T) { p := Procstat{ Exe: exe, ProcessName: "custom_name", + Properties: []string{"mmap"}, createPIDFinder: pidFinder([]PID{pid}), createProcess: newTestProc, } @@ -223,6 +229,7 @@ func TestGather_NoProcessNameUsesReal(t *testing.T) { p := Procstat{ Exe: exe, + Properties: []string{"mmap"}, createPIDFinder: pidFinder([]PID{pid}), createProcess: newTestProc, } @@ -236,6 +243,7 @@ func TestGather_NoPidTag(t *testing.T) { p := Procstat{ Exe: exe, + Properties: []string{"mmap"}, createPIDFinder: pidFinder([]PID{pid}), createProcess: newTestProc, } @@ -250,6 +258,7 @@ func TestGather_PidTag(t *testing.T) { p := Procstat{ Exe: exe, PidTag: true, + Properties: []string{"mmap"}, createPIDFinder: pidFinder([]PID{pid}), createProcess: newTestProc, } @@ -264,6 +273,7 @@ func TestGather_Prefix(t *testing.T) { p := Procstat{ Exe: exe, Prefix: "custom_prefix", + Properties: []string{"mmap"}, createPIDFinder: pidFinder([]PID{pid}), createProcess: newTestProc, } @@ -276,6 +286,7 @@ func TestGather_Exe(t *testing.T) { p := Procstat{ Exe: exe, + Properties: []string{"mmap"}, createPIDFinder: pidFinder([]PID{pid}), createProcess: newTestProc, } @@ -290,6 +301,7 @@ func TestGather_User(t *testing.T) { p := Procstat{ User: user, + Properties: []string{"mmap"}, createPIDFinder: pidFinder([]PID{pid}), createProcess: newTestProc, } @@ -304,6 +316,7 @@ func TestGather_Pattern(t *testing.T) { p := Procstat{ Pattern: pattern, + Properties: []string{"mmap"}, createPIDFinder: pidFinder([]PID{pid}), createProcess: newTestProc, } @@ -328,6 +341,7 @@ func TestGather_PidFile(t *testing.T) { p := Procstat{ PidFile: pidfile, + Properties: []string{"mmap"}, createPIDFinder: pidFinder([]PID{pid}), createProcess: newTestProc, } @@ -343,6 +357,7 @@ func TestGather_PercentFirstPass(t *testing.T) { p := Procstat{ Pattern: "foo", PidTag: true, + Properties: []string{"mmap"}, createPIDFinder: pidFinder([]PID{pid}), createProcess: NewProc, } @@ -359,6 +374,7 @@ func TestGather_PercentSecondPass(t *testing.T) { p := Procstat{ Pattern: "foo", PidTag: true, + Properties: []string{"mmap"}, createPIDFinder: pidFinder([]PID{pid}), createProcess: NewProc, }