Skip to content

Commit

Permalink
Add running field to procstat_lookup (influxdata#5069)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson authored and otherpirate committed Mar 15, 2019
1 parent 7f6e565 commit 699157d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
1 change: 1 addition & 0 deletions agent/accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (ac *accumulator) AddHistogram(
}

func (ac *accumulator) AddMetric(m telegraf.Metric) {
m.SetTime(m.Time().Round(ac.precision))
if m := ac.maker.MakeMetric(m); m != nil {
ac.metrics <- m
}
Expand Down
22 changes: 13 additions & 9 deletions plugins/inputs/procstat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**)
- procstat_lookup
- tags:
- exe (string)
- pid_finder (string)
- pid_file (string)
- pattern (string)
- prefix (string)
- user (string)
- systemd_unit (string)
- cgroup (string)
- win_service (string)
- exe
- pid_finder
- pid_file
- pattern
- prefix
- user
- systemd_unit
- cgroup
- win_service
- result
- fields:
- pid_count (int)
- running (int)
- result_code (int, success = 0, lookup_error = 1)

*NOTE: Resource limit > 2147483647 will be reported as 2147483647.*

### Example Output:
Expand Down
51 changes: 30 additions & 21 deletions plugins/inputs/procstat/procstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (p *Procstat) Gather(acc telegraf.Accumulator) error {
case "pgrep":
p.createPIDFinder = NewPgrep
default:
p.PidFinder = "pgrep"
p.createPIDFinder = defaultPIDFinder
}

Expand All @@ -101,22 +102,46 @@ func (p *Procstat) Gather(acc telegraf.Accumulator) error {
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 {
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.procs = 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
}

// 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
if p.Prefix != "" {
prefix = p.Prefix + "_"
Expand Down Expand Up @@ -242,12 +267,7 @@ func (p *Procstat) addMetrics(proc Process, acc telegraf.Accumulator) {
}

// Update monitored Processes
func (p *Procstat) updateProcesses(acc telegraf.Accumulator, prevInfo map[PID]Process) (map[PID]Process, error) {
pids, tags, err := p.findPids(acc)
if err != nil {
return nil, err
}

func (p *Procstat) updateProcesses(pids []PID, tags map[string]string, prevInfo map[PID]Process) (map[PID]Process, error) {
procs := make(map[PID]Process, len(prevInfo))

for _, pid := range pids {
Expand Down Expand Up @@ -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")
}

rTags := make(map[string]string)
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
return pids, tags, err
}

// execCommand is so tests can mock out exec.Command usage.
Expand Down

0 comments on commit 699157d

Please sign in to comment.