Use pgrep with a pattern

This commit is contained in:
Eduard Carreras 2015-11-24 12:00:57 +01:00 committed by Cameron Sparr
parent d505be1fd4
commit e2aa0e8a35
1 changed files with 27 additions and 3 deletions

View File

@ -18,6 +18,7 @@ type Specification struct {
PidFile string `toml:"pid_file"` PidFile string `toml:"pid_file"`
Exe string Exe string
Prefix string Prefix string
Pattern string
} }
type Procstat struct { type Procstat struct {
@ -35,6 +36,7 @@ var sampleConfig = `
pid_file = "/var/run/nginx.pid" pid_file = "/var/run/nginx.pid"
# executable name (used by pgrep) # executable name (used by pgrep)
# exe = "nginx" # exe = "nginx"
# pattern = "nginx"
` `
func (_ *Procstat) SampleConfig() string { func (_ *Procstat) SampleConfig() string {
@ -54,8 +56,8 @@ func (p *Procstat) Gather(acc plugins.Accumulator) error {
defer wg.Done() defer wg.Done()
procs, err := spec.createProcesses() procs, err := spec.createProcesses()
if err != nil { if err != nil {
log.Printf("Error: procstat getting process, exe: [%s] pidfile: [%s] %s", log.Printf("Error: procstat getting process, exe: [%s] pidfile: [%s] pattern: [%s] %s",
spec.Exe, spec.PidFile, err.Error()) spec.Exe, spec.PidFile, spec.Pattern, err.Error())
} else { } else {
for _, proc := range procs { for _, proc := range procs {
p := NewSpecProcessor(spec.Prefix, acc, proc) p := NewSpecProcessor(spec.Prefix, acc, proc)
@ -103,8 +105,10 @@ func (spec *Specification) getAllPids() ([]int32, error) {
pids, err = pidsFromFile(spec.PidFile) pids, err = pidsFromFile(spec.PidFile)
} else if spec.Exe != "" { } else if spec.Exe != "" {
pids, err = pidsFromExe(spec.Exe) pids, err = pidsFromExe(spec.Exe)
} else if spec.Pattern != "" {
pids, err = pidsFromPattern(spec.Pattern)
} else { } else {
err = fmt.Errorf("Either exe or pid_file has to be specified") err = fmt.Errorf("Either exe, pid_file or pattern has to be specified")
} }
return pids, err return pids, err
@ -147,6 +151,26 @@ func pidsFromExe(exe string) ([]int32, error) {
return out, outerr return out, outerr
} }
func pidsFromPattern(pattern string) ([]int32, error) {
var out []int32
var outerr error
pgrep, err := exec.Command("pgrep", "-f", pattern).Output()
if err != nil {
return out, fmt.Errorf("Failed to execute pgrep. Error: '%s'", err)
} else {
pids := strings.Fields(string(pgrep))
for _, pid := range pids {
ipid, err := strconv.Atoi(pid)
if err == nil {
out = append(out, int32(ipid))
} else {
outerr = err
}
}
}
return out, outerr
}
func init() { func init() {
plugins.Add("procstat", func() plugins.Plugin { plugins.Add("procstat", func() plugins.Plugin {
return NewProcstat() return NewProcstat()