2015-10-13 23:15:39 +00:00
|
|
|
package zookeeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2018-03-29 19:42:25 +00:00
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2015-10-13 23:15:39 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2018-03-29 19:42:25 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2018-05-04 23:33:23 +00:00
|
|
|
tlsint "github.com/influxdata/telegraf/internal/tls"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-10-13 23:15:39 +00:00
|
|
|
)
|
|
|
|
|
2019-06-07 19:25:55 +00:00
|
|
|
var zookeeperFormatRE = regexp.MustCompile(`^zk_(\w+)\s+([\w\.\-]+)`)
|
|
|
|
|
2015-10-13 23:15:39 +00:00
|
|
|
// Zookeeper is a zookeeper plugin
|
|
|
|
type Zookeeper struct {
|
|
|
|
Servers []string
|
2018-03-29 19:42:25 +00:00
|
|
|
Timeout internal.Duration
|
|
|
|
|
2018-05-04 23:33:23 +00:00
|
|
|
EnableTLS bool `toml:"enable_tls"`
|
|
|
|
EnableSSL bool `toml:"enable_ssl"` // deprecated in 1.7; use enable_tls
|
|
|
|
tlsint.ClientConfig
|
2018-03-29 19:42:25 +00:00
|
|
|
|
|
|
|
initialized bool
|
|
|
|
tlsConfig *tls.Config
|
2015-10-13 23:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"]
|
2018-03-29 19:42:25 +00:00
|
|
|
|
|
|
|
## Timeout for metric collections from all servers. Minimum timeout is "1s".
|
|
|
|
# timeout = "5s"
|
|
|
|
|
2018-05-04 23:33:23 +00:00
|
|
|
## Optional TLS Config
|
|
|
|
# enable_tls = true
|
|
|
|
# tls_ca = "/etc/telegraf/ca.pem"
|
|
|
|
# tls_cert = "/etc/telegraf/cert.pem"
|
|
|
|
# tls_key = "/etc/telegraf/key.pem"
|
2018-03-29 19:42:25 +00:00
|
|
|
## If false, skip chain & host verification
|
|
|
|
# insecure_skip_verify = true
|
2015-10-13 23:15:39 +00:00
|
|
|
`
|
|
|
|
|
2018-03-29 19:42:25 +00:00
|
|
|
var defaultTimeout = 5 * time.Second
|
2015-10-13 23:15:39 +00:00
|
|
|
|
|
|
|
// 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`
|
|
|
|
}
|
|
|
|
|
2018-03-29 19:42:25 +00:00
|
|
|
func (z *Zookeeper) dial(ctx context.Context, addr string) (net.Conn, error) {
|
|
|
|
var dialer net.Dialer
|
2018-05-04 23:33:23 +00:00
|
|
|
if z.EnableTLS || z.EnableSSL {
|
2018-03-29 19:42:25 +00:00
|
|
|
deadline, ok := ctx.Deadline()
|
|
|
|
if ok {
|
|
|
|
dialer.Deadline = deadline
|
|
|
|
}
|
|
|
|
return tls.DialWithDialer(&dialer, "tcp", addr, z.tlsConfig)
|
|
|
|
} else {
|
|
|
|
return dialer.DialContext(ctx, "tcp", addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-13 23:15:39 +00:00
|
|
|
// 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 {
|
2018-03-29 19:42:25 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
if !z.initialized {
|
2018-05-04 23:33:23 +00:00
|
|
|
tlsConfig, err := z.ClientConfig.TLSConfig()
|
2018-03-29 19:42:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
z.tlsConfig = tlsConfig
|
|
|
|
z.initialized = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if z.Timeout.Duration < 1*time.Second {
|
|
|
|
z.Timeout.Duration = defaultTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, z.Timeout.Duration)
|
|
|
|
defer cancel()
|
|
|
|
|
2015-10-13 23:15:39 +00:00
|
|
|
if len(z.Servers) == 0 {
|
2017-07-26 00:08:32 +00:00
|
|
|
z.Servers = []string{":2181"}
|
2015-10-13 23:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, serverAddress := range z.Servers {
|
2018-03-29 19:42:25 +00:00
|
|
|
acc.AddError(z.gatherServer(ctx, serverAddress, acc))
|
2015-10-13 23:15:39 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-29 19:42:25 +00:00
|
|
|
func (z *Zookeeper) gatherServer(ctx context.Context, address string, acc telegraf.Accumulator) error {
|
2016-07-16 18:19:21 +00:00
|
|
|
var zookeeper_state string
|
2015-10-13 23:15:39 +00:00
|
|
|
_, _, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
address = address + ":2181"
|
|
|
|
}
|
|
|
|
|
2018-03-29 19:42:25 +00:00
|
|
|
c, err := z.dial(ctx, address)
|
2015-10-13 23:15:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
2018-03-29 19:42:25 +00:00
|
|
|
// Apply deadline to connection
|
|
|
|
deadline, ok := ctx.Deadline()
|
|
|
|
if ok {
|
|
|
|
c.SetDeadline(deadline)
|
|
|
|
}
|
2016-02-29 16:52:58 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := make(map[string]interface{})
|
2015-10-13 23:15:39 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2019-06-07 19:25:55 +00:00
|
|
|
parts := zookeeperFormatRE.FindStringSubmatch(string(line))
|
2015-10-13 23:15:39 +00:00
|
|
|
|
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_")
|
2016-07-16 18:19:21 +00:00
|
|
|
if measurement == "server_state" {
|
|
|
|
zookeeper_state = parts[2]
|
2015-10-13 23:15:39 +00:00
|
|
|
} else {
|
2016-07-16 18:19:21 +00:00
|
|
|
sValue := string(parts[2])
|
|
|
|
|
|
|
|
iVal, err := strconv.ParseInt(sValue, 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
fields[measurement] = iVal
|
|
|
|
} else {
|
|
|
|
fields[measurement] = sValue
|
|
|
|
}
|
2015-10-13 23:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-05 21:37:13 +00:00
|
|
|
|
|
|
|
srv := "localhost"
|
|
|
|
if service[0] != "" {
|
|
|
|
srv = service[0]
|
|
|
|
}
|
|
|
|
|
2016-07-16 18:19:21 +00:00
|
|
|
tags := map[string]string{
|
2018-07-05 21:37:13 +00:00
|
|
|
"server": srv,
|
2016-07-16 18:19:21 +00:00
|
|
|
"port": service[1],
|
|
|
|
"state": zookeeper_state,
|
|
|
|
}
|
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{}
|
|
|
|
})
|
|
|
|
}
|