done code for tomcat plugin

This commit is contained in:
Ulric Qin 2022-04-28 11:47:28 +08:00
parent 9aa13e35ec
commit 1860337c47
1 changed files with 167 additions and 0 deletions

View File

@ -1,7 +1,12 @@
package tomcat
import (
"encoding/xml"
"errors"
"log"
"net/http"
"net/url"
"strconv"
"sync"
"sync/atomic"
"time"
@ -15,6 +20,51 @@ import (
const inputName = "tomcat"
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 Instance struct {
URL string `toml:"url"`
Username string `toml:"username"`
@ -24,6 +74,8 @@ type Instance struct {
IntervalTimes int64 `toml:"interval_times"`
tls.ClientConfig
client *http.Client
request *http.Request
}
func (ins *Instance) Init() error {
@ -31,9 +83,55 @@ func (ins *Instance) Init() error {
return errors.New("url is blank")
}
if ins.Timeout <= 0 {
ins.Timeout = config.Duration(time.Second * 3)
}
client, err := ins.createHTTPClient()
if err != nil {
return err
}
ins.client = client
_, err = url.Parse(ins.URL)
if err != nil {
return err
}
request, err := http.NewRequest("GET", ins.URL, nil)
if err != nil {
return err
}
if ins.Username != "" && ins.Password != "" {
request.SetBasicAuth(ins.Username, ins.Password)
}
ins.request = request
return nil
}
func (ins *Instance) createHTTPClient() (*http.Client, error) {
trans := &http.Transport{}
if ins.UseTLS {
tlsConfig, err := ins.ClientConfig.TLSConfig()
if err != nil {
return nil, err
}
trans.TLSClientConfig = tlsConfig
}
client := &http.Client{
Transport: trans,
Timeout: time.Duration(ins.Timeout),
}
return client, nil
}
type Tomcat struct {
Interval config.Duration `toml:"interval"`
Instances []*Instance `toml:"instances"`
@ -106,5 +204,74 @@ func (t *Tomcat) gatherOnce(slist *list.SafeList, ins *Instance) {
}(begun)
// url cannot connect? up = 0
resp, err := ins.client.Do(ins.request)
if err != nil {
slist.PushFront(inputs.NewSample("up", 0, tags))
log.Println("E! failed to query tomcat url:", err)
return
}
if resp.StatusCode != http.StatusOK {
slist.PushFront(inputs.NewSample("up", 0, tags))
log.Println("E! received HTTP status code:", resp.StatusCode, "expected: 200")
return
}
defer resp.Body.Close()
var status TomcatStatus
if err := xml.NewDecoder(resp.Body).Decode(&status); err != nil {
slist.PushFront(inputs.NewSample("up", 0, tags))
log.Println("E! failed to decode response body:", err)
return
}
slist.PushFront(inputs.NewSample("up", 1, tags))
slist.PushFront(inputs.NewSample("jvm_memory_free", status.TomcatJvm.JvmMemory.Free, tags))
slist.PushFront(inputs.NewSample("jvm_memory_total", status.TomcatJvm.JvmMemory.Total, tags))
slist.PushFront(inputs.NewSample("jvm_memory_max", status.TomcatJvm.JvmMemory.Max, tags))
// 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{}{
"jvm_memorypool_init": mp.UsageInit,
"jvm_memorypool_committed": mp.UsageCommitted,
"jvm_memorypool_max": mp.UsageMax,
"jvm_memorypool_used": mp.UsageUsed,
}
inputs.PushSamples(slist, tcmpFields, tags, 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{}{
"connector_max_threads": c.ThreadInfo.MaxThreads,
"connector_current_thread_count": c.ThreadInfo.CurrentThreadCount,
"connector_current_threads_busy": c.ThreadInfo.CurrentThreadsBusy,
"connector_max_time": c.RequestInfo.MaxTime,
"connector_processing_time": c.RequestInfo.ProcessingTime,
"connector_request_count": c.RequestInfo.RequestCount,
"connector_error_count": c.RequestInfo.ErrorCount,
"connector_bytes_received": c.RequestInfo.BytesReceived,
"connector_bytes_sent": c.RequestInfo.BytesSent,
}
inputs.PushSamples(slist, tccFields, tags, tccTags)
}
}