From 50a82d3f4ffc1eb9d32c5e89825db467e7de273f Mon Sep 17 00:00:00 2001 From: Phil Bracikowski Date: Mon, 2 Dec 2024 18:00:33 -0800 Subject: [PATCH] fix(plugin,procstat): fix cpu usage percentage with multiple filters With the "new" style plugin filters, if multiple filters were used, the list of running processes was being reset on each filter iteration instead of globally. This commit fixes this by setting the running pid map outside the filter iteration. The cpu usage percentage calculation needs multiple passes of gather to calculate the difference so recreating the proc on each iteration meant it never got past the first pass. --- plugins/inputs/procstat/procstat.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/plugins/inputs/procstat/procstat.go b/plugins/inputs/procstat/procstat.go index 6a50cb317ad2b..4e3e4df6d38c0 100644 --- a/plugins/inputs/procstat/procstat.go +++ b/plugins/inputs/procstat/procstat.go @@ -324,7 +324,7 @@ func (p *Procstat) gatherOld(acc telegraf.Accumulator) error { func (p *Procstat) gatherNew(acc telegraf.Accumulator) error { now := time.Now() - + running := make(map[PID]bool) for _, f := range p.Filter { groups, err := f.ApplyFilter() if err != nil { @@ -347,7 +347,6 @@ func (p *Procstat) gatherNew(acc telegraf.Accumulator) error { } var count int - running := make(map[PID]bool) for _, g := range groups { count += len(g.processes) for _, gp := range g.processes { @@ -397,13 +396,6 @@ func (p *Procstat) gatherNew(acc telegraf.Accumulator) error { } } - // Cleanup processes that are not running anymore - for pid := range p.processes { - if !running[pid] { - delete(p.processes, pid) - } - } - // Add lookup statistics-metric acc.AddFields( "procstat_lookup", @@ -419,6 +411,13 @@ func (p *Procstat) gatherNew(acc telegraf.Accumulator) error { now, ) } + + // Cleanup processes that are not running anymore across all filters/groups + for pid := range p.processes { + if !running[pid] { + delete(p.processes, pid) + } + } return nil }