telegraf/plugins/inputs/procstat/spec_processor.go

93 lines
2.1 KiB
Go
Raw Normal View History

2015-10-04 05:09:18 +00:00
package procstat
import (
"time"
2015-10-04 05:09:18 +00:00
"github.com/shirou/gopsutil/process"
"github.com/influxdata/telegraf"
2015-10-04 05:09:18 +00:00
)
type SpecProcessor struct {
Prefix string
tags map[string]string
2015-12-15 05:37:25 +00:00
fields map[string]interface{}
acc telegraf.Accumulator
2015-10-04 05:09:18 +00:00
proc *process.Process
}
func NewSpecProcessor(
prefix string,
acc telegraf.Accumulator,
p *process.Process,
tags map[string]string,
) *SpecProcessor {
if name, err := p.Name(); err == nil {
tags["process_name"] = name
}
2015-10-04 05:09:18 +00:00
return &SpecProcessor{
Prefix: prefix,
tags: tags,
2015-12-15 05:37:25 +00:00
fields: make(map[string]interface{}),
2015-10-04 05:09:18 +00:00
acc: acc,
proc: p,
}
}
func (p *SpecProcessor) pushMetrics() {
2016-04-26 01:57:38 +00:00
fields := map[string]interface{}{}
2015-10-04 05:09:18 +00:00
2016-03-06 06:42:14 +00:00
numThreads, err := p.proc.NumThreads()
2016-04-26 01:57:38 +00:00
if err == nil {
fields["num_threads"] = numThreads
2016-03-06 06:42:14 +00:00
}
2015-10-04 05:09:18 +00:00
fds, err := p.proc.NumFDs()
2016-04-26 01:57:38 +00:00
if err == nil {
fields["num_fds"] = fds
2015-10-04 05:09:18 +00:00
}
ctx, err := p.proc.NumCtxSwitches()
2016-04-26 01:57:38 +00:00
if err == nil {
fields["voluntary_context_switches"] = ctx.Voluntary
fields["involuntary_context_switches"] = ctx.Involuntary
2015-10-04 05:09:18 +00:00
}
io, err := p.proc.IOCounters()
2016-04-26 01:57:38 +00:00
if err == nil {
fields["read_count"] = io.ReadCount
fields["write_count"] = io.WriteCount
fields["read_bytes"] = io.ReadBytes
fields["write_bytes"] = io.WriteCount
2015-10-04 05:09:18 +00:00
}
2015-12-15 05:37:25 +00:00
cpu_time, err := p.proc.CPUTimes()
2016-04-26 01:57:38 +00:00
if err == nil {
fields["cpu_time_user"] = cpu_time.User
fields["cpu_time_system"] = cpu_time.System
fields["cpu_time_idle"] = cpu_time.Idle
fields["cpu_time_nice"] = cpu_time.Nice
fields["cpu_time_iowait"] = cpu_time.Iowait
fields["cpu_time_irq"] = cpu_time.Irq
fields["cpu_time_soft_irq"] = cpu_time.Softirq
fields["cpu_time_steal"] = cpu_time.Steal
fields["cpu_time_stolen"] = cpu_time.Stolen
fields["cpu_time_guest"] = cpu_time.Guest
fields["cpu_time_guest_nice"] = cpu_time.GuestNice
2015-10-04 05:09:18 +00:00
}
cpu_perc, err := p.proc.CPUPercent(time.Duration(0))
2016-04-26 01:57:38 +00:00
if err == nil && cpu_perc != 0 {
fields["cpu_usage"] = cpu_perc
}
2015-12-15 05:37:25 +00:00
2015-10-04 05:09:18 +00:00
mem, err := p.proc.MemoryInfo()
2016-04-26 01:57:38 +00:00
if err == nil {
fields["memory_rss"] = mem.RSS
fields["memory_vms"] = mem.VMS
fields["memory_swap"] = mem.Swap
2015-10-04 05:09:18 +00:00
}
2016-04-26 01:57:38 +00:00
p.acc.AddFields("procstat", fields, p.tags)
2015-10-04 05:09:18 +00:00
}