Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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