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-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-10-04 05:09:18 +00:00
|
|
|
)
|
|
|
|
|
2015-12-19 23:06:21 +00:00
|
|
|
type Procstat struct {
|
2016-05-19 10:34:25 +00:00
|
|
|
PidFile string `toml:"pid_file"`
|
|
|
|
Exe string
|
|
|
|
Pattern string
|
|
|
|
Prefix string
|
|
|
|
ProcessName string
|
|
|
|
User string
|
2016-01-20 00:28:02 +00:00
|
|
|
|
2016-04-20 19:18:07 +00:00
|
|
|
// pidmap maps a pid to a process object, so we don't recreate every gather
|
2016-01-20 00:28:02 +00:00
|
|
|
pidmap map[int32]*process.Process
|
2016-04-20 19:18:07 +00:00
|
|
|
// tagmap maps a pid to a map of tags for that pid
|
|
|
|
tagmap map[int32]map[string]string
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewProcstat() *Procstat {
|
2016-01-20 00:28:02 +00:00
|
|
|
return &Procstat{
|
|
|
|
pidmap: make(map[int32]*process.Process),
|
2016-04-20 19:18:07 +00:00
|
|
|
tagmap: make(map[int32]map[string]string),
|
2016-01-20 00:28:02 +00:00
|
|
|
}
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +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"
|
2016-02-18 21:26:51 +00:00
|
|
|
## executable name (ie, pgrep <exe>)
|
2015-10-15 21:53:29 +00:00
|
|
|
# exe = "nginx"
|
2016-02-18 21:26:51 +00:00
|
|
|
## pattern as argument for pgrep (ie, pgrep -f <pattern>)
|
2015-11-24 11:00:57 +00:00
|
|
|
# pattern = "nginx"
|
2016-02-19 18:25:17 +00:00
|
|
|
## user as argument for pgrep (ie, pgrep -u <user>)
|
|
|
|
# user = "nginx"
|
2015-12-19 23:06:21 +00:00
|
|
|
|
2016-05-19 10:34:25 +00:00
|
|
|
## override for process_name
|
|
|
|
## This is optional; default is sourced from /proc/<pid>/status
|
|
|
|
# process_name = "bar"
|
2016-02-18 21:26:51 +00:00
|
|
|
## Field name prefix
|
2015-12-19 23:06:21 +00:00
|
|
|
prefix = ""
|
2016-04-04 21:16:58 +00:00
|
|
|
## comment this out if you want raw cpu_time stats
|
|
|
|
fielddrop = ["cpu_time_*"]
|
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-27 21:21:36 +00:00
|
|
|
func (p *Procstat) Gather(acc telegraf.Accumulator) error {
|
2016-01-20 00:28:02 +00:00
|
|
|
err := p.createProcesses()
|
2015-12-19 23:06:21 +00:00
|
|
|
if err != nil {
|
2016-02-19 18:27:47 +00:00
|
|
|
log.Printf("Error: procstat getting process, exe: [%s] pidfile: [%s] pattern: [%s] user: [%s] %s",
|
2016-02-19 18:25:17 +00:00
|
|
|
p.Exe, p.PidFile, p.Pattern, p.User, err.Error())
|
2015-12-19 23:06:21 +00:00
|
|
|
} else {
|
2016-04-20 19:18:07 +00:00
|
|
|
for pid, proc := range p.pidmap {
|
2016-05-19 10:34:25 +00:00
|
|
|
p := NewSpecProcessor(p.ProcessName, p.Prefix, acc, proc, p.tagmap[pid])
|
2015-12-19 23:06:21 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-20 00:28:02 +00:00
|
|
|
func (p *Procstat) createProcesses() error {
|
2015-10-07 18:31:49 +00:00
|
|
|
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 {
|
2016-01-20 00:28:02 +00:00
|
|
|
_, ok := p.pidmap[pid]
|
|
|
|
if !ok {
|
|
|
|
proc, err := process.NewProcess(pid)
|
|
|
|
if err == nil {
|
|
|
|
p.pidmap[pid] = proc
|
|
|
|
} else {
|
|
|
|
errstring += err.Error() + " "
|
|
|
|
}
|
2015-10-07 18:31:49 +00:00
|
|
|
}
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
2015-10-07 18:31:49 +00:00
|
|
|
|
|
|
|
if errstring != "" {
|
|
|
|
outerr = fmt.Errorf("%s", errstring)
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:28:02 +00:00
|
|
|
return 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 != "" {
|
2016-04-20 19:18:07 +00:00
|
|
|
pids, err = p.pidsFromFile()
|
2015-12-19 23:06:21 +00:00
|
|
|
} else if p.Exe != "" {
|
2016-04-20 19:18:07 +00:00
|
|
|
pids, err = p.pidsFromExe()
|
2015-12-19 23:06:21 +00:00
|
|
|
} else if p.Pattern != "" {
|
2016-04-20 19:18:07 +00:00
|
|
|
pids, err = p.pidsFromPattern()
|
2016-02-19 18:25:17 +00:00
|
|
|
} else if p.User != "" {
|
2016-04-20 19:18:07 +00:00
|
|
|
pids, err = p.pidsFromUser()
|
2015-10-07 20:13:33 +00:00
|
|
|
} else {
|
2016-04-20 19:18:07 +00:00
|
|
|
err = fmt.Errorf("Either exe, pid_file, user, or pattern has to be specified")
|
2015-10-07 20:13:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pids, err
|
|
|
|
}
|
|
|
|
|
2016-04-20 19:18:07 +00:00
|
|
|
func (p *Procstat) pidsFromFile() ([]int32, error) {
|
2015-10-07 20:13:33 +00:00
|
|
|
var out []int32
|
|
|
|
var outerr error
|
2016-04-20 19:18:07 +00:00
|
|
|
pidString, err := ioutil.ReadFile(p.PidFile)
|
2015-10-04 05:09:18 +00:00
|
|
|
if err != nil {
|
2016-04-20 19:18:07 +00:00
|
|
|
outerr = fmt.Errorf("Failed to read pidfile '%s'. Error: '%s'",
|
|
|
|
p.PidFile, 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))
|
2016-04-20 19:18:07 +00:00
|
|
|
p.tagmap[int32(pid)] = map[string]string{
|
|
|
|
"pidfile": p.PidFile,
|
2016-04-26 01:57:38 +00:00
|
|
|
"pid": strings.TrimSpace(string(pidString)),
|
2016-04-20 19:18:07 +00:00
|
|
|
}
|
2015-10-07 20:13:33 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2016-04-20 19:18:07 +00:00
|
|
|
func (p *Procstat) pidsFromExe() ([]int32, error) {
|
2015-10-07 18:31:49 +00:00
|
|
|
var out []int32
|
|
|
|
var outerr error
|
2016-01-22 18:54:12 +00:00
|
|
|
bin, err := exec.LookPath("pgrep")
|
2015-10-04 05:09:18 +00:00
|
|
|
if err != nil {
|
2016-01-22 18:54:12 +00:00
|
|
|
return out, fmt.Errorf("Couldn't find pgrep binary: %s", err)
|
|
|
|
}
|
2016-04-20 19:18:07 +00:00
|
|
|
pgrep, err := exec.Command(bin, p.Exe).Output()
|
2016-01-22 18:54:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return out, fmt.Errorf("Failed to execute %s. Error: '%s'", bin, 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))
|
2016-04-20 19:18:07 +00:00
|
|
|
p.tagmap[int32(ipid)] = map[string]string{
|
|
|
|
"exe": p.Exe,
|
|
|
|
"pid": pid,
|
|
|
|
}
|
2015-10-07 18:31:49 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2016-04-20 19:18:07 +00:00
|
|
|
func (p *Procstat) pidsFromPattern() ([]int32, error) {
|
2015-11-24 11:00:57 +00:00
|
|
|
var out []int32
|
|
|
|
var outerr error
|
2016-01-22 18:54:12 +00:00
|
|
|
bin, err := exec.LookPath("pgrep")
|
|
|
|
if err != nil {
|
|
|
|
return out, fmt.Errorf("Couldn't find pgrep binary: %s", err)
|
|
|
|
}
|
2016-04-20 19:18:07 +00:00
|
|
|
pgrep, err := exec.Command(bin, "-f", p.Pattern).Output()
|
2015-11-24 11:00:57 +00:00
|
|
|
if err != nil {
|
2016-01-22 18:54:12 +00:00
|
|
|
return out, fmt.Errorf("Failed to execute %s. Error: '%s'", bin, err)
|
2015-11-24 11:00:57 +00:00
|
|
|
} else {
|
|
|
|
pids := strings.Fields(string(pgrep))
|
|
|
|
for _, pid := range pids {
|
|
|
|
ipid, err := strconv.Atoi(pid)
|
|
|
|
if err == nil {
|
|
|
|
out = append(out, int32(ipid))
|
2016-04-20 19:18:07 +00:00
|
|
|
p.tagmap[int32(ipid)] = map[string]string{
|
|
|
|
"pattern": p.Pattern,
|
|
|
|
"pid": pid,
|
|
|
|
}
|
2015-11-24 11:00:57 +00:00
|
|
|
} else {
|
|
|
|
outerr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out, outerr
|
|
|
|
}
|
|
|
|
|
2016-04-20 19:18:07 +00:00
|
|
|
func (p *Procstat) pidsFromUser() ([]int32, error) {
|
2016-02-19 18:25:17 +00:00
|
|
|
var out []int32
|
|
|
|
var outerr error
|
|
|
|
bin, err := exec.LookPath("pgrep")
|
|
|
|
if err != nil {
|
|
|
|
return out, fmt.Errorf("Couldn't find pgrep binary: %s", err)
|
|
|
|
}
|
2016-04-20 19:18:07 +00:00
|
|
|
pgrep, err := exec.Command(bin, "-u", p.User).Output()
|
2016-02-19 18:25:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return out, fmt.Errorf("Failed to execute %s. Error: '%s'", bin, err)
|
|
|
|
} else {
|
|
|
|
pids := strings.Fields(string(pgrep))
|
|
|
|
for _, pid := range pids {
|
|
|
|
ipid, err := strconv.Atoi(pid)
|
|
|
|
if err == nil {
|
|
|
|
out = append(out, int32(ipid))
|
2016-04-20 19:18:07 +00:00
|
|
|
p.tagmap[int32(ipid)] = map[string]string{
|
|
|
|
"user": p.User,
|
|
|
|
"pid": pid,
|
|
|
|
}
|
2016-02-19 18:25:17 +00:00
|
|
|
} else {
|
|
|
|
outerr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out, outerr
|
|
|
|
}
|
|
|
|
|
2015-10-04 05:09:18 +00:00
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("procstat", func() telegraf.Input {
|
2015-10-04 05:09:18 +00:00
|
|
|
return NewProcstat()
|
|
|
|
})
|
|
|
|
}
|