Skip to content

Commit

Permalink
jolokia: use always POST
Browse files Browse the repository at this point in the history
code refactor to use same prepareRequest method
for both 'agent' and 'proxy' mode
  • Loading branch information
saiello committed Apr 14, 2016
1 parent dc160b3 commit 83aa86c
Showing 1 changed file with 82 additions and 106 deletions.
188 changes: 82 additions & 106 deletions plugins/inputs/jolokia/jolokia.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,148 +142,124 @@ func (j *Jolokia) doRequest(req *http.Request) (map[string]interface{}, error) {
return jsonOut, nil
}

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
}

return j.doRequest(req)
}

func (j *Jolokia) prepareRequest(server Server, metric Metric) (*http.Request, error) {
var jolokiaUrl *url.URL
context := j.Context // Usually "/jolokia"

func (j *Jolokia) collectMeasurement(measurement string, out map[string]interface{}, fields map[string]interface{}) {
// Create bodyContent
bodyContent := map[string]interface{}{
"type": "read",
"mbean": metric.Mbean,
}

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
if metric.Attribute != "" {
bodyContent["attribute"] = metric.Attribute
if metric.Path != "" {
bodyContent["path"] = metric.Path
}
} else {
fmt.Printf("Missing key 'value' in output response\n")
}

}


func (j *Jolokia) Gather(acc telegraf.Accumulator) error {
context := j.Context // Usually "/jolokia"
servers := j.Servers
metrics := j.Metrics
tags := make(map[string]string)
mode := j.Mode

if( mode == "agent" || mode == ""){
// Add target, only in proxy mode
if ( j.Mode == "proxy") {

for _, server := range servers {
tags["server"] = server.Name
tags["port"] = server.Port
tags["host"] = server.Host
fields := make(map[string]interface{})
for _, metric := range metrics {
serviceUrl := fmt.Sprintf("service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi", server.Host, server.Port)

measurement := metric.Name
jmxPath := "/" + metric.Mbean
if metric.Attribute != "" {
jmxPath = jmxPath + "/" + metric.Attribute
target := map[string]string{
"url": serviceUrl,
}

if metric.Path != "" {
jmxPath = jmxPath + "/" + metric.Path
}
}
if server.Username != "" {
target["user"] = server.Username
}

// Prepare URL
requestUrl, err := url.Parse("http://" + server.Host + ":" +
server.Port + context + "/read" + jmxPath)
if err != nil {
return err
}
if server.Username != "" || server.Password != "" {
requestUrl.User = url.UserPassword(server.Username, server.Password)
}
out, _ := j.getAttr(requestUrl)
j.collectMeasurement(measurement, out, fields)
}
acc.AddFields("jolokia", fields, tags)
if server.Password != "" {
target["password"] = server.Password
}

} else if ( mode == "proxy") {
bodyContent["target"] = target

proxy := j.Proxy

// Prepare ProxyURL
proxyURL, err := url.Parse("http://" + proxy.Host + ":" +
proxy.Port + context)
proxyUrl, err := url.Parse("http://" + proxy.Host + ":" + proxy.Port + context)
if err != nil {
return err
return nil, err
}
if proxy.Username != "" || proxy.Password != "" {
proxyURL.User = url.UserPassword(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)
}

for _, server := range servers {
tags["server"] = server.Name
tags["port"] = server.Port
tags["host"] = server.Host
fields := make(map[string]interface{})
for _, metric := range metrics {
jolokiaUrl = serverUrl
}

measurement := metric.Name
// Prepare URL
serviceUrl := fmt.Sprintf("service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi", server.Host, server.Port)
requestBody, err := json.Marshal(bodyContent)

target := map[string]string{
"url": serviceUrl,
}
req, err := http.NewRequest("POST", jolokiaUrl.String(), bytes.NewBuffer(requestBody))

if server.Username != "" {
target["user"] = server.Username
}
if err != nil {
return nil, err
}

if server.Password != "" {
target["password"] = server.Password
}
req.Header.Add("Content-type", "application/json")

// Create + send request
bodyContent := map[string]interface{}{
"type": "read",
"mbean": metric.Mbean,
"target": target,
}
return req, nil
}

if metric.Attribute != "" {
bodyContent["attribute"] = metric.Attribute
if metric.Path != "" {
bodyContent["path"] = metric.Path
}
}

requestBody, err := json.Marshal(bodyContent)
func (j *Jolokia) Gather(acc telegraf.Accumulator) error {
servers := j.Servers
metrics := j.Metrics
tags := make(map[string]string)

req, err := http.NewRequest("POST", proxyURL.String(), bytes.NewBuffer(requestBody))
for _, server := range servers {
tags["server"] = server.Name
tags["port"] = server.Port
tags["host"] = server.Host
fields := make(map[string]interface{})

if err != nil {
return err
}
for _, metric := range metrics {
measurement := metric.Name

req, err := j.prepareRequest(server, metric)
if err != nil{
return err
}

req.Header.Add("Content-type", "application/json")
out, err := j.doRequest(req)

out, err := j.doRequest(req)

if err != nil {
fmt.Printf("Error handling response: %s\n", err)
}else {
j.collectMeasurement(measurement, out, fields)
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
}
} else {
fmt.Printf("Missing key 'value' in output response\n")
}

}
acc.AddFields("jolokia", fields, tags)
}

acc.AddFields("jolokia", fields, tags)
}

return nil
Expand Down

0 comments on commit 83aa86c

Please sign in to comment.