From 102bb5c443dd2d4e3da1e74144a12b381fbf6d26 Mon Sep 17 00:00:00 2001 From: Martin Molnar Date: Thu, 19 May 2022 11:29:00 +0200 Subject: [PATCH] feat(inputs/cpu): add tags with core id or physical id to cpus --- plugins/inputs/cpu/README.md | 3 +++ plugins/inputs/cpu/cpu.go | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/plugins/inputs/cpu/README.md b/plugins/inputs/cpu/README.md index 5b82b038d768a..e21a26149f791 100644 --- a/plugins/inputs/cpu/README.md +++ b/plugins/inputs/cpu/README.md @@ -15,6 +15,8 @@ The `cpu` plugin gather metrics on the system CPUs. collect_cpu_time = false ## If true, compute and report the sum of all non-idle CPU states report_active = false + ## If true and the info is available then add core_id and physical_id tags + core_tags = false ``` ## Metrics @@ -52,6 +54,7 @@ On Linux, consult `man proc` for details on the meanings of these values. On Linux systems the `/proc/stat` file is used to gather CPU times. Percentages are based on the last 2 samples. +Tags core_id and physical_id are read from `/proc/cpuinfo` on Linux systems ## Example Output diff --git a/plugins/inputs/cpu/cpu.go b/plugins/inputs/cpu/cpu.go index 611902c29b283..9a01de5a04bda 100644 --- a/plugins/inputs/cpu/cpu.go +++ b/plugins/inputs/cpu/cpu.go @@ -12,13 +12,19 @@ import ( ) type CPUStats struct { - ps system.PS - lastStats map[string]cpuUtil.TimesStat + ps system.PS + lastStats map[string]cpuUtil.TimesStat + cpuInfo map[string]cpuUtil.InfoStat + coreID bool + physicalID bool PerCPU bool `toml:"percpu"` TotalCPU bool `toml:"totalcpu"` CollectCPUTime bool `toml:"collect_cpu_time"` ReportActive bool `toml:"report_active"` + CoreTags bool `toml:"core_tags"` + + Log telegraf.Logger `toml:"-"` } func NewCPUStats(ps system.PS) *CPUStats { @@ -40,6 +46,12 @@ func (c *CPUStats) Gather(acc telegraf.Accumulator) error { tags := map[string]string{ "cpu": cts.CPU, } + if c.coreID { + tags["core_id"] = c.cpuInfo[cts.CPU].CoreID + } + if c.physicalID { + tags["physical_id"] = c.cpuInfo[cts.CPU].PhysicalID + } total := totalCPUTime(cts) active := activeCPUTime(cts) @@ -113,6 +125,25 @@ func (c *CPUStats) Gather(acc telegraf.Accumulator) error { return err } +func (c *CPUStats) Init() error { + if c.CoreTags { + cpuInfo, err := cpuUtil.Info() + if err == nil { + c.coreID = cpuInfo[0].CoreID != "" + c.physicalID = cpuInfo[0].PhysicalID != "" + + c.cpuInfo = make(map[string]cpuUtil.InfoStat) + for _, ci := range cpuInfo { + c.cpuInfo[fmt.Sprintf("cpu%d", ci.CPU)] = ci + } + } else { + c.Log.Warnf("Failed to gather info about CPUs: %s", err) + } + } + + return nil +} + func totalCPUTime(t cpuUtil.TimesStat) float64 { total := t.User + t.System + t.Nice + t.Iowait + t.Irq + t.Softirq + t.Steal + t.Idle return total