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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| topics = ['heimdallr-dev']
monitor_group_ids = ['heimdallr']
servers = 'localhost:9092'
time_interval = 10
history_time_interval = 5 * 60
import time
import sys
from kafka.client import KafkaClient from kafka.protocol.commit import OffsetFetchRequest_v1, OffsetFetchResponse_v1, OffsetFetchRequest_v0, \ OffsetFetchResponse_v0 from kafka.protocol.offset import OffsetRequest_v0, OffsetResponse_v0
duration = 0 client = None conn = None partition_cache = {} brokers_cache = [] kafka_type = ['heimdallr'] zk_type = []
def get_brokers(): if not brokers_cache: brokers = client.cluster.brokers() if brokers: brokers_cache.extend([x.nodeId for x in brokers]) return brokers_cache
def get_partitions(topic): if not partition_cache or topic not in partition_cache: partitions = client.cluster.available_partitions_for_topic(topic) if partitions: partition_cache[topic] = [x for x in partitions] else: return [] return partition_cache[topic]
def get_logsize(): """ 获取topic 下每个partition的logsize(各个broker的累加) :return: """ tp = {} brokers = get_brokers() for topic in topics: partitions = get_partitions(topic) pl = {} for broker in brokers: for partition in partitions: client.send(broker, OffsetRequest_v0(replica_id=-1, topics=[(topic, [(partition, -1, 1)])])) responses = client.poll() pdict = parse_logsize(topic, partition, responses) append(pl, pdict) tp[topic] = pl return tp
def append(rdict, pdict): if rdict: for k, v in pdict.items(): if k in rdict: rdict[k] = rdict[k] + v else: rdict[k] = v else: rdict.update(pdict)
def parse_logsize(t, p, responses): """ 单个broker中单个partition的logsize :param responses: :param p: :param t: :return: """ for response in responses: if not isinstance(response, OffsetResponse_v0): return {} tps = response.topics topic = tps[0][0] partition_list = tps[0][1] partition = partition_list[0][0] if topic == t and partition == p and partition_list[0][1] == 0: logsize_list = partition_list[0][2] logsize = logsize_list[0] return {partition: logsize} return {}
def parse_offsets(t, responses): dr = {} for response in responses: if not isinstance(response, (OffsetFetchResponse_v1, OffsetFetchResponse_v0)): return {} tps = response.topics topic = tps[0][0] partition_list = tps[0][1] if topic == t: for partition_tunple in partition_list: if partition_tunple[3] == 0: offset = partition_tunple[1] dr[partition_tunple[0]] = offset return dr
def get_offsets(): gd = {} for gid in monitor_group_ids: td = {} for topic in topics: pd = {} for broker in get_brokers(): partitions = get_partitions(topic) if not partitions: return {} else: responses = optionnal_send(broker, gid, topic, partitions) dr = parse_offsets(topic, responses) append(pd, dr) td[topic] = pd gd[gid] = td return gd
def optionnal_send(broker, gid, topic, partitions): if gid in kafka_type: return kafka_send(broker, gid, topic, partitions) elif gid in zk_type: return zk_send(broker, gid, topic, partitions) else: responses = zk_send(broker, gid, topic, partitions) dct = parse_offsets(topic, responses) if is_suitable(dct): zk_type.append(gid) return responses responses = kafka_send(broker, gid, topic, partitions) dct = parse_offsets(topic, responses) if is_suitable(dct): kafka_type.append(gid) return responses
def is_suitable(dct): for x in dct.values(): if x != -1: return True
def kafka_send(broker, gid, topic, partitions): client.send(broker, OffsetFetchRequest_v1(consumer_group=gid, topics=[(topic, partitions)])) return client.poll()
def zk_send(broker, gid, topic, partitions): client.send(broker, OffsetFetchRequest_v0(consumer_group=gid, topics=[(topic, partitions)])) return client.poll()
def do_task(): offset_dict = get_offsets() logsize_dict = get_logsize() print ('----------kafka monitor, info:-------------') for gk, gv in offset_dict.items(): for tk, tv in gv.items(): for pk, pv in tv.items(): if logsize_dict and tk in logsize_dict: dr = logsize_dict[tk] if dr and pk in dr: param = (gk, tk, pk, pv, dr[pk], time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) print(param)
if __name__ == "__main__": client = KafkaClient(bootstrap_servers=servers, request_timeout_ms=3000) while True: do_task() time.sleep(time_interval) duration += time_interval
|