2017-09-27 00:34:46 +00:00
|
|
|
package jolokia2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2018-05-12 00:47:38 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2018-05-04 23:33:23 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/tls"
|
2017-09-27 00:34:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type JolokiaAgent struct {
|
|
|
|
DefaultFieldPrefix string
|
|
|
|
DefaultFieldSeparator string
|
|
|
|
DefaultTagPrefix string
|
|
|
|
|
|
|
|
URLs []string `toml:"urls"`
|
|
|
|
Username string
|
|
|
|
Password string
|
2018-05-12 00:47:38 +00:00
|
|
|
ResponseTimeout internal.Duration `toml:"response_timeout"`
|
2017-09-27 00:34:46 +00:00
|
|
|
|
2018-05-04 23:33:23 +00:00
|
|
|
tls.ClientConfig
|
2017-09-27 00:34:46 +00:00
|
|
|
|
|
|
|
Metrics []MetricConfig `toml:"metric"`
|
|
|
|
gatherer *Gatherer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ja *JolokiaAgent) SampleConfig() string {
|
|
|
|
return `
|
|
|
|
# default_tag_prefix = ""
|
|
|
|
# default_field_prefix = ""
|
|
|
|
# default_field_separator = "."
|
|
|
|
|
|
|
|
# Add agents URLs to query
|
|
|
|
urls = ["http://localhost:8080/jolokia"]
|
|
|
|
# username = ""
|
|
|
|
# password = ""
|
|
|
|
# response_timeout = "5s"
|
|
|
|
|
2018-05-04 23:33:23 +00:00
|
|
|
## Optional TLS config
|
|
|
|
# tls_ca = "/var/private/ca.pem"
|
|
|
|
# tls_cert = "/var/private/client.pem"
|
|
|
|
# tls_key = "/var/private/client-key.pem"
|
2017-09-27 00:34:46 +00:00
|
|
|
# insecure_skip_verify = false
|
|
|
|
|
|
|
|
## Add metrics to read
|
2017-12-29 00:10:00 +00:00
|
|
|
[[inputs.jolokia2_agent.metric]]
|
2017-09-27 00:34:46 +00:00
|
|
|
name = "java_runtime"
|
|
|
|
mbean = "java.lang:type=Runtime"
|
|
|
|
paths = ["Uptime"]
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ja *JolokiaAgent) Description() string {
|
|
|
|
return "Read JMX metrics from a Jolokia REST agent endpoint"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ja *JolokiaAgent) Gather(acc telegraf.Accumulator) error {
|
|
|
|
if ja.gatherer == nil {
|
|
|
|
ja.gatherer = NewGatherer(ja.createMetrics())
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for _, url := range ja.URLs {
|
|
|
|
client, err := ja.createClient(url)
|
|
|
|
if err != nil {
|
|
|
|
acc.AddError(fmt.Errorf("Unable to create client for %s: %v", url, err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func(client *Client) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
err = ja.gatherer.Gather(client, acc)
|
|
|
|
if err != nil {
|
|
|
|
acc.AddError(fmt.Errorf("Unable to gather metrics for %s: %v", client.URL, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
}(client)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ja *JolokiaAgent) createMetrics() []Metric {
|
|
|
|
var metrics []Metric
|
|
|
|
|
|
|
|
for _, config := range ja.Metrics {
|
|
|
|
metrics = append(metrics, NewMetric(config,
|
|
|
|
ja.DefaultFieldPrefix, ja.DefaultFieldSeparator, ja.DefaultTagPrefix))
|
|
|
|
}
|
|
|
|
|
|
|
|
return metrics
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ja *JolokiaAgent) createClient(url string) (*Client, error) {
|
|
|
|
return NewClient(url, &ClientConfig{
|
2018-05-04 23:33:23 +00:00
|
|
|
Username: ja.Username,
|
|
|
|
Password: ja.Password,
|
2018-05-12 00:47:38 +00:00
|
|
|
ResponseTimeout: ja.ResponseTimeout.Duration,
|
2018-05-04 23:33:23 +00:00
|
|
|
ClientConfig: ja.ClientConfig,
|
2017-09-27 00:34:46 +00:00
|
|
|
})
|
|
|
|
}
|