renaming plugins -> inputs
This commit is contained in:
62
plugins/inputs/zookeeper/README.md
Normal file
62
plugins/inputs/zookeeper/README.md
Normal file
@@ -0,0 +1,62 @@
|
||||
## Telegraf Plugin: Zookeeper
|
||||
|
||||
#### Description
|
||||
|
||||
The zookeeper plugin collects variables outputted from the 'mntr' command
|
||||
[Zookeeper Admin](https://zookeeper.apache.org/doc/trunk/zookeeperAdmin.html).
|
||||
|
||||
```
|
||||
echo mntr | nc localhost 2181
|
||||
|
||||
zk_version 3.4.0
|
||||
zk_avg_latency 0
|
||||
zk_max_latency 0
|
||||
zk_min_latency 0
|
||||
zk_packets_received 70
|
||||
zk_packets_sent 69
|
||||
zk_outstanding_requests 0
|
||||
zk_server_state leader
|
||||
zk_znode_count 4
|
||||
zk_watch_count 0
|
||||
zk_ephemerals_count 0
|
||||
zk_approximate_data_size 27
|
||||
zk_followers 4 - only exposed by the Leader
|
||||
zk_synced_followers 4 - only exposed by the Leader
|
||||
zk_pending_syncs 0 - only exposed by the Leader
|
||||
zk_open_file_descriptor_count 23 - only available on Unix platforms
|
||||
zk_max_file_descriptor_count 1024 - only available on Unix platforms
|
||||
```
|
||||
|
||||
## Measurements:
|
||||
#### Zookeeper measurements:
|
||||
|
||||
Meta:
|
||||
- units: int64
|
||||
- tags: `server=<hostname> port=<port>`
|
||||
|
||||
Measurement names:
|
||||
- zookeeper_avg_latency
|
||||
- zookeeper_max_latency
|
||||
- zookeeper_min_latency
|
||||
- zookeeper_packets_received
|
||||
- zookeeper_packets_sent
|
||||
- zookeeper_outstanding_requests
|
||||
- zookeeper_znode_count
|
||||
- zookeeper_watch_count
|
||||
- zookeeper_ephemerals_count
|
||||
- zookeeper_approximate_data_size
|
||||
- zookeeper_followers #only exposed by the Leader
|
||||
- zookeeper_synced_followers #only exposed by the Leader
|
||||
- zookeeper_pending_syncs #only exposed by the Leader
|
||||
- zookeeper_open_file_descriptor_count
|
||||
- zookeeper_max_file_descriptor_count
|
||||
|
||||
#### Zookeeper string measurements:
|
||||
|
||||
Meta:
|
||||
- units: string
|
||||
- tags: `server=<hostname> port=<port>`
|
||||
|
||||
Measurement names:
|
||||
- zookeeper_version
|
||||
- zookeeper_server_state
|
||||
109
plugins/inputs/zookeeper/zookeeper.go
Normal file
109
plugins/inputs/zookeeper/zookeeper.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package zookeeper
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/influxdb/telegraf/plugins/inputs"
|
||||
)
|
||||
|
||||
// Zookeeper is a zookeeper plugin
|
||||
type Zookeeper struct {
|
||||
Servers []string
|
||||
}
|
||||
|
||||
var sampleConfig = `
|
||||
# An array of address to gather stats about. Specify an ip or hostname
|
||||
# with port. ie localhost:2181, 10.0.0.1:2181, etc.
|
||||
|
||||
# If no servers are specified, then localhost is used as the host.
|
||||
# If no port is specified, 2181 is used
|
||||
servers = [":2181"]
|
||||
`
|
||||
|
||||
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
|
||||
func (z *Zookeeper) Gather(acc inputs.Accumulator) error {
|
||||
if len(z.Servers) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, serverAddress := range z.Servers {
|
||||
if err := z.gatherServer(serverAddress, acc); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (z *Zookeeper) gatherServer(address string, acc inputs.Accumulator) error {
|
||||
_, _, 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()
|
||||
|
||||
fmt.Fprintf(c, "%s\n", "mntr")
|
||||
rdr := bufio.NewReader(c)
|
||||
scanner := bufio.NewScanner(rdr)
|
||||
|
||||
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{})
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
re := regexp.MustCompile(`^zk_(\w+)\s+([\w\.\-]+)`)
|
||||
parts := re.FindStringSubmatch(string(line))
|
||||
|
||||
if len(parts) != 3 {
|
||||
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 {
|
||||
fields[measurement] = iVal
|
||||
} else {
|
||||
fields[measurement] = sValue
|
||||
}
|
||||
}
|
||||
acc.AddFields("zookeeper", fields, tags)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
inputs.Add("zookeeper", func() inputs.Input {
|
||||
return &Zookeeper{}
|
||||
})
|
||||
}
|
||||
43
plugins/inputs/zookeeper/zookeeper_test.go
Normal file
43
plugins/inputs/zookeeper/zookeeper_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package zookeeper
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/influxdb/telegraf/testutil"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestZookeeperGeneratesMetrics(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping integration test in short mode")
|
||||
}
|
||||
|
||||
z := &Zookeeper{
|
||||
Servers: []string{testutil.GetLocalHost() + ":2181"},
|
||||
}
|
||||
|
||||
var acc testutil.Accumulator
|
||||
|
||||
err := z.Gather(&acc)
|
||||
require.NoError(t, err)
|
||||
|
||||
intMetrics := []string{
|
||||
"avg_latency",
|
||||
"max_latency",
|
||||
"min_latency",
|
||||
"packets_received",
|
||||
"packets_sent",
|
||||
"outstanding_requests",
|
||||
"znode_count",
|
||||
"watch_count",
|
||||
"ephemerals_count",
|
||||
"approximate_data_size",
|
||||
"open_file_descriptor_count",
|
||||
"max_file_descriptor_count",
|
||||
}
|
||||
|
||||
for _, metric := range intMetrics {
|
||||
assert.True(t, acc.HasIntField("zookeeper", metric), metric)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user