Skip to content

Commit

Permalink
[metricbeat] Add system/entropy metricset (#12450)
Browse files Browse the repository at this point in the history
* add system/entropy metricset
  • Loading branch information
fearful-symmetry authored Jun 12, 2019
1 parent 016a0b1 commit 260f095
Show file tree
Hide file tree
Showing 17 changed files with 286 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Always report Pod UID in the `pod` metricset. {pull}12345[12345]
- Add Vsphere Virtual Machine operating system to `os` field in Vsphere virtualmachine module. {pull}12391[12391]
- Add validation for elasticsearch and kibana modules' metricsets when xpack.enabled is set to true. {pull}12386[12386]
- Add a system/entropy metricset {pull}12450[12450]

*Packetbeat*

Expand Down
29 changes: 29 additions & 0 deletions metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -24566,6 +24566,35 @@ type: float
Percentage of CPU time during which I/O requests were issued to the device (bandwidth utilization for the device). Device saturation occurs when this value is close to 100%.
--
[float]
== entropy fields
Available system entropy
*`system.entropy.available_bits`*::
+
--
type: long
The available bits of entropy
--
*`system.entropy.pct`*::
+
--
type: scaled_float
format: percent
The percentage of available entropy, relative to the pool size of 4096
--
[float]
Expand Down
4 changes: 4 additions & 0 deletions metricbeat/docs/modules/system.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ The following metricsets are available:

* <<metricbeat-metricset-system-diskio,diskio>>

* <<metricbeat-metricset-system-entropy,entropy>>

* <<metricbeat-metricset-system-filesystem,filesystem>>

* <<metricbeat-metricset-system-fsstat,fsstat>>
Expand Down Expand Up @@ -144,6 +146,8 @@ include::system/cpu.asciidoc[]

include::system/diskio.asciidoc[]

include::system/entropy.asciidoc[]

include::system/filesystem.asciidoc[]

include::system/fsstat.asciidoc[]
Expand Down
21 changes: 21 additions & 0 deletions metricbeat/docs/modules/system/entropy.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
////
This file is generated! See scripts/docs_collector.py
////

[[metricbeat-metricset-system-entropy]]
=== System entropy metricset

include::../../../module/system/entropy/_meta/docs.asciidoc[]


==== Fields

For a description of each field in the metricset, see the
<<exported-fields-system,exported fields>> section.

Here is an example document generated by this metricset:

[source,json]
----
include::../../../module/system/entropy/_meta/data.json[]
----
3 changes: 2 additions & 1 deletion metricbeat/docs/modules_list.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ This file is generated! See scripts/docs_collector.py
|<<metricbeat-metricset-redis-key,key>>
|<<metricbeat-metricset-redis-keyspace,keyspace>>
|<<metricbeat-module-system,System>> |image:./images/icon-yes.png[Prebuilt dashboards are available] |
.14+| .14+| |<<metricbeat-metricset-system-core,core>>
.15+| .15+| |<<metricbeat-metricset-system-core,core>>
|<<metricbeat-metricset-system-cpu,cpu>>
|<<metricbeat-metricset-system-diskio,diskio>>
|<<metricbeat-metricset-system-entropy,entropy>>
|<<metricbeat-metricset-system-filesystem,filesystem>>
|<<metricbeat-metricset-system-fsstat,fsstat>>
|<<metricbeat-metricset-system-load,load>>
Expand Down
1 change: 1 addition & 0 deletions metricbeat/include/list.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions metricbeat/module/system/_meta/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- process
- process_summary
- socket_summary
#- entropy
#- core
#- diskio
#- socket
Expand Down
20 changes: 20 additions & 0 deletions metricbeat/module/system/entropy/_meta/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"@timestamp": "2017-10-12T08:05:34.853Z",
"event": {
"dataset": "system.entropy",
"duration": 115000,
"module": "system"
},
"metricset": {
"name": "entropy"
},
"service": {
"type": "system"
},
"system": {
"entropy": {
"available_bits": 2801,
"pct": 0.683837890625
}
}
}
5 changes: 5 additions & 0 deletions metricbeat/module/system/entropy/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This is the entropy metricset of the module system. It collects the amount of available entropy in bits. On kernel versions greater than 2.6, entropy will be out of a total pool size of 4096.

This Metricset is available on:

- linux
15 changes: 15 additions & 0 deletions metricbeat/module/system/entropy/_meta/fields.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- name: entropy
type: group
description: >
Available system entropy
release: ga
fields:
- name: available_bits
type: long
description: >
The available bits of entropy
- name: pct
type: scaled_float
format: percent
description: >
The percentage of available entropy, relative to the pool size of 4096
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2801
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4096
18 changes: 18 additions & 0 deletions metricbeat/module/system/entropy/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package entropy
106 changes: 106 additions & 0 deletions metricbeat/module/system/entropy/entropy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

// +build linux

package entropy

import (
"io/ioutil"
"path"
"strconv"
"strings"

"github.com/pkg/errors"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/cfgwarn"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/module/system"
)

// init registers the MetricSet with the central registry as soon as the program
// starts. The New function will be called later to instantiate an instance of
// the MetricSet for each host defined in the module's configuration. After the
// MetricSet has been created then Fetch will begin to be called periodically.
func init() {
mb.Registry.MustAddMetricSet("system", "entropy", New)
}

// MetricSet holds any configuration or state information. It must implement
// the mb.MetricSet interface. And this is best achieved by embedding
// mb.BaseMetricSet because it implements all of the required mb.MetricSet
// interface methods except for Fetch.
type MetricSet struct {
mb.BaseMetricSet
randomPath string
}

// New creates a new instance of the MetricSet. New is responsible for unpacking
// any MetricSet specific configuration options if there are any.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
cfgwarn.Beta("The system entropy metricset is beta.")

systemModule, ok := base.Module().(*system.Module)
if !ok {
return nil, errors.New("unexpected module type")
}

totalPath := path.Join(systemModule.HostFS, "/proc/sys/kernel/random")

return &MetricSet{
BaseMetricSet: base,
randomPath: totalPath,
}, nil
}

// Fetch methods implements the data gathering and data conversion to the right
// format. It publishes the event which is then forwarded to the output. In case
// of an error set the Error field of mb.Event or simply call report.Error().
func (m *MetricSet) Fetch(report mb.ReporterV2) error {
entropy, err := getEntropyData(path.Join(m.randomPath, "entropy_avail"))
if err != nil {
return errors.Wrap(err, "error getting entropy")
}
poolsize, err := getEntropyData(path.Join(m.randomPath, "poolsize"))
if err != nil {
return errors.Wrap(err, "error getting poolsize")
}
report.Event(mb.Event{
MetricSetFields: common.MapStr{
"available_bits": entropy,
"pct": float64(entropy) / float64(poolsize),
},
})

return nil
}

func getEntropyData(path string) (int, error) {
//This will be a number in the range 0 to 4096.
raw, err := ioutil.ReadFile(path)
if err != nil {
return 0, errors.Wrap(err, "error reading from random")
}

intval, err := strconv.ParseInt(strings.TrimSpace(string(raw)), 10, 64)
if err != nil {
return 0, errors.Wrap(err, "error parsing from random")
}

return int(intval), nil
}
59 changes: 59 additions & 0 deletions metricbeat/module/system/entropy/entropy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

// +build linux

package entropy

import (
"testing"

"github.com/stretchr/testify/assert"

mbtest "github.com/elastic/beats/metricbeat/mb/testing"
"github.com/elastic/beats/metricbeat/module/system"
)

func TestData(t *testing.T) {
testdata := "./_meta/testdata"
system.HostFS = &testdata
f := mbtest.NewReportingMetricSetV2Error(t, getConfig())
err := mbtest.WriteEventsReporterV2Error(f, t, ".")
if err != nil {
t.Fatal("write", err)
}
}

func TestFetch(t *testing.T) {
testdata := "./_meta/testdata"
system.HostFS = &testdata
f := mbtest.NewReportingMetricSetV2Error(t, getConfig())
events, errs := mbtest.ReportingFetchV2Error(f)

assert.Empty(t, errs)
if !assert.NotEmpty(t, events) {
t.FailNow()
}
assert.Equal(t, events[0].MetricSetFields["available_bits"], 2801)
}

func getConfig() map[string]interface{} {
return map[string]interface{}{
"module": "system",
"metricsets": []string{"entropy"},
}
}
2 changes: 1 addition & 1 deletion metricbeat/module/system/fields.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions metricbeat/modules.d/system.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- process
- process_summary
- socket_summary
#- entropy
#- core
#- diskio
#- socket
Expand Down

0 comments on commit 260f095

Please sign in to comment.