From 6a6db3430f7c1c4a6f6b88f249e80e87df9d80dd Mon Sep 17 00:00:00 2001 From: Pavel Yudin Date: Fri, 29 Jan 2016 18:07:07 +0300 Subject: [PATCH] Apply `go fmt` --- plugins/inputs/powerdns/powerdns.go | 136 +++++++++---------- plugins/inputs/powerdns/powerdns_test.go | 161 ++++++++++++----------- 2 files changed, 149 insertions(+), 148 deletions(-) diff --git a/plugins/inputs/powerdns/powerdns.go b/plugins/inputs/powerdns/powerdns.go index e60a6decc..6c0c6882d 100644 --- a/plugins/inputs/powerdns/powerdns.go +++ b/plugins/inputs/powerdns/powerdns.go @@ -1,19 +1,19 @@ package powerdns import ( - "net" - "fmt" - "bufio" - "io" - "time" - "strings" - "strconv" + "bufio" + "fmt" + "io" + "net" + "strconv" + "strings" + "time" - "github.com/influxdata/telegraf/plugins/inputs" + "github.com/influxdata/telegraf/plugins/inputs" ) type Powerdns struct { - UnixSockets []string + UnixSockets []string } var sampleConfig = ` @@ -27,95 +27,95 @@ var sampleConfig = ` var defaultTimeout = 5 * time.Second func (p *Powerdns) SampleConfig() string { - return sampleConfig + return sampleConfig } func (p *Powerdns) Description() string { - return "Read metrics from one or many PowerDNS servers" + return "Read metrics from one or many PowerDNS servers" } func (p *Powerdns) Gather(acc inputs.Accumulator) error { - if len(p.UnixSockets) == 0 { - return p.gatherServer("/var/run/pdns.controlsocket", acc) - } + if len(p.UnixSockets) == 0 { + return p.gatherServer("/var/run/pdns.controlsocket", acc) + } - for _, serverSocket := range p.UnixSockets { - if err := p.gatherServer(serverSocket, acc); err != nil { - return err - } - } + for _, serverSocket := range p.UnixSockets { + if err := p.gatherServer(serverSocket, acc); err != nil { + return err + } + } - return nil + return nil } func (p *Powerdns) gatherServer(address string, acc inputs.Accumulator) error { - conn, err := net.DialTimeout("unix", address, defaultTimeout) - if err != nil { - return err - } + conn, err := net.DialTimeout("unix", address, defaultTimeout) + if err != nil { + return err + } - defer conn.Close() + defer conn.Close() - conn.SetDeadline(time.Now().Add(defaultTimeout)) + conn.SetDeadline(time.Now().Add(defaultTimeout)) - // Read and write buffer - rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn)) + // Read and write buffer + rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn)) - // Send command - if _, err := fmt.Fprint(conn, "show * \n"); err != nil { - return nil - } - if err := rw.Flush(); err != nil { - return err - } + // Send command + if _, err := fmt.Fprint(conn, "show * \n"); err != nil { + return nil + } + if err := rw.Flush(); err != nil { + return err + } - // Read data - buf := make([]byte, 0, 4096) - tmp := make([]byte, 1024) - for { - n, err := rw.Read(tmp) - if err != nil { - if err != io.EOF { - return err - } + // Read data + buf := make([]byte, 0, 4096) + tmp := make([]byte, 1024) + for { + n, err := rw.Read(tmp) + if err != nil { + if err != io.EOF { + return err + } - break - } - buf = append(buf, tmp[:n]...) - } + break + } + buf = append(buf, tmp[:n]...) + } - metrics := string(buf) + metrics := string(buf) - // Process data - fields, err := parseResponse(metrics) - if err != nil { - return err - } + // Process data + fields, err := parseResponse(metrics) + if err != nil { + return err + } - // Add server socket as a tag + // Add server socket as a tag tags := map[string]string{"server": address} - acc.AddFields("powerdns", fields, tags) + acc.AddFields("powerdns", fields, tags) - return nil + return nil } func parseResponse(metrics string) (map[string]interface{}, error) { - values := make(map[string]interface{}) + values := make(map[string]interface{}) - s := strings.Split(metrics, ",") + s := strings.Split(metrics, ",") - for _, metric := range s[:len(s)-1] { - m := strings.Split(metric, "=") + for _, metric := range s[:len(s)-1] { + m := strings.Split(metric, "=") - i, err := strconv.ParseInt(m[1], 10, 64) - if err != nil { - return values, err - } - values[m[0]] = i - } + i, err := strconv.ParseInt(m[1], 10, 64) + if err != nil { + return values, err + } + values[m[0]] = i + } - return values, nil + return values, nil } func init() { diff --git a/plugins/inputs/powerdns/powerdns_test.go b/plugins/inputs/powerdns/powerdns_test.go index e6477f01e..9d44b02bb 100644 --- a/plugins/inputs/powerdns/powerdns_test.go +++ b/plugins/inputs/powerdns/powerdns_test.go @@ -1,11 +1,11 @@ package powerdns import ( + "crypto/rand" + "encoding/binary" + "fmt" + "net" "testing" - "encoding/binary" - "net" - "crypto/rand" - "fmt" "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/assert" @@ -13,62 +13,63 @@ import ( ) type statServer struct{} + var metrics = "corrupt-packets=0,deferred-cache-inserts=0,deferred-cache-lookup=0,dnsupdate-answers=0,dnsupdate-changes=0,dnsupdate-queries=0,dnsupdate-refused=0,packetcache-hit=0,packetcache-miss=1,packetcache-size=0,query-cache-hit=0,query-cache-miss=6,rd-queries=1,recursing-answers=0,recursing-questions=0,recursion-unanswered=0,security-status=3,servfail-packets=0,signatures=0,tcp-answers=0,tcp-queries=0,timedout-packets=0,udp-answers=1,udp-answers-bytes=50,udp-do-queries=0,udp-queries=0,udp4-answers=1,udp4-queries=1,udp6-answers=0,udp6-queries=0,key-cache-size=0,latency=26,meta-cache-size=0,qsize-q=0,signature-cache-size=0,sys-msec=2889,uptime=86317,user-msec=2167," func (s statServer) serverSocket(l net.Listener) { - for { - conn, err := l.Accept() - if err != nil { - return - } + for { + conn, err := l.Accept() + if err != nil { + return + } - go func(c net.Conn) { - buf := make([]byte, 1024) - n, _ := c.Read(buf) + go func(c net.Conn) { + buf := make([]byte, 1024) + n, _ := c.Read(buf) - data := buf[:n] - if string(data) == "show * \n" { - c.Write([]byte(metrics)) - c.Close() - } - }(conn) - } + data := buf[:n] + if string(data) == "show * \n" { + c.Write([]byte(metrics)) + c.Close() + } + }(conn) + } } func TestMemcachedGeneratesMetrics(t *testing.T) { - // We create a fake server to return test data - var randomNumber int64 + // We create a fake server to return test data + var randomNumber int64 binary.Read(rand.Reader, binary.LittleEndian, &randomNumber) - socket, err := net.Listen("unix", fmt.Sprintf("/tmp/pdns%d.controlsocket", randomNumber)) - if err != nil { - t.Fatal("Cannot initalize server on port ") - } + socket, err := net.Listen("unix", fmt.Sprintf("/tmp/pdns%d.controlsocket", randomNumber)) + if err != nil { + t.Fatal("Cannot initalize server on port ") + } - defer socket.Close() + defer socket.Close() - s := statServer{} - go s.serverSocket(socket) + s := statServer{} + go s.serverSocket(socket) - p := &Powerdns{ + p := &Powerdns{ UnixSockets: []string{fmt.Sprintf("/tmp/pdns%d.controlsocket", randomNumber)}, } - var acc testutil.Accumulator + var acc testutil.Accumulator - err = p.Gather(&acc) + err = p.Gather(&acc) require.NoError(t, err) - intMetrics := []string{"corrupt-packets", "deferred-cache-inserts", - "deferred-cache-lookup", "dnsupdate-answers", "dnsupdate-changes", - "dnsupdate-queries", "dnsupdate-refused", "packetcache-hit", - "packetcache-miss", "packetcache-size", "query-cache-hit", "query-cache-miss", - "rd-queries", "recursing-answers", "recursing-questions", - "recursion-unanswered", "security-status", "servfail-packets", "signatures", - "tcp-answers", "tcp-queries", "timedout-packets", "udp-answers", - "udp-answers-bytes", "udp-do-queries", "udp-queries", "udp4-answers", - "udp4-queries", "udp6-answers", "udp6-queries", "key-cache-size", "latency", - "meta-cache-size", "qsize-q", "signature-cache-size", "sys-msec", "uptime", "user-msec"} + intMetrics := []string{"corrupt-packets", "deferred-cache-inserts", + "deferred-cache-lookup", "dnsupdate-answers", "dnsupdate-changes", + "dnsupdate-queries", "dnsupdate-refused", "packetcache-hit", + "packetcache-miss", "packetcache-size", "query-cache-hit", "query-cache-miss", + "rd-queries", "recursing-answers", "recursing-questions", + "recursion-unanswered", "security-status", "servfail-packets", "signatures", + "tcp-answers", "tcp-queries", "timedout-packets", "udp-answers", + "udp-answers-bytes", "udp-do-queries", "udp-queries", "udp4-answers", + "udp4-queries", "udp6-answers", "udp6-queries", "key-cache-size", "latency", + "meta-cache-size", "qsize-q", "signature-cache-size", "sys-msec", "uptime", "user-msec"} for _, metric := range intMetrics { assert.True(t, acc.HasIntField("powerdns", metric), metric) @@ -76,52 +77,52 @@ func TestMemcachedGeneratesMetrics(t *testing.T) { } func TestPowerdnsParseMetrics(t *testing.T) { - values, err := parseResponse(metrics) - require.NoError(t, err, "Error parsing memcached response") + values, err := parseResponse(metrics) + require.NoError(t, err, "Error parsing memcached response") - tests := []struct { + tests := []struct { key string value int64 }{ {"corrupt-packets", 0}, {"deferred-cache-inserts", 0}, {"deferred-cache-lookup", 0}, - {"dnsupdate-answers", 0}, - {"dnsupdate-changes", 0}, - {"dnsupdate-queries", 0}, - {"dnsupdate-refused", 0}, - {"packetcache-hit", 0}, - {"packetcache-miss", 1}, - {"packetcache-size", 0}, - {"query-cache-hit", 0}, - {"query-cache-miss", 6}, - {"rd-queries", 1}, - {"recursing-answers", 0}, - {"recursing-questions", 0}, - {"recursion-unanswered", 0}, - {"security-status", 3}, - {"servfail-packets", 0}, - {"signatures", 0}, - {"tcp-answers", 0}, - {"tcp-queries", 0}, - {"timedout-packets", 0}, - {"udp-answers", 1}, - {"udp-answers-bytes", 50}, - {"udp-do-queries", 0}, - {"udp-queries", 0}, - {"udp4-answers", 1}, - {"udp4-queries", 1}, - {"udp6-answers", 0}, - {"udp6-queries", 0}, - {"key-cache-size", 0}, - {"latency", 26}, - {"meta-cache-size", 0}, - {"qsize-q" ,0}, - {"signature-cache-size", 0}, - {"sys-msec", 2889}, - {"uptime", 86317}, - {"user-msec", 2167}, - } + {"dnsupdate-answers", 0}, + {"dnsupdate-changes", 0}, + {"dnsupdate-queries", 0}, + {"dnsupdate-refused", 0}, + {"packetcache-hit", 0}, + {"packetcache-miss", 1}, + {"packetcache-size", 0}, + {"query-cache-hit", 0}, + {"query-cache-miss", 6}, + {"rd-queries", 1}, + {"recursing-answers", 0}, + {"recursing-questions", 0}, + {"recursion-unanswered", 0}, + {"security-status", 3}, + {"servfail-packets", 0}, + {"signatures", 0}, + {"tcp-answers", 0}, + {"tcp-queries", 0}, + {"timedout-packets", 0}, + {"udp-answers", 1}, + {"udp-answers-bytes", 50}, + {"udp-do-queries", 0}, + {"udp-queries", 0}, + {"udp4-answers", 1}, + {"udp4-queries", 1}, + {"udp6-answers", 0}, + {"udp6-queries", 0}, + {"key-cache-size", 0}, + {"latency", 26}, + {"meta-cache-size", 0}, + {"qsize-q", 0}, + {"signature-cache-size", 0}, + {"sys-msec", 2889}, + {"uptime", 86317}, + {"user-msec", 2167}, + } for _, test := range tests { value, ok := values[test.key]