Add user tag to procstat input (#4386)
This commit is contained in:
@@ -21,6 +21,7 @@ type Process interface {
|
|||||||
Percent(interval time.Duration) (float64, error)
|
Percent(interval time.Duration) (float64, error)
|
||||||
Times() (*cpu.TimesStat, error)
|
Times() (*cpu.TimesStat, error)
|
||||||
RlimitUsage(bool) ([]process.RlimitStat, error)
|
RlimitUsage(bool) ([]process.RlimitStat, error)
|
||||||
|
Username() (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type PIDFinder interface {
|
type PIDFinder interface {
|
||||||
@@ -58,6 +59,10 @@ func (p *Proc) PID() PID {
|
|||||||
return PID(p.Process.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) {
|
func (p *Proc) Percent(interval time.Duration) (float64, error) {
|
||||||
cpu_perc, err := p.Process.Percent(time.Duration(0))
|
cpu_perc, err := p.Process.Percent(time.Duration(0))
|
||||||
if !p.hasCPUTimes && err == nil {
|
if !p.hasCPUTimes && err == nil {
|
||||||
|
|||||||
@@ -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 pid is not present as a tag, include it as a field.
|
||||||
if _, pidInTags := proc.Tags()["pid"]; !pidInTags {
|
if _, pidInTags := proc.Tags()["pid"]; !pidInTags {
|
||||||
fields["pid"] = int32(proc.PID())
|
fields["pid"] = int32(proc.PID())
|
||||||
|
|||||||
@@ -104,6 +104,10 @@ func (p *testProc) PID() PID {
|
|||||||
return p.pid
|
return p.pid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *testProc) Username() (string, error) {
|
||||||
|
return "testuser", nil
|
||||||
|
}
|
||||||
|
|
||||||
func (p *testProc) Tags() map[string]string {
|
func (p *testProc) Tags() map[string]string {
|
||||||
return p.tags
|
return p.tags
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user