Skip to content

Commit

Permalink
making it optional to have memory_swap
Browse files Browse the repository at this point in the history
  • Loading branch information
Paramadon committed Sep 23, 2024
1 parent 31ff893 commit e454d36
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
5 changes: 5 additions & 0 deletions plugins/inputs/procstat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions plugins/inputs/procstat/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package procstat

import (
"fmt"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"time"

"github.com/shirou/gopsutil/v3/cpu"
Expand All @@ -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)
Expand Down Expand Up @@ -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{})
}
33 changes: 27 additions & 6 deletions plugins/inputs/procstat/procstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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 = `
Expand Down Expand Up @@ -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,
}
})
}
20 changes: 18 additions & 2 deletions plugins/inputs/procstat/procstat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package procstat

import (
"fmt"
"github.com/influxdata/telegraf"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
Expand All @@ -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,
}
Expand All @@ -223,6 +229,7 @@ func TestGather_NoProcessNameUsesReal(t *testing.T) {

p := Procstat{
Exe: exe,
Properties: []string{"mmap"},
createPIDFinder: pidFinder([]PID{pid}),
createProcess: newTestProc,
}
Expand All @@ -236,6 +243,7 @@ func TestGather_NoPidTag(t *testing.T) {

p := Procstat{
Exe: exe,
Properties: []string{"mmap"},
createPIDFinder: pidFinder([]PID{pid}),
createProcess: newTestProc,
}
Expand All @@ -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,
}
Expand All @@ -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,
}
Expand All @@ -276,6 +286,7 @@ func TestGather_Exe(t *testing.T) {

p := Procstat{
Exe: exe,
Properties: []string{"mmap"},
createPIDFinder: pidFinder([]PID{pid}),
createProcess: newTestProc,
}
Expand All @@ -290,6 +301,7 @@ func TestGather_User(t *testing.T) {

p := Procstat{
User: user,
Properties: []string{"mmap"},
createPIDFinder: pidFinder([]PID{pid}),
createProcess: newTestProc,
}
Expand All @@ -304,6 +316,7 @@ func TestGather_Pattern(t *testing.T) {

p := Procstat{
Pattern: pattern,
Properties: []string{"mmap"},
createPIDFinder: pidFinder([]PID{pid}),
createProcess: newTestProc,
}
Expand All @@ -328,6 +341,7 @@ func TestGather_PidFile(t *testing.T) {

p := Procstat{
PidFile: pidfile,
Properties: []string{"mmap"},
createPIDFinder: pidFinder([]PID{pid}),
createProcess: newTestProc,
}
Expand All @@ -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,
}
Expand All @@ -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,
}
Expand Down

0 comments on commit e454d36

Please sign in to comment.