Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions plugins/procstat/procstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Specification struct {
PidFile string `toml:"pid_file"`
Exe string
Prefix string
Pattern string
}

type Procstat struct {
Expand All @@ -35,6 +36,7 @@ var sampleConfig = `
pid_file = "/var/run/nginx.pid"
# executable name (used by pgrep)
# exe = "nginx"
# pattern = "nginx"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment above this one to let users know it is doing a pgrep -f

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done via 63065c7

`

func (_ *Procstat) SampleConfig() string {
Expand All @@ -54,8 +56,8 @@ func (p *Procstat) Gather(acc plugins.Accumulator) error {
defer wg.Done()
procs, err := spec.createProcesses()
if err != nil {
log.Printf("Error: procstat getting process, exe: [%s] pidfile: [%s] %s",
spec.Exe, spec.PidFile, err.Error())
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)
Expand Down Expand Up @@ -103,8 +105,10 @@ func (spec *Specification) getAllPids() ([]int32, error) {
pids, err = pidsFromFile(spec.PidFile)
} else if spec.Exe != "" {
pids, err = pidsFromExe(spec.Exe)
} else if spec.Pattern != "" {
pids, err = pidsFromPattern(spec.Pattern)
} 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
Expand Down Expand Up @@ -147,6 +151,26 @@ func pidsFromExe(exe string) ([]int32, error) {
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() {
plugins.Add("procstat", func() plugins.Plugin {
return NewProcstat()
Expand Down