2015-10-13 23:15:39 +00:00
|
|
|
package zookeeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-10-13 23:15:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Zookeeper is a zookeeper plugin
|
|
|
|
type Zookeeper struct {
|
|
|
|
Servers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## An array of address to gather stats about. Specify an ip or hostname
|
|
|
|
## with port. ie localhost:2181, 10.0.0.1:2181, etc.
|
2015-10-13 23:15:39 +00:00
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## If no servers are specified, then localhost is used as the host.
|
|
|
|
## If no port is specified, 2181 is used
|
2015-10-15 21:53:29 +00:00
|
|
|
servers = [":2181"]
|
2015-10-13 23:15:39 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
var defaultTimeout = time.Second * time.Duration(5)
|
|
|
|
|
|
|
|
// SampleConfig returns sample configuration message
|
|
|
|
func (z *Zookeeper) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// Description returns description of Zookeeper plugin
|
|
|
|
func (z *Zookeeper) Description() string {
|
|
|
|
return `Reads 'mntr' stats from one or many zookeeper servers`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather reads stats from all configured servers accumulates stats
|
2016-01-27 21:21:36 +00:00
|
|
|
func (z *Zookeeper) Gather(acc telegraf.Accumulator) error {
|
2015-10-13 23:15:39 +00:00
|
|
|
if len(z.Servers) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, serverAddress := range z.Servers {
|
|
|
|
if err := z.gatherServer(serverAddress, acc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (z *Zookeeper) gatherServer(address string, acc telegraf.Accumulator) error {
|
2015-10-13 23:15:39 +00:00
|
|
|
_, _, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
address = address + ":2181"
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := net.DialTimeout("tcp", address, defaultTimeout)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
2016-02-29 16:52:58 +00:00
|
|
|
// Extend connection
|
|
|
|
c.SetDeadline(time.Now().Add(defaultTimeout))
|
|
|
|
|
2015-10-13 23:15:39 +00:00
|
|
|
fmt.Fprintf(c, "%s\n", "mntr")
|
|
|
|
rdr := bufio.NewReader(c)
|
|
|
|
scanner := bufio.NewScanner(rdr)
|
|
|
|
|
2015-12-15 21:11:23 +00:00
|
|
|
service := strings.Split(address, ":")
|
|
|
|
if len(service) != 2 {
|
|
|
|
return fmt.Errorf("Invalid service address: %s", address)
|
|
|
|
}
|
|
|
|
tags := map[string]string{"server": service[0], "port": service[1]}
|
|
|
|
|
|
|
|
fields := make(map[string]interface{})
|
2015-10-13 23:15:39 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
|
|
|
|
re := regexp.MustCompile(`^zk_(\w+)\s+([\w\.\-]+)`)
|
|
|
|
parts := re.FindStringSubmatch(string(line))
|
|
|
|
|
2015-12-15 21:11:23 +00:00
|
|
|
if len(parts) != 3 {
|
2015-10-13 23:15:39 +00:00
|
|
|
return fmt.Errorf("unexpected line in mntr response: %q", line)
|
|
|
|
}
|
|
|
|
|
|
|
|
measurement := strings.TrimPrefix(parts[1], "zk_")
|
|
|
|
sValue := string(parts[2])
|
|
|
|
|
|
|
|
iVal, err := strconv.ParseInt(sValue, 10, 64)
|
|
|
|
if err == nil {
|
2015-12-15 21:11:23 +00:00
|
|
|
fields[measurement] = iVal
|
2015-10-13 23:15:39 +00:00
|
|
|
} else {
|
2015-12-15 21:11:23 +00:00
|
|
|
fields[measurement] = sValue
|
2015-10-13 23:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 21:11:23 +00:00
|
|
|
acc.AddFields("zookeeper", fields, tags)
|
2015-10-13 23:15:39 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("zookeeper", func() telegraf.Input {
|
2015-10-13 23:15:39 +00:00
|
|
|
return &Zookeeper{}
|
|
|
|
})
|
|
|
|
}
|