This repository has been archived by the owner on Aug 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zooki.py
85 lines (73 loc) · 3.23 KB
/
zooki.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
#!/usr/bin/env python3
# usage: python3 zooki.py /zookeeper /zookeeper/zookeeper-logs/ dev-env-zookeeper
# This script suppose to export all zookeeper metric from one node and write to file
# from where either splunk like tools can read it.
from urllib import request
import shutil
from socket import gethostname
from datetime import datetime
import json
import sys
class zooki:
def __init__(self):
self.zAddr = gethostname()
self.zPort = 8080
self.zHttpAddr = "http://" + self.zAddr + ":" + str(self.zPort) + "/commands/"
self.cTimeNow = str(datetime.now())
def getStorageMetric(self):
total, used, free = shutil.disk_usage(sys.argv[1])
_sMetric = {
"@timestamp": self.cTimeNow,
"command": "disk",
"environment": sys.argv[3],
"totalInGB": total // (2**30),
"usedInGB": used // (2**30),
"freeInGB": free // (2**30)
}
return json.dumps(_sMetric)
def getZMetric(self, commandPath):
with request.urlopen( self.zHttpAddr + commandPath ) as f:
if f.status == 200:
_zMetric = json.loads(f.read().decode('utf-8'))
_zMetric["@timestamp"] = self.cTimeNow
_zMetric["environment"] = sys.argv[3]
else:
_zMetric = {}
return json.dumps(_zMetric)
# json retruned by monitor is too big to be handled by splunk indexer
# so separate function to reduce the json size
def getMonitorMetric(self):
with request.urlopen( self.zHttpAddr + "monitor" ) as f:
if f.status == 200:
_MM = json.loads(f.read().decode('utf-8'))
_zMetric = {
"@timestamp": self.cTimeNow,
"environment": sys.argv[3],
"command": _MM["command"],
"znode_count": _MM["znode_count"],
"watch_count": _MM["watch_count"],
"outstanding_requests": _MM["outstanding_requests"],
"open_file_descriptor_count": _MM["open_file_descriptor_count"],
"ephemerals_count": _MM["ephemerals_count"],
"max_latency": _MM["max_latency"],
"avg_latency": _MM["avg_latency"],
"synced_followers": _MM["synced_followers"] if _MM["server_state"] == "leader" else 0,
"pending_syncs": _MM["pending_syncs"] if _MM["server_state"] == "leader" else 0,
"version": _MM["version"],
"quorum_size": _MM["quorum_size"],
"uptime": _MM["uptime"]
}
else:
_zMetric = {}
return json.dumps(_zMetric)
def main():
commandPaths = ['connections', 'leader', 'watch_summary']
z = zooki()
with open(sys.argv[2] + "disk.out", "w") as zMetricFile:
zMetricFile.write(z.getStorageMetric())
for command in commandPaths:
with open(sys.argv[2] + command + ".out", "w") as zMetricFile:
zMetricFile.write(z.getZMetric(command))
with open(sys.argv[2] + "monitor.out", "w") as zMetricFile:
zMetricFile.write(z.getMonitorMetric())
main()