From a2f429bdaee1baa8b6c84a1e790cdea294df4e84 Mon Sep 17 00:00:00 2001 From: Ben Aldrich Date: Sat, 9 Dec 2017 19:23:31 -0700 Subject: [PATCH] working implementation using the new gopsutil --- plugins/inputs/procstat/pgrep.go | 91 ------------------------ plugins/inputs/procstat/process.go | 7 ++ plugins/inputs/procstat/procstat_test.go | 2 + 3 files changed, 9 insertions(+), 91 deletions(-) delete mode 100644 plugins/inputs/procstat/pgrep.go diff --git a/plugins/inputs/procstat/pgrep.go b/plugins/inputs/procstat/pgrep.go deleted file mode 100644 index bae5161e4fe6d..0000000000000 --- a/plugins/inputs/procstat/pgrep.go +++ /dev/null @@ -1,91 +0,0 @@ -package procstat - -import ( - "fmt" - "io/ioutil" - "os/exec" - "strconv" - "strings" -) - -type PIDFinder interface { - PidFile(path string) ([]PID, error) - Pattern(pattern string) ([]PID, error) - Uid(user string) ([]PID, error) - FullPattern(path string) ([]PID, error) -} - -// Implemention of PIDGatherer that execs pgrep to find processes -type Pgrep struct { - path string -} - -func NewPgrep() (PIDFinder, error) { - path, err := exec.LookPath("pgrep") - if err != nil { - return nil, fmt.Errorf("Could not find pgrep binary: %s", err) - } - return &Pgrep{path}, nil -} - -func (pg *Pgrep) PidFile(path string) ([]PID, error) { - var pids []PID - pidString, err := ioutil.ReadFile(path) - if err != nil { - return pids, fmt.Errorf("Failed to read pidfile '%s'. Error: '%s'", - path, err) - } - pid, err := strconv.Atoi(strings.TrimSpace(string(pidString))) - if err != nil { - return pids, err - } - pids = append(pids, PID(pid)) - return pids, nil -} - -func (pg *Pgrep) Pattern(pattern string) ([]PID, error) { - args := []string{pattern} - return find(pg.path, args) -} - -func (pg *Pgrep) Uid(user string) ([]PID, error) { - args := []string{"-u", user} - return find(pg.path, args) -} - -func (pg *Pgrep) FullPattern(pattern string) ([]PID, error) { - args := []string{"-f", pattern} - return find(pg.path, args) -} - -func find(path string, args []string) ([]PID, error) { - out, err := run(path, args) - if err != nil { - return nil, err - } - - return parseOutput(out) -} - -func run(path string, args []string) (string, error) { - out, err := exec.Command(path, args...).Output() - if err != nil { - return "", fmt.Errorf("Error running %s: %s", path, err) - } - return string(out), err -} - -func parseOutput(out string) ([]PID, error) { - pids := []PID{} - fields := strings.Fields(out) - for _, field := range fields { - pid, err := strconv.Atoi(field) - if err != nil { - return nil, err - } - if err == nil { - pids = append(pids, PID(pid)) - } - } - return pids, nil -} diff --git a/plugins/inputs/procstat/process.go b/plugins/inputs/procstat/process.go index 3470a8a94cbf5..361582c334119 100644 --- a/plugins/inputs/procstat/process.go +++ b/plugins/inputs/procstat/process.go @@ -23,6 +23,13 @@ type Process interface { RlimitUsage(bool) ([]process.RlimitStat, error) } +type PIDFinder interface { + PidFile(path string) ([]PID, error) + Pattern(pattern string) ([]PID, error) + Uid(user string) ([]PID, error) + FullPattern(path string) ([]PID, error) +} + type Proc struct { hasCPUTimes bool tags map[string]string diff --git a/plugins/inputs/procstat/procstat_test.go b/plugins/inputs/procstat/procstat_test.go index 7b9d6f0c3a4b1..4fbc769c549ee 100644 --- a/plugins/inputs/procstat/procstat_test.go +++ b/plugins/inputs/procstat/procstat_test.go @@ -1,3 +1,5 @@ +// +build !windows + package procstat import (