From 7308692b4fb9708058e2ebecff3066d1fc9dca2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9mi=20V=C3=A1nyi?= Date: Tue, 22 Aug 2017 16:29:07 +0200 Subject: [PATCH 1/2] metricbeat: add cpu.total.pct && cpu.total.norm.pct Closes #4898 --- CHANGELOG.asciidoc | 1 + metricbeat/docs/fields.asciidoc | 20 +++++++++++++++++++ metricbeat/module/system/cpu/_meta/fields.yml | 13 ++++++++++++ metricbeat/module/system/cpu/cpu.go | 2 ++ metricbeat/module/system/util.go | 6 ++++++ metricbeat/module/system/util_test.go | 6 ++++++ 6 files changed, 48 insertions(+) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index f10062d1b9a7..b84ad52244bf 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -59,6 +59,7 @@ https://github.com/elastic/beats/compare/v6.0.0-beta1...master[Check the HEAD di - Support `npipe` protocol (Windows) in Docker module. {pull}4751[4751] - Added missing mongodb configuration file to the `modules.d` folder. {pull}4870[4870] - Fix wrong MySQL CRUD queries timelion visualization {pull}4857[4857] +- Add new metrics to CPU metricsset {pull}4969[4969] *Packetbeat* diff --git a/metricbeat/docs/fields.asciidoc b/metricbeat/docs/fields.asciidoc index 8258d3dddd3c..cabf00631fa9 100644 --- a/metricbeat/docs/fields.asciidoc +++ b/metricbeat/docs/fields.asciidoc @@ -8543,6 +8543,16 @@ format: percent The percentage of CPU time spent in involuntary wait by the virtual CPU while the hypervisor was servicing another processor. Available only on Unix. +[float] +=== `system.cpu.total.pct` + +type: scaled_float + +format: percent + +The percentage of CPU time spent in non-idle state. + + [float] === `system.cpu.user.norm.pct` @@ -8623,6 +8633,16 @@ format: percent The percentage of CPU time spent in involuntary wait by the virtual CPU while the hypervisor was servicing another processor. Available only on Unix. +[float] +=== `system.cpu.total.norm.pct` + +type: scaled_float + +format: percent + +The percentage of CPU time spent in non-idle state. + + [float] === `system.cpu.user.ticks` diff --git a/metricbeat/module/system/cpu/_meta/fields.yml b/metricbeat/module/system/cpu/_meta/fields.yml index 8747e6f5b41d..b68af75bc394 100644 --- a/metricbeat/module/system/cpu/_meta/fields.yml +++ b/metricbeat/module/system/cpu/_meta/fields.yml @@ -64,6 +64,12 @@ was servicing another processor. Available only on Unix. + - name: total.pct + type: scaled_float + format: percent + description: > + The percentage of CPU time spent in non-idle state. + # Normalized Percentages - name: user.norm.pct type: scaled_float @@ -115,6 +121,13 @@ was servicing another processor. Available only on Unix. + - name: total.norm.pct + type: scaled_float + format: percent + description: > + The percentage of CPU time spent in non-idle state. + + # Ticks - name: user.ticks type: long diff --git a/metricbeat/module/system/cpu/cpu.go b/metricbeat/module/system/cpu/cpu.go index d9be2ca0b156..a088234f0e3c 100644 --- a/metricbeat/module/system/cpu/cpu.go +++ b/metricbeat/module/system/cpu/cpu.go @@ -65,6 +65,7 @@ func (m *MetricSet) Fetch() (common.MapStr, error) { event.Put("nice.pct", pct.Nice) event.Put("softirq.pct", pct.SoftIRQ) event.Put("steal.pct", pct.Steal) + event.Put("total.pct", pct.Total) case normalizedPercentages: normalizedPct := sample.NormalizedPercentages() event.Put("user.norm.pct", normalizedPct.User) @@ -75,6 +76,7 @@ func (m *MetricSet) Fetch() (common.MapStr, error) { event.Put("nice.norm.pct", normalizedPct.Nice) event.Put("softirq.norm.pct", normalizedPct.SoftIRQ) event.Put("steal.norm.pct", normalizedPct.Steal) + event.Put("total.norm.pct", normalizedPct.Total) case ticks: ticks := sample.Ticks() event.Put("user.ticks", ticks.User) diff --git a/metricbeat/module/system/util.go b/metricbeat/module/system/util.go index 7e6eb895e11d..87c87a36763a 100644 --- a/metricbeat/module/system/util.go +++ b/metricbeat/module/system/util.go @@ -47,6 +47,7 @@ type CPUPercentages struct { Nice float64 SoftIRQ float64 Steal float64 + Total float64 } type CPUTicks struct { @@ -98,6 +99,10 @@ func cpuPercentages(s0, s1 *sigar.Cpu, numCPU int) CPUPercentages { return Round(pct * float64(numCPU)) } + calculateTotalPct := func() float64 { + return Round(float64(numCPU) - calculatePct(s0.Idle, s1.Idle)) + } + return CPUPercentages{ User: calculatePct(s0.User, s1.User), System: calculatePct(s0.Sys, s1.Sys), @@ -107,6 +112,7 @@ func cpuPercentages(s0, s1 *sigar.Cpu, numCPU int) CPUPercentages { Nice: calculatePct(s0.Nice, s1.Nice), SoftIRQ: calculatePct(s0.SoftIrq, s1.SoftIrq), Steal: calculatePct(s0.Stolen, s1.Stolen), + Total: calculateTotalPct(), } } diff --git a/metricbeat/module/system/util_test.go b/metricbeat/module/system/util_test.go index dadede272e36..41cf5ff86f3b 100644 --- a/metricbeat/module/system/util_test.go +++ b/metricbeat/module/system/util_test.go @@ -52,6 +52,8 @@ func TestCPUCoresMonitorSample(t *testing.T) { assert.True(t, normPct.System <= 100) assert.True(t, normPct.Idle > 0) assert.True(t, normPct.Idle <= 100) + assert.True(t, normPct.Total > 0) + assert.True(t, normPct.Total <= 100) ticks := s.Ticks() assert.True(t, ticks.User > 0) @@ -110,10 +112,14 @@ func TestCPUMetricsPercentages(t *testing.T) { pct := sample.NormalizedPercentages() assert.EqualValues(t, .3, pct.User) assert.EqualValues(t, .7, pct.System) + assert.EqualValues(t, .0, pct.Idle) + assert.EqualValues(t, 1., pct.Total) pct = sample.Percentages() assert.EqualValues(t, .3*float64(NumCPU), pct.User) assert.EqualValues(t, .7*float64(NumCPU), pct.System) + assert.EqualValues(t, .0*float64(NumCPU), pct.Idle) + assert.EqualValues(t, 1.*float64(NumCPU), pct.Total) } func TestRound(t *testing.T) { From 535888f5ffca74dea2eb4f052bd0fca490d33e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9mi=20V=C3=A1nyi?= Date: Tue, 22 Aug 2017 19:06:25 +0200 Subject: [PATCH 2/2] fix system tests --- metricbeat/tests/system/test_processors.py | 2 +- metricbeat/tests/system/test_system.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/metricbeat/tests/system/test_processors.py b/metricbeat/tests/system/test_processors.py index 40619e851ebf..1c83e2288407 100644 --- a/metricbeat/tests/system/test_processors.py +++ b/metricbeat/tests/system/test_processors.py @@ -41,7 +41,7 @@ def test_drop_fields(self): print(cpu.keys()) self.assertItemsEqual(self.de_dot([ "system", "cores", "user", "softirq", "iowait", - "idle", "irq", "steal", "nice" + "idle", "irq", "steal", "nice", "total" ]), cpu.keys()) def test_dropfields_with_condition(self): diff --git a/metricbeat/tests/system/test_system.py b/metricbeat/tests/system/test_system.py index 10c4b046f047..bd1d3dc58e5d 100644 --- a/metricbeat/tests/system/test_system.py +++ b/metricbeat/tests/system/test_system.py @@ -7,12 +7,12 @@ import os SYSTEM_CPU_FIELDS = ["cores", "idle.pct", "iowait.pct", "irq.pct", "nice.pct", - "softirq.pct", "steal.pct", "system.pct", "user.pct"] + "softirq.pct", "steal.pct", "system.pct", "user.pct", "total.pct"] 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", "idle.norm.pct", "iowait.norm.pct", "irq.norm.pct", "nice.norm.pct", "softirq.norm.pct", - "steal.norm.pct", "system.norm.pct", "user.norm.pct"] + "steal.norm.pct", "system.norm.pct", "user.norm.pct", "total.norm.pct"] SYSTEM_LOAD_FIELDS = ["cores", "1", "5", "15", "norm.1", "norm.5", "norm.15"]