procstat: fix newlines in tags

This commit is contained in:
Cameron Sparr 2016-04-25 19:57:38 -06:00
parent 0367a39e1f
commit 249a860c6f
2 changed files with 34 additions and 82 deletions

View File

@ -136,7 +136,7 @@ func (p *Procstat) pidsFromFile() ([]int32, error) {
out = append(out, int32(pid)) out = append(out, int32(pid))
p.tagmap[int32(pid)] = map[string]string{ p.tagmap[int32(pid)] = map[string]string{
"pidfile": p.PidFile, "pidfile": p.PidFile,
"pid": string(pidString), "pid": strings.TrimSpace(string(pidString)),
} }
} }
} }

View File

@ -1,7 +1,6 @@
package procstat package procstat
import ( import (
"fmt"
"time" "time"
"github.com/shirou/gopsutil/process" "github.com/shirou/gopsutil/process"
@ -17,21 +16,6 @@ type SpecProcessor struct {
proc *process.Process 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( func NewSpecProcessor(
prefix string, prefix string,
acc telegraf.Accumulator, acc telegraf.Accumulator,
@ -51,90 +35,58 @@ func NewSpecProcessor(
} }
func (p *SpecProcessor) pushMetrics() { func (p *SpecProcessor) pushMetrics() {
p.pushNThreadsStats() fields := map[string]interface{}{}
p.pushFDStats()
p.pushCtxStats()
p.pushIOStats()
p.pushCPUStats()
p.pushMemoryStats()
p.flush()
}
func (p *SpecProcessor) pushNThreadsStats() error {
numThreads, err := p.proc.NumThreads() numThreads, err := p.proc.NumThreads()
if err != nil { if err == nil {
return fmt.Errorf("NumThreads error: %s\n", err) fields["num_threads"] = numThreads
} }
p.add("num_threads", numThreads)
return nil
}
func (p *SpecProcessor) pushFDStats() error {
fds, err := p.proc.NumFDs() fds, err := p.proc.NumFDs()
if err != nil { if err == nil {
return fmt.Errorf("NumFD error: %s\n", err) fields["num_fds"] = fds
} }
p.add("num_fds", fds)
return nil
}
func (p *SpecProcessor) pushCtxStats() error {
ctx, err := p.proc.NumCtxSwitches() ctx, err := p.proc.NumCtxSwitches()
if err != nil { if err == nil {
return fmt.Errorf("ContextSwitch error: %s\n", err) fields["voluntary_context_switches"] = ctx.Voluntary
fields["involuntary_context_switches"] = ctx.Involuntary
} }
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() io, err := p.proc.IOCounters()
if err != nil { if err == nil {
return fmt.Errorf("IOCounters error: %s\n", err) fields["read_count"] = io.ReadCount
fields["write_count"] = io.WriteCount
fields["read_bytes"] = io.ReadBytes
fields["write_bytes"] = io.WriteCount
} }
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() cpu_time, err := p.proc.CPUTimes()
if err != nil { if err == nil {
return err 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
} }
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_steal", cpu_time.Steal)
p.add("cpu_time_stolen", cpu_time.Stolen)
p.add("cpu_time_guest", cpu_time.Guest)
p.add("cpu_time_guest_nice", cpu_time.GuestNice)
cpu_perc, err := p.proc.CPUPercent(time.Duration(0)) cpu_perc, err := p.proc.CPUPercent(time.Duration(0))
if err != nil { if err == nil && cpu_perc != 0 {
return err fields["cpu_usage"] = cpu_perc
} else if cpu_perc == 0 {
return nil
} }
p.add("cpu_usage", cpu_perc)
return nil
}
func (p *SpecProcessor) pushMemoryStats() error {
mem, err := p.proc.MemoryInfo() mem, err := p.proc.MemoryInfo()
if err != nil { if err == nil {
return err fields["memory_rss"] = mem.RSS
fields["memory_vms"] = mem.VMS
fields["memory_swap"] = mem.Swap
} }
p.add("memory_rss", mem.RSS)
p.add("memory_vms", mem.VMS) p.acc.AddFields("procstat", fields, p.tags)
p.add("memory_swap", mem.Swap)
return nil
} }