|
| 1 | +package tomcat |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/xml" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "strconv" |
| 10 | + |
| 11 | + "github.com/influxdata/telegraf" |
| 12 | + "github.com/influxdata/telegraf/plugins/inputs" |
| 13 | +) |
| 14 | + |
| 15 | +type TomcatStatus struct { |
| 16 | + TomcatJvm TomcatJvm `xml:"jvm"` |
| 17 | + TomcatConnectors []TomcatConnector `xml:"connector"` |
| 18 | +} |
| 19 | + |
| 20 | +type TomcatJvm struct { |
| 21 | + JvmMemory JvmMemoryStat `xml:"memory"` |
| 22 | + JvmMemoryPools []JvmMemoryPoolStat `xml:"memorypool"` |
| 23 | +} |
| 24 | + |
| 25 | +type JvmMemoryStat struct { |
| 26 | + Free int64 `xml:"free,attr"` |
| 27 | + Total int64 `xml:"total,attr"` |
| 28 | + Max int64 `xml:"max,attr"` |
| 29 | +} |
| 30 | + |
| 31 | +type JvmMemoryPoolStat struct { |
| 32 | + Name string `xml:"name,attr"` |
| 33 | + Type string `xml:"type,attr"` |
| 34 | + UsageInit int64 `xml:"usageInit,attr"` |
| 35 | + UsageCommitted int64 `xml:"usageCommitted,attr"` |
| 36 | + UsageMax int64 `xml:"usageMax,attr"` |
| 37 | + UsageUsed int64 `xml:"usageUsed,attr"` |
| 38 | +} |
| 39 | + |
| 40 | +type TomcatConnector struct { |
| 41 | + Name string `xml:"name,attr"` |
| 42 | + ThreadInfo ThreadInfo `xml:"threadInfo"` |
| 43 | + RequestInfo RequestInfo `xml:"requestInfo"` |
| 44 | +} |
| 45 | + |
| 46 | +type ThreadInfo struct { |
| 47 | + MaxThreads int64 `xml:"maxThreads,attr"` |
| 48 | + CurrentThreadCount int64 `xml:"currentThreadCount,attr"` |
| 49 | + CurrentThreadsBusy int64 `xml:"currentThreadsBusy,attr"` |
| 50 | +} |
| 51 | +type RequestInfo struct { |
| 52 | + MaxTime int `xml:"maxTime,attr"` |
| 53 | + ProcessingTime int `xml:"processingTime,attr"` |
| 54 | + RequestCount int `xml:"requestCount,attr"` |
| 55 | + ErrorCount int `xml:"errorCount,attr"` |
| 56 | + BytesReceived int64 `xml:"bytesReceived,attr"` |
| 57 | + BytesSent int64 `xml:"bytesSent,attr"` |
| 58 | +} |
| 59 | + |
| 60 | +type Tomcat struct { |
| 61 | + URL string |
| 62 | + Username string |
| 63 | + Password string |
| 64 | +} |
| 65 | + |
| 66 | +var sampleconfig = ` |
| 67 | + ## A Tomcat status URI to gather stats. |
| 68 | + ## Default is "http://127.0.0.1:8080/manager/status/all?XML=true". |
| 69 | + url = "http://127.0.0.1:8080/manager/status/all?XML=true" |
| 70 | + ## Credentials for status URI. |
| 71 | + ## Default is tomcat/s3cret. |
| 72 | + username = "tomcat" |
| 73 | + password = "s3cret" |
| 74 | +` |
| 75 | + |
| 76 | +func (s *Tomcat) Description() string { |
| 77 | + return "A Telegraf plugin to collect tomcat metrics." |
| 78 | +} |
| 79 | + |
| 80 | +func (s *Tomcat) SampleConfig() string { |
| 81 | + return sampleconfig |
| 82 | +} |
| 83 | + |
| 84 | +func (s *Tomcat) Gather(acc telegraf.Accumulator) error { |
| 85 | + |
| 86 | + if s.URL == "" { |
| 87 | + s.URL = "http://127.0.0.1:8080/manager/status/all?XML=true" |
| 88 | + } |
| 89 | + |
| 90 | + if s.Username == "" { |
| 91 | + s.Username = "tomcat" |
| 92 | + } |
| 93 | + |
| 94 | + if s.Password == "" { |
| 95 | + s.Password = "s3cret" |
| 96 | + } |
| 97 | + |
| 98 | + _, err := url.Parse(s.URL) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("Unable to parse address '%s': %s", s.URL, err) |
| 101 | + } |
| 102 | + |
| 103 | + req, err := http.NewRequest("GET", s.URL, nil) |
| 104 | + req.SetBasicAuth(s.Username, s.Password) |
| 105 | + cli := &http.Client{} |
| 106 | + resp, err := cli.Do(req) |
| 107 | + if err != nil { |
| 108 | + return fmt.Errorf("Unable to call URL '%s': %s", s.URL, err) |
| 109 | + } |
| 110 | + defer resp.Body.Close() |
| 111 | + body, err := ioutil.ReadAll(resp.Body) |
| 112 | + |
| 113 | + var status TomcatStatus |
| 114 | + xml.Unmarshal(body, &status) |
| 115 | + |
| 116 | + // add tomcat_jvm_memory measurements |
| 117 | + tcm := map[string]interface{}{ |
| 118 | + "free": status.TomcatJvm.JvmMemory.Free, |
| 119 | + "total": status.TomcatJvm.JvmMemory.Total, |
| 120 | + "max": status.TomcatJvm.JvmMemory.Max, |
| 121 | + } |
| 122 | + acc.AddFields("tomcat_jvm_memory", tcm, nil) |
| 123 | + |
| 124 | + // add tomcat_jvm_memorypool measurements |
| 125 | + for _, mp := range status.TomcatJvm.JvmMemoryPools { |
| 126 | + |
| 127 | + tcmpTags := map[string]string{ |
| 128 | + "name": mp.Name, |
| 129 | + "type": mp.Type, |
| 130 | + } |
| 131 | + |
| 132 | + tcmpFields := map[string]interface{}{ |
| 133 | + "init": mp.UsageInit, |
| 134 | + "committed": mp.UsageCommitted, |
| 135 | + "max": mp.UsageMax, |
| 136 | + "used": mp.UsageUsed, |
| 137 | + } |
| 138 | + |
| 139 | + acc.AddFields("tomcat_jvm_memorypool", tcmpFields, tcmpTags) |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + // add tomcat_connector measurements |
| 144 | + for _, c := range status.TomcatConnectors { |
| 145 | + |
| 146 | + name, err := strconv.Unquote(c.Name) |
| 147 | + if err != nil { |
| 148 | + return fmt.Errorf("Unable to unquote name '%s': %s", c.Name, err) |
| 149 | + } |
| 150 | + |
| 151 | + tccTags := map[string]string{ |
| 152 | + "name": name, |
| 153 | + } |
| 154 | + |
| 155 | + tccFields := map[string]interface{}{ |
| 156 | + "max_threads": c.ThreadInfo.MaxThreads, |
| 157 | + "current_thread_count": c.ThreadInfo.CurrentThreadCount, |
| 158 | + "current_threads_busy": c.ThreadInfo.CurrentThreadsBusy, |
| 159 | + "max_time": c.RequestInfo.MaxTime, |
| 160 | + "processing_time": c.RequestInfo.ProcessingTime, |
| 161 | + "request_count": c.RequestInfo.RequestCount, |
| 162 | + "error_count": c.RequestInfo.ErrorCount, |
| 163 | + "bytes_received": c.RequestInfo.BytesReceived, |
| 164 | + "bytes_sent": c.RequestInfo.BytesSent, |
| 165 | + } |
| 166 | + |
| 167 | + acc.AddFields("tomcat_connector", tccFields, tccTags) |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | + return nil |
| 172 | +} |
| 173 | + |
| 174 | +func init() { |
| 175 | + inputs.Add("tomcat", func() telegraf.Input { return &Tomcat{} }) |
| 176 | +} |
0 commit comments