Update the number of logical CPUs dynamically in system plugin (#6214)

This commit is contained in:
Adrián López 2019-08-07 00:55:06 +02:00 committed by Daniel Nelson
parent d7b69af9cd
commit a3a6752f04
2 changed files with 9 additions and 2 deletions

View File

@ -3,6 +3,8 @@
The system plugin gathers general stats on system load, uptime, The system plugin gathers general stats on system load, uptime,
and number of users logged in. It is similar to the unix `uptime` command. and number of users logged in. It is similar to the unix `uptime` command.
Number of CPUs is obtained from the /proc/cpuinfo file.
### Configuration: ### Configuration:
```toml ```toml

View File

@ -6,12 +6,12 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"runtime"
"strings" "strings"
"time" "time"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host" "github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load" "github.com/shirou/gopsutil/load"
) )
@ -35,11 +35,16 @@ func (_ *SystemStats) Gather(acc telegraf.Accumulator) error {
return err return err
} }
numCPUs, err := cpu.Counts(true)
if err != nil {
return err
}
fields := map[string]interface{}{ fields := map[string]interface{}{
"load1": loadavg.Load1, "load1": loadavg.Load1,
"load5": loadavg.Load5, "load5": loadavg.Load5,
"load15": loadavg.Load15, "load15": loadavg.Load15,
"n_cpus": runtime.NumCPU(), "n_cpus": numCPUs,
} }
users, err := host.Users() users, err := host.Users()