-
Notifications
You must be signed in to change notification settings - Fork 0
/
zk-find.py
executable file
·90 lines (80 loc) · 2.59 KB
/
zk-find.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
#!/usr/bin/env python
#
# This is a FIND utility for Zookeeper
#
# Author: Aleksandr Vinokurov <aleksandr.vin@gmail.com>
# Url: https://github.com/aleksandr-vin/zk-find
#
from __future__ import print_function
import logging
import logging.config
import argparse
import sys
def error(*objs):
print("ERROR: ", *objs, file=sys.stderr)
try:
logging.config.fileConfig('logging.conf')
except:
logging.basicConfig()
logger = logging.getLogger('zk-find')
from kazoo.client import KazooClient
from kazoo.client import KazooState
from kazoo.exceptions import NoNodeError
from kazoo.handlers.threading import KazooTimeoutError as TimeoutError
import re
def list_children(parent,prog):
try:
for node in zk.get_children(parent):
path = parent + "/" + node
if prog:
if prog.search(node):
print(path)
else:
pass
else:
print(path)
list_children(path,prog)
except NoNodeError:
pass
except ValueError as e:
error(e)
def my_listener(state):
if state == KazooState.LOST:
logger.debug('Session lost')
elif state == KazooState.SUSPENDED:
logger.debug('Session suspended')
else:
logger.info('Session connected')
# defaults
defaults = {
'hosts' : '127.0.0.1:2181'
,'root' : ''
}
parser = argparse.ArgumentParser(epilog='''
Report (and track progress on fixing) bugs via the github issues
page at https://github.com/aleksandr-vin/zk-find/issues or,
if you have no web access, by sending email to <aleksandr.vin+bug-zk-find@gmail.com>.
''')
parser.add_argument('root', nargs='?', type=str,
help='root of the search', default='%s' % defaults['root'],);
parser.add_argument('--hosts', default='%s' % defaults['hosts'],
type=str, metavar='HOST:PORT[,HOST:PORT]', dest='hosts', required=False,
help='comma-separated list of hosts to connect to (default: %s)' % defaults['hosts'])
parser.add_argument('--name',
type=str, metavar='REGEXP', dest='name',
help='regexp for matching node names')
if __name__ == "__main__":
# setting run-time args by the command-line parameters
settings = parser.parse_args()
zk = KazooClient(hosts=settings.hosts)
zk.add_listener(my_listener)
try:
zk.start()
global prog
prog = None
if (settings.name):
prog = re.compile(settings.name)
list_children(settings.root,prog)
zk.stop()
except TimeoutError as e:
error(e)