Skip to content

Commit

Permalink
metricbeat: add cpu.total.pct && cpu.total.norm.pct
Browse files Browse the repository at this point in the history
Closes #4898
  • Loading branch information
kvch committed Aug 22, 2017
1 parent 02a59a1 commit 7308692
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand Down
20 changes: 20 additions & 0 deletions metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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`
Expand Down
13 changes: 13 additions & 0 deletions metricbeat/module/system/cpu/_meta/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions metricbeat/module/system/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions metricbeat/module/system/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type CPUPercentages struct {
Nice float64
SoftIRQ float64
Steal float64
Total float64
}

type CPUTicks struct {
Expand Down Expand Up @@ -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),
Expand All @@ -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(),
}
}

Expand Down
6 changes: 6 additions & 0 deletions metricbeat/module/system/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7308692

Please sign in to comment.