首页 > 其他 > 详细

从zk监控canal-client消费延迟情况

时间:2020-04-22 20:26:09      阅读:117      评论:0      收藏:0      [点我收藏+]
#zk监控
import json
from datetime import datetime, timedelta
from kazoo.client import KazooClient
from kazoo.exceptions import NoNodeError


def canal_monitor(**kwargs):
    zk = KazooClient(hosts=host1:port,host2:port,host3:port)
    zk.start()
    instance_list = zk.get_children(/otter/canal/destinations)
    count = 0
    err_count = 0
    for instance in instance_list:
        if instance :
            count = count + 1
            try:
                result = zk.get(/otter/canal/destinations/{instance}/1001/cursor.format(instance=instance))
                time_tuple = get_time(result)
                content = {}.canal consume cursor:{},delay {} minutes.format(instance,
                                                                               time_tuple[0].strftime(%Y-%m-%d %H:%M:%S),time_tuple[1])
                if time_tuple[1] > 60:
                    err_count = err_count + 1
                    print(content)
                else:
                    print(content)
            except NoNodeError as e:
                print(e)

    zk.stop()
    if err_count != 0:
        print(zookeeper监控完毕,当前订阅业务数{},{}延迟!.format(str(count), str(err_count)))


def get_time(result):
    result_str = str(result[0], encoding="utf-8")
    result_json = json.loads(result_str)
    result_ts = result_json[postion][timestamp]
    result_utc_dt = datetime.fromtimestamp(result_ts / 1000)
    result_dt = result_utc_dt + timedelta(hours=8)
    now = datetime.now()
    time_delay = now - result_utc_dt
    return tuple([result_dt, int(time_delay.seconds / 60)])

 

从zk监控canal-client消费延迟情况

原文:https://www.cnblogs.com/wangbin2188/p/12755514.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!