zoukankan      html  css  js  c++  java
  • 从zk监控canal-client消费延迟情况

    #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)])
  • 相关阅读:
    立则存
    如何在UI控件上绑定一个变量
    欢迎大家使用www.eugene.org.cn访问我的blog
    属性与字段变量的区别
    在C#中联合Excel的Com编程入门
    我和我的土匪奶奶 全集 全 下载 txt
    大菲波数
    Hat's Fibonacci
    Fibonacci 取余,直接做超时
    How Many Fibs? 字符串转换处理
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/12755514.html
Copyright © 2011-2022 走看看