Skip to content

Commit

Permalink
Include CPU usage percent with procstat data
Browse files Browse the repository at this point in the history
closes #484
  • Loading branch information
sparrc committed Jan 20, 2016
1 parent d3a5cca commit 5e23dd5
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions plugins/inputs/procstat/procstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Procstat struct {
Exe string
Pattern string
Prefix string

pidmap map[int32]*process.Process
}

func NewProcstat() *Procstat {
Expand Down Expand Up @@ -46,12 +48,12 @@ func (_ *Procstat) Description() string {
}

func (p *Procstat) Gather(acc inputs.Accumulator) error {
procs, err := p.createProcesses()
err := p.createProcesses()
if err != nil {
log.Printf("Error: procstat getting process, exe: [%s] pidfile: [%s] pattern: [%s] %s",
p.Exe, p.PidFile, p.Pattern, err.Error())
} else {
for _, proc := range procs {
for _, proc := range p.pidmap {
p := NewSpecProcessor(p.Prefix, acc, proc)
p.pushMetrics()
}
Expand All @@ -60,8 +62,7 @@ func (p *Procstat) Gather(acc inputs.Accumulator) error {
return nil
}

func (p *Procstat) createProcesses() ([]*process.Process, error) {
var out []*process.Process
func (p *Procstat) createProcesses() error {
var errstring string
var outerr error

Expand All @@ -71,19 +72,22 @@ func (p *Procstat) createProcesses() ([]*process.Process, error) {
}

for _, pid := range pids {
p, err := process.NewProcess(int32(pid))
if err == nil {
out = append(out, p)
} else {
errstring += err.Error() + " "
_, ok := p.pidmap[pid]
if !ok {
proc, err := process.NewProcess(pid)
if err == nil {
p.pidmap[pid] = proc
} else {
errstring += err.Error() + " "
}
}
}

if errstring != "" {
outerr = fmt.Errorf("%s", errstring)
}

return out, outerr
return outerr
}

func (p *Procstat) getAllPids() ([]int32, error) {
Expand Down

0 comments on commit 5e23dd5

Please sign in to comment.