jolokia: use always POST

code refactor to use same prepareRequest method
for both 'agent' and 'proxy' mode
This commit is contained in:
Simone Aiello 2016-04-15 00:38:46 +02:00
parent dc160b307e
commit 83aa86c404
1 changed files with 86 additions and 110 deletions

View File

@ -142,148 +142,124 @@ func (j *Jolokia) doRequest(req *http.Request) (map[string]interface{}, error) {
return jsonOut, nil 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
func (j *Jolokia) collectMeasurement(measurement string, out map[string]interface{}, fields map[string]interface{}) {
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")
}
}
func (j *Jolokia) Gather(acc telegraf.Accumulator) error {
context := j.Context // Usually "/jolokia" context := j.Context // Usually "/jolokia"
servers := j.Servers
metrics := j.Metrics
tags := make(map[string]string)
mode := j.Mode
if( mode == "agent" || mode == ""){ // Create bodyContent
bodyContent := map[string]interface{}{
"type": "read",
"mbean": metric.Mbean,
}
for _, server := range servers { if metric.Attribute != "" {
tags["server"] = server.Name bodyContent["attribute"] = metric.Attribute
tags["port"] = server.Port if metric.Path != "" {
tags["host"] = server.Host bodyContent["path"] = metric.Path
fields := make(map[string]interface{}) }
for _, metric := range metrics { }
measurement := metric.Name // Add target, only in proxy mode
jmxPath := "/" + metric.Mbean if ( j.Mode == "proxy") {
if metric.Attribute != "" {
jmxPath = jmxPath + "/" + metric.Attribute
if metric.Path != "" { serviceUrl := fmt.Sprintf("service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi", server.Host, server.Port)
jmxPath = jmxPath + "/" + metric.Path
}
}
// Prepare URL target := map[string]string{
requestUrl, err := url.Parse("http://" + server.Host + ":" + "url": serviceUrl,
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)
} }
} else if ( mode == "proxy") { if server.Username != "" {
target["user"] = server.Username
}
if server.Password != "" {
target["password"] = server.Password
}
bodyContent["target"] = target
proxy := j.Proxy proxy := j.Proxy
// Prepare ProxyURL // Prepare ProxyURL
proxyURL, err := url.Parse("http://" + proxy.Host + ":" + proxyUrl, err := url.Parse("http://" + proxy.Host + ":" + proxy.Port + context)
proxy.Port + context)
if err != nil { if err != nil {
return err return nil, err
} }
if proxy.Username != "" || proxy.Password != "" { if proxy.Username != "" || proxy.Password != "" {
proxyURL.User = url.UserPassword(proxy.Username, proxy.Password) proxyUrl.User = url.UserPassword(proxy.Username, proxy.Password)
} }
for _, server := range servers { jolokiaUrl = proxyUrl
tags["server"] = server.Name } else {
tags["port"] = server.Port
tags["host"] = server.Host
fields := make(map[string]interface{})
for _, metric := range metrics {
measurement := metric.Name serverUrl, err := url.Parse("http://" + server.Host + ":" + server.Port + context)
// Prepare URL if err != nil {
serviceUrl := fmt.Sprintf("service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi", server.Host, server.Port) return nil, err
}
if server.Username != "" || server.Password != "" {
serverUrl.User = url.UserPassword(server.Username, server.Password)
}
target := map[string]string{ jolokiaUrl = serverUrl
"url": serviceUrl, }
}
if server.Username != "" { requestBody, err := json.Marshal(bodyContent)
target["user"] = server.Username
}
if server.Password != "" { req, err := http.NewRequest("POST", jolokiaUrl.String(), bytes.NewBuffer(requestBody))
target["password"] = server.Password
}
// Create + send request if err != nil {
bodyContent := map[string]interface{}{ return nil, err
"type": "read", }
"mbean": metric.Mbean,
"target": target,
}
if metric.Attribute != "" { req.Header.Add("Content-type", "application/json")
bodyContent["attribute"] = metric.Attribute
if metric.Path != "" {
bodyContent["path"] = metric.Path
}
}
requestBody, err := json.Marshal(bodyContent) return req, nil
}
req, err := http.NewRequest("POST", proxyURL.String(), bytes.NewBuffer(requestBody))
if err != nil { func (j *Jolokia) Gather(acc telegraf.Accumulator) error {
return err servers := j.Servers
} metrics := j.Metrics
tags := make(map[string]string)
req.Header.Add("Content-type", "application/json") for _, server := range servers {
tags["server"] = server.Name
tags["port"] = server.Port
tags["host"] = server.Host
fields := make(map[string]interface{})
out, err := j.doRequest(req) for _, metric := range metrics {
measurement := metric.Name
req, err := j.prepareRequest(server, metric)
if err != nil{
return err
}
out, err := j.doRequest(req)
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")
}
if err != nil {
fmt.Printf("Error handling response: %s\n", err)
}else {
j.collectMeasurement(measurement, out, fields)
}
} }
acc.AddFields("jolokia", fields, tags)
} }
acc.AddFields("jolokia", fields, tags)
} }
return nil return nil