-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_zk_split_brain.py
230 lines (177 loc) · 6.06 KB
/
check_zk_split_brain.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python
"""
This Nagios plugin checks the state of a zookeeper cluster via the "4 letter words".
Its main purpose is to check a Zookeeper status for OK nodes and check if more than one
believes he is a leader.
"""
from __future__ import division
__author__ = 'jcrsilva'
__license__ = 'The Unlicense'
__version__ = '1.0.0'
import argparse
import re
import socket
import sys
from telnetlib import Telnet
import nagiosplugin
# Regex to validate a zookeeper connection string
ZK_CONNECTION_STRING_RE = '^([a-zA-Z0-9.]+(:[0-9]+)?)(,[a-zA-Z0-9.]+(:[0-9]+)?)*$'
# Timeout when attempting to connect to remote zk server
ZK_TIMEOUT = 5
class CheckZK(nagiosplugin.Resource):
"""CheckZK class."""
def __init__(self, hosts):
"""
Class constructor.
:param hosts: command line arguments
"""
self.hosts = hosts
@staticmethod
def _zk_tn(host_tpl, data):
try:
tn = Telnet(host=host_tpl[0], port=host_tpl[1], timeout=ZK_TIMEOUT)
tn.write(data)
ret = tn.read_all()
tn.close()
return ret
except socket.error:
return None
def probe(self):
"""
Nagios probe method.
:return: Metrics
"""
def probe_ok(host):
return CheckZK._zk_tn(host, 'ruok') == 'imok'
def probe_leader(host):
return re.search("Mode: ([a-z]+)", CheckZK._zk_tn(host, 'stat')).group(1) == 'leader'
status = {}
for host in self.hosts:
status[host] = {
"ok": probe_ok(host)
}
if status[host]["ok"]:
status[host]["leader"] = probe_leader(host)
return [
nagiosplugin.Metric("ruok", status),
nagiosplugin.Metric("leader", status)
]
class CheckZKLeaderContext(nagiosplugin.Context):
"""Context to evaluate leaders."""
def __init__(self):
"""Constructor."""
super(self.__class__, self).__init__('leader')
def evaluate(self, metric, resource):
"""
Evaluate metric result.
:param metric: Metric
:param resource: Resource
:return: Metric state
"""
leaders = [node for node in metric.value if metric.value[node].get("leader")]
if len(leaders) <= 0:
return self.result_cls(nagiosplugin.state.Critical, "Cluster has no leader", metric=metric)
elif len(leaders) == 1:
return self.result_cls(nagiosplugin.state.Ok, metric=metric)
else:
return self.result_cls(nagiosplugin.state.Critical, "More than one leader detected!", metric=metric)
def describe(self, metric):
"""
Describe metric.
:param metric: Metric
:return: Metric description
"""
return "{} are both leaders".format(
",".join([":".join(node) for node in metric.value if metric.value[node].get("leader")])
)
class CheckZKOKContext(nagiosplugin.Context):
"""Context to evaluate if nodes are OK."""
def __init__(self):
"""Constructor."""
super(self.__class__, self).__init__('ruok')
def evaluate(self, metric, resource):
"""
Evaluate metric result.
:param metric: Metric
:param resource: Resource
:return: Metric state
"""
has_quorum_number = int(round(len(metric.value) / 2))
up_nodes = len([node for node in metric.value.values() if node.get("ok")])
if up_nodes >= len(metric.value):
return self.result_cls(nagiosplugin.state.Ok, metric=metric)
elif up_nodes >= has_quorum_number:
return self.result_cls(nagiosplugin.state.Warn, "Cluster has nodes down but has quorum", metric=metric)
else:
return self.result_cls(nagiosplugin.state.Critical, "Cluster does not have quorum", metric=metric)
def describe(self, metric):
"""
Describe metric.
:param metric: Metric
:return: Metric description
"""
return "{}/{} nodes are OK".format(
len([node for node in metric.value.values() if node.get("ok")]), len(metric.value)
)
def performance(self, metric, resource):
"""
Format performance data.
:param metric: Metric
:param resource: Resource
:return: Performance
"""
# for dev mode
if len(metric.value) == 1:
crit = nagiosplugin.Range("~:0".format(int(round(len(metric.value) / 2))))
warn = None
# for 3 node cluster
elif 1 < len(metric.value) < 4:
crit = nagiosplugin.Range("~:2".format(int(round(len(metric.value) / 2))))
warn = None
# anything bigger
if len(metric.value) >= 4:
crit = nagiosplugin.Range("~:{}".format(int(round(len(metric.value) / 2))))
warn = nagiosplugin.Range("{}:{}".format(int(round(len(metric.value) / 2))+1, len(metric.value)-1))
return nagiosplugin.Performance(
label="OK ZK nodes",
value=len(metric.value),
uom="Number",
warn=warn,
crit=crit,
)
def get_args():
"""
Grab args from CLI.
:return: Argument dict
"""
argp = argparse.ArgumentParser(
description=__doc__
)
argp.add_argument('-S', '--connection', action='store', required=True,
help='Zookeeper connection string'
)
return argp.parse_args()
@nagiosplugin.guarded
def main():
"""
Main.
:return:
"""
args = get_args()
# Validate connection string
if not re.match(ZK_CONNECTION_STRING_RE, args.connection):
print "Zookeeper connection string is not valid"
sys.exit(2)
hosts = []
for host in args.connection.split(','):
if ':' in host:
hosts.append(tuple(host.split(':')))
else:
hosts.append((host, 2181))
nagiosplugin.Check(
CheckZK(hosts),
CheckZKOKContext(),
CheckZKLeaderContext(),
).main()
if __name__ == '__main__':
main()