From 9aa13e35ec4da546adc1b37bbaab8e0d2c654221 Mon Sep 17 00:00:00 2001 From: Ulric Qin Date: Thu, 28 Apr 2022 10:14:49 +0800 Subject: [PATCH] add tomcat plugin framework --- agent/agent.go | 1 + conf/input.tomcat/tomcat.toml | 29 +++++++++ inputs/tomcat/tomcat.go | 110 ++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 conf/input.tomcat/tomcat.toml create mode 100644 inputs/tomcat/tomcat.go diff --git a/agent/agent.go b/agent/agent.go index e26bad9..9733bdf 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -34,6 +34,7 @@ import ( _ "flashcat.cloud/categraf/inputs/procstat" _ "flashcat.cloud/categraf/inputs/redis" _ "flashcat.cloud/categraf/inputs/system" + _ "flashcat.cloud/categraf/inputs/tomcat" ) type Agent struct { diff --git a/conf/input.tomcat/tomcat.toml b/conf/input.tomcat/tomcat.toml new file mode 100644 index 0000000..25ffd7c --- /dev/null +++ b/conf/input.tomcat/tomcat.toml @@ -0,0 +1,29 @@ +# # collect interval +# interval = 15 + +# Gather metrics from the Tomcat server status page. +[[instances]] +## 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" + +# # interval = global.interval * interval_times +# interval_times = 1 + +# important! use global unique string to specify instance +# labels = { instance="192.168.1.2:8080", service="tomcat" } + +## Optional TLS Config +# use_tls = false +# tls_min_version = "1.2" +# tls_ca = "/etc/categraf/ca.pem" +# tls_cert = "/etc/categraf/cert.pem" +# tls_key = "/etc/categraf/key.pem" +## Use TLS but skip chain & host verification +# insecure_skip_verify = true diff --git a/inputs/tomcat/tomcat.go b/inputs/tomcat/tomcat.go new file mode 100644 index 0000000..39242a5 --- /dev/null +++ b/inputs/tomcat/tomcat.go @@ -0,0 +1,110 @@ +package tomcat + +import ( + "errors" + "sync" + "sync/atomic" + "time" + + "flashcat.cloud/categraf/config" + "flashcat.cloud/categraf/inputs" + "flashcat.cloud/categraf/pkg/tls" + "flashcat.cloud/categraf/types" + "github.com/toolkits/pkg/container/list" +) + +const inputName = "tomcat" + +type Instance struct { + URL string `toml:"url"` + Username string `toml:"username"` + Password string `toml:"password"` + Timeout config.Duration `toml:"timeout"` + Labels map[string]string `toml:"labels"` + IntervalTimes int64 `toml:"interval_times"` + + tls.ClientConfig +} + +func (ins *Instance) Init() error { + if ins.URL == "" { + return errors.New("url is blank") + } + + return nil +} + +type Tomcat struct { + Interval config.Duration `toml:"interval"` + Instances []*Instance `toml:"instances"` + + Counter uint64 + wg sync.WaitGroup +} + +func init() { + inputs.Add(inputName, func() inputs.Input { + return &Tomcat{} + }) +} + +func (t *Tomcat) GetInputName() string { + return inputName +} + +func (t *Tomcat) GetInterval() config.Duration { + return t.Interval +} + +func (t *Tomcat) Init() error { + if len(t.Instances) == 0 { + return types.ErrInstancesEmpty + } + + for i := 0; i < len(t.Instances); i++ { + if err := t.Instances[i].Init(); err != nil { + return err + } + } + + return nil +} + +func (t *Tomcat) Drop() {} + +func (t *Tomcat) Gather(slist *list.SafeList) { + atomic.AddUint64(&t.Counter, 1) + for i := range t.Instances { + ins := t.Instances[i] + t.wg.Add(1) + go t.gatherOnce(slist, ins) + } + t.wg.Wait() +} + +func (t *Tomcat) gatherOnce(slist *list.SafeList, ins *Instance) { + defer t.wg.Done() + + if ins.IntervalTimes > 0 { + counter := atomic.LoadUint64(&t.Counter) + if counter%uint64(ins.IntervalTimes) != 0 { + return + } + } + + tags := map[string]string{"url": ins.URL} + for k, v := range ins.Labels { + tags[k] = v + } + + begun := time.Now() + + // scrape use seconds + defer func(begun time.Time) { + use := time.Since(begun).Seconds() + slist.PushFront(inputs.NewSample("scrape_use_seconds", use, tags)) + }(begun) + + // url cannot connect? up = 0 + +}