Skip to content

Instantly share code, notes, and snippets.

@gmambro
Created March 27, 2018 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmambro/fa6859b62328ccdc85f9ae4c46f43cf3 to your computer and use it in GitHub Desktop.
Save gmambro/fa6859b62328ccdc85f9ae4c46f43cf3 to your computer and use it in GitHub Desktop.
Zookeeper stats snippet
import socket
host = '127.0.0.1'
port = 2181
facts = {}
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host, port))
f = client.makefile()
f.write('stat')
f.flush()
for line in f:
line = line.strip()
#Receiving from client
print line
k,v = line.split(':', 1)
if k == 'Zookeeper version':
facts['version'] = v
elif k == 'Clients':
clients = []
line2 = f.readline().strip()
while line2 != '':
print "* %s *" % line2
clients.append(line2)
line2 = f.readline().strip()
elif k == 'Latency min/avg/max':
values = v.split('/')
facts['latency'] = { 'min': values[0], 'avg': values[1], 'max': values[2] }
elif k in [ 'Received', 'Sent', 'Connections', 'Outstanding', 'Zxid', 'Mode', 'Node count' ]:
facts[k.lower()] = v
client.close()
print facts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment