-
Notifications
You must be signed in to change notification settings - Fork 21
/
zookeeper.go
164 lines (135 loc) · 3.21 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
package command
import (
"flag"
"fmt"
"strings"
"time"
"github.com/funkygao/gafka/ctx"
"github.com/funkygao/gafka/zk"
"github.com/funkygao/gocli"
"github.com/funkygao/golib/color"
)
type Zookeeper struct {
Ui cli.Ui
Cmd string
flw string // zk four letter word command
cmdHelp string
zkHost string
watchMode bool
leaderOnly bool
}
func (this *Zookeeper) Run(args []string) (exitCode int) {
var (
zone string
)
cmdFlags := flag.NewFlagSet("zookeeper", flag.ContinueOnError)
cmdFlags.Usage = func() { this.Ui.Output(this.Help()) }
cmdFlags.StringVar(&zone, "z", ctx.DefaultZone(), "")
cmdFlags.StringVar(&this.flw, "c", "", "")
cmdFlags.StringVar(&this.zkHost, "host", "", "")
cmdFlags.BoolVar(&this.watchMode, "watch", false, "")
cmdFlags.BoolVar(&this.leaderOnly, "leader", false, "")
if err := cmdFlags.Parse(args); err != nil {
return 1
}
if !this.leaderOnly && validateArgs(this, this.Ui).require("-c").invalid(args) {
return 2
}
validCmds := []string{
"conf",
"cons",
"dump",
"envi",
"reqs",
"ruok",
"srvr",
"stat",
"wchs",
"wchc",
"wchp",
"mntr",
}
foundCmd := false
for _, c := range validCmds {
this.cmdHelp += c + " "
if this.flw == c {
foundCmd = true
break
}
}
if !foundCmd && !this.leaderOnly {
this.Ui.Error(fmt.Sprintf("invalid command: %s", this.flw))
this.Ui.Info(this.Help())
return 2
}
if this.watchMode {
refreshScreen()
}
zkzone := zk.NewZkZone(zk.DefaultConfig(zone, ctx.ZoneZkAddrs(zone)))
defer printSwallowedErrors(this.Ui, zkzone)
if this.leaderOnly {
this.printLeader(zkzone)
} else {
this.printZkStats(zkzone)
}
return
}
func (this *Zookeeper) printLeader(zkzone *zk.ZkZone) {
// FIXME all zones will only show the 1st zone info because it blocks others
for {
this.Ui.Output(color.Blue(zkzone.Name()))
for zkhost, lines := range zkzone.RunZkFourLetterCommand("mntr") {
if this.zkHost != "" && !strings.HasPrefix(zkhost, this.zkHost+":") {
continue
}
parts := strings.Split(lines, "\n")
for _, l := range parts {
if strings.HasPrefix(l, "zk_server_state") && strings.HasSuffix(l, "leader") {
this.Ui.Output(color.Green("%28s", zkhost))
break
}
}
}
if this.watchMode {
time.Sleep(time.Second * 5)
} else {
break
}
}
}
func (this *Zookeeper) printZkStats(zkzone *zk.ZkZone) {
for {
this.Ui.Output(color.Blue(zkzone.Name()))
for zkhost, lines := range zkzone.RunZkFourLetterCommand(this.flw) {
if this.zkHost != "" && !strings.HasPrefix(zkhost, this.zkHost+":") {
continue
}
this.Ui.Output(fmt.Sprintf("%s\n%s", color.Green("%28s", zkhost), lines))
}
if this.watchMode {
time.Sleep(time.Second * 5)
} else {
break
}
}
}
func (*Zookeeper) Synopsis() string {
return "Monitor zone Zookeeper status by four letter word command"
}
func (this *Zookeeper) Help() string {
help := fmt.Sprintf(`
Usage: %s zookeeper [options]
%s
Options:
-z zone
-host ip
Display the zk host only.
-watch
Watch mode.
-leader
Display zk leader only.
-c zk four letter word command
conf cons dump envi reqs ruok srvr stat wchs wchc wchp mntr
`, this.Cmd, this.Synopsis())
return strings.TrimSpace(help)
}