From 5793fc1e651373e77ad4717a9713ca984ec4518b Mon Sep 17 00:00:00 2001 From: Alexander Oleinik Date: Thu, 27 Aug 2015 09:24:26 +0000 Subject: [PATCH 1/7] apache plugin added --- plugins/apache/apache.go | 149 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 plugins/apache/apache.go diff --git a/plugins/apache/apache.go b/plugins/apache/apache.go new file mode 100644 index 000000000..042b3a59d --- /dev/null +++ b/plugins/apache/apache.go @@ -0,0 +1,149 @@ +package apache + +import ( + "bufio" + "fmt" + "net" + "net/http" + "net/url" + "strconv" + "strings" + "sync" + "time" + + "github.com/influxdb/telegraf/plugins" +) + +type ApacheConf struct { + Urls []string +} + +var sampleConfig = ` +# An array of Apache status URI to gather stats. +urls = ["http://localhost/server-status?auto"]` + +func (n *ApacheConf) SampleConfig() string { + return sampleConfig +} + +func (n *ApacheConf) Description() string { + return "Read Apache status information (mod_status)" +} + +func (n *ApacheConf) Gather(acc plugins.Accumulator) error { + var wg sync.WaitGroup + var outerr error + + for _, u := range n.Urls { + addr, err := url.Parse(u) + if err != nil { + return fmt.Errorf("Unable to parse address '%s': %s", u, err) + } + + wg.Add(1) + go func(addr *url.URL) { + defer wg.Done() + outerr = n.gatherUrl(addr, acc) + }(addr) + } + + wg.Wait() + + return outerr +} + +var tr = &http.Transport{ + ResponseHeaderTimeout: time.Duration(3 * time.Second), +} + +var client = &http.Client{Transport: tr} + +func (n *ApacheConf) gatherUrl(addr *url.URL, acc plugins.Accumulator) error { + resp, err := client.Get(addr.String()) + if err != nil { + return fmt.Errorf("error making HTTP request to %s: %s", addr.String(), err) + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("%s returned HTTP status %s", addr.String(), resp.Status) + } + + tags := getTags(addr) + + sc := bufio.NewScanner(resp.Body) + for sc.Scan() { + line := sc.Text() + if strings.Contains(line, ":") { + + parts := strings.SplitN(line, ":", 2) + key, part := strings.Replace(parts[0], " ", "", -1), strings.TrimSpace(parts[1]) + + switch key { + + case "Scoreboard": + n.gatherScores(part, acc) + default: + value, err := strconv.ParseFloat(part, 32) + if err != nil { + continue + } + acc.Add(key, value, tags) + } + } + } + + return nil +} + +func (n *Apache) gatherScores(data string, acc plugins.Accumulator, tags map[string]string) { + + var waiting, open int = 0, 0 + var S, R, W, K, D, C, L, G, I int = 0, 0, 0, 0, 0, 0, 0, 0, 0 + + for _, s := range strings.Split(data, "") { + + switch s { + case "_": waiting++ + case "S": S++ + case "R": R++ + case "W": W++ + case "K": K++ + case "D": D++ + case "C": C++ + case "L": L++ + case "G": G++ + case "I": I++ + case ".": open++ + } + } + + acc.Add("scboard_waiting", float64(waiting), tags); + acc.Add("scboard_starting", float64(S), tags); + acc.Add("scboard_reading", float64(R), tags); + acc.Add("scboard_sending", float64(W), tags); + acc.Add("scboard_keepalive", float64(K), tags); + acc.Add("scboard_dnslookup", float64(D), tags); + acc.Add("scboard_closing", float64(C), tags); + acc.Add("scboard_logging", float64(L), tags); + acc.Add("scboard_finishing", float64(G), tags); + acc.Add("scboard_idle_cleanup", float64(I), tags); + acc.Add("scboard_open", float64(open), tags); +} + +// Get tag(s) for the apache plugin +func getTags(addr *url.URL) map[string]string { + h := addr.Host + var htag string + if host, _, err := net.SplitHostPort(h); err == nil { + htag = host + } else { + htag = h + } + return map[string]string{"server": htag} +} + +func init() { + plugins.Add("apache", func() plugins.Plugin { + return &ApacheConf{} + }) +} From 68ca16c0cc262ef3b41026e4d3ffa2d45b98952a Mon Sep 17 00:00:00 2001 From: Alexander Oleinik Date: Thu, 27 Aug 2015 14:02:35 +0000 Subject: [PATCH 2/7] apache plugin: added to all.go --- plugins/all/all.go | 1 + plugins/apache/apache.go | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/plugins/all/all.go b/plugins/all/all.go index 8670c3d8b..a66ea7f21 100644 --- a/plugins/all/all.go +++ b/plugins/all/all.go @@ -19,4 +19,5 @@ import ( _ "github.com/influxdb/telegraf/plugins/redis" _ "github.com/influxdb/telegraf/plugins/rethinkdb" _ "github.com/influxdb/telegraf/plugins/system" + _ "github.com/influxdb/telegraf/plugins/apache" ) diff --git a/plugins/apache/apache.go b/plugins/apache/apache.go index 042b3a59d..18830e810 100644 --- a/plugins/apache/apache.go +++ b/plugins/apache/apache.go @@ -14,7 +14,7 @@ import ( "github.com/influxdb/telegraf/plugins" ) -type ApacheConf struct { +type Apache struct { Urls []string } @@ -22,15 +22,15 @@ var sampleConfig = ` # An array of Apache status URI to gather stats. urls = ["http://localhost/server-status?auto"]` -func (n *ApacheConf) SampleConfig() string { +func (n *Apache) SampleConfig() string { return sampleConfig } -func (n *ApacheConf) Description() string { +func (n *Apache) Description() string { return "Read Apache status information (mod_status)" } -func (n *ApacheConf) Gather(acc plugins.Accumulator) error { +func (n *Apache) Gather(acc plugins.Accumulator) error { var wg sync.WaitGroup var outerr error @@ -58,7 +58,7 @@ var tr = &http.Transport{ var client = &http.Client{Transport: tr} -func (n *ApacheConf) gatherUrl(addr *url.URL, acc plugins.Accumulator) error { +func (n *Apache) gatherUrl(addr *url.URL, acc plugins.Accumulator) error { resp, err := client.Get(addr.String()) if err != nil { return fmt.Errorf("error making HTTP request to %s: %s", addr.String(), err) @@ -144,6 +144,6 @@ func getTags(addr *url.URL) map[string]string { func init() { plugins.Add("apache", func() plugins.Plugin { - return &ApacheConf{} + return &Apache{} }) } From a1a5a5183a098fa4948a28aa13e77c70fb06cb4c Mon Sep 17 00:00:00 2001 From: KPACHbIuLLIAnO4 Date: Fri, 28 Aug 2015 22:10:38 +0300 Subject: [PATCH 3/7] Update all.go --- plugins/all/all.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/all/all.go b/plugins/all/all.go index a66ea7f21..53051a2e4 100644 --- a/plugins/all/all.go +++ b/plugins/all/all.go @@ -19,5 +19,5 @@ import ( _ "github.com/influxdb/telegraf/plugins/redis" _ "github.com/influxdb/telegraf/plugins/rethinkdb" _ "github.com/influxdb/telegraf/plugins/system" - _ "github.com/influxdb/telegraf/plugins/apache" + _ "github.com/KPACHbIuLLIAnO4/telegraf/plugins/apache" ) From 9acf7f688a596e3b03848aec5dd105aba478aa54 Mon Sep 17 00:00:00 2001 From: Alexander Oleinik Date: Sat, 29 Aug 2015 09:35:00 +0000 Subject: [PATCH 4/7] Fixed apache.go --- plugins/apache/apache.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/apache/apache.go b/plugins/apache/apache.go index 18830e810..ca91acf9d 100644 --- a/plugins/apache/apache.go +++ b/plugins/apache/apache.go @@ -81,7 +81,7 @@ func (n *Apache) gatherUrl(addr *url.URL, acc plugins.Accumulator) error { switch key { case "Scoreboard": - n.gatherScores(part, acc) + n.gatherScores(part, acc, tags) default: value, err := strconv.ParseFloat(part, 32) if err != nil { From cc3f492e62bf2d18db32c635e7076fc950c96fc7 Mon Sep 17 00:00:00 2001 From: Alexander Oleinik Date: Sat, 29 Aug 2015 09:57:02 +0000 Subject: [PATCH 5/7] added newline apache.go --- plugins/apache/apache.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/apache/apache.go b/plugins/apache/apache.go index ca91acf9d..307f84907 100644 --- a/plugins/apache/apache.go +++ b/plugins/apache/apache.go @@ -20,7 +20,8 @@ type Apache struct { var sampleConfig = ` # An array of Apache status URI to gather stats. -urls = ["http://localhost/server-status?auto"]` +urls = ["http://localhost/server-status?auto"] +` func (n *Apache) SampleConfig() string { return sampleConfig From fb7d255b8be512206bbe1bbdddc6e8a0b89276a4 Mon Sep 17 00:00:00 2001 From: Alexander Oleinik Date: Sat, 29 Aug 2015 09:57:40 +0000 Subject: [PATCH 6/7] fixed warning in Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5c27c5d95..7fb52dc7b 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ VERSION := $(shell sh -c 'git describe --always --tags') build: prepare $(GOPATH)/bin/godep go build -o telegraf -ldflags \ - "-X main.Version $(VERSION)" \ + "-X main.Version=$(VERSION)" \ ./cmd/telegraf/telegraf.go prepare: From f993302a20c46d045b49615fe629da3c4e30604e Mon Sep 17 00:00:00 2001 From: Alexander Oleinik Date: Sat, 29 Aug 2015 10:37:58 +0000 Subject: [PATCH 7/7] added apache plugin --- cmd/telegraf/telegraf.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index 38f323215..d502019b5 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -10,7 +10,7 @@ import ( "github.com/influxdb/telegraf" _ "github.com/influxdb/telegraf/outputs/all" - _ "github.com/influxdb/telegraf/plugins/all" + _ "github.com/KPACHbIuLLIAnO4/telegraf/plugins/all" ) var fDebug = flag.Bool("debug", false,