Provide method to include core count when reporting cpu_usage in procstat input (#6165)

* Provide a non-irix reporting of cpu_usage in procstat input

* Update sample config to include cpu gathering mode

* cleanup readme from merge
This commit is contained in:
Greg 2020-12-23 08:30:47 -07:00 committed by GitHub
parent ed72aac0be
commit 7c17055178
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -44,6 +44,9 @@ Processes can be selected for monitoring using one of several methods:
## When true add the full cmdline as a tag.
# cmdline_tag = false
## Mode to use when calculating CPU usage. Can be one of 'solaris' or 'irix'.
# mode = "irix"
## 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.

View File

@ -6,7 +6,9 @@ import (
"io/ioutil"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/influxdata/telegraf"
@ -34,6 +36,9 @@ type Procstat struct {
CGroup string `toml:"cgroup"`
PidTag bool
WinService string `toml:"win_service"`
Mode string
solarisMode bool
finder PIDFinder
@ -69,6 +74,9 @@ var sampleConfig = `
## When true add the full cmdline as a tag.
# cmdline_tag = false
## Mode to use when calculating CPU usage. Can be one of 'solaris' or 'irix'.
# mode = "irix"
## 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.
@ -240,7 +248,11 @@ func (p *Procstat) addMetric(proc Process, acc telegraf.Accumulator) {
cpu_perc, err := proc.Percent(time.Duration(0))
if err == nil {
fields[prefix+"cpu_usage"] = cpu_perc
if p.solarisMode {
fields[prefix+"cpu_usage"] = cpu_perc / float64(runtime.NumCPU())
} else {
fields[prefix+"cpu_usage"] = cpu_perc
}
}
mem, err := proc.MemoryInfo()
@ -461,6 +473,14 @@ func (p *Procstat) winServicePIDs() ([]PID, error) {
return pids, nil
}
func (p *Procstat) Init() error {
if strings.ToLower(p.Mode) == "solaris" {
p.solarisMode = true
}
return nil
}
func init() {
inputs.Add("procstat", func() telegraf.Input {
return &Procstat{}