diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index aec270cb735..2abd530d3a7 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -54,6 +54,7 @@ https://github.com/elastic/beats/compare/v5.1.1...master[Check the HEAD diff] *Metricbeat* - Kafka module broker matching enhancements. {pull}3129[3129] - Add a couchbase module with metricsets for node, cluster and bucker. {pull}3081[3081] +- Export number of cores for cpu module. {pull}3192[3192] *Packetbeat* diff --git a/metricbeat/docs/fields.asciidoc b/metricbeat/docs/fields.asciidoc index 51fab60cf96..c338ac5e0a4 100644 --- a/metricbeat/docs/fields.asciidoc +++ b/metricbeat/docs/fields.asciidoc @@ -4774,6 +4774,14 @@ The amount of CPU time spent in involuntary wait by the virtual CPU while the hy +[float] +=== system.cpu.cores + +type: long + +The number of CPU cores. + + [float] === system.cpu.user.pct diff --git a/metricbeat/metricbeat.template-es2x.json b/metricbeat/metricbeat.template-es2x.json index aa8b35cab53..7f5df5604cd 100644 --- a/metricbeat/metricbeat.template-es2x.json +++ b/metricbeat/metricbeat.template-es2x.json @@ -2712,6 +2712,9 @@ }, "cpu": { "properties": { + "cores": { + "type": "long" + }, "idle": { "properties": { "pct": { diff --git a/metricbeat/metricbeat.template.json b/metricbeat/metricbeat.template.json index 68a02d23aa3..92170307704 100644 --- a/metricbeat/metricbeat.template.json +++ b/metricbeat/metricbeat.template.json @@ -2694,6 +2694,9 @@ }, "cpu": { "properties": { + "cores": { + "type": "long" + }, "idle": { "properties": { "pct": { diff --git a/metricbeat/module/system/cpu/_meta/fields.yml b/metricbeat/module/system/cpu/_meta/fields.yml index 054ab19b9d2..abb3e03ed3f 100644 --- a/metricbeat/module/system/cpu/_meta/fields.yml +++ b/metricbeat/module/system/cpu/_meta/fields.yml @@ -3,6 +3,11 @@ description: > `cpu` contains local CPU stats. fields: + - name: cores + type: long + description: > + The number of CPU cores. + - name: user.pct type: scaled_float format: percent diff --git a/metricbeat/module/system/cpu/cpu.go b/metricbeat/module/system/cpu/cpu.go index 62ed8664b41..a07a261a70c 100644 --- a/metricbeat/module/system/cpu/cpu.go +++ b/metricbeat/module/system/cpu/cpu.go @@ -52,7 +52,10 @@ func (m *MetricSet) Fetch() (common.MapStr, error) { } m.cpu.AddCpuPercentage(stat) + cpuCores := GetCores() + cpuStat := common.MapStr{ + "cores": cpuCores, "user": common.MapStr{ "pct": stat.UserPercent, }, diff --git a/metricbeat/module/system/cpu/helper.go b/metricbeat/module/system/cpu/helper.go index f97776c04c2..391fed94b2c 100644 --- a/metricbeat/module/system/cpu/helper.go +++ b/metricbeat/module/system/cpu/helper.go @@ -3,6 +3,8 @@ package cpu import ( + "runtime" + "github.com/elastic/beats/metricbeat/module/system" sigar "github.com/elastic/gosigar" ) @@ -12,6 +14,7 @@ type CPU struct { LastCpuTimes *CpuTimes LastCpuTimesList []CpuTimes CpuTicks bool + Cores int } type CpuTimes struct { @@ -115,6 +118,11 @@ func GetCpuPercentageList(last, current []CpuTimes) []CpuTimes { return current } +func GetCores() int { + cores := runtime.NumCPU() + return cores +} + func (cpu *CPU) AddCpuPercentage(t2 *CpuTimes) { cpu.LastCpuTimes = GetCpuPercentage(cpu.LastCpuTimes, t2) } diff --git a/metricbeat/tests/system/test_processors.py b/metricbeat/tests/system/test_processors.py index e5b0202d6d2..371672c7308 100644 --- a/metricbeat/tests/system/test_processors.py +++ b/metricbeat/tests/system/test_processors.py @@ -39,7 +39,7 @@ def test_drop_fields(self): cpu = evt["system"]["cpu"] print(cpu.keys()) self.assertItemsEqual(self.de_dot([ - "system", "user", "softirq", "iowait", + "system", "cores", "user", "softirq", "iowait", "idle", "irq", "steal", "nice" ]), cpu.keys()) diff --git a/metricbeat/tests/system/test_system.py b/metricbeat/tests/system/test_system.py index e9910f3f82b..25400d74044 100644 --- a/metricbeat/tests/system/test_system.py +++ b/metricbeat/tests/system/test_system.py @@ -5,10 +5,10 @@ import getpass import os -SYSTEM_CPU_FIELDS = ["idle.pct", "iowait.pct", "irq.pct", "nice.pct", +SYSTEM_CPU_FIELDS = ["cores", "idle.pct", "iowait.pct", "irq.pct", "nice.pct", "softirq.pct", "steal.pct", "system.pct", "user.pct"] -SYSTEM_CPU_FIELDS_ALL = ["idle.pct", "idle.ticks", "iowait.pct", "iowait.ticks", "irq.pct", "irq.ticks", "nice.pct", "nice.ticks", +SYSTEM_CPU_FIELDS_ALL = ["cores", "idle.pct", "idle.ticks", "iowait.pct", "iowait.ticks", "irq.pct", "irq.ticks", "nice.pct", "nice.ticks", "softirq.pct", "softirq.ticks", "steal.pct", "steal.ticks", "system.pct", "system.ticks", "user.pct", "user.ticks"] SYSTEM_LOAD_FIELDS = ["1", "5", "15", "norm.1", "norm.5", "norm.15"]