Apply `go fmt`
This commit is contained in:
parent
d209ad98bd
commit
6a6db3430f
|
@ -1,19 +1,19 @@
|
||||||
package powerdns
|
package powerdns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"bufio"
|
"io"
|
||||||
"io"
|
"net"
|
||||||
"time"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"strconv"
|
"time"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf/plugins/inputs"
|
"github.com/influxdata/telegraf/plugins/inputs"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Powerdns struct {
|
type Powerdns struct {
|
||||||
UnixSockets []string
|
UnixSockets []string
|
||||||
}
|
}
|
||||||
|
|
||||||
var sampleConfig = `
|
var sampleConfig = `
|
||||||
|
@ -27,95 +27,95 @@ var sampleConfig = `
|
||||||
var defaultTimeout = 5 * time.Second
|
var defaultTimeout = 5 * time.Second
|
||||||
|
|
||||||
func (p *Powerdns) SampleConfig() string {
|
func (p *Powerdns) SampleConfig() string {
|
||||||
return sampleConfig
|
return sampleConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Powerdns) Description() string {
|
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 {
|
func (p *Powerdns) Gather(acc inputs.Accumulator) error {
|
||||||
if len(p.UnixSockets) == 0 {
|
if len(p.UnixSockets) == 0 {
|
||||||
return p.gatherServer("/var/run/pdns.controlsocket", acc)
|
return p.gatherServer("/var/run/pdns.controlsocket", acc)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, serverSocket := range p.UnixSockets {
|
for _, serverSocket := range p.UnixSockets {
|
||||||
if err := p.gatherServer(serverSocket, acc); err != nil {
|
if err := p.gatherServer(serverSocket, acc); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Powerdns) gatherServer(address string, acc inputs.Accumulator) error {
|
func (p *Powerdns) gatherServer(address string, acc inputs.Accumulator) error {
|
||||||
conn, err := net.DialTimeout("unix", address, defaultTimeout)
|
conn, err := net.DialTimeout("unix", address, defaultTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
conn.SetDeadline(time.Now().Add(defaultTimeout))
|
conn.SetDeadline(time.Now().Add(defaultTimeout))
|
||||||
|
|
||||||
// Read and write buffer
|
// Read and write buffer
|
||||||
rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
|
rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
|
||||||
|
|
||||||
// Send command
|
// Send command
|
||||||
if _, err := fmt.Fprint(conn, "show * \n"); err != nil {
|
if _, err := fmt.Fprint(conn, "show * \n"); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err := rw.Flush(); err != nil {
|
if err := rw.Flush(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read data
|
// Read data
|
||||||
buf := make([]byte, 0, 4096)
|
buf := make([]byte, 0, 4096)
|
||||||
tmp := make([]byte, 1024)
|
tmp := make([]byte, 1024)
|
||||||
for {
|
for {
|
||||||
n, err := rw.Read(tmp)
|
n, err := rw.Read(tmp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err != io.EOF {
|
if err != io.EOF {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
buf = append(buf, tmp[:n]...)
|
buf = append(buf, tmp[:n]...)
|
||||||
}
|
}
|
||||||
|
|
||||||
metrics := string(buf)
|
metrics := string(buf)
|
||||||
|
|
||||||
// Process data
|
// Process data
|
||||||
fields, err := parseResponse(metrics)
|
fields, err := parseResponse(metrics)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add server socket as a tag
|
// Add server socket as a tag
|
||||||
tags := map[string]string{"server": address}
|
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) {
|
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] {
|
for _, metric := range s[:len(s)-1] {
|
||||||
m := strings.Split(metric, "=")
|
m := strings.Split(metric, "=")
|
||||||
|
|
||||||
i, err := strconv.ParseInt(m[1], 10, 64)
|
i, err := strconv.ParseInt(m[1], 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return values, err
|
return values, err
|
||||||
}
|
}
|
||||||
values[m[0]] = i
|
values[m[0]] = i
|
||||||
}
|
}
|
||||||
|
|
||||||
return values, nil
|
return values, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
package powerdns
|
package powerdns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
"encoding/binary"
|
|
||||||
"net"
|
|
||||||
"crypto/rand"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/influxdata/telegraf/testutil"
|
"github.com/influxdata/telegraf/testutil"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -13,62 +13,63 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type statServer struct{}
|
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,"
|
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) {
|
func (s statServer) serverSocket(l net.Listener) {
|
||||||
|
|
||||||
for {
|
for {
|
||||||
conn, err := l.Accept()
|
conn, err := l.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go func(c net.Conn) {
|
go func(c net.Conn) {
|
||||||
buf := make([]byte, 1024)
|
buf := make([]byte, 1024)
|
||||||
n, _ := c.Read(buf)
|
n, _ := c.Read(buf)
|
||||||
|
|
||||||
data := buf[:n]
|
data := buf[:n]
|
||||||
if string(data) == "show * \n" {
|
if string(data) == "show * \n" {
|
||||||
c.Write([]byte(metrics))
|
c.Write([]byte(metrics))
|
||||||
c.Close()
|
c.Close()
|
||||||
}
|
}
|
||||||
}(conn)
|
}(conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMemcachedGeneratesMetrics(t *testing.T) {
|
func TestMemcachedGeneratesMetrics(t *testing.T) {
|
||||||
// We create a fake server to return test data
|
// We create a fake server to return test data
|
||||||
var randomNumber int64
|
var randomNumber int64
|
||||||
binary.Read(rand.Reader, binary.LittleEndian, &randomNumber)
|
binary.Read(rand.Reader, binary.LittleEndian, &randomNumber)
|
||||||
socket, err := net.Listen("unix", fmt.Sprintf("/tmp/pdns%d.controlsocket", randomNumber))
|
socket, err := net.Listen("unix", fmt.Sprintf("/tmp/pdns%d.controlsocket", randomNumber))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Cannot initalize server on port ")
|
t.Fatal("Cannot initalize server on port ")
|
||||||
}
|
}
|
||||||
|
|
||||||
defer socket.Close()
|
defer socket.Close()
|
||||||
|
|
||||||
s := statServer{}
|
s := statServer{}
|
||||||
go s.serverSocket(socket)
|
go s.serverSocket(socket)
|
||||||
|
|
||||||
p := &Powerdns{
|
p := &Powerdns{
|
||||||
UnixSockets: []string{fmt.Sprintf("/tmp/pdns%d.controlsocket", randomNumber)},
|
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)
|
require.NoError(t, err)
|
||||||
|
|
||||||
intMetrics := []string{"corrupt-packets", "deferred-cache-inserts",
|
intMetrics := []string{"corrupt-packets", "deferred-cache-inserts",
|
||||||
"deferred-cache-lookup", "dnsupdate-answers", "dnsupdate-changes",
|
"deferred-cache-lookup", "dnsupdate-answers", "dnsupdate-changes",
|
||||||
"dnsupdate-queries", "dnsupdate-refused", "packetcache-hit",
|
"dnsupdate-queries", "dnsupdate-refused", "packetcache-hit",
|
||||||
"packetcache-miss", "packetcache-size", "query-cache-hit", "query-cache-miss",
|
"packetcache-miss", "packetcache-size", "query-cache-hit", "query-cache-miss",
|
||||||
"rd-queries", "recursing-answers", "recursing-questions",
|
"rd-queries", "recursing-answers", "recursing-questions",
|
||||||
"recursion-unanswered", "security-status", "servfail-packets", "signatures",
|
"recursion-unanswered", "security-status", "servfail-packets", "signatures",
|
||||||
"tcp-answers", "tcp-queries", "timedout-packets", "udp-answers",
|
"tcp-answers", "tcp-queries", "timedout-packets", "udp-answers",
|
||||||
"udp-answers-bytes", "udp-do-queries", "udp-queries", "udp4-answers",
|
"udp-answers-bytes", "udp-do-queries", "udp-queries", "udp4-answers",
|
||||||
"udp4-queries", "udp6-answers", "udp6-queries", "key-cache-size", "latency",
|
"udp4-queries", "udp6-answers", "udp6-queries", "key-cache-size", "latency",
|
||||||
"meta-cache-size", "qsize-q", "signature-cache-size", "sys-msec", "uptime", "user-msec"}
|
"meta-cache-size", "qsize-q", "signature-cache-size", "sys-msec", "uptime", "user-msec"}
|
||||||
|
|
||||||
for _, metric := range intMetrics {
|
for _, metric := range intMetrics {
|
||||||
assert.True(t, acc.HasIntField("powerdns", metric), metric)
|
assert.True(t, acc.HasIntField("powerdns", metric), metric)
|
||||||
|
@ -76,52 +77,52 @@ func TestMemcachedGeneratesMetrics(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPowerdnsParseMetrics(t *testing.T) {
|
func TestPowerdnsParseMetrics(t *testing.T) {
|
||||||
values, err := parseResponse(metrics)
|
values, err := parseResponse(metrics)
|
||||||
require.NoError(t, err, "Error parsing memcached response")
|
require.NoError(t, err, "Error parsing memcached response")
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
key string
|
key string
|
||||||
value int64
|
value int64
|
||||||
}{
|
}{
|
||||||
{"corrupt-packets", 0},
|
{"corrupt-packets", 0},
|
||||||
{"deferred-cache-inserts", 0},
|
{"deferred-cache-inserts", 0},
|
||||||
{"deferred-cache-lookup", 0},
|
{"deferred-cache-lookup", 0},
|
||||||
{"dnsupdate-answers", 0},
|
{"dnsupdate-answers", 0},
|
||||||
{"dnsupdate-changes", 0},
|
{"dnsupdate-changes", 0},
|
||||||
{"dnsupdate-queries", 0},
|
{"dnsupdate-queries", 0},
|
||||||
{"dnsupdate-refused", 0},
|
{"dnsupdate-refused", 0},
|
||||||
{"packetcache-hit", 0},
|
{"packetcache-hit", 0},
|
||||||
{"packetcache-miss", 1},
|
{"packetcache-miss", 1},
|
||||||
{"packetcache-size", 0},
|
{"packetcache-size", 0},
|
||||||
{"query-cache-hit", 0},
|
{"query-cache-hit", 0},
|
||||||
{"query-cache-miss", 6},
|
{"query-cache-miss", 6},
|
||||||
{"rd-queries", 1},
|
{"rd-queries", 1},
|
||||||
{"recursing-answers", 0},
|
{"recursing-answers", 0},
|
||||||
{"recursing-questions", 0},
|
{"recursing-questions", 0},
|
||||||
{"recursion-unanswered", 0},
|
{"recursion-unanswered", 0},
|
||||||
{"security-status", 3},
|
{"security-status", 3},
|
||||||
{"servfail-packets", 0},
|
{"servfail-packets", 0},
|
||||||
{"signatures", 0},
|
{"signatures", 0},
|
||||||
{"tcp-answers", 0},
|
{"tcp-answers", 0},
|
||||||
{"tcp-queries", 0},
|
{"tcp-queries", 0},
|
||||||
{"timedout-packets", 0},
|
{"timedout-packets", 0},
|
||||||
{"udp-answers", 1},
|
{"udp-answers", 1},
|
||||||
{"udp-answers-bytes", 50},
|
{"udp-answers-bytes", 50},
|
||||||
{"udp-do-queries", 0},
|
{"udp-do-queries", 0},
|
||||||
{"udp-queries", 0},
|
{"udp-queries", 0},
|
||||||
{"udp4-answers", 1},
|
{"udp4-answers", 1},
|
||||||
{"udp4-queries", 1},
|
{"udp4-queries", 1},
|
||||||
{"udp6-answers", 0},
|
{"udp6-answers", 0},
|
||||||
{"udp6-queries", 0},
|
{"udp6-queries", 0},
|
||||||
{"key-cache-size", 0},
|
{"key-cache-size", 0},
|
||||||
{"latency", 26},
|
{"latency", 26},
|
||||||
{"meta-cache-size", 0},
|
{"meta-cache-size", 0},
|
||||||
{"qsize-q" ,0},
|
{"qsize-q", 0},
|
||||||
{"signature-cache-size", 0},
|
{"signature-cache-size", 0},
|
||||||
{"sys-msec", 2889},
|
{"sys-msec", 2889},
|
||||||
{"uptime", 86317},
|
{"uptime", 86317},
|
||||||
{"user-msec", 2167},
|
{"user-msec", 2167},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
value, ok := values[test.key]
|
value, ok := values[test.key]
|
||||||
|
|
Loading…
Reference in New Issue