From dc84cce95c457c5efa9c9a1c5355a9b4b496c12b Mon Sep 17 00:00:00 2001 From: root Date: Fri, 30 Sep 2016 18:48:17 +0300 Subject: [PATCH] added -x option for pgrep in procstat --- etc/telegraf.conf | 2 ++ plugins/inputs/procstat/procstat.go | 11 ++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/etc/telegraf.conf b/etc/telegraf.conf index 5652a7a8b..e6cf3c698 100644 --- a/etc/telegraf.conf +++ b/etc/telegraf.conf @@ -1350,6 +1350,8 @@ # # exe = "nginx" # ## pattern as argument for pgrep (ie, pgrep -f ) # # pattern = "nginx" +# ## match the exact name of the process (ie, pgrep -xf ) +# # exact = false # ## user as argument for pgrep (ie, pgrep -u ) # # user = "nginx" # diff --git a/plugins/inputs/procstat/procstat.go b/plugins/inputs/procstat/procstat.go index 358dc4c0f..0dcbcef99 100644 --- a/plugins/inputs/procstat/procstat.go +++ b/plugins/inputs/procstat/procstat.go @@ -16,6 +16,7 @@ import ( type Procstat struct { PidFile string `toml:"pid_file"` + Exact bool Exe string Pattern string Prefix string @@ -43,6 +44,8 @@ var sampleConfig = ` # exe = "nginx" ## pattern as argument for pgrep (ie, pgrep -f ) # pattern = "nginx" + ## match the exact name of the process (ie, pgrep -xf ) + # exact = false ## user as argument for pgrep (ie, pgrep -u ) # user = "nginx" @@ -176,11 +179,17 @@ func (p *Procstat) pidsFromExe() ([]int32, error) { func (p *Procstat) pidsFromPattern() ([]int32, error) { var out []int32 var outerr error + var options string bin, err := exec.LookPath("pgrep") if err != nil { return out, fmt.Errorf("Couldn't find pgrep binary: %s", err) } - pgrep, err := exec.Command(bin, "-f", p.Pattern).Output() + if p.Exact == true { + options = "-xf" + } else { + options = "-f" + } + pgrep, err := exec.Command(bin, options, p.Pattern).Output() if err != nil { return out, fmt.Errorf("Failed to execute %s. Error: '%s'", bin, err) } else {