-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/jolokia proxy mode #1031
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package jolokia | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
@@ -22,8 +23,10 @@ type Server struct { | |
} | ||
|
||
type Metric struct { | ||
Name string | ||
Jmx string | ||
Name string | ||
Mbean string | ||
Attribute string | ||
Path string | ||
} | ||
|
||
type JolokiaClient interface { | ||
|
@@ -41,20 +44,32 @@ func (c JolokiaClientImpl) MakeRequest(req *http.Request) (*http.Response, error | |
type Jolokia struct { | ||
jClient JolokiaClient | ||
Context string | ||
Mode string | ||
Servers []Server | ||
Metrics []Metric | ||
Proxy Server | ||
} | ||
|
||
func (j *Jolokia) SampleConfig() string { | ||
return ` | ||
## This is the context root used to compose the jolokia url | ||
context = "/jolokia/read" | ||
# This is the context root used to compose the jolokia url | ||
context = "/jolokia" | ||
|
||
## List of servers exposing jolokia read service | ||
# This specifies the mode used | ||
# mode = "proxy" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are the options besides "proxy"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently any value different to "proxy", make the input plugin executing in agent mode. |
||
# | ||
# When in proxy mode this section is used to specify further proxy address configurations. | ||
# Remember to change servers addresses | ||
# [inputs.jolokia.proxy] | ||
# host = "127.0.0.1" | ||
# port = "8080" | ||
|
||
|
||
# List of servers exposing jolokia read service | ||
[[inputs.jolokia.servers]] | ||
name = "stable" | ||
host = "192.168.103.2" | ||
port = "8180" | ||
name = "as-server-01" | ||
host = "127.0.0.1" | ||
port = "8080" | ||
# username = "myuser" | ||
# password = "mypassword" | ||
|
||
|
@@ -64,30 +79,28 @@ func (j *Jolokia) SampleConfig() string { | |
## This collect all heap memory usage metrics. | ||
[[inputs.jolokia.metrics]] | ||
name = "heap_memory_usage" | ||
jmx = "/java.lang:type=Memory/HeapMemoryUsage" | ||
|
||
mbean = "java.lang:type=Memory" | ||
attribute = "HeapMemoryUsage" | ||
|
||
## This collect thread counts metrics. | ||
[[inputs.jolokia.metrics]] | ||
name = "thread_count" | ||
jmx = "/java.lang:type=Threading/TotalStartedThreadCount,ThreadCount,DaemonThreadCount,PeakThreadCount" | ||
|
||
mbean = "java.lang:type=Threading" | ||
attribute = "TotalStartedThreadCount,ThreadCount,DaemonThreadCount,PeakThreadCount" | ||
|
||
## This collect number of class loaded/unloaded counts metrics. | ||
[[inputs.jolokia.metrics]] | ||
name = "class_count" | ||
jmx = "/java.lang:type=ClassLoading/LoadedClassCount,UnloadedClassCount,TotalLoadedClassCount" | ||
mbean = "java.lang:type=ClassLoading" | ||
attribute = "LoadedClassCount,UnloadedClassCount,TotalLoadedClassCount" | ||
` | ||
} | ||
|
||
func (j *Jolokia) Description() string { | ||
return "Read JMX metrics through Jolokia" | ||
} | ||
|
||
func (j *Jolokia) getAttr(requestUrl *url.URL) (map[string]interface{}, error) { | ||
// Create + send request | ||
req, err := http.NewRequest("GET", requestUrl.String(), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
func (j *Jolokia) doRequest(req *http.Request) (map[string]interface{}, error) { | ||
|
||
resp, err := j.jClient.MakeRequest(req) | ||
if err != nil { | ||
|
@@ -98,7 +111,7 @@ func (j *Jolokia) getAttr(requestUrl *url.URL) (map[string]interface{}, error) { | |
// Process response | ||
if resp.StatusCode != http.StatusOK { | ||
err = fmt.Errorf("Response from url \"%s\" has status code %d (%s), expected %d (%s)", | ||
requestUrl, | ||
req.RequestURI, | ||
resp.StatusCode, | ||
http.StatusText(resp.StatusCode), | ||
http.StatusOK, | ||
|
@@ -118,51 +131,132 @@ func (j *Jolokia) getAttr(requestUrl *url.URL) (map[string]interface{}, error) { | |
return nil, errors.New("Error decoding JSON response") | ||
} | ||
|
||
if status, ok := jsonOut["status"]; ok { | ||
if status != float64(200) { | ||
return nil, fmt.Errorf("Not expected status value in response body: %3.f", status) | ||
} | ||
} else { | ||
return nil, fmt.Errorf("Missing status in response body") | ||
} | ||
|
||
return jsonOut, nil | ||
} | ||
|
||
func (j *Jolokia) prepareRequest(server Server, metric Metric) (*http.Request, error) { | ||
var jolokiaUrl *url.URL | ||
context := j.Context // Usually "/jolokia" | ||
|
||
// Create bodyContent | ||
bodyContent := map[string]interface{}{ | ||
"type": "read", | ||
"mbean": metric.Mbean, | ||
} | ||
|
||
if metric.Attribute != "" { | ||
bodyContent["attribute"] = metric.Attribute | ||
if metric.Path != "" { | ||
bodyContent["path"] = metric.Path | ||
} | ||
} | ||
|
||
// Add target, only in proxy mode | ||
if j.Mode == "proxy" { | ||
|
||
serviceUrl := fmt.Sprintf("service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi", server.Host, server.Port) | ||
|
||
target := map[string]string{ | ||
"url": serviceUrl, | ||
} | ||
|
||
if server.Username != "" { | ||
target["user"] = server.Username | ||
} | ||
|
||
if server.Password != "" { | ||
target["password"] = server.Password | ||
} | ||
|
||
bodyContent["target"] = target | ||
|
||
proxy := j.Proxy | ||
|
||
// Prepare ProxyURL | ||
proxyUrl, err := url.Parse("http://" + proxy.Host + ":" + proxy.Port + context) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if proxy.Username != "" || proxy.Password != "" { | ||
proxyUrl.User = url.UserPassword(proxy.Username, proxy.Password) | ||
} | ||
|
||
jolokiaUrl = proxyUrl | ||
} else { | ||
|
||
serverUrl, err := url.Parse("http://" + server.Host + ":" + server.Port + context) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if server.Username != "" || server.Password != "" { | ||
serverUrl.User = url.UserPassword(server.Username, server.Password) | ||
} | ||
|
||
jolokiaUrl = serverUrl | ||
} | ||
|
||
requestBody, err := json.Marshal(bodyContent) | ||
|
||
req, err := http.NewRequest("POST", jolokiaUrl.String(), bytes.NewBuffer(requestBody)) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.Header.Add("Content-type", "application/json") | ||
|
||
return req, nil | ||
} | ||
|
||
func (j *Jolokia) Gather(acc telegraf.Accumulator) error { | ||
context := j.Context //"/jolokia/read" | ||
servers := j.Servers | ||
metrics := j.Metrics | ||
tags := make(map[string]string) | ||
|
||
for _, server := range servers { | ||
tags["server"] = server.Name | ||
tags["port"] = server.Port | ||
tags["host"] = server.Host | ||
tags["server_name"] = server.Name | ||
tags["server_port"] = server.Port | ||
tags["server_host"] = server.Host | ||
fields := make(map[string]interface{}) | ||
for _, metric := range metrics { | ||
|
||
for _, metric := range metrics { | ||
measurement := metric.Name | ||
jmxPath := metric.Jmx | ||
|
||
// Prepare URL | ||
requestUrl, err := url.Parse("http://" + server.Host + ":" + | ||
server.Port + context + jmxPath) | ||
req, err := j.prepareRequest(server, metric) | ||
if err != nil { | ||
return err | ||
} | ||
if server.Username != "" || server.Password != "" { | ||
requestUrl.User = url.UserPassword(server.Username, server.Password) | ||
} | ||
|
||
out, _ := j.getAttr(requestUrl) | ||
out, err := j.doRequest(req) | ||
|
||
if values, ok := out["value"]; ok { | ||
switch t := values.(type) { | ||
case map[string]interface{}: | ||
for k, v := range t { | ||
fields[measurement+"_"+k] = v | ||
if err != nil { | ||
fmt.Printf("Error handling response: %s\n", err) | ||
} else { | ||
|
||
if values, ok := out["value"]; ok { | ||
switch t := values.(type) { | ||
case map[string]interface{}: | ||
for k, v := range t { | ||
fields[measurement+"_"+k] = v | ||
} | ||
case interface{}: | ||
fields[measurement] = t | ||
} | ||
case interface{}: | ||
fields[measurement] = t | ||
} else { | ||
fmt.Printf("Missing key 'value' in output response\n") | ||
} | ||
} else { | ||
fmt.Printf("Missing key 'value' in '%s' output response\n", | ||
requestUrl.String()) | ||
|
||
} | ||
} | ||
|
||
acc.AddFields("jolokia", fields, tags) | ||
} | ||
|
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are you changing the default context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'/jolokia' is actually the context. 'read' is the operation. I started to use POST request to support proxy mode and I moved the operation type into the request body.