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