forked from cisco-ie/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jolokia_proxy.go
123 lines (100 loc) · 2.84 KB
/
jolokia_proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package jolokia2
import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/tls"
)
type JolokiaProxy struct {
DefaultFieldPrefix string
DefaultFieldSeparator string
DefaultTagPrefix string
URL string `toml:"url"`
DefaultTargetPassword string
DefaultTargetUsername string
Targets []JolokiaProxyTargetConfig `toml:"target"`
Username string
Password string
ResponseTimeout internal.Duration `toml:"response_timeout"`
tls.ClientConfig
Metrics []MetricConfig `toml:"metric"`
client *Client
gatherer *Gatherer
}
type JolokiaProxyTargetConfig struct {
URL string `toml:"url"`
Username string
Password string
}
func (jp *JolokiaProxy) SampleConfig() string {
return `
# default_tag_prefix = ""
# default_field_prefix = ""
# default_field_separator = "."
## Proxy agent
url = "http://localhost:8080/jolokia"
# username = ""
# password = ""
# response_timeout = "5s"
## Optional TLS config
# tls_ca = "/var/private/ca.pem"
# tls_cert = "/var/private/client.pem"
# tls_key = "/var/private/client-key.pem"
# insecure_skip_verify = false
## Add proxy targets to query
# default_target_username = ""
# default_target_password = ""
[[inputs.jolokia2_proxy.target]]
url = "service:jmx:rmi:///jndi/rmi://targethost:9999/jmxrmi"
# username = ""
# password = ""
## Add metrics to read
[[inputs.jolokia2_proxy.metric]]
name = "java_runtime"
mbean = "java.lang:type=Runtime"
paths = ["Uptime"]
`
}
func (jp *JolokiaProxy) Description() string {
return "Read JMX metrics from a Jolokia REST proxy endpoint"
}
func (jp *JolokiaProxy) Gather(acc telegraf.Accumulator) error {
if jp.gatherer == nil {
jp.gatherer = NewGatherer(jp.createMetrics())
}
if jp.client == nil {
client, err := jp.createClient()
if err != nil {
return err
}
jp.client = client
}
return jp.gatherer.Gather(jp.client, acc)
}
func (jp *JolokiaProxy) createMetrics() []Metric {
var metrics []Metric
for _, config := range jp.Metrics {
metrics = append(metrics, NewMetric(config,
jp.DefaultFieldPrefix, jp.DefaultFieldSeparator, jp.DefaultTagPrefix))
}
return metrics
}
func (jp *JolokiaProxy) createClient() (*Client, error) {
proxyConfig := &ProxyConfig{
DefaultTargetUsername: jp.DefaultTargetUsername,
DefaultTargetPassword: jp.DefaultTargetPassword,
}
for _, target := range jp.Targets {
proxyConfig.Targets = append(proxyConfig.Targets, ProxyTargetConfig{
URL: target.URL,
Username: target.Username,
Password: target.Password,
})
}
return NewClient(jp.URL, &ClientConfig{
Username: jp.Username,
Password: jp.Password,
ResponseTimeout: jp.ResponseTimeout.Duration,
ClientConfig: jp.ClientConfig,
ProxyConfig: proxyConfig,
})
}