Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pgrep with a pattern #392

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
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
31 changes: 28 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,8 @@ var sampleConfig = `
pid_file = "/var/run/nginx.pid"
# executable name (used by pgrep)
# exe = "nginx"
# pattern as argument for pgrep -f
# 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 +57,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 +106,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 +152,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