• 从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)])
  • 相关阅读:
    firFox的HttpRequest插件的使用
    注解事务
    log4j配置说明
    对反射用法的初探
    关于set集合和Iterator迭代器的使用
    Eclipse的使用
    各种地图坐标系转换工具
    day67-CSS字体属性、文字属性、背景属性、css盒子模型
    day66-CSS伪类选择器和伪元素选择器
    day65-CSS选择器和样式优先级
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/12755514.html
Copyright © 2020-2023  润新知