renaming plugins -> inputs
This commit is contained in:
72
plugins/inputs/procstat/README.md
Normal file
72
plugins/inputs/procstat/README.md
Normal file
@@ -0,0 +1,72 @@
|
||||
# Telegraf plugin: procstat
|
||||
|
||||
#### Description
|
||||
|
||||
The procstat plugin can be used to monitor system resource usage by an
|
||||
individual process using their /proc data.
|
||||
|
||||
The plugin will tag processes by their PID and their process name.
|
||||
|
||||
Processes can be specified either by pid file or by executable name. Procstat
|
||||
plugin will use `pgrep` when executable name is provided to obtain the pid.
|
||||
Proctstas plugin will transmit IO, memory, cpu, file descriptor related
|
||||
measurements for every process specified. A prefix can be set to isolate
|
||||
individual process specific measurements.
|
||||
|
||||
Example:
|
||||
|
||||
```
|
||||
[procstat]
|
||||
|
||||
[[procstat.specifications]]
|
||||
exe = "influxd"
|
||||
prefix = "influxd"
|
||||
|
||||
[[procstat.specifications]]
|
||||
pid_file = "/var/run/lxc/dnsmasq.pid"
|
||||
```
|
||||
|
||||
The above configuration would result in output like:
|
||||
|
||||
```
|
||||
[...]
|
||||
> [name="dnsmasq" pid="44979"] procstat_cpu_user value=0.14
|
||||
> [name="dnsmasq" pid="44979"] procstat_cpu_system value=0.07
|
||||
[...]
|
||||
> [name="influxd" pid="34337"] procstat_influxd_cpu_user value=25.43
|
||||
> [name="influxd" pid="34337"] procstat_influxd_cpu_system value=21.82
|
||||
```
|
||||
|
||||
# Measurements
|
||||
Note: prefix can be set by the user, per process.
|
||||
|
||||
File descriptor related measurement names:
|
||||
- procstat_[prefix_]num_fds value=4
|
||||
|
||||
Context switch related measurement names:
|
||||
- procstat_[prefix_]voluntary_context_switches value=250
|
||||
- procstat_[prefix_]involuntary_context_switches value=0
|
||||
|
||||
I/O related measurement names:
|
||||
- procstat_[prefix_]read_count value=396
|
||||
- procstat_[prefix_]write_count value=1
|
||||
- procstat_[prefix_]read_bytes value=1019904
|
||||
- procstat_[prefix_]write_bytes value=1
|
||||
|
||||
CPU related measurement names:
|
||||
- procstat_[prefix_]cpu_user value=0
|
||||
- procstat_[prefix_]cpu_system value=0.01
|
||||
- procstat_[prefix_]cpu_idle value=0
|
||||
- procstat_[prefix_]cpu_nice value=0
|
||||
- procstat_[prefix_]cpu_iowait value=0
|
||||
- procstat_[prefix_]cpu_irq value=0
|
||||
- procstat_[prefix_]cpu_soft_irq value=0
|
||||
- procstat_[prefix_]cpu_soft_steal value=0
|
||||
- procstat_[prefix_]cpu_soft_stolen value=0
|
||||
- procstat_[prefix_]cpu_soft_guest value=0
|
||||
- procstat_[prefix_]cpu_soft_guest_nice value=0
|
||||
|
||||
Memory related measurement names:
|
||||
- procstat_[prefix_]memory_rss value=1777664
|
||||
- procstat_[prefix_]memory_vms value=24227840
|
||||
- procstat_[prefix_]memory_swap value=282624
|
||||
167
plugins/inputs/procstat/procstat.go
Normal file
167
plugins/inputs/procstat/procstat.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package procstat
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/shirou/gopsutil/process"
|
||||
|
||||
"github.com/influxdb/telegraf/plugins/inputs"
|
||||
)
|
||||
|
||||
type Procstat struct {
|
||||
PidFile string `toml:"pid_file"`
|
||||
Exe string
|
||||
Pattern string
|
||||
Prefix string
|
||||
}
|
||||
|
||||
func NewProcstat() *Procstat {
|
||||
return &Procstat{}
|
||||
}
|
||||
|
||||
var sampleConfig = `
|
||||
# Must specify one of: pid_file, exe, or pattern
|
||||
# PID file to monitor process
|
||||
pid_file = "/var/run/nginx.pid"
|
||||
# executable name (ie, pgrep <exe>)
|
||||
# exe = "nginx"
|
||||
# pattern as argument for pgrep (ie, pgrep -f <pattern>)
|
||||
# pattern = "nginx"
|
||||
|
||||
# Field name prefix
|
||||
prefix = ""
|
||||
`
|
||||
|
||||
func (_ *Procstat) SampleConfig() string {
|
||||
return sampleConfig
|
||||
}
|
||||
|
||||
func (_ *Procstat) Description() string {
|
||||
return "Monitor process cpu and memory usage"
|
||||
}
|
||||
|
||||
func (p *Procstat) Gather(acc inputs.Accumulator) error {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Procstat) createProcesses() ([]*process.Process, error) {
|
||||
var out []*process.Process
|
||||
var errstring string
|
||||
var outerr error
|
||||
|
||||
pids, err := p.getAllPids()
|
||||
if err != nil {
|
||||
errstring += err.Error() + " "
|
||||
}
|
||||
|
||||
for _, pid := range pids {
|
||||
p, err := process.NewProcess(int32(pid))
|
||||
if err == nil {
|
||||
out = append(out, p)
|
||||
} else {
|
||||
errstring += err.Error() + " "
|
||||
}
|
||||
}
|
||||
|
||||
if errstring != "" {
|
||||
outerr = fmt.Errorf("%s", errstring)
|
||||
}
|
||||
|
||||
return out, outerr
|
||||
}
|
||||
|
||||
func (p *Procstat) getAllPids() ([]int32, error) {
|
||||
var pids []int32
|
||||
var err error
|
||||
|
||||
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)
|
||||
} else {
|
||||
err = fmt.Errorf("Either exe, pid_file or pattern has to be specified")
|
||||
}
|
||||
|
||||
return pids, err
|
||||
}
|
||||
|
||||
func pidsFromFile(file string) ([]int32, error) {
|
||||
var out []int32
|
||||
var outerr error
|
||||
pidString, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
outerr = fmt.Errorf("Failed to read pidfile '%s'. Error: '%s'", file, err)
|
||||
} else {
|
||||
pid, err := strconv.Atoi(strings.TrimSpace(string(pidString)))
|
||||
if err != nil {
|
||||
outerr = err
|
||||
} else {
|
||||
out = append(out, int32(pid))
|
||||
}
|
||||
}
|
||||
return out, outerr
|
||||
}
|
||||
|
||||
func pidsFromExe(exe string) ([]int32, error) {
|
||||
var out []int32
|
||||
var outerr error
|
||||
pgrep, err := exec.Command("pgrep", exe).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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func init() {
|
||||
inputs.Add("procstat", func() inputs.Input {
|
||||
return NewProcstat()
|
||||
})
|
||||
}
|
||||
30
plugins/inputs/procstat/procstat_test.go
Normal file
30
plugins/inputs/procstat/procstat_test.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package procstat
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/influxdb/telegraf/testutil"
|
||||
)
|
||||
|
||||
func TestGather(t *testing.T) {
|
||||
var acc testutil.Accumulator
|
||||
pid := os.Getpid()
|
||||
file, err := ioutil.TempFile(os.TempDir(), "telegraf")
|
||||
require.NoError(t, err)
|
||||
file.Write([]byte(strconv.Itoa(pid)))
|
||||
file.Close()
|
||||
defer os.Remove(file.Name())
|
||||
p := Procstat{
|
||||
PidFile: file.Name(),
|
||||
Prefix: "foo",
|
||||
}
|
||||
p.Gather(&acc)
|
||||
assert.True(t, acc.HasFloatField("procstat", "foo_cpu_time_user"))
|
||||
assert.True(t, acc.HasUIntField("procstat", "foo_memory_vms"))
|
||||
}
|
||||
133
plugins/inputs/procstat/spec_processor.go
Normal file
133
plugins/inputs/procstat/spec_processor.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package procstat
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/shirou/gopsutil/process"
|
||||
|
||||
"github.com/influxdb/telegraf/plugins/inputs"
|
||||
)
|
||||
|
||||
type SpecProcessor struct {
|
||||
Prefix string
|
||||
tags map[string]string
|
||||
fields map[string]interface{}
|
||||
acc inputs.Accumulator
|
||||
proc *process.Process
|
||||
}
|
||||
|
||||
func (p *SpecProcessor) add(metric string, value interface{}) {
|
||||
var mname string
|
||||
if p.Prefix == "" {
|
||||
mname = metric
|
||||
} else {
|
||||
mname = p.Prefix + "_" + metric
|
||||
}
|
||||
p.fields[mname] = value
|
||||
}
|
||||
|
||||
func (p *SpecProcessor) flush() {
|
||||
p.acc.AddFields("procstat", p.fields, p.tags)
|
||||
p.fields = make(map[string]interface{})
|
||||
}
|
||||
|
||||
func NewSpecProcessor(
|
||||
prefix string,
|
||||
acc inputs.Accumulator,
|
||||
p *process.Process,
|
||||
) *SpecProcessor {
|
||||
tags := make(map[string]string)
|
||||
tags["pid"] = fmt.Sprintf("%v", p.Pid)
|
||||
if name, err := p.Name(); err == nil {
|
||||
tags["name"] = name
|
||||
}
|
||||
return &SpecProcessor{
|
||||
Prefix: prefix,
|
||||
tags: tags,
|
||||
fields: make(map[string]interface{}),
|
||||
acc: acc,
|
||||
proc: p,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *SpecProcessor) pushMetrics() {
|
||||
if err := p.pushFDStats(); err != nil {
|
||||
log.Printf("procstat, fd stats not available: %s", err.Error())
|
||||
}
|
||||
if err := p.pushCtxStats(); err != nil {
|
||||
log.Printf("procstat, ctx stats not available: %s", err.Error())
|
||||
}
|
||||
if err := p.pushIOStats(); err != nil {
|
||||
log.Printf("procstat, io stats not available: %s", err.Error())
|
||||
}
|
||||
if err := p.pushCPUStats(); err != nil {
|
||||
log.Printf("procstat, cpu stats not available: %s", err.Error())
|
||||
}
|
||||
if err := p.pushMemoryStats(); err != nil {
|
||||
log.Printf("procstat, mem stats not available: %s", err.Error())
|
||||
}
|
||||
p.flush()
|
||||
}
|
||||
|
||||
func (p *SpecProcessor) pushFDStats() error {
|
||||
fds, err := p.proc.NumFDs()
|
||||
if err != nil {
|
||||
return fmt.Errorf("NumFD error: %s\n", err)
|
||||
}
|
||||
p.add("num_fds", fds)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *SpecProcessor) pushCtxStats() error {
|
||||
ctx, err := p.proc.NumCtxSwitches()
|
||||
if err != nil {
|
||||
return fmt.Errorf("ContextSwitch error: %s\n", err)
|
||||
}
|
||||
p.add("voluntary_context_switches", ctx.Voluntary)
|
||||
p.add("involuntary_context_switches", ctx.Involuntary)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *SpecProcessor) pushIOStats() error {
|
||||
io, err := p.proc.IOCounters()
|
||||
if err != nil {
|
||||
return fmt.Errorf("IOCounters error: %s\n", err)
|
||||
}
|
||||
p.add("read_count", io.ReadCount)
|
||||
p.add("write_count", io.WriteCount)
|
||||
p.add("read_bytes", io.ReadBytes)
|
||||
p.add("write_bytes", io.WriteCount)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *SpecProcessor) pushCPUStats() error {
|
||||
cpu_time, err := p.proc.CPUTimes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.add("cpu_time_user", cpu_time.User)
|
||||
p.add("cpu_time_system", cpu_time.System)
|
||||
p.add("cpu_time_idle", cpu_time.Idle)
|
||||
p.add("cpu_time_nice", cpu_time.Nice)
|
||||
p.add("cpu_time_iowait", cpu_time.Iowait)
|
||||
p.add("cpu_time_irq", cpu_time.Irq)
|
||||
p.add("cpu_time_soft_irq", cpu_time.Softirq)
|
||||
p.add("cpu_time_soft_steal", cpu_time.Steal)
|
||||
p.add("cpu_time_soft_stolen", cpu_time.Stolen)
|
||||
p.add("cpu_time_soft_guest", cpu_time.Guest)
|
||||
p.add("cpu_time_soft_guest_nice", cpu_time.GuestNice)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *SpecProcessor) pushMemoryStats() error {
|
||||
mem, err := p.proc.MemoryInfo()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.add("memory_rss", mem.RSS)
|
||||
p.add("memory_vms", mem.VMS)
|
||||
p.add("memory_swap", mem.Swap)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user