Skip to content

Commit

Permalink
Get the number of logcal CPUs dynamically
Browse files Browse the repository at this point in the history
runtime.NumCPU() return the number of logical CPUs at the startup of the
process, https://golang.org/pkg/runtime/#NumCPU.
gopsutil return the current number of CPUs, giving the correct number
when CPU hot un/plug are being done.
  • Loading branch information
adrianlzt committed Aug 6, 2019
1 parent e65324d commit b217b1e
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions plugins/inputs/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"fmt"
"log"
"os"
"runtime"
"strings"
"time"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load"
)
Expand All @@ -35,11 +35,16 @@ func (_ *SystemStats) Gather(acc telegraf.Accumulator) error {
return err
}

numCPUs, err := cpu.Counts(true)
if err != nil {
return err
}

fields := map[string]interface{}{
"load1": loadavg.Load1,
"load5": loadavg.Load5,
"load15": loadavg.Load15,
"n_cpus": runtime.NumCPU(),
"n_cpus": numCPUs,
}

users, err := host.Users()
Expand Down

0 comments on commit b217b1e

Please sign in to comment.