2015-10-04 05:09:18 +00:00
|
|
|
package procstat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2015-10-05 22:45:40 +00:00
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/process"
|
|
|
|
|
|
|
|
"github.com/influxdb/telegraf/plugins"
|
2015-10-04 05:09:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Specification struct {
|
2015-10-05 20:27:51 +00:00
|
|
|
PidFile string `toml:"pid_file"`
|
2015-10-04 05:09:18 +00:00
|
|
|
Exe string
|
|
|
|
Prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Procstat struct {
|
|
|
|
Specifications []*Specification
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProcstat() *Procstat {
|
|
|
|
return &Procstat{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2015-10-05 22:45:40 +00:00
|
|
|
[[procstat.specifications]]
|
|
|
|
prefix = "nginx" # required
|
|
|
|
# Use one of pid_file or exe to find process
|
|
|
|
pid_file = "/var/run/nginx.pid"
|
2015-10-04 05:09:18 +00:00
|
|
|
# executable name (used by pgrep)
|
2015-10-05 22:45:40 +00:00
|
|
|
# exe = "nginx"
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Procstat) Gather(acc plugins.Accumulator) error {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var outerr error
|
|
|
|
for _, specification := range p.Specifications {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(spec *Specification, acc plugins.Accumulator) {
|
|
|
|
defer wg.Done()
|
|
|
|
proc, err := spec.createProcess()
|
|
|
|
if err != nil {
|
|
|
|
outerr = err
|
|
|
|
} else {
|
|
|
|
outerr = NewSpecProcessor(spec.Prefix, acc, proc).pushMetrics()
|
|
|
|
}
|
|
|
|
}(specification, acc)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return outerr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (spec *Specification) createProcess() (*process.Process, error) {
|
|
|
|
if spec.PidFile != "" {
|
|
|
|
pid, err := pidFromFile(spec.PidFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return process.NewProcess(int32(pid))
|
|
|
|
} else if spec.Exe != "" {
|
|
|
|
pid, err := pidFromExe(spec.Exe)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return process.NewProcess(int32(pid))
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("Either exe or pid_file has to be specified")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pidFromFile(file string) (int, error) {
|
|
|
|
pidString, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return -1, fmt.Errorf("Failed to read pidfile '%s'. Error: '%s'", file, err)
|
|
|
|
} else {
|
|
|
|
return strconv.Atoi(strings.TrimSpace(string(pidString)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pidFromExe(exe string) (int, error) {
|
|
|
|
pidString, err := exec.Command("pgrep", exe).Output()
|
|
|
|
if err != nil {
|
|
|
|
return -1, fmt.Errorf("Failed to execute pgrep. Error: '%s'", err)
|
|
|
|
} else {
|
|
|
|
return strconv.Atoi(strings.TrimSpace(string(pidString)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2015-10-05 22:38:59 +00:00
|
|
|
plugins.Add("procstat", func() plugins.Plugin {
|
2015-10-04 05:09:18 +00:00
|
|
|
return NewProcstat()
|
|
|
|
})
|
|
|
|
}
|