0.3.0 Removing internal parallelism: procstat

This commit is contained in:
Cameron Sparr 2015-12-19 16:06:21 -07:00
parent a8c164432a
commit 148b93bafb
1 changed files with 23 additions and 36 deletions

View File

@ -7,22 +7,17 @@ import (
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"sync"
"github.com/shirou/gopsutil/process" "github.com/shirou/gopsutil/process"
"github.com/influxdb/telegraf/plugins" "github.com/influxdb/telegraf/plugins"
) )
type Specification struct { type Procstat struct {
PidFile string `toml:"pid_file"` PidFile string `toml:"pid_file"`
Exe string Exe string
Prefix string
Pattern string Pattern string
} Prefix string
type Procstat struct {
Specifications []*Specification
} }
func NewProcstat() *Procstat { func NewProcstat() *Procstat {
@ -30,8 +25,6 @@ func NewProcstat() *Procstat {
} }
var sampleConfig = ` var sampleConfig = `
[[plugins.procstat.specifications]]
prefix = "" # optional string to prefix measurements
# Must specify one of: pid_file, exe, or pattern # Must specify one of: pid_file, exe, or pattern
# PID file to monitor process # PID file to monitor process
pid_file = "/var/run/nginx.pid" pid_file = "/var/run/nginx.pid"
@ -39,6 +32,9 @@ var sampleConfig = `
# exe = "nginx" # exe = "nginx"
# pattern as argument for pgrep (ie, pgrep -f <pattern>) # pattern as argument for pgrep (ie, pgrep -f <pattern>)
# pattern = "nginx" # pattern = "nginx"
# Field name prefix
prefix = ""
` `
func (_ *Procstat) SampleConfig() string { func (_ *Procstat) SampleConfig() string {
@ -50,35 +46,26 @@ func (_ *Procstat) Description() string {
} }
func (p *Procstat) Gather(acc plugins.Accumulator) error { func (p *Procstat) Gather(acc plugins.Accumulator) error {
var wg sync.WaitGroup procs, err := p.createProcesses()
if err != nil {
for _, specification := range p.Specifications { log.Printf("Error: procstat getting process, exe: [%s] pidfile: [%s] pattern: [%s] %s",
wg.Add(1) p.Exe, p.PidFile, p.Pattern, err.Error())
go func(spec *Specification, acc plugins.Accumulator) { } else {
defer wg.Done() for _, proc := range procs {
procs, err := spec.createProcesses() p := NewSpecProcessor(p.Prefix, acc, proc)
if err != nil { p.pushMetrics()
log.Printf("Error: procstat getting process, exe: [%s] pidfile: [%s] pattern: [%s] %s", }
spec.Exe, spec.PidFile, spec.Pattern, err.Error())
} else {
for _, proc := range procs {
p := NewSpecProcessor(spec.Prefix, acc, proc)
p.pushMetrics()
}
}
}(specification, acc)
} }
wg.Wait()
return nil return nil
} }
func (spec *Specification) createProcesses() ([]*process.Process, error) { func (p *Procstat) createProcesses() ([]*process.Process, error) {
var out []*process.Process var out []*process.Process
var errstring string var errstring string
var outerr error var outerr error
pids, err := spec.getAllPids() pids, err := p.getAllPids()
if err != nil { if err != nil {
errstring += err.Error() + " " errstring += err.Error() + " "
} }
@ -99,16 +86,16 @@ func (spec *Specification) createProcesses() ([]*process.Process, error) {
return out, outerr return out, outerr
} }
func (spec *Specification) getAllPids() ([]int32, error) { func (p *Procstat) getAllPids() ([]int32, error) {
var pids []int32 var pids []int32
var err error var err error
if spec.PidFile != "" { if p.PidFile != "" {
pids, err = pidsFromFile(spec.PidFile) pids, err = pidsFromFile(p.PidFile)
} else if spec.Exe != "" { } else if p.Exe != "" {
pids, err = pidsFromExe(spec.Exe) pids, err = pidsFromExe(p.Exe)
} else if spec.Pattern != "" { } else if p.Pattern != "" {
pids, err = pidsFromPattern(spec.Pattern) pids, err = pidsFromPattern(p.Pattern)
} else { } else {
err = fmt.Errorf("Either exe, pid_file or pattern has to be specified") err = fmt.Errorf("Either exe, pid_file or pattern has to be specified")
} }