forked from TencentBlueKing/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zookeeper.go
45 lines (38 loc) · 1.08 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
/*
Package zookeeper is a Metricbeat module for ZooKeeper servers.
*/
package zookeeper
import (
"bytes"
"io"
"io/ioutil"
"net"
"time"
"github.com/pkg/errors"
)
// RunCommand establishes a TCP connection the ZooKeeper command port that
// accepts the four-letter ZooKeeper commands and sends the given command. It
// reads all response data received on the socket and returns an io.Reader
// containing that data.
func RunCommand(command, address string, timeout time.Duration) (io.Reader, error) {
conn, err := net.DialTimeout("tcp", address, timeout)
if err != nil {
return nil, errors.Wrapf(err, "connection to host '%s' failed", address)
}
defer conn.Close()
// Set read and write timeout.
err = conn.SetDeadline(time.Now().Add(timeout))
if err != nil {
return nil, err
}
// Write four-letter command.
_, err = conn.Write([]byte(command))
if err != nil {
return nil, errors.Wrapf(err, "writing command '%s' failed", command)
}
result, err := ioutil.ReadAll(conn)
if err != nil {
return nil, errors.Wrap(err, "read failed")
}
return bytes.NewReader(result), nil
}