2015-10-04 05:09:18 +00:00
|
|
|
package procstat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-10-07 18:31:49 +00:00
|
|
|
"log"
|
2015-10-04 05:09:18 +00:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-10-05 22:45:40 +00:00
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/process"
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
"github.com/influxdb/telegraf/plugins/inputs"
|
2015-10-04 05:09:18 +00:00
|
|
|
)
|
|
|
|
|
2015-12-19 23:06:21 +00:00
|
|
|
type Procstat struct {
|
2015-10-05 20:27:51 +00:00
|
|
|
PidFile string `toml:"pid_file"`
|
2015-10-04 05:09:18 +00:00
|
|
|
Exe string
|
2015-11-24 11:00:57 +00:00
|
|
|
Pattern string
|
2015-12-19 23:06:21 +00:00
|
|
|
Prefix string
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewProcstat() *Procstat {
|
|
|
|
return &Procstat{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2015-11-25 21:30:54 +00:00
|
|
|
# Must specify one of: pid_file, exe, or pattern
|
|
|
|
# PID file to monitor process
|
2015-10-15 21:53:29 +00:00
|
|
|
pid_file = "/var/run/nginx.pid"
|
2015-11-25 21:30:54 +00:00
|
|
|
# executable name (ie, pgrep <exe>)
|
2015-10-15 21:53:29 +00:00
|
|
|
# exe = "nginx"
|
2015-11-25 21:30:54 +00:00
|
|
|
# pattern as argument for pgrep (ie, pgrep -f <pattern>)
|
2015-11-24 11:00:57 +00:00
|
|
|
# pattern = "nginx"
|
2015-12-19 23:06:21 +00:00
|
|
|
|
|
|
|
# Field name prefix
|
|
|
|
prefix = ""
|
2015-10-04 05:09:18 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (_ *Procstat) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *Procstat) Description() string {
|
2015-10-05 22:45:40 +00:00
|
|
|
return "Monitor process cpu and memory usage"
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
func (p *Procstat) Gather(acc inputs.Accumulator) error {
|
2015-12-19 23:06:21 +00:00
|
|
|
procs, err := p.createProcesses()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error: procstat getting process, exe: [%s] pidfile: [%s] pattern: [%s] %s",
|
|
|
|
p.Exe, p.PidFile, p.Pattern, err.Error())
|
|
|
|
} else {
|
|
|
|
for _, proc := range procs {
|
|
|
|
p := NewSpecProcessor(p.Prefix, acc, proc)
|
|
|
|
p.pushMetrics()
|
|
|
|
}
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
2015-10-07 18:31:49 +00:00
|
|
|
|
|
|
|
return nil
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 23:06:21 +00:00
|
|
|
func (p *Procstat) createProcesses() ([]*process.Process, error) {
|
2015-10-07 18:31:49 +00:00
|
|
|
var out []*process.Process
|
|
|
|
var errstring string
|
|
|
|
var outerr error
|
|
|
|
|
2015-12-19 23:06:21 +00:00
|
|
|
pids, err := p.getAllPids()
|
2015-10-07 20:13:33 +00:00
|
|
|
if err != nil {
|
|
|
|
errstring += err.Error() + " "
|
2015-10-07 18:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, pid := range pids {
|
|
|
|
p, err := process.NewProcess(int32(pid))
|
|
|
|
if err == nil {
|
|
|
|
out = append(out, p)
|
|
|
|
} else {
|
|
|
|
errstring += err.Error() + " "
|
|
|
|
}
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
2015-10-07 18:31:49 +00:00
|
|
|
|
|
|
|
if errstring != "" {
|
|
|
|
outerr = fmt.Errorf("%s", errstring)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, outerr
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 23:06:21 +00:00
|
|
|
func (p *Procstat) getAllPids() ([]int32, error) {
|
2015-10-07 20:13:33 +00:00
|
|
|
var pids []int32
|
|
|
|
var err error
|
|
|
|
|
2015-12-19 23:06:21 +00:00
|
|
|
if p.PidFile != "" {
|
|
|
|
pids, err = pidsFromFile(p.PidFile)
|
|
|
|
} else if p.Exe != "" {
|
|
|
|
pids, err = pidsFromExe(p.Exe)
|
|
|
|
} else if p.Pattern != "" {
|
|
|
|
pids, err = pidsFromPattern(p.Pattern)
|
2015-10-07 20:13:33 +00:00
|
|
|
} else {
|
2015-11-24 11:00:57 +00:00
|
|
|
err = fmt.Errorf("Either exe, pid_file or pattern has to be specified")
|
2015-10-07 20:13:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pids, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func pidsFromFile(file string) ([]int32, error) {
|
|
|
|
var out []int32
|
|
|
|
var outerr error
|
2015-10-04 05:09:18 +00:00
|
|
|
pidString, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
2015-10-07 20:13:33 +00:00
|
|
|
outerr = fmt.Errorf("Failed to read pidfile '%s'. Error: '%s'", file, err)
|
2015-10-04 05:09:18 +00:00
|
|
|
} else {
|
2015-10-07 20:13:33 +00:00
|
|
|
pid, err := strconv.Atoi(strings.TrimSpace(string(pidString)))
|
|
|
|
if err != nil {
|
|
|
|
outerr = err
|
|
|
|
} else {
|
|
|
|
out = append(out, int32(pid))
|
|
|
|
}
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
2015-10-07 20:13:33 +00:00
|
|
|
return out, outerr
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 18:31:49 +00:00
|
|
|
func pidsFromExe(exe string) ([]int32, error) {
|
|
|
|
var out []int32
|
|
|
|
var outerr error
|
|
|
|
pgrep, err := exec.Command("pgrep", exe).Output()
|
2015-10-04 05:09:18 +00:00
|
|
|
if err != nil {
|
2015-10-07 18:31:49 +00:00
|
|
|
return out, fmt.Errorf("Failed to execute pgrep. Error: '%s'", err)
|
2015-10-04 05:09:18 +00:00
|
|
|
} else {
|
2015-10-07 18:31:49 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
2015-10-07 18:31:49 +00:00
|
|
|
return out, outerr
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 11:00:57 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-10-04 05:09:18 +00:00
|
|
|
func init() {
|
2016-01-07 20:39:43 +00:00
|
|
|
inputs.Add("procstat", func() inputs.Input {
|
2015-10-04 05:09:18 +00:00
|
|
|
return NewProcstat()
|
|
|
|
})
|
|
|
|
}
|