-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_zookeeper.py
executable file
·88 lines (70 loc) · 2.8 KB
/
check_zookeeper.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
#!/usr/bin/env python
# check_zookeeper.py
#
# Nagios plugin; checks a server's status in a Zookeeper cluster.
from telnetlib import Telnet
import socket
from pynag.Plugins import PluginHelper, ok, warning, critical, unknown
TELNET_TIMEOUT = 3
class ZkClient:
def __init__(self, host, port, timeout=TELNET_TIMEOUT):
"""Connect to zookeper's client.
"""
self.host = host
self.port = port
self.timeout = timeout
def cmd(self, word):
"""Connect and send a 4letter command to Zookeeper.
"""
# Zookeeper closes the socket after every command, so we must reconnect every time.
tn = Telnet(self.host, self.port, self.timeout)
tn.write('{}\n'.format(word))
return tn.read_all()
if __name__ == '__main__':
plugin = PluginHelper()
plugin.parser.add_option("-H","--hostname", help="Zookeeper's host", default='127.0.0.1')
plugin.parser.add_option("-p","--port", help="Zookeeper's port", default='2181')
plugin.parse_arguments()
try:
zk = ZkClient(plugin.options.hostname, plugin.options.port)
except socket.error:
plugin.status(critical)
plugin.add_summary("Can't connect to {}:{}".format(plugin.options.hostname, plugin.options.port))
plugin.exit()
try:
if zk.cmd('ruok') != 'imok':
plugin.status(critical)
plugin.add_summary("Command 'ruok' failed")
plugin.exit()
except socket.error, socket.timeout:
plugin.status(critical)
plugin.add_summary("Can't connect to {}:{}".format(plugin.options.hostname, plugin.options.port))
plugin.exit()
try:
if zk.cmd('isro') != 'rw':
plugin.status(critical)
plugin.add_summary("Zookeeper is not read-write (network partition? quorum?)")
plugin.exit()
except socket.error, socket.timeout:
plugin.status(critical)
plugin.add_summary("Can't connect to {}:{}".format(plugin.options.hostname, plugin.options.port))
plugin.exit()
# Get Zookeeper's status.
txt = zk.cmd('mntr')
# Parse lines of keys/values into a dictionary.
mntr = dict( l.split('\t') for l in txt.strip().split('\n') if '\t' in l )
# Run checks.
state = mntr.get('zk_server_state', None)
if state in ['observer', 'standalone']:
plugin.status(critical)
plugin.add_summary("zk_server_state: {}".format(state))
elif state in ['leader_election']:
plugin.status(warning)
plugin.add_summary("zk_server_state: {}".format(state))
elif state not in ['leader', 'follower']:
plugin.status(critical)
plugin.add_summary("Unknown zk_server_state ({})".format(state))
else:
plugin.status(ok)
plugin.add_summary("zk_server_state: {}".format(state))
plugin.exit()