Skip to content

Commit

Permalink
Provide method to include core count when reporting cpu_usage in proc…
Browse files Browse the repository at this point in the history
…stat 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
  • Loading branch information
glinton committed Dec 23, 2020
1 parent ed72aac commit 7c17055
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions plugins/inputs/procstat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 21 additions & 1 deletion plugins/inputs/procstat/procstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"io/ioutil"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"

"github.com/influxdata/telegraf"
Expand Down Expand Up @@ -34,6 +36,9 @@ type Procstat struct {
CGroup string `toml:"cgroup"`
PidTag bool
WinService string `toml:"win_service"`
Mode string

solarisMode bool

finder PIDFinder

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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{}
Expand Down

0 comments on commit 7c17055

Please sign in to comment.