From 27806223e78ad3cc13e5c5799866e55acd6308ae Mon Sep 17 00:00:00 2001 From: Greg Linton Date: Thu, 5 Jul 2018 10:58:11 -0600 Subject: [PATCH 1/2] Add process' user as a tag Resolves #4378 --- plugins/inputs/procstat/process.go | 5 +++++ plugins/inputs/procstat/procstat.go | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/plugins/inputs/procstat/process.go b/plugins/inputs/procstat/process.go index 361582c334119..30e8f182f7515 100644 --- a/plugins/inputs/procstat/process.go +++ b/plugins/inputs/procstat/process.go @@ -21,6 +21,7 @@ type Process interface { Percent(interval time.Duration) (float64, error) Times() (*cpu.TimesStat, error) RlimitUsage(bool) ([]process.RlimitStat, error) + Username() (string, error) } type PIDFinder interface { @@ -58,6 +59,10 @@ func (p *Proc) PID() PID { return PID(p.Process.Pid) } +func (p *Proc) Username() (string, error) { + return p.Process.Username() +} + func (p *Proc) Percent(interval time.Duration) (float64, error) { cpu_perc, err := p.Process.Percent(time.Duration(0)) if !p.hasCPUTimes && err == nil { diff --git a/plugins/inputs/procstat/procstat.go b/plugins/inputs/procstat/procstat.go index 01505bdd081c6..4b253fd1c969b 100644 --- a/plugins/inputs/procstat/procstat.go +++ b/plugins/inputs/procstat/procstat.go @@ -128,6 +128,14 @@ func (p *Procstat) addMetrics(proc Process, acc telegraf.Accumulator) { } } + //If user tag is not already set, set to actual name + if _, ok := proc.Tags()["user"]; !ok { + user, err := proc.Username() + if err == nil { + proc.Tags()["user"] = user + } + } + //If pid is not present as a tag, include it as a field. if _, pidInTags := proc.Tags()["pid"]; !pidInTags { fields["pid"] = int32(proc.PID()) From 3d9a2151e1cd5a4e23660e678091fb5f52d3f8a5 Mon Sep 17 00:00:00 2001 From: Greg Linton Date: Thu, 5 Jul 2018 11:11:44 -0600 Subject: [PATCH 2/2] Update testProc to match new interface --- plugins/inputs/procstat/procstat_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/inputs/procstat/procstat_test.go b/plugins/inputs/procstat/procstat_test.go index ff4b7006084db..7a2eaf9eee3a9 100644 --- a/plugins/inputs/procstat/procstat_test.go +++ b/plugins/inputs/procstat/procstat_test.go @@ -104,6 +104,10 @@ func (p *testProc) PID() PID { return p.pid } +func (p *testProc) Username() (string, error) { + return "testuser", nil +} + func (p *testProc) Tags() map[string]string { return p.tags }