forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jolokia Module with dynamic JMX Metricset (elastic#3570)
This is the implementation of a module for Jolokia which contains a dynamic jmx metricset. An example configuration looks as following: ``` - module: jolokia metricsets: ["jmx"] enabled: true period: 1s hosts: ["localhost:8778"] namespace: "metrics" jmx.mappings: - mbean: 'java.lang:type=Runtime' attributes: - attr: Uptime field: uptime - mbean: 'java.lang:type=GarbageCollector,name=ConcurrentMarkSweep' attributes: - attr: CollectionTime field: gc.cms_collection_time - attr: CollectionCount field: gc.cms_collection_count - mbean: 'java.lang:type=Memory' attributes: - attr: HeapMemoryUsage field: memory.heap_usage - attr: NonHeapMemoryUsage field: memory.non_heap_usage ``` For each mbeat the attributes which should be fetched can be defined. The field defines under which field name the event will be put. The namespace defines the metricset namespace. This PR replaces elastic#3051 Further changes: * Added support for method and body to http helper * Handle empty fields in generators. This happens for a module which only contains dynamic metricsets which is currently the case for jolokia. TODO: * [x] Add system tests * [x] Check documentation * [x] Add integration test * [ ] Open issue for metricset which contains basic memory info
- Loading branch information
Showing
31 changed files
with
938 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//// | ||
This file is generated! See scripts/docs_collector.py | ||
//// | ||
|
||
[[metricbeat-module-jolokia]] | ||
== Jolokia Module | ||
|
||
beta[] | ||
|
||
This is the Jolokia Module. | ||
|
||
|
||
|
||
[float] | ||
=== Example Configuration | ||
|
||
The Jolokia module supports the standard configuration options that are described | ||
in <<configuration-metricbeat>>. Here is an example configuration: | ||
|
||
[source,yaml] | ||
---- | ||
metricbeat.modules: | ||
#- module: jolokia | ||
# metricsets: ["jmx"] | ||
# enabled: true | ||
# period: 10s | ||
# hosts: ["localhost"] | ||
# namespace: "metrics" | ||
# path: "/jolokia/?ignoreErrors=true&canonicalNaming=false" | ||
# jmx.mapping: | ||
# jmx.application: | ||
# jmx.instance: | ||
---- | ||
|
||
[float] | ||
=== Metricsets | ||
|
||
The following metricsets are available: | ||
|
||
* <<metricbeat-metricset-jolokia-jmx,jmx>> | ||
|
||
include::jolokia/jmx.asciidoc[] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//// | ||
This file is generated! See scripts/docs_collector.py | ||
//// | ||
|
||
[[metricbeat-metricset-jolokia-jmx]] | ||
include::../../../module/jolokia/jmx/_meta/docs.asciidoc[] | ||
|
||
|
||
==== Fields | ||
|
||
For a description of each field in the metricset, see the | ||
<<exported-fields-jolokia,exported fields>> section. | ||
|
||
Here is an example document generated by this metricset: | ||
|
||
[source,json] | ||
---- | ||
include::../../../module/jolokia/jmx/_meta/data.json[] | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package helper | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/elastic/beats/metricbeat/mb" | ||
) | ||
|
||
type HTTP struct { | ||
base mb.BaseMetricSet | ||
client *http.Client // HTTP client that is reused across requests. | ||
headers map[string]string | ||
method string | ||
body []byte | ||
} | ||
|
||
// NewHTTP creates new http helper | ||
func NewHTTP(base mb.BaseMetricSet) *HTTP { | ||
return &HTTP{ | ||
base: base, | ||
client: &http.Client{Timeout: base.Module().Config().Timeout}, | ||
headers: map[string]string{}, | ||
method: "GET", | ||
body: nil, | ||
} | ||
} | ||
|
||
// FetchResponse fetches a response for the http metricset. | ||
// It's important that resp.Body has to be closed if this method is used. Before using this method | ||
// check if one of the other Fetch* methods could be used as they ensure that the Body is properly closed. | ||
func (h *HTTP) FetchResponse() (*http.Response, error) { | ||
|
||
// Create a fresh reader every time | ||
var reader io.Reader | ||
if h.body != nil { | ||
reader = bytes.NewReader(h.body) | ||
} | ||
|
||
req, err := http.NewRequest(h.method, h.base.HostData().SanitizedURI, reader) | ||
if h.base.HostData().User != "" || h.base.HostData().Password != "" { | ||
req.SetBasicAuth(h.base.HostData().User, h.base.HostData().Password) | ||
} | ||
|
||
for k, v := range h.headers { | ||
req.Header.Set(k, v) | ||
} | ||
|
||
resp, err := h.client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("error making http request: %v", err) | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func (h *HTTP) SetHeader(key, value string) { | ||
h.headers[key] = value | ||
} | ||
|
||
func (h *HTTP) SetMethod(method string) { | ||
h.method = method | ||
} | ||
|
||
func (h *HTTP) SetBody(body []byte) { | ||
h.body = body | ||
} | ||
|
||
// FetchContent makes an HTTP request to the configured url and returns the body content. | ||
func (h *HTTP) FetchContent() ([]byte, error) { | ||
resp, err := h.FetchResponse() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != 200 { | ||
return nil, fmt.Errorf("HTTP error %d in %s: %s", resp.StatusCode, h.base.Name(), resp.Status) | ||
} | ||
|
||
return ioutil.ReadAll(resp.Body) | ||
} | ||
|
||
// FetchScanner returns a Scanner for the content. | ||
func (h *HTTP) FetchScanner() (*bufio.Scanner, error) { | ||
content, err := h.FetchContent() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return bufio.NewScanner(bytes.NewReader(content)), nil | ||
} | ||
|
||
// FetchJSON makes an HTTP request to the configured url and returns the JSON content. | ||
// This only works if the JSON output needed is in map[string]interface format. | ||
func (h *HTTP) FetchJSON() (map[string]interface{}, error) { | ||
|
||
body, err := h.FetchContent() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var data map[string]interface{} | ||
|
||
err = json.Unmarshal(body, &data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Tomcat is started to fetch Jolokia metrics from it | ||
FROM jolokia/java-jolokia:7 | ||
ENV TOMCAT_VERSION 7.0.55 | ||
ENV TC apache-tomcat-${TOMCAT_VERSION} | ||
|
||
EXPOSE 8778 | ||
RUN wget http://archive.apache.org/dist/tomcat/tomcat-7/v${TOMCAT_VERSION}/bin/${TC}.tar.gz | ||
RUN tar xzf ${TC}.tar.gz -C /opt | ||
|
||
CMD env CATALINA_OPTS=$(jolokia_opts) /opt/${TC}/bin/catalina.sh run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#- module: jolokia | ||
# metricsets: ["jmx"] | ||
# enabled: true | ||
# period: 10s | ||
# hosts: ["localhost"] | ||
# namespace: "metrics" | ||
# path: "/jolokia/?ignoreErrors=true&canonicalNaming=false" | ||
# jmx.mapping: | ||
# jmx.application: | ||
# jmx.instance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
== Jolokia Module | ||
|
||
beta[] | ||
|
||
This is the Jolokia Module. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
JOLOKIA_HOST=jolokia | ||
JOLOKIA_PORT=8778 |
Oops, something went wrong.