Add running field to procstat_lookup (#5069)

This commit is contained in:
Daniel Nelson 2018-12-11 19:11:33 -08:00 committed by GitHub
parent d954218f75
commit cf2b85f383
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 30 deletions

View File

@ -82,6 +82,7 @@ func (ac *accumulator) AddHistogram(
} }
func (ac *accumulator) AddMetric(m telegraf.Metric) { func (ac *accumulator) AddMetric(m telegraf.Metric) {
m.SetTime(m.Time().Round(ac.precision))
if m := ac.maker.MakeMetric(m); m != nil { if m := ac.maker.MakeMetric(m); m != nil {
ac.metrics <- m ac.metrics <- m
} }

View File

@ -136,17 +136,21 @@ implemented as a WMI query. The pattern allows fuzzy matching using only
- write_count (int, *telegraf* may need to be ran as **root**) - write_count (int, *telegraf* may need to be ran as **root**)
- procstat_lookup - procstat_lookup
- tags: - tags:
- exe (string) - exe
- pid_finder (string) - pid_finder
- pid_file (string) - pid_file
- pattern (string) - pattern
- prefix (string) - prefix
- user (string) - user
- systemd_unit (string) - systemd_unit
- cgroup (string) - cgroup
- win_service (string) - win_service
- result
- fields: - fields:
- pid_count (int) - pid_count (int)
- running (int)
- result_code (int, success = 0, lookup_error = 1)
*NOTE: Resource limit > 2147483647 will be reported as 2147483647.* *NOTE: Resource limit > 2147483647 will be reported as 2147483647.*
### Example Output: ### Example Output:

View File

@ -93,6 +93,7 @@ func (p *Procstat) Gather(acc telegraf.Accumulator) error {
case "pgrep": case "pgrep":
p.createPIDFinder = NewPgrep p.createPIDFinder = NewPgrep
default: default:
p.PidFinder = "pgrep"
p.createPIDFinder = defaultPIDFinder p.createPIDFinder = defaultPIDFinder
} }
@ -101,7 +102,22 @@ func (p *Procstat) Gather(acc telegraf.Accumulator) error {
p.createProcess = defaultProcess p.createProcess = defaultProcess
} }
procs, err := p.updateProcesses(acc, p.procs) pids, tags, err := p.findPids(acc)
if err != nil {
fields := map[string]interface{}{
"pid_count": 0,
"running": 0,
"result_code": 1,
}
tags := map[string]string{
"pid_finder": p.PidFinder,
"result": "lookup_error",
}
acc.AddFields("procstat_lookup", fields, tags)
return err
}
procs, err := p.updateProcesses(pids, tags, p.procs)
if err != nil { if err != nil {
acc.AddError(fmt.Errorf("E! Error: procstat getting process, exe: [%s] pidfile: [%s] pattern: [%s] user: [%s] %s", acc.AddError(fmt.Errorf("E! Error: procstat getting process, exe: [%s] pidfile: [%s] pattern: [%s] user: [%s] %s",
p.Exe, p.PidFile, p.Pattern, p.User, err.Error())) p.Exe, p.PidFile, p.Pattern, p.User, err.Error()))
@ -109,14 +125,23 @@ func (p *Procstat) Gather(acc telegraf.Accumulator) error {
p.procs = procs p.procs = procs
for _, proc := range p.procs { for _, proc := range p.procs {
p.addMetrics(proc, acc) p.addMetric(proc, acc)
} }
fields := map[string]interface{}{
"pid_count": len(pids),
"running": len(procs),
"result_code": 0,
}
tags["pid_finder"] = p.PidFinder
tags["result"] = "success"
acc.AddFields("procstat_lookup", fields, tags)
return nil return nil
} }
// Add metrics a single Process // Add metrics a single Process
func (p *Procstat) addMetrics(proc Process, acc telegraf.Accumulator) { func (p *Procstat) addMetric(proc Process, acc telegraf.Accumulator) {
var prefix string var prefix string
if p.Prefix != "" { if p.Prefix != "" {
prefix = p.Prefix + "_" prefix = p.Prefix + "_"
@ -242,12 +267,7 @@ func (p *Procstat) addMetrics(proc Process, acc telegraf.Accumulator) {
} }
// Update monitored Processes // Update monitored Processes
func (p *Procstat) updateProcesses(acc telegraf.Accumulator, prevInfo map[PID]Process) (map[PID]Process, error) { func (p *Procstat) updateProcesses(pids []PID, tags map[string]string, prevInfo map[PID]Process) (map[PID]Process, error) {
pids, tags, err := p.findPids(acc)
if err != nil {
return nil, err
}
procs := make(map[PID]Process, len(prevInfo)) procs := make(map[PID]Process, len(prevInfo))
for _, pid := range pids { for _, pid := range pids {
@ -327,18 +347,7 @@ func (p *Procstat) findPids(acc telegraf.Accumulator) ([]PID, map[string]string,
err = fmt.Errorf("Either exe, pid_file, user, pattern, systemd_unit, cgroup, or win_service must be specified") err = fmt.Errorf("Either exe, pid_file, user, pattern, systemd_unit, cgroup, or win_service must be specified")
} }
rTags := make(map[string]string) return pids, tags, err
for k, v := range tags {
rTags[k] = v
}
//adds a metric with info on the pgrep query
fields := make(map[string]interface{})
tags["pid_finder"] = p.PidFinder
fields["pid_count"] = len(pids)
acc.AddFields("procstat_lookup", fields, tags)
return pids, rTags, err
} }
// execCommand is so tests can mock out exec.Command usage. // execCommand is so tests can mock out exec.Command usage.