From f9190fbff937f233046d5418dcb4c4b4e814c971 Mon Sep 17 00:00:00 2001 From: = <=> Date: Wed, 6 Apr 2016 21:05:23 +0200 Subject: [PATCH] #920 Support servers on unix and tcp sockets. --- plugins/inputs/uwsgi/uwsgi.go | 54 ++++++++++++++++-------------- plugins/inputs/uwsgi/uwsgi_test.go | 2 +- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/plugins/inputs/uwsgi/uwsgi.go b/plugins/inputs/uwsgi/uwsgi.go index de5ae7746..70c35d8df 100644 --- a/plugins/inputs/uwsgi/uwsgi.go +++ b/plugins/inputs/uwsgi/uwsgi.go @@ -2,24 +2,19 @@ package uwsgi import ( "encoding/json" + "fmt" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/inputs" - "net/http" + "net" + "net/url" "strconv" "time" ) -var tr = &http.Transport{ - ResponseHeaderTimeout: time.Duration(3 * time.Second), -} - -var client = &http.Client{ - Transport: tr, - Timeout: time.Duration(4 * time.Second), -} +var timeout = 5 * time.Second type Uwsgi struct { - URLs []string `toml:"urls"` + Servers []string `toml:"server"` } func (u *Uwsgi) Description() string { @@ -28,40 +23,49 @@ func (u *Uwsgi) Description() string { func (u *Uwsgi) SampleConfig() string { return ` - ### List with urls of uWSGI Stats servers - urls = [] + ## List with urls of uWSGI Stats servers. Url must match pattern: + ## scheme://address[:port] + ## + ## For example: + ## servers = ["tcp://localhost:5050", "http://localhost:1717", "unix:///tmp/statsock"] + servers = [] ` } func (u *Uwsgi) Gather(acc telegraf.Accumulator) error { - for _, url := range u.URLs { - err := u.gatherURL(acc, url) + for _, s := range u.Servers { + n, err := url.Parse(s) if err != nil { - return err + return fmt.Errorf("Could not parse uWSGI Stats Server url '%s': %s", s, err) } + u.gatherServer(acc, n) + } return nil } -func (u *Uwsgi) gatherURL(acc telegraf.Accumulator, url string) error { - resp, err := client.Get(url) +func (u *Uwsgi) gatherServer(acc telegraf.Accumulator, url *url.URL) error { + var err error + var conn net.Conn + + if url.Scheme == "unix" { + conn, err = net.DialTimeout(url.Scheme, url.Path, timeout) + } else { + conn, err = net.DialTimeout(url.Scheme, url.Host, timeout) + } if err != nil { - return err + return fmt.Errorf("Could not connect to uWSGI Stats Server '%s': %s", url.String(), err) } - defer resp.Body.Close() + defer conn.Close() var s StatsServer - s.Url = url + s.Url = url.String() - dec := json.NewDecoder(resp.Body) + dec := json.NewDecoder(conn) dec.Decode(&s) - if err != nil { - return err - } - u.gatherStatServer(acc, &s) u.gatherWorkers(acc, &s) diff --git a/plugins/inputs/uwsgi/uwsgi_test.go b/plugins/inputs/uwsgi/uwsgi_test.go index 7ee8013d5..9b1eb074e 100644 --- a/plugins/inputs/uwsgi/uwsgi_test.go +++ b/plugins/inputs/uwsgi/uwsgi_test.go @@ -118,7 +118,7 @@ func TestBasic(t *testing.T) { defer fakeServer.Close() plugin := &uwsgi.Uwsgi{ - URLs: []string{fakeServer.URL + "/"}, + Servers: []string{fakeServer.URL + "/"}, } var acc testutil.Accumulator require.NoError(t, plugin.Gather(&acc))