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] 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 f10062d1b9a..b84ad52244b 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 8258d3dddd3..cabf00631fa 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 8747e6f5b41..b68af75bc39 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 d9be2ca0b15..a088234f0e3 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 7e6eb895e11..87c87a36763 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 dadede272e3..41cf5ff86f3 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) {