-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
zookeeper.go
177 lines (145 loc) · 3.76 KB
/
zookeeper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//go:generate ../../../tools/readme_config_includer/generator
package zookeeper
import (
"bufio"
"context"
"crypto/tls"
_ "embed"
"fmt"
"net"
"regexp"
"strconv"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
common_tls "github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/inputs"
)
//go:embed sample.conf
var sampleConfig string
var zookeeperFormatRE = regexp.MustCompile(`^zk_(\w[\w\.\-]*)\s+([\w\.\-]+)`)
// Zookeeper is a zookeeper plugin
type Zookeeper struct {
Servers []string `toml:"servers"`
Timeout config.Duration `toml:"timeout"`
ParseFloats string `toml:"parse_floats"`
EnableTLS bool `toml:"enable_tls"`
EnableSSL bool `toml:"enable_ssl" deprecated:"1.7.0;1.35.0;use 'enable_tls' instead"`
common_tls.ClientConfig
initialized bool
tlsConfig *tls.Config
}
var defaultTimeout = 5 * time.Second
func (z *Zookeeper) dial(ctx context.Context, addr string) (net.Conn, error) {
var dialer net.Dialer
if z.EnableTLS || z.EnableSSL {
deadline, ok := ctx.Deadline()
if ok {
dialer.Deadline = deadline
}
return tls.DialWithDialer(&dialer, "tcp", addr, z.tlsConfig)
}
return dialer.DialContext(ctx, "tcp", addr)
}
func (*Zookeeper) SampleConfig() string {
return sampleConfig
}
// Gather reads stats from all configured servers accumulates stats
func (z *Zookeeper) Gather(acc telegraf.Accumulator) error {
ctx := context.Background()
if !z.initialized {
tlsConfig, err := z.ClientConfig.TLSConfig()
if err != nil {
return err
}
z.tlsConfig = tlsConfig
z.initialized = true
}
if z.Timeout < config.Duration(1*time.Second) {
z.Timeout = config.Duration(defaultTimeout)
}
ctx, cancel := context.WithTimeout(ctx, time.Duration(z.Timeout))
defer cancel()
if len(z.Servers) == 0 {
z.Servers = []string{":2181"}
}
for _, serverAddress := range z.Servers {
acc.AddError(z.gatherServer(ctx, serverAddress, acc))
}
return nil
}
func (z *Zookeeper) gatherServer(ctx context.Context, address string, acc telegraf.Accumulator) error {
var zookeeperState string
_, _, err := net.SplitHostPort(address)
if err != nil {
address = address + ":2181"
}
c, err := z.dial(ctx, address)
if err != nil {
return err
}
defer c.Close()
// Apply deadline to connection
deadline, ok := ctx.Deadline()
if ok {
if err := c.SetDeadline(deadline); err != nil {
return err
}
}
if _, err := fmt.Fprintf(c, "%s\n", "mntr"); err != nil {
return err
}
rdr := bufio.NewReader(c)
scanner := bufio.NewScanner(rdr)
service := strings.Split(address, ":")
if len(service) != 2 {
return fmt.Errorf("invalid service address: %s", address)
}
fields := make(map[string]interface{})
for scanner.Scan() {
line := scanner.Text()
parts := zookeeperFormatRE.FindStringSubmatch(line)
if len(parts) != 3 {
return fmt.Errorf("unexpected line in mntr response: %q", line)
}
measurement := strings.TrimPrefix(parts[1], "zk_")
if measurement == "server_state" {
zookeeperState = parts[2]
continue
}
sValue := parts[2]
// First attempt to parse as an int
iVal, err := strconv.ParseInt(sValue, 10, 64)
if err == nil {
fields[measurement] = iVal
continue
}
// If set, attempt to parse as a float
if z.ParseFloats == "float" {
fVal, err := strconv.ParseFloat(sValue, 64)
if err == nil {
fields[measurement] = fVal
continue
}
}
// Finally, save as a string
fields[measurement] = sValue
}
srv := "localhost"
if service[0] != "" {
srv = service[0]
}
tags := map[string]string{
"server": srv,
"port": service[1],
"state": zookeeperState,
}
acc.AddFields("zookeeper", fields, tags)
return nil
}
func init() {
inputs.Add("zookeeper", func() telegraf.Input {
return &Zookeeper{}
})
}