Add files via upload
added https for individual servers (and proxy) and response/header timeout.
This commit is contained in:
parent
b4f9bc8745
commit
f253dbb2ef
|
@ -11,15 +11,25 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
|
"github.com/influxdata/telegraf/internal"
|
||||||
"github.com/influxdata/telegraf/plugins/inputs"
|
"github.com/influxdata/telegraf/plugins/inputs"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Name string
|
Name string
|
||||||
Host string
|
Url string
|
||||||
|
Headertimeout internal.Duration
|
||||||
|
Requesttimeout internal.Duration
|
||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
Port string
|
// Path to CA file
|
||||||
|
Ca string
|
||||||
|
// Path to host cert file
|
||||||
|
Cert string
|
||||||
|
// Path to cert key file
|
||||||
|
Key string
|
||||||
|
// Use SSL but skip chain & host verification
|
||||||
|
Insecureverify bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Metric struct {
|
type Metric struct {
|
||||||
|
@ -29,25 +39,13 @@ type Metric struct {
|
||||||
Path string
|
Path string
|
||||||
}
|
}
|
||||||
|
|
||||||
type JolokiaClient interface {
|
|
||||||
MakeRequest(req *http.Request) (*http.Response, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type JolokiaClientImpl struct {
|
|
||||||
client *http.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c JolokiaClientImpl) MakeRequest(req *http.Request) (*http.Response, error) {
|
|
||||||
return c.client.Do(req)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Jolokia struct {
|
type Jolokia struct {
|
||||||
jClient JolokiaClient
|
Context string
|
||||||
Context string
|
Mode string
|
||||||
Mode string
|
Proxy Server
|
||||||
Servers []Server
|
Servers []Server
|
||||||
Metrics []Metric
|
Metrics []Metric
|
||||||
Proxy Server
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const sampleConfig = `
|
const sampleConfig = `
|
||||||
|
@ -62,38 +60,54 @@ const sampleConfig = `
|
||||||
## proxy address configurations.
|
## proxy address configurations.
|
||||||
## Remember to change host address to fit your environment.
|
## Remember to change host address to fit your environment.
|
||||||
# [inputs.jolokia.proxy]
|
# [inputs.jolokia.proxy]
|
||||||
# host = "127.0.0.1"
|
# url = "localhost:8080"
|
||||||
# port = "8080"
|
# headertimeout = 30
|
||||||
|
# requesttimeout = 30
|
||||||
|
# username = "myuser"
|
||||||
|
# password = "mypassword"
|
||||||
|
## Optional SSL Config
|
||||||
|
# ca = "/etc/telegraf/ca.pem"
|
||||||
|
# cert = "/etc/telegraf/cert.pem"
|
||||||
|
# key = "/etc/telegraf/key.pem"
|
||||||
|
## Use SSL but skip chain & host verification
|
||||||
|
# insecureverify = false
|
||||||
|
|
||||||
## List of servers exposing jolokia read service
|
## List of servers exposing jolokia read service
|
||||||
[[inputs.jolokia.servers]]
|
[[inputs.jolokia.servers]]
|
||||||
name = "as-server-01"
|
name = "as-server-01"
|
||||||
host = "127.0.0.1"
|
url = "http://as-server-01:8080"
|
||||||
port = "8080"
|
# headertimeout = 30
|
||||||
|
# requesttimeout = 30
|
||||||
# username = "myuser"
|
# username = "myuser"
|
||||||
# password = "mypassword"
|
# password = "mypassword"
|
||||||
|
## Optional SSL Config
|
||||||
|
# ca = "/etc/telegraf/ca.pem"
|
||||||
|
# cert = "/etc/telegraf/cert.pem"
|
||||||
|
# key = "/etc/telegraf/key.pem"
|
||||||
|
## Use SSL but skip chain & host verification
|
||||||
|
# insecureverify = false
|
||||||
|
|
||||||
## List of metrics collected on above servers
|
## List of metrics collected on above servers
|
||||||
## Each metric consists in a name, a jmx path and either
|
## Each metric consists in a name, a jmx path and either
|
||||||
## a pass or drop slice attribute.
|
## a pass or drop slice attribute.
|
||||||
## This collect all heap memory usage metrics.
|
## This collect all heap memory usage metrics.
|
||||||
[[inputs.jolokia.metrics]]
|
[[inputs.jolokia.metrics]]
|
||||||
name = "heap_memory_usage"
|
name = "heap_memory_usage"
|
||||||
mbean = "java.lang:type=Memory"
|
mbean = "java.lang:type=Memory"
|
||||||
attribute = "HeapMemoryUsage"
|
attribute = "HeapMemoryUsage"
|
||||||
|
|
||||||
## This collect thread counts metrics.
|
## This collect thread counts metrics.
|
||||||
[[inputs.jolokia.metrics]]
|
[[inputs.jolokia.metrics]]
|
||||||
name = "thread_count"
|
name = "thread_count"
|
||||||
mbean = "java.lang:type=Threading"
|
mbean = "java.lang:type=Threading"
|
||||||
attribute = "TotalStartedThreadCount,ThreadCount,DaemonThreadCount,PeakThreadCount"
|
attribute = "TotalStartedThreadCount,ThreadCount,DaemonThreadCount,PeakThreadCount"
|
||||||
|
|
||||||
## This collect number of class loaded/unloaded counts metrics.
|
## This collect number of class loaded/unloaded counts metrics.
|
||||||
[[inputs.jolokia.metrics]]
|
[[inputs.jolokia.metrics]]
|
||||||
name = "class_count"
|
name = "class_count"
|
||||||
mbean = "java.lang:type=ClassLoading"
|
mbean = "java.lang:type=ClassLoading"
|
||||||
attribute = "LoadedClassCount,UnloadedClassCount,TotalLoadedClassCount"
|
attribute = "LoadedClassCount,UnloadedClassCount,TotalLoadedClassCount"
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
func (j *Jolokia) SampleConfig() string {
|
func (j *Jolokia) SampleConfig() string {
|
||||||
|
@ -104,8 +118,71 @@ func (j *Jolokia) Description() string {
|
||||||
return "Read JMX metrics through Jolokia"
|
return "Read JMX metrics through Jolokia"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *Jolokia) doRequest(req *http.Request) (map[string]interface{}, error) {
|
func (j *Jolokia) createHttpClient(server Server) (*http.Client, error) {
|
||||||
resp, err := j.jClient.MakeRequest(req)
|
var tr *http.Transport
|
||||||
|
|
||||||
|
if server.Headertimeout.Duration < time.Second {
|
||||||
|
server.Headertimeout.Duration = time.Second * 5
|
||||||
|
}
|
||||||
|
|
||||||
|
if server.Requesttimeout.Duration < time.Second {
|
||||||
|
server.Requesttimeout.Duration = time.Second * 5
|
||||||
|
}
|
||||||
|
|
||||||
|
serverUrl, err := url.Parse(server.Url)
|
||||||
|
if err != nil || serverUrl.String() == "" {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if serverUrl.Scheme == "https" {
|
||||||
|
if server.Cert == "" && server.Key == "" && server.Ca == "" {
|
||||||
|
err = fmt.Errorf("No SSL configuration provided")
|
||||||
|
return nil,err
|
||||||
|
}
|
||||||
|
tlsCfg, err := internal.GetTLSConfig(
|
||||||
|
server.Cert, server.Key, server.Ca, server.Insecureverify)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tr = &http.Transport{
|
||||||
|
ResponseHeaderTimeout: server.Headertimeout.Duration,
|
||||||
|
TLSClientConfig: tlsCfg,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
tr = &http.Transport{
|
||||||
|
ResponseHeaderTimeout: server.Headertimeout.Duration,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &http.Client{
|
||||||
|
Transport: tr,
|
||||||
|
Timeout: server.Requesttimeout.Duration,
|
||||||
|
}
|
||||||
|
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *Jolokia) doRequest(server Server,metric Metric) (map[string]interface{}, error) {
|
||||||
|
var client *http.Client
|
||||||
|
|
||||||
|
req, err := j.prepareRequest(server, metric)
|
||||||
|
if err != nil || req.URL.String() == "" {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if j.Mode == "proxy" {
|
||||||
|
client, err = j.createHttpClient(j.Proxy)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
client, err = j.createHttpClient(server)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -114,7 +191,7 @@ func (j *Jolokia) doRequest(req *http.Request) (map[string]interface{}, error) {
|
||||||
// Process response
|
// Process response
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
err = fmt.Errorf("Response from url \"%s\" has status code %d (%s), expected %d (%s)",
|
err = fmt.Errorf("Response from url \"%s\" has status code %d (%s), expected %d (%s)",
|
||||||
req.RequestURI,
|
req.URL.String(),
|
||||||
resp.StatusCode,
|
resp.StatusCode,
|
||||||
http.StatusText(resp.StatusCode),
|
http.StatusText(resp.StatusCode),
|
||||||
http.StatusOK,
|
http.StatusOK,
|
||||||
|
@ -148,6 +225,7 @@ func (j *Jolokia) doRequest(req *http.Request) (map[string]interface{}, error) {
|
||||||
|
|
||||||
func (j *Jolokia) prepareRequest(server Server, metric Metric) (*http.Request, error) {
|
func (j *Jolokia) prepareRequest(server Server, metric Metric) (*http.Request, error) {
|
||||||
var jolokiaUrl *url.URL
|
var jolokiaUrl *url.URL
|
||||||
|
|
||||||
context := j.Context // Usually "/jolokia"
|
context := j.Context // Usually "/jolokia"
|
||||||
|
|
||||||
// Create bodyContent
|
// Create bodyContent
|
||||||
|
@ -163,10 +241,15 @@ func (j *Jolokia) prepareRequest(server Server, metric Metric) (*http.Request, e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
serverUrl, err := url.Parse(server.Url + context)
|
||||||
|
if err != nil || serverUrl.String() == "" {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// Add target, only in proxy mode
|
// Add target, only in proxy mode
|
||||||
if j.Mode == "proxy" {
|
if j.Mode == "proxy" {
|
||||||
serviceUrl := fmt.Sprintf("service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi",
|
serviceUrl := fmt.Sprintf("service:jmx:rmi:///jndi/rmi://%s/jmxrmi",
|
||||||
server.Host, server.Port)
|
serverUrl.Host)
|
||||||
|
|
||||||
target := map[string]string{
|
target := map[string]string{
|
||||||
"url": serviceUrl,
|
"url": serviceUrl,
|
||||||
|
@ -185,8 +268,8 @@ func (j *Jolokia) prepareRequest(server Server, metric Metric) (*http.Request, e
|
||||||
proxy := j.Proxy
|
proxy := j.Proxy
|
||||||
|
|
||||||
// Prepare ProxyURL
|
// Prepare ProxyURL
|
||||||
proxyUrl, err := url.Parse("http://" + proxy.Host + ":" + proxy.Port + context)
|
proxyUrl, err := url.Parse(proxy.Url + context)
|
||||||
if err != nil {
|
if err != nil || proxyUrl.String() == "" {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if proxy.Username != "" || proxy.Password != "" {
|
if proxy.Username != "" || proxy.Password != "" {
|
||||||
|
@ -196,10 +279,6 @@ func (j *Jolokia) prepareRequest(server Server, metric Metric) (*http.Request, e
|
||||||
jolokiaUrl = proxyUrl
|
jolokiaUrl = proxyUrl
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
serverUrl, err := url.Parse("http://" + server.Host + ":" + server.Port + context)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if server.Username != "" || server.Password != "" {
|
if server.Username != "" || server.Password != "" {
|
||||||
serverUrl.User = url.UserPassword(server.Username, server.Password)
|
serverUrl.User = url.UserPassword(server.Username, server.Password)
|
||||||
}
|
}
|
||||||
|
@ -225,21 +304,16 @@ func (j *Jolokia) Gather(acc telegraf.Accumulator) error {
|
||||||
metrics := j.Metrics
|
metrics := j.Metrics
|
||||||
tags := make(map[string]string)
|
tags := make(map[string]string)
|
||||||
|
|
||||||
|
|
||||||
for _, server := range servers {
|
for _, server := range servers {
|
||||||
tags["jolokia_name"] = server.Name
|
tags["jolokia_name"] = server.Name
|
||||||
tags["jolokia_port"] = server.Port
|
tags["jolokia_url"] = server.Url
|
||||||
tags["jolokia_host"] = server.Host
|
|
||||||
fields := make(map[string]interface{})
|
fields := make(map[string]interface{})
|
||||||
|
|
||||||
for _, metric := range metrics {
|
for _, metric := range metrics {
|
||||||
measurement := metric.Name
|
measurement := metric.Name
|
||||||
|
|
||||||
req, err := j.prepareRequest(server, metric)
|
out, err := j.doRequest(server,metric)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
||||||
|
@ -276,11 +350,6 @@ func (j *Jolokia) Gather(acc telegraf.Accumulator) error {
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
inputs.Add("jolokia", func() telegraf.Input {
|
inputs.Add("jolokia", func() telegraf.Input {
|
||||||
tr := &http.Transport{ResponseHeaderTimeout: time.Duration(3 * time.Second)}
|
return &Jolokia{}
|
||||||
client := &http.Client{
|
|
||||||
Transport: tr,
|
|
||||||
Timeout: time.Duration(4 * time.Second),
|
|
||||||
}
|
|
||||||
return &Jolokia{jClient: &JolokiaClientImpl{client: client}}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue