2015-10-04 05:09:18 +00:00
|
|
|
package procstat
|
|
|
|
|
|
|
|
import (
|
2017-11-13 18:59:27 +00:00
|
|
|
"bytes"
|
2015-10-04 05:09:18 +00:00
|
|
|
"fmt"
|
2017-11-13 18:59:27 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os/exec"
|
2017-11-13 22:45:31 +00:00
|
|
|
"path/filepath"
|
2015-10-04 05:09:18 +00:00
|
|
|
"strconv"
|
2017-03-17 23:49:11 +00:00
|
|
|
"time"
|
2017-03-06 15:59:36 +00:00
|
|
|
|
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"
|
2017-09-15 18:16:44 +00:00
|
|
|
"github.com/shirou/gopsutil/process"
|
2015-10-04 05:09:18 +00:00
|
|
|
)
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
var (
|
|
|
|
defaultPIDFinder = NewPgrep
|
|
|
|
defaultProcess = NewProc
|
|
|
|
)
|
|
|
|
|
|
|
|
type PID int32
|
|
|
|
|
2015-12-19 23:06:21 +00:00
|
|
|
type Procstat struct {
|
2018-02-01 23:14:27 +00:00
|
|
|
PidFinder string `toml:"pid_finder"`
|
2016-05-19 10:34:25 +00:00
|
|
|
PidFile string `toml:"pid_file"`
|
|
|
|
Exe string
|
|
|
|
Pattern string
|
|
|
|
Prefix string
|
2019-04-11 01:42:38 +00:00
|
|
|
CmdLineTag bool `toml:"cmdline_tag"`
|
2016-05-19 10:34:25 +00:00
|
|
|
ProcessName string
|
|
|
|
User string
|
2017-11-13 18:59:27 +00:00
|
|
|
SystemdUnit string
|
|
|
|
CGroup string `toml:"cgroup"`
|
2016-10-04 00:24:18 +00:00
|
|
|
PidTag bool
|
2018-11-05 21:33:02 +00:00
|
|
|
WinService string `toml:"win_service"`
|
2016-01-20 00:28:02 +00:00
|
|
|
|
2018-02-01 23:14:27 +00:00
|
|
|
finder PIDFinder
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
createPIDFinder func() (PIDFinder, error)
|
|
|
|
procs map[PID]Process
|
|
|
|
createProcess func(PID) (Process, error)
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## 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"
|
2017-11-13 18:59:27 +00:00
|
|
|
## Systemd unit name
|
|
|
|
# systemd_unit = "nginx.service"
|
2017-11-13 19:02:01 +00:00
|
|
|
## CGroup name or path
|
|
|
|
# cgroup = "systemd/system.slice/nginx.service"
|
2015-12-19 23:06:21 +00:00
|
|
|
|
2018-10-05 18:14:44 +00:00
|
|
|
## Windows service name
|
|
|
|
# win_service = ""
|
|
|
|
|
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"
|
2018-02-02 00:12:08 +00:00
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## Field name prefix
|
2018-02-02 00:12:08 +00:00
|
|
|
# prefix = ""
|
|
|
|
|
2019-04-11 01:42:38 +00:00
|
|
|
## When true add the full cmdline as a tag.
|
|
|
|
# cmdline_tag = false
|
|
|
|
|
2020-05-14 07:48:05 +00:00
|
|
|
## Add the PID as a tag instead of as a field. When collecting multiple
|
|
|
|
## processes with otherwise matching tags this setting should be enabled to
|
|
|
|
## ensure each process has a unique identity.
|
|
|
|
##
|
|
|
|
## Enabling this option may result in a large number of series, especially
|
|
|
|
## when processes have a short lifetime.
|
2018-02-02 00:12:08 +00:00
|
|
|
# pid_tag = false
|
|
|
|
|
|
|
|
## Method to use when finding process IDs. Can be one of 'pgrep', or
|
|
|
|
## 'native'. The pgrep finder calls the pgrep executable in the PATH while
|
|
|
|
## the native finder performs the search directly in a manor dependent on the
|
|
|
|
## platform. Default is 'pgrep'
|
|
|
|
# pid_finder = "pgrep"
|
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 {
|
2017-03-21 18:40:51 +00:00
|
|
|
if p.createPIDFinder == nil {
|
2018-02-01 23:14:27 +00:00
|
|
|
switch p.PidFinder {
|
|
|
|
case "native":
|
|
|
|
p.createPIDFinder = NewNativeFinder
|
|
|
|
case "pgrep":
|
|
|
|
p.createPIDFinder = NewPgrep
|
|
|
|
default:
|
2018-12-12 03:11:33 +00:00
|
|
|
p.PidFinder = "pgrep"
|
2018-02-01 23:14:27 +00:00
|
|
|
p.createPIDFinder = defaultPIDFinder
|
|
|
|
}
|
|
|
|
|
2017-03-21 18:40:51 +00:00
|
|
|
}
|
|
|
|
if p.createProcess == nil {
|
|
|
|
p.createProcess = defaultProcess
|
|
|
|
}
|
|
|
|
|
2018-12-12 03:11:33 +00:00
|
|
|
pids, tags, err := p.findPids(acc)
|
|
|
|
if err != nil {
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"pid_count": 0,
|
|
|
|
"running": 0,
|
|
|
|
"result_code": 1,
|
|
|
|
}
|
|
|
|
tags := map[string]string{
|
|
|
|
"pid_finder": p.PidFinder,
|
|
|
|
"result": "lookup_error",
|
|
|
|
}
|
|
|
|
acc.AddFields("procstat_lookup", fields, tags)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
procs, err := p.updateProcesses(pids, tags, p.procs)
|
2015-12-19 23:06:21 +00:00
|
|
|
if err != nil {
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.AddError(fmt.Errorf("E! Error: procstat getting process, exe: [%s] pidfile: [%s] pattern: [%s] user: [%s] %s",
|
|
|
|
p.Exe, p.PidFile, p.Pattern, p.User, err.Error()))
|
2017-03-17 23:49:11 +00:00
|
|
|
}
|
|
|
|
p.procs = procs
|
|
|
|
|
|
|
|
for _, proc := range p.procs {
|
2018-12-12 03:11:33 +00:00
|
|
|
p.addMetric(proc, acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"pid_count": len(pids),
|
|
|
|
"running": len(procs),
|
|
|
|
"result_code": 0,
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
2018-12-12 03:11:33 +00:00
|
|
|
tags["pid_finder"] = p.PidFinder
|
|
|
|
tags["result"] = "success"
|
|
|
|
acc.AddFields("procstat_lookup", fields, tags)
|
2015-10-07 18:31:49 +00:00
|
|
|
|
2017-02-02 14:12:22 +00:00
|
|
|
return nil
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
// Add metrics a single Process
|
2018-12-12 03:11:33 +00:00
|
|
|
func (p *Procstat) addMetric(proc Process, acc telegraf.Accumulator) {
|
2017-03-17 23:49:11 +00:00
|
|
|
var prefix string
|
|
|
|
if p.Prefix != "" {
|
|
|
|
prefix = p.Prefix + "_"
|
2017-03-06 15:59:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
fields := map[string]interface{}{}
|
|
|
|
|
|
|
|
//If process_name tag is not already set, set to actual name
|
|
|
|
if _, nameInTags := proc.Tags()["process_name"]; !nameInTags {
|
|
|
|
name, err := proc.Name()
|
|
|
|
if err == nil {
|
|
|
|
proc.Tags()["process_name"] = name
|
2017-03-06 15:59:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 20:56:41 +00:00
|
|
|
//If user tag is not already set, set to actual name
|
|
|
|
if _, ok := proc.Tags()["user"]; !ok {
|
|
|
|
user, err := proc.Username()
|
|
|
|
if err == nil {
|
|
|
|
proc.Tags()["user"] = user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
//If pid is not present as a tag, include it as a field.
|
|
|
|
if _, pidInTags := proc.Tags()["pid"]; !pidInTags {
|
|
|
|
fields["pid"] = int32(proc.PID())
|
2017-03-06 15:59:36 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 01:42:38 +00:00
|
|
|
//If cmd_line tag is true and it is not already set add cmdline as a tag
|
|
|
|
if p.CmdLineTag {
|
|
|
|
if _, ok := proc.Tags()["cmdline"]; !ok {
|
|
|
|
Cmdline, err := proc.Cmdline()
|
|
|
|
if err == nil {
|
|
|
|
proc.Tags()["cmdline"] = Cmdline
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
numThreads, err := proc.NumThreads()
|
|
|
|
if err == nil {
|
|
|
|
fields[prefix+"num_threads"] = numThreads
|
|
|
|
}
|
2017-03-06 15:59:36 +00:00
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
fds, err := proc.NumFDs()
|
|
|
|
if err == nil {
|
|
|
|
fields[prefix+"num_fds"] = fds
|
|
|
|
}
|
2015-10-07 20:13:33 +00:00
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
ctx, err := proc.NumCtxSwitches()
|
|
|
|
if err == nil {
|
|
|
|
fields[prefix+"voluntary_context_switches"] = ctx.Voluntary
|
|
|
|
fields[prefix+"involuntary_context_switches"] = ctx.Involuntary
|
2015-10-07 20:13:33 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 00:21:02 +00:00
|
|
|
faults, err := proc.PageFaults()
|
|
|
|
if err == nil {
|
|
|
|
fields[prefix+"minor_faults"] = faults.MinorFaults
|
|
|
|
fields[prefix+"major_faults"] = faults.MajorFaults
|
|
|
|
fields[prefix+"child_minor_faults"] = faults.ChildMinorFaults
|
|
|
|
fields[prefix+"child_major_faults"] = faults.ChildMajorFaults
|
|
|
|
}
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
io, err := proc.IOCounters()
|
|
|
|
if err == nil {
|
|
|
|
fields[prefix+"read_count"] = io.ReadCount
|
|
|
|
fields[prefix+"write_count"] = io.WriteCount
|
|
|
|
fields[prefix+"read_bytes"] = io.ReadBytes
|
|
|
|
fields[prefix+"write_bytes"] = io.WriteBytes
|
|
|
|
}
|
2015-10-07 20:13:33 +00:00
|
|
|
|
2020-02-19 19:57:07 +00:00
|
|
|
createdAt, err := proc.CreateTime() //Returns epoch in ms
|
|
|
|
if err == nil {
|
|
|
|
fields[prefix+"created_at"] = createdAt * 1000000 //Convert ms to ns
|
|
|
|
}
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
cpu_time, err := proc.Times()
|
|
|
|
if err == nil {
|
|
|
|
fields[prefix+"cpu_time_user"] = cpu_time.User
|
|
|
|
fields[prefix+"cpu_time_system"] = cpu_time.System
|
|
|
|
fields[prefix+"cpu_time_idle"] = cpu_time.Idle
|
|
|
|
fields[prefix+"cpu_time_nice"] = cpu_time.Nice
|
|
|
|
fields[prefix+"cpu_time_iowait"] = cpu_time.Iowait
|
|
|
|
fields[prefix+"cpu_time_irq"] = cpu_time.Irq
|
|
|
|
fields[prefix+"cpu_time_soft_irq"] = cpu_time.Softirq
|
|
|
|
fields[prefix+"cpu_time_steal"] = cpu_time.Steal
|
|
|
|
fields[prefix+"cpu_time_guest"] = cpu_time.Guest
|
|
|
|
fields[prefix+"cpu_time_guest_nice"] = cpu_time.GuestNice
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
2017-03-17 23:49:11 +00:00
|
|
|
|
|
|
|
cpu_perc, err := proc.Percent(time.Duration(0))
|
|
|
|
if err == nil {
|
|
|
|
fields[prefix+"cpu_usage"] = cpu_perc
|
|
|
|
}
|
|
|
|
|
|
|
|
mem, err := proc.MemoryInfo()
|
|
|
|
if err == nil {
|
|
|
|
fields[prefix+"memory_rss"] = mem.RSS
|
|
|
|
fields[prefix+"memory_vms"] = mem.VMS
|
|
|
|
fields[prefix+"memory_swap"] = mem.Swap
|
2017-09-15 18:16:44 +00:00
|
|
|
fields[prefix+"memory_data"] = mem.Data
|
|
|
|
fields[prefix+"memory_stack"] = mem.Stack
|
|
|
|
fields[prefix+"memory_locked"] = mem.Locked
|
|
|
|
}
|
|
|
|
|
2019-08-14 17:17:38 +00:00
|
|
|
mem_perc, err := proc.MemoryPercent()
|
|
|
|
if err == nil {
|
|
|
|
fields[prefix+"memory_usage"] = mem_perc
|
|
|
|
}
|
|
|
|
|
2017-09-15 18:16:44 +00:00
|
|
|
rlims, err := proc.RlimitUsage(true)
|
|
|
|
if err == nil {
|
|
|
|
for _, rlim := range rlims {
|
|
|
|
var name string
|
|
|
|
switch rlim.Resource {
|
|
|
|
case process.RLIMIT_CPU:
|
|
|
|
name = "cpu_time"
|
|
|
|
case process.RLIMIT_DATA:
|
|
|
|
name = "memory_data"
|
|
|
|
case process.RLIMIT_STACK:
|
|
|
|
name = "memory_stack"
|
|
|
|
case process.RLIMIT_RSS:
|
|
|
|
name = "memory_rss"
|
|
|
|
case process.RLIMIT_NOFILE:
|
|
|
|
name = "num_fds"
|
|
|
|
case process.RLIMIT_MEMLOCK:
|
|
|
|
name = "memory_locked"
|
|
|
|
case process.RLIMIT_AS:
|
|
|
|
name = "memory_vms"
|
|
|
|
case process.RLIMIT_LOCKS:
|
|
|
|
name = "file_locks"
|
|
|
|
case process.RLIMIT_SIGPENDING:
|
|
|
|
name = "signals_pending"
|
|
|
|
case process.RLIMIT_NICE:
|
|
|
|
name = "nice_priority"
|
|
|
|
case process.RLIMIT_RTPRIO:
|
|
|
|
name = "realtime_priority"
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fields[prefix+"rlimit_"+name+"_soft"] = rlim.Soft
|
|
|
|
fields[prefix+"rlimit_"+name+"_hard"] = rlim.Hard
|
|
|
|
if name != "file_locks" { // gopsutil doesn't currently track the used file locks count
|
|
|
|
fields[prefix+name] = rlim.Used
|
|
|
|
}
|
|
|
|
}
|
2017-03-17 23:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
acc.AddFields("procstat", fields, proc.Tags())
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
// Update monitored Processes
|
2018-12-12 03:11:33 +00:00
|
|
|
func (p *Procstat) updateProcesses(pids []PID, tags map[string]string, prevInfo map[PID]Process) (map[PID]Process, error) {
|
2017-03-17 23:49:11 +00:00
|
|
|
procs := make(map[PID]Process, len(prevInfo))
|
|
|
|
|
|
|
|
for _, pid := range pids {
|
|
|
|
info, ok := prevInfo[pid]
|
|
|
|
if ok {
|
2019-04-29 22:32:10 +00:00
|
|
|
// Assumption: if a process has no name, it probably does not exist
|
|
|
|
if name, _ := info.Name(); name == "" {
|
|
|
|
continue
|
|
|
|
}
|
2017-03-17 23:49:11 +00:00
|
|
|
procs[pid] = info
|
|
|
|
} else {
|
|
|
|
proc, err := p.createProcess(pid)
|
|
|
|
if err != nil {
|
|
|
|
// No problem; process may have ended after we found it
|
|
|
|
continue
|
|
|
|
}
|
2019-04-29 22:32:10 +00:00
|
|
|
// Assumption: if a process has no name, it probably does not exist
|
|
|
|
if name, _ := proc.Name(); name == "" {
|
|
|
|
continue
|
|
|
|
}
|
2017-03-17 23:49:11 +00:00
|
|
|
procs[pid] = proc
|
|
|
|
|
|
|
|
// Add initial tags
|
|
|
|
for k, v := range tags {
|
|
|
|
proc.Tags()[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add pid tag if needed
|
|
|
|
if p.PidTag {
|
|
|
|
proc.Tags()["pid"] = strconv.Itoa(int(pid))
|
|
|
|
}
|
|
|
|
if p.ProcessName != "" {
|
|
|
|
proc.Tags()["process_name"] = p.ProcessName
|
2015-10-07 18:31:49 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
2017-03-17 23:49:11 +00:00
|
|
|
return procs, nil
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
// Create and return PIDGatherer lazily
|
|
|
|
func (p *Procstat) getPIDFinder() (PIDFinder, error) {
|
2018-02-01 23:14:27 +00:00
|
|
|
if p.finder == nil {
|
2017-03-17 23:49:11 +00:00
|
|
|
f, err := p.createPIDFinder()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-11-24 11:00:57 +00:00
|
|
|
}
|
2018-02-01 23:14:27 +00:00
|
|
|
p.finder = f
|
2015-11-24 11:00:57 +00:00
|
|
|
}
|
2018-02-01 23:14:27 +00:00
|
|
|
return p.finder, nil
|
2015-11-24 11:00:57 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 23:49:11 +00:00
|
|
|
// Get matching PIDs and their initial tags
|
2018-06-19 18:47:13 +00:00
|
|
|
func (p *Procstat) findPids(acc telegraf.Accumulator) ([]PID, map[string]string, error) {
|
2017-03-17 23:49:11 +00:00
|
|
|
var pids []PID
|
2018-06-19 18:47:13 +00:00
|
|
|
tags := make(map[string]string)
|
2017-03-17 23:49:11 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
f, err := p.getPIDFinder()
|
2016-02-19 18:25:17 +00:00
|
|
|
if err != nil {
|
2017-03-17 23:49:11 +00:00
|
|
|
return nil, nil, err
|
2016-02-19 18:25:17 +00:00
|
|
|
}
|
2017-03-17 23:49:11 +00:00
|
|
|
|
|
|
|
if p.PidFile != "" {
|
|
|
|
pids, err = f.PidFile(p.PidFile)
|
|
|
|
tags = map[string]string{"pidfile": p.PidFile}
|
|
|
|
} else if p.Exe != "" {
|
|
|
|
pids, err = f.Pattern(p.Exe)
|
|
|
|
tags = map[string]string{"exe": p.Exe}
|
|
|
|
} else if p.Pattern != "" {
|
|
|
|
pids, err = f.FullPattern(p.Pattern)
|
|
|
|
tags = map[string]string{"pattern": p.Pattern}
|
|
|
|
} else if p.User != "" {
|
|
|
|
pids, err = f.Uid(p.User)
|
|
|
|
tags = map[string]string{"user": p.User}
|
2017-11-13 18:59:27 +00:00
|
|
|
} else if p.SystemdUnit != "" {
|
|
|
|
pids, err = p.systemdUnitPIDs()
|
|
|
|
tags = map[string]string{"systemd_unit": p.SystemdUnit}
|
|
|
|
} else if p.CGroup != "" {
|
|
|
|
pids, err = p.cgroupPIDs()
|
|
|
|
tags = map[string]string{"cgroup": p.CGroup}
|
2018-10-05 18:14:44 +00:00
|
|
|
} else if p.WinService != "" {
|
|
|
|
pids, err = p.winServicePIDs()
|
|
|
|
tags = map[string]string{"win_service": p.WinService}
|
2016-02-19 18:25:17 +00:00
|
|
|
} else {
|
2018-10-05 18:14:44 +00:00
|
|
|
err = fmt.Errorf("Either exe, pid_file, user, pattern, systemd_unit, cgroup, or win_service must be specified")
|
2016-02-19 18:25:17 +00:00
|
|
|
}
|
2017-03-17 23:49:11 +00:00
|
|
|
|
2018-12-12 03:11:33 +00:00
|
|
|
return pids, tags, err
|
2016-02-19 18:25:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 22:45:31 +00:00
|
|
|
// execCommand is so tests can mock out exec.Command usage.
|
|
|
|
var execCommand = exec.Command
|
|
|
|
|
2017-11-13 18:59:27 +00:00
|
|
|
func (p *Procstat) systemdUnitPIDs() ([]PID, error) {
|
|
|
|
var pids []PID
|
2017-11-13 22:45:31 +00:00
|
|
|
cmd := execCommand("systemctl", "show", p.SystemdUnit)
|
2017-11-13 18:59:27 +00:00
|
|
|
out, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, line := range bytes.Split(out, []byte{'\n'}) {
|
|
|
|
kv := bytes.SplitN(line, []byte{'='}, 2)
|
|
|
|
if len(kv) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !bytes.Equal(kv[0], []byte("MainPID")) {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-14 19:00:14 +00:00
|
|
|
if len(kv[1]) == 0 || bytes.Equal(kv[1], []byte("0")) {
|
2017-11-13 18:59:27 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
pid, err := strconv.Atoi(string(kv[1]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid pid '%s'", kv[1])
|
|
|
|
}
|
|
|
|
pids = append(pids, PID(pid))
|
|
|
|
}
|
|
|
|
return pids, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Procstat) cgroupPIDs() ([]PID, error) {
|
|
|
|
var pids []PID
|
|
|
|
|
|
|
|
procsPath := p.CGroup
|
|
|
|
if procsPath[0] != '/' {
|
|
|
|
procsPath = "/sys/fs/cgroup/" + procsPath
|
|
|
|
}
|
2017-11-13 22:45:31 +00:00
|
|
|
procsPath = filepath.Join(procsPath, "cgroup.procs")
|
2017-11-13 18:59:27 +00:00
|
|
|
out, err := ioutil.ReadFile(procsPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, pidBS := range bytes.Split(out, []byte{'\n'}) {
|
|
|
|
if len(pidBS) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pid, err := strconv.Atoi(string(pidBS))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid pid '%s'", pidBS)
|
|
|
|
}
|
|
|
|
pids = append(pids, PID(pid))
|
|
|
|
}
|
|
|
|
|
|
|
|
return pids, nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 18:14:44 +00:00
|
|
|
func (p *Procstat) winServicePIDs() ([]PID, error) {
|
|
|
|
var pids []PID
|
|
|
|
|
|
|
|
pid, err := queryPidWithWinServiceName(p.WinService)
|
|
|
|
if err != nil {
|
|
|
|
return pids, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pids = append(pids, PID(pid))
|
|
|
|
|
|
|
|
return pids, nil
|
|
|
|
}
|
|
|
|
|
2015-10-04 05:09:18 +00:00
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("procstat", func() telegraf.Input {
|
2017-03-21 18:40:51 +00:00
|
|
|
return &Procstat{}
|
2015-10-04 05:09:18 +00:00
|
|
|
})
|
|
|
|
}
|