#920 Support servers on unix and tcp sockets.

This commit is contained in:
= 2016-04-06 21:05:23 +02:00 committed by David Arnold
parent e712b031c7
commit a047bb7319
2 changed files with 30 additions and 26 deletions

View File

@ -2,24 +2,19 @@ package uwsgi
import ( import (
"encoding/json" "encoding/json"
"fmt"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
"net/http" "net"
"net/url"
"strconv" "strconv"
"time" "time"
) )
var tr = &http.Transport{ var timeout = 5 * time.Second
ResponseHeaderTimeout: time.Duration(3 * time.Second),
}
var client = &http.Client{
Transport: tr,
Timeout: time.Duration(4 * time.Second),
}
type Uwsgi struct { type Uwsgi struct {
URLs []string `toml:"urls"` Servers []string `toml:"server"`
} }
func (u *Uwsgi) Description() string { func (u *Uwsgi) Description() string {
@ -28,40 +23,49 @@ func (u *Uwsgi) Description() string {
func (u *Uwsgi) SampleConfig() string { func (u *Uwsgi) SampleConfig() string {
return ` return `
### List with urls of uWSGI Stats servers ## List with urls of uWSGI Stats servers. Url must match pattern:
urls = [] ## scheme://address[:port]
##
## For example:
## servers = ["tcp://localhost:5050", "http://localhost:1717", "unix:///tmp/statsock"]
servers = []
` `
} }
func (u *Uwsgi) Gather(acc telegraf.Accumulator) error { func (u *Uwsgi) Gather(acc telegraf.Accumulator) error {
for _, url := range u.URLs { for _, s := range u.Servers {
err := u.gatherURL(acc, url) n, err := url.Parse(s)
if err != nil { 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 return nil
} }
func (u *Uwsgi) gatherURL(acc telegraf.Accumulator, url string) error { func (u *Uwsgi) gatherServer(acc telegraf.Accumulator, url *url.URL) error {
resp, err := client.Get(url) 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 { 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 var s StatsServer
s.Url = url s.Url = url.String()
dec := json.NewDecoder(resp.Body) dec := json.NewDecoder(conn)
dec.Decode(&s) dec.Decode(&s)
if err != nil {
return err
}
u.gatherStatServer(acc, &s) u.gatherStatServer(acc, &s)
u.gatherWorkers(acc, &s) u.gatherWorkers(acc, &s)

View File

@ -118,7 +118,7 @@ func TestBasic(t *testing.T) {
defer fakeServer.Close() defer fakeServer.Close()
plugin := &uwsgi.Uwsgi{ plugin := &uwsgi.Uwsgi{
URLs: []string{fakeServer.URL + "/"}, Servers: []string{fakeServer.URL + "/"},
} }
var acc testutil.Accumulator var acc testutil.Accumulator
require.NoError(t, plugin.Gather(&acc)) require.NoError(t, plugin.Gather(&acc))