-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogsize_exporter.py
81 lines (56 loc) · 1.93 KB
/
logsize_exporter.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
# encoding: utf-8
import sys
import threading
import ConfigParser
import prometheus_client
from prometheus_client import Counter, Gauge
from prometheus_client.core import CollectorRegistry
from flask import Response, Flask
from kazoo.client import KazooClient
from kafka.consumer import KafkaConsumer
app = Flask(__name__)
REGISTRY = CollectorRegistry(auto_describe=False)
conf = ConfigParser.ConfigParser()
conf.read("cluster.conf")
cluster = conf.sections()[0]
zookeepers = conf.get(cluster, "zk")
kafka_broker = conf.get(cluster, "brokers")
broker_list = kafka_broker.split(",")
zk = KazooClient(hosts=zookeepers, read_only=True)
zk.start()
if not zk.exists("/brokers/topics"):
sys.exit(0)
kafka_logsize = Gauge("logsize",
"Total count of kafka offset logsize",
["topic", "partition"],
registry=REGISTRY)
requests_total = Counter("request_count",
"Total request count of the hosts",
registry=REGISTRY)
def thread_main(topic):
consumer = KafkaConsumer(topic,
group_id='kafka_monitor',
metadata_broker_list=broker_list)
offset = consumer._offsets.fetch
for part in offset:
kafka_logsize.labels(topic=part[0], partition=part[1]).set(
offset[part]
)
@app.route("/metrics")
def log_size():
# request_count + 1
requests_total.inc()
thread_list = []
topics = zk.get_children("/brokers/topics")
for i in range(len(topics)):
topic = topics[i]
t = threading.Thread(target=thread_main, args=(topic, ))
thread_list.append(t)
for t in thread_list:
t.start()
for t in thread_list:
t.join()
return Response(prometheus_client.generate_latest(REGISTRY),
mimetype="text/plain")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8082, threaded=True)