-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathzookeeper.go
89 lines (75 loc) · 1.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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"net"
"strings"
)
const (
monitorCMD = "mntr"
okCMD = "ruok"
)
type zooKeeper struct {
addr string
}
func newZooKeeper(addr string) *zooKeeper {
return &zooKeeper{addr: addr}
}
func (zk *zooKeeper) fetchStats() (map[string]string, error) {
stats, err := zk.fetchMntrStats()
if err != nil {
return stats, err
}
isOK, err := zk.fetchOKStat()
if err != nil {
return stats, err
}
stats[zkOK] = isOK
return stats, nil
}
func (zk *zooKeeper) fetchMntrStats() (map[string]string, error) {
stats := make(map[string]string)
byts, err := zk.sendCommand(monitorCMD)
if err != nil {
return stats, err
}
scanner := bufio.NewScanner(bytes.NewReader(byts))
for scanner.Scan() {
splits := strings.Split(scanner.Text(), "\t")
if splits[0] == "zk_version" {
continue
}
if len(splits) != 2 {
log.Printf("zookeeper: expected a key value pair separated by a tab, got [%v]\n", splits)
continue
}
stats[splits[0]] = splits[1]
}
return stats, nil
}
func (zk *zooKeeper) fetchOKStat() (string, error) {
byts, err := zk.sendCommand(okCMD)
return string(byts), err
}
func (zk *zooKeeper) sendCommand(cmd string) ([]byte, error) {
conn, err := net.Dial("tcp", zk.addr)
if err != nil {
return nil, fmt.Errorf("zookeeper: dial failed, err=%#v", err)
}
defer func() {
if err := conn.Close(); err != nil {
fmt.Printf("Failed to close connection, err=%#v\n", err)
}
}()
if _, err = fmt.Fprintf(conn, fmt.Sprintf("%s\n", cmd)); err != nil {
return nil, fmt.Errorf("zookeeper: command send failed, err=%#v", err)
}
var buf bytes.Buffer
if _, err = io.Copy(&buf, conn); err != nil {
return nil, fmt.Errorf("zookeeper: fetch response failed, err=%#v", err)
}
return buf.Bytes(), nil
}