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,18 +142,106 @@ 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)
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
}
return j.doRequest(req)
req.Header.Add("Content-type", "application/json")
return req, nil
}
func (j *Jolokia) collectMeasurement(measurement string, out map[string]interface{}, fields map[string]interface{}) {
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)
if err != nil {
fmt.Printf("Error handling response: %s\n", err)
}else {
if values, ok := out["value"]; ok {
switch t := values.(type) {
@ -169,123 +257,11 @@ func (j *Jolokia) collectMeasurement(measurement string, out map[string]interfac
}
}
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 == ""){
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 != "" {
jmxPath = jmxPath + "/" + metric.Attribute
if metric.Path != "" {
jmxPath = jmxPath + "/" + metric.Path
}
}
// 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)
}
} 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)
target := map[string]string{
"url": serviceUrl,
}
if server.Username != "" {
target["user"] = server.Username
}
if server.Password != "" {
target["password"] = server.Password
}
// Create + send request
bodyContent := map[string]interface{}{
"type": "read",
"mbean": metric.Mbean,
"target": target,
}
if metric.Attribute != "" {
bodyContent["attribute"] = metric.Attribute
if metric.Path != "" {
bodyContent["path"] = metric.Path
}
}
requestBody, err := json.Marshal(bodyContent)
req, err := http.NewRequest("POST", proxyURL.String(), bytes.NewBuffer(requestBody))
if err != nil {
return err
}
req.Header.Add("Content-type", "application/json")
out, err := j.doRequest(req)
if err != nil {
fmt.Printf("Error handling response: %s\n", err)
}else {
j.collectMeasurement(measurement, out, fields)
}
}
acc.AddFields("jolokia", fields, tags)
}
}
return nil
}