-
Notifications
You must be signed in to change notification settings - Fork 4
/
file_to_graphite.py
66 lines (52 loc) · 1.65 KB
/
file_to_graphite.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
import sys
import time
import json
import graphyte
def main(args):
graphyte.init("localhost", prefix="test")
with open("/tmp/docker-kafka/kafka-metrics.json") as file:
while True:
line = file.readline()
if line not in ("", "\n", "\r"):
try:
dict = json.loads(line)
extract_data(dict)
except(Exception):
pass
else:
time.sleep(1)
print("no more metrics")
def extract_data(dict):
id = dict["identifier"]
ts = dict["timestamp"]
name = dict["name"]
values = dict["values"]
send_metric(id, ts, name, values)
def send_metric(id, ts, name, values):
short_name = ""
value = ""
if "ActiveControllerCount" in name:
short_name = "ActiveControllerCount"
value = values["Value"]
elif "IsrExpandsPerSec" in name:
short_name = "IsrExpandsPerSec"
value = values["Count"]
elif "IsrShrinksPerSec" in name:
short_name = "IsrShrinksPerSec"
value = values["Count"]
elif "OfflinePartitionsCount" in name:
short_name = "OfflinePartitionsCount"
value = values["Value"]
elif "UncleanLeaderElectionsPerSec" in name:
short_name = "UncleanLeaderElectionsPerSec"
value = values["Count"]
elif "UnderReplicatedPartitions" in name:
short_name = "UnderReplicatedPartitions"
value = values["Value"]
if value != "":
path = id + "." + short_name
graphyte.send(path, int(value), timestamp=ts)
def run():
sys.exit(main(sys.argv[1:]))
if __name__ == "__main__":
run()