forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tomcat.go
215 lines (181 loc) · 5.09 KB
/
tomcat.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package tomcat
import (
"encoding/xml"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/tls"
"github.com/influxdata/telegraf/plugins/inputs"
)
type TomcatStatus struct {
TomcatJvm TomcatJvm `xml:"jvm"`
TomcatConnectors []TomcatConnector `xml:"connector"`
}
type TomcatJvm struct {
JvmMemory JvmMemoryStat `xml:"memory"`
JvmMemoryPools []JvmMemoryPoolStat `xml:"memorypool"`
}
type JvmMemoryStat struct {
Free int64 `xml:"free,attr"`
Total int64 `xml:"total,attr"`
Max int64 `xml:"max,attr"`
}
type JvmMemoryPoolStat struct {
Name string `xml:"name,attr"`
Type string `xml:"type,attr"`
UsageInit int64 `xml:"usageInit,attr"`
UsageCommitted int64 `xml:"usageCommitted,attr"`
UsageMax int64 `xml:"usageMax,attr"`
UsageUsed int64 `xml:"usageUsed,attr"`
}
type TomcatConnector struct {
Name string `xml:"name,attr"`
ThreadInfo ThreadInfo `xml:"threadInfo"`
RequestInfo RequestInfo `xml:"requestInfo"`
}
type ThreadInfo struct {
MaxThreads int64 `xml:"maxThreads,attr"`
CurrentThreadCount int64 `xml:"currentThreadCount,attr"`
CurrentThreadsBusy int64 `xml:"currentThreadsBusy,attr"`
}
type RequestInfo struct {
MaxTime int `xml:"maxTime,attr"`
ProcessingTime int `xml:"processingTime,attr"`
RequestCount int `xml:"requestCount,attr"`
ErrorCount int `xml:"errorCount,attr"`
BytesReceived int64 `xml:"bytesReceived,attr"`
BytesSent int64 `xml:"bytesSent,attr"`
}
type Tomcat struct {
URL string
Username string
Password string
Timeout internal.Duration
tls.ClientConfig
client *http.Client
request *http.Request
}
var sampleconfig = `
## URL of the Tomcat server status
# url = "http://127.0.0.1:8080/manager/status/all?XML=true"
## HTTP Basic Auth Credentials
# username = "tomcat"
# password = "s3cret"
## Request timeout
# timeout = "5s"
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
`
func (s *Tomcat) Description() string {
return "Gather metrics from the Tomcat server status page."
}
func (s *Tomcat) SampleConfig() string {
return sampleconfig
}
func (s *Tomcat) Gather(acc telegraf.Accumulator) error {
if s.client == nil {
client, err := s.createHttpClient()
if err != nil {
return err
}
s.client = client
}
if s.request == nil {
_, err := url.Parse(s.URL)
if err != nil {
return err
}
request, err := http.NewRequest("GET", s.URL, nil)
if err != nil {
return err
}
request.SetBasicAuth(s.Username, s.Password)
s.request = request
}
resp, err := s.client.Do(s.request)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("received HTTP status code %d from %q; expected 200",
resp.StatusCode, s.URL)
}
var status TomcatStatus
xml.NewDecoder(resp.Body).Decode(&status)
// add tomcat_jvm_memory measurements
tcm := map[string]interface{}{
"free": status.TomcatJvm.JvmMemory.Free,
"total": status.TomcatJvm.JvmMemory.Total,
"max": status.TomcatJvm.JvmMemory.Max,
}
acc.AddFields("tomcat_jvm_memory", tcm, nil)
// add tomcat_jvm_memorypool measurements
for _, mp := range status.TomcatJvm.JvmMemoryPools {
tcmpTags := map[string]string{
"name": mp.Name,
"type": mp.Type,
}
tcmpFields := map[string]interface{}{
"init": mp.UsageInit,
"committed": mp.UsageCommitted,
"max": mp.UsageMax,
"used": mp.UsageUsed,
}
acc.AddFields("tomcat_jvm_memorypool", tcmpFields, tcmpTags)
}
// add tomcat_connector measurements
for _, c := range status.TomcatConnectors {
name, err := strconv.Unquote(c.Name)
if err != nil {
name = c.Name
}
tccTags := map[string]string{
"name": name,
}
tccFields := map[string]interface{}{
"max_threads": c.ThreadInfo.MaxThreads,
"current_thread_count": c.ThreadInfo.CurrentThreadCount,
"current_threads_busy": c.ThreadInfo.CurrentThreadsBusy,
"max_time": c.RequestInfo.MaxTime,
"processing_time": c.RequestInfo.ProcessingTime,
"request_count": c.RequestInfo.RequestCount,
"error_count": c.RequestInfo.ErrorCount,
"bytes_received": c.RequestInfo.BytesReceived,
"bytes_sent": c.RequestInfo.BytesSent,
}
acc.AddFields("tomcat_connector", tccFields, tccTags)
}
return nil
}
func (s *Tomcat) createHttpClient() (*http.Client, error) {
tlsConfig, err := s.ClientConfig.TLSConfig()
if err != nil {
return nil, err
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
Timeout: s.Timeout.Duration,
}
return client, nil
}
func init() {
inputs.Add("tomcat", func() telegraf.Input {
return &Tomcat{
URL: "http://127.0.0.1:8080/manager/status/all?XML=true",
Username: "tomcat",
Password: "s3cret",
Timeout: internal.Duration{Duration: 5 * time.Second},
}
})
}