Add error status handle to tomcat input

This commit is contained in:
Daniel Nelson 2017-08-16 15:33:47 -07:00
parent cd52747ced
commit 3aa8e61e21
3 changed files with 135 additions and 87 deletions

View File

@ -83,6 +83,7 @@ import (
_ "github.com/influxdata/telegraf/plugins/inputs/system" _ "github.com/influxdata/telegraf/plugins/inputs/system"
_ "github.com/influxdata/telegraf/plugins/inputs/tail" _ "github.com/influxdata/telegraf/plugins/inputs/tail"
_ "github.com/influxdata/telegraf/plugins/inputs/tcp_listener" _ "github.com/influxdata/telegraf/plugins/inputs/tcp_listener"
_ "github.com/influxdata/telegraf/plugins/inputs/tomcat"
_ "github.com/influxdata/telegraf/plugins/inputs/trig" _ "github.com/influxdata/telegraf/plugins/inputs/trig"
_ "github.com/influxdata/telegraf/plugins/inputs/twemproxy" _ "github.com/influxdata/telegraf/plugins/inputs/twemproxy"
_ "github.com/influxdata/telegraf/plugins/inputs/udp_listener" _ "github.com/influxdata/telegraf/plugins/inputs/udp_listener"

View File

@ -1,73 +1,77 @@
# Tomcat Input Plugin # Tomcat Input Plugin
The Tomcat plugin collects statistics available from the tomcat manager status page from the `http://<host>/manager/status/all?XML=true URL.` The Tomcat plugin collects statistics available from the tomcat manager status page from the `http://<host>/manager/status/all?XML=true URL.` (`XML=true` will return only xml data).
(`XML=true` will return only xml data). See the [Tomcat documentation](https://tomcat.apache.org/tomcat-9.0-doc/manager-howto.html#Server_Status) for details of these statistics.
See the [Tomcat documentation](https://tomcat.apache.org/tomcat-9.0-doc/manager-howto.html#Server_Status) for details of these statistics.
### Configuration: ### Configuration:
```toml ```toml
# A Telegraf plugin to collect tomcat metrics. # Gather metrics from the Tomcat server status page.
[[inputs.tomcat]] [[inputs.tomcat]]
# A Tomcat status URI to gather stats. ## URL of the Tomcat server status
# Default is "http://127.0.0.1:8080/manager/status/all?XML=true". # url = "http://127.0.0.1:8080/manager/status/all?XML=true"
url = "http://127.0.0.1:8080/manager/status/all?XML=true"
# Credentials for status URI. ## HTTP Basic Auth Credentials
# Default is tomcat/s3cret. # username = "tomcat"
username = "tomcat" # password = "s3cret"
password = "s3cret"
## Request timeout
# timeout = "5s"
## Optional SSL Config
# ssl_ca = "/etc/telegraf/ca.pem"
# ssl_cert = "/etc/telegraf/cert.pem"
# ssl_key = "/etc/telegraf/key.pem"
## Use SSL but skip chain & host verification
# insecure_skip_verify = false
``` ```
### Measurements & Fields: ### Measurements & Fields:
- tomcat\_jvm\_memory - tomcat_jvm_memory
- free - free
- total - total
- max - max
- tomcat\_jvm\_memorypool - tomcat_jvm_memorypool
- max\_threads - max_threads
- current\_thread\_count - current_thread_count
- current\_threads\_busy - current_threads_busy
- max\_time - max_time
- processing\_time - processing_time
- request\_count - request_count
- error\_count - error_count
- bytes\_received - bytes_received
- bytes\_sent - bytes_sent
- tomcat\_connector - tomcat_connector
- max\_threads - max_threads
- current\_thread\_count - current_thread_count
- current\_thread\_busy - current_thread_busy
- max\_time - max_time
- processing\_time - processing_time
- request\_count - request_count
- error\_count - error_count
- bytes\_received - bytes_received
- bytes\_sent - bytes_sent
### Tags: ### Tags:
- tomcat\_jvm\_memorypool has the following tags: - tomcat_jvm_memorypool has the following tags:
- name - name
- type - type
- tomcat\_connector - tomcat_connector
- name - name
### Sample Queries:
TODO
### Example Output: ### Example Output:
``` ```
$ ./telegraf -config telegraf.conf -input-filter tomcat -test tomcat_jvm_memory,host=N8-MBP free=20014352i,max=127729664i,total=41459712i 1474663361000000000
* Plugin: tomcat, Collection 1 tomcat_jvm_memorypool,host=N8-MBP,name=Eden\ Space,type=Heap\ memory committed=11534336i,init=2228224i,max=35258368i,used=1941200i 1474663361000000000
> tomcat_jvm_memory,host=N8-MBP free=20014352i,max=127729664i,total=41459712i 1474663361000000000 tomcat_jvm_memorypool,host=N8-MBP,name=Survivor\ Space,type=Heap\ memory committed=1376256i,init=262144i,max=4390912i,used=1376248i 1474663361000000000
> tomcat_jvm_memorypool,host=N8-MBP,name=Eden\ Space,type=Heap\ memory committed=11534336i,init=2228224i,max=35258368i,used=1941200i 1474663361000000000 tomcat_jvm_memorypool,host=N8-MBP,name=Tenured\ Gen,type=Heap\ memory committed=28549120i,init=5636096i,max=88080384i,used=18127912i 1474663361000000000
> tomcat_jvm_memorypool,host=N8-MBP,name=Survivor\ Space,type=Heap\ memory committed=1376256i,init=262144i,max=4390912i,used=1376248i 1474663361000000000 tomcat_jvm_memorypool,host=N8-MBP,name=Code\ Cache,type=Non-heap\ memory committed=6946816i,init=2555904i,max=251658240i,used=6406528i 1474663361000000000
> tomcat_jvm_memorypool,host=N8-MBP,name=Tenured\ Gen,type=Heap\ memory committed=28549120i,init=5636096i,max=88080384i,used=18127912i 1474663361000000000 tomcat_jvm_memorypool,host=N8-MBP,name=Compressed\ Class\ Space,type=Non-heap\ memory committed=1966080i,init=0i,max=1073741824i,used=1816120i 1474663361000000000
> tomcat_jvm_memorypool,host=N8-MBP,name=Code\ Cache,type=Non-heap\ memory committed=6946816i,init=2555904i,max=251658240i,used=6406528i 1474663361000000000 tomcat_jvm_memorypool,host=N8-MBP,name=Metaspace,type=Non-heap\ memory committed=18219008i,init=0i,max=-1i,used=17559376i 1474663361000000000
> tomcat_jvm_memorypool,host=N8-MBP,name=Compressed\ Class\ Space,type=Non-heap\ memory committed=1966080i,init=0i,max=1073741824i,used=1816120i 1474663361000000000 tomcat_connector,host=N8-MBP,name=ajp-bio-8009 bytes_received=0i,bytes_sent=0i,current_thread_count=0i,current_threads_busy=0i,error_count=0i,max_threads=200i,max_time=0i,processing_time=0i,request_count=0i 1474663361000000000
> tomcat_jvm_memorypool,host=N8-MBP,name=Metaspace,type=Non-heap\ memory committed=18219008i,init=0i,max=-1i,used=17559376i 1474663361000000000 tomcat_connector,host=N8-MBP,name=http-bio-8080 bytes_received=0i,bytes_sent=86435i,current_thread_count=10i,current_threads_busy=1i,error_count=2i,max_threads=200i,max_time=167i,processing_time=245i,request_count=15i 1474663361000000000
> tomcat_connector,host=N8-MBP,name=ajp-bio-8009 bytes_received=0i,bytes_sent=0i,current_thread_count=0i,current_threads_busy=0i,error_count=0i,max_threads=200i,max_time=0i,processing_time=0i,request_count=0i 1474663361000000000
> tomcat_connector,host=N8-MBP,name=http-bio-8080 bytes_received=0i,bytes_sent=86435i,current_thread_count=10i,current_threads_busy=1i,error_count=2i,max_threads=200i,max_time=167i,processing_time=245i,request_count=15i 1474663361000000000
``` ```

View File

@ -3,12 +3,13 @@ package tomcat
import ( import (
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
"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"
) )
@ -61,20 +62,38 @@ type Tomcat struct {
URL string URL string
Username string Username string
Password string Password string
Timeout internal.Duration
SSLCA string `toml:"ssl_ca"`
SSLCert string `toml:"ssl_cert"`
SSLKey string `toml:"ssl_key"`
InsecureSkipVerify bool
client *http.Client
request *http.Request
} }
var sampleconfig = ` var sampleconfig = `
## A Tomcat status URI to gather stats. ## URL of the Tomcat server status
## Default is "http://127.0.0.1:8080/manager/status/all?XML=true". # url = "http://127.0.0.1:8080/manager/status/all?XML=true"
url = "http://127.0.0.1:8080/manager/status/all?XML=true"
## Credentials for status URI. ## HTTP Basic Auth Credentials
## Default is tomcat/s3cret. # username = "tomcat"
username = "tomcat" # password = "s3cret"
password = "s3cret"
## Request timeout
# timeout = "5s"
## Optional SSL Config
# ssl_ca = "/etc/telegraf/ca.pem"
# ssl_cert = "/etc/telegraf/cert.pem"
# ssl_key = "/etc/telegraf/key.pem"
## Use SSL but skip chain & host verification
# insecure_skip_verify = false
` `
func (s *Tomcat) Description() string { func (s *Tomcat) Description() string {
return "A Telegraf plugin to collect tomcat metrics." return "Gather metrics from the Tomcat server status page."
} }
func (s *Tomcat) SampleConfig() string { func (s *Tomcat) SampleConfig() string {
@ -82,36 +101,40 @@ func (s *Tomcat) SampleConfig() string {
} }
func (s *Tomcat) Gather(acc telegraf.Accumulator) error { func (s *Tomcat) Gather(acc telegraf.Accumulator) error {
if s.client == nil {
if s.URL == "" { client, err := s.createHttpClient()
s.URL = "http://127.0.0.1:8080/manager/status/all?XML=true" if err != nil {
return err
}
s.client = client
} }
if s.Username == "" { if s.request == nil {
s.Username = "tomcat" _, 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
} }
if s.Password == "" { resp, err := s.client.Do(s.request)
s.Password = "s3cret"
}
_, err := url.Parse(s.URL)
if err != nil { if err != nil {
return fmt.Errorf("Unable to parse address '%s': %s", s.URL, err) return err
}
req, err := http.NewRequest("GET", s.URL, nil)
req.SetBasicAuth(s.Username, s.Password)
cli := &http.Client{}
resp, err := cli.Do(req)
if err != nil {
return fmt.Errorf("Unable to call URL '%s': %s", s.URL, err)
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("received HTTP status code %d from %q; expected 200",
resp.StatusCode, s.URL)
}
var status TomcatStatus var status TomcatStatus
xml.Unmarshal(body, &status) xml.NewDecoder(resp.Body).Decode(&status)
// add tomcat_jvm_memory measurements // add tomcat_jvm_memory measurements
tcm := map[string]interface{}{ tcm := map[string]interface{}{
@ -123,7 +146,6 @@ func (s *Tomcat) Gather(acc telegraf.Accumulator) error {
// add tomcat_jvm_memorypool measurements // add tomcat_jvm_memorypool measurements
for _, mp := range status.TomcatJvm.JvmMemoryPools { for _, mp := range status.TomcatJvm.JvmMemoryPools {
tcmpTags := map[string]string{ tcmpTags := map[string]string{
"name": mp.Name, "name": mp.Name,
"type": mp.Type, "type": mp.Type,
@ -137,12 +159,10 @@ func (s *Tomcat) Gather(acc telegraf.Accumulator) error {
} }
acc.AddFields("tomcat_jvm_memorypool", tcmpFields, tcmpTags) acc.AddFields("tomcat_jvm_memorypool", tcmpFields, tcmpTags)
} }
// add tomcat_connector measurements // add tomcat_connector measurements
for _, c := range status.TomcatConnectors { for _, c := range status.TomcatConnectors {
name, err := strconv.Unquote(c.Name) name, err := strconv.Unquote(c.Name)
if err != nil { if err != nil {
return fmt.Errorf("Unable to unquote name '%s': %s", c.Name, err) return fmt.Errorf("Unable to unquote name '%s': %s", c.Name, err)
@ -165,12 +185,35 @@ func (s *Tomcat) Gather(acc telegraf.Accumulator) error {
} }
acc.AddFields("tomcat_connector", tccFields, tccTags) acc.AddFields("tomcat_connector", tccFields, tccTags)
} }
return nil return nil
} }
func init() { func (s *Tomcat) createHttpClient() (*http.Client, error) {
inputs.Add("tomcat", func() telegraf.Input { return &Tomcat{} }) tlsConfig, err := internal.GetTLSConfig(
s.SSLCert, s.SSLKey, s.SSLCA, s.InsecureSkipVerify)
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},
}
})
} }