2015-10-04 05:09:18 +00:00
|
|
|
package procstat
|
|
|
|
|
|
|
|
import (
|
2016-01-20 00:28:02 +00:00
|
|
|
"time"
|
2015-10-05 22:45:40 +00:00
|
|
|
|
2015-10-04 05:09:18 +00:00
|
|
|
"github.com/shirou/gopsutil/process"
|
2015-10-05 22:45:40 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2015-10-04 05:09:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SpecProcessor struct {
|
|
|
|
Prefix string
|
2016-07-14 00:49:17 +00:00
|
|
|
pid int32
|
2015-10-04 05:09:18 +00:00
|
|
|
tags map[string]string
|
2015-12-15 05:37:25 +00:00
|
|
|
fields map[string]interface{}
|
2016-01-27 21:21:36 +00:00
|
|
|
acc telegraf.Accumulator
|
2015-10-04 05:09:18 +00:00
|
|
|
proc *process.Process
|
|
|
|
}
|
|
|
|
|
2015-10-07 17:42:50 +00:00
|
|
|
func NewSpecProcessor(
|
2016-05-19 10:34:25 +00:00
|
|
|
processName string,
|
2015-10-07 17:42:50 +00:00
|
|
|
prefix string,
|
2016-07-14 00:49:17 +00:00
|
|
|
pid int32,
|
2016-01-27 21:21:36 +00:00
|
|
|
acc telegraf.Accumulator,
|
2015-10-07 17:42:50 +00:00
|
|
|
p *process.Process,
|
2016-04-20 19:18:07 +00:00
|
|
|
tags map[string]string,
|
2015-10-07 17:42:50 +00:00
|
|
|
) *SpecProcessor {
|
2016-05-19 10:34:25 +00:00
|
|
|
if processName != "" {
|
|
|
|
tags["process_name"] = processName
|
|
|
|
} else {
|
|
|
|
name, err := p.Name()
|
|
|
|
if err == nil {
|
|
|
|
tags["process_name"] = name
|
|
|
|
}
|
2015-10-07 18:31:49 +00:00
|
|
|
}
|
2015-10-04 05:09:18 +00:00
|
|
|
return &SpecProcessor{
|
|
|
|
Prefix: prefix,
|
2016-07-14 00:49:17 +00:00
|
|
|
pid: pid,
|
2015-10-07 17:42:50 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-07 18:31:49 +00:00
|
|
|
func (p *SpecProcessor) pushMetrics() {
|
2016-04-26 02:10:34 +00:00
|
|
|
var prefix string
|
|
|
|
if p.Prefix != "" {
|
|
|
|
prefix = p.Prefix + "_"
|
|
|
|
}
|
2016-10-04 00:24:18 +00:00
|
|
|
fields := map[string]interface{}{}
|
|
|
|
|
|
|
|
//If pid is not present as a tag, include it as a field.
|
|
|
|
if _, pidInTags := p.tags["pid"]; !pidInTags {
|
|
|
|
fields["pid"] = p.pid
|
|
|
|
}
|
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 {
|
2016-04-26 02:10:34 +00:00
|
|
|
fields[prefix+"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 {
|
2016-04-26 02:10:34 +00:00
|
|
|
fields[prefix+"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 {
|
2016-04-26 02:10:34 +00:00
|
|
|
fields[prefix+"voluntary_context_switches"] = ctx.Voluntary
|
|
|
|
fields[prefix+"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 {
|
2016-04-26 02:10:34 +00:00
|
|
|
fields[prefix+"read_count"] = io.ReadCount
|
|
|
|
fields[prefix+"write_count"] = io.WriteCount
|
|
|
|
fields[prefix+"read_bytes"] = io.ReadBytes
|
2016-08-05 08:51:20 +00:00
|
|
|
fields[prefix+"write_bytes"] = io.WriteBytes
|
2015-10-04 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 14:05:08 +00:00
|
|
|
cpu_time, err := p.proc.Times()
|
2016-04-26 01:57:38 +00:00
|
|
|
if err == nil {
|
2016-04-26 02:10:34 +00:00
|
|
|
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_stolen"] = cpu_time.Stolen
|
|
|
|
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
|
|
|
}
|
2016-01-20 00:28:02 +00:00
|
|
|
|
2016-05-19 14:05:08 +00:00
|
|
|
cpu_perc, err := p.proc.Percent(time.Duration(0))
|
2016-04-26 01:57:38 +00:00
|
|
|
if err == nil && cpu_perc != 0 {
|
2016-04-26 02:10:34 +00:00
|
|
|
fields[prefix+"cpu_usage"] = cpu_perc
|
2016-01-20 00:28:02 +00:00
|
|
|
}
|
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 {
|
2016-04-26 02:10:34 +00:00
|
|
|
fields[prefix+"memory_rss"] = mem.RSS
|
|
|
|
fields[prefix+"memory_vms"] = mem.VMS
|
|
|
|
fields[prefix+"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
|
|
|
}
|