Skip to content

Commit

Permalink
fix(inputs.procstat) Collect swap via /proc//smaps
Browse files Browse the repository at this point in the history
  • Loading branch information
Paramadon committed Sep 22, 2024
1 parent 68b3396 commit ca1a069
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/influxdata/telegraf

go 1.20
go 1.21

toolchain go1.22.5

require (
cloud.google.com/go/bigquery v1.55.0
Expand Down
19 changes: 19 additions & 0 deletions plugins/inputs/procstat/memmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build linux

package procstat

func collectMemmap(proc Process, prefix string, fields map[string]any) {
memMapStats, err := proc.MemoryMaps(true)
if err == nil && len(*memMapStats) == 1 {
memMap := (*memMapStats)[0]
fields[prefix+"memory_size"] = memMap.Size
fields[prefix+"memory_pss"] = memMap.Pss
fields[prefix+"memory_shared_clean"] = memMap.SharedClean
fields[prefix+"memory_shared_dirty"] = memMap.SharedDirty
fields[prefix+"memory_private_clean"] = memMap.PrivateClean
fields[prefix+"memory_private_dirty"] = memMap.PrivateDirty
fields[prefix+"memory_referenced"] = memMap.Referenced
fields[prefix+"memory_anonymous"] = memMap.Anonymous
fields[prefix+"memory_swap"] = memMap.Swap
}
}
5 changes: 5 additions & 0 deletions plugins/inputs/procstat/memmap_notlinux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !linux

package procstat

func collectMemmap(Process, string, map[string]any) {}
1 change: 1 addition & 0 deletions plugins/inputs/procstat/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Process interface {
IOCounters() (*process.IOCountersStat, error)
MemoryInfo() (*process.MemoryInfoStat, error)
Name() (string, error)
MemoryMaps(bool) (*[]process.MemoryMapsStat, error)
Cmdline() (string, error)
NumCtxSwitches() (*process.NumCtxSwitchesStat, error)
NumFDs() (int32, error)
Expand Down
6 changes: 2 additions & 4 deletions plugins/inputs/procstat/procstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,10 @@ func (p *Procstat) addMetric(proc Process, acc telegraf.Accumulator, t time.Time
if err == nil {
fields[prefix+"memory_rss"] = mem.RSS
fields[prefix+"memory_vms"] = mem.VMS
fields[prefix+"memory_swap"] = mem.Swap
fields[prefix+"memory_data"] = mem.Data
fields[prefix+"memory_stack"] = mem.Stack
fields[prefix+"memory_locked"] = mem.Locked
}

collectMemmap(proc, prefix, fields)

memPerc, err := proc.MemoryPercent()
if err == nil {
fields[prefix+"memory_usage"] = memPerc
Expand Down
4 changes: 4 additions & 0 deletions plugins/inputs/procstat/procstat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func (p *testProc) MemoryInfo() (*process.MemoryInfoStat, error) {
return &process.MemoryInfoStat{}, nil
}

func (p *testProc) MemoryMaps(bool) (*[]process.MemoryMapsStat, error) {
return &[]process.MemoryMapsStat{}, nil
}

func (p *testProc) Name() (string, error) {
return "test_proc", nil
}
Expand Down

0 comments on commit ca1a069

Please sign in to comment.