美文网首页
使用Python执行Zookeeper四字命令

使用Python执行Zookeeper四字命令

作者: liuhailong | 来源:发表于2019-04-28 11:37 被阅读0次

    1. 缘起

    实现Zookeeper集群监控任务时候,需要用4字命令探测Zookeeper运行状态。

    2. 分析

    如果只需要执行Zookeeper四字命令,不需要ZkClient,简单的Socket链接即可。

    3. 基本思路

    • 建立Socket链接;

    • 发送四字命令;

    • 接收返回信息;

    • 解析返回信息。

    4. 代码

    import socket
     
     
    """
        by liuhailong2008#foxmail.com
    """
     
     
    def multi_line_with_split(data, split_char):
        lines = data.split('\n')
        ret = {}
        for line in lines:
            if len(line) == 0:
                continue
            item = line.split(split_char)
            if len(item) > 1:
                ret[item[0]] = item[1]
        return ret
     
     
    def multi_line_with_equal_split(data):
        return multi_line_with_split(data, '=')
     
     
    def multi_line_with_tab_split(data):
        return multi_line_with_split(data, '\t')
     
     
    def multi_line_with_colon_split(data):
        return multi_line_with_split(data, ':')
     
     
    def single_line(data):
        return {'result':data}
     
     
    response_parser_repository = {
        'conf': multi_line_with_equal_split,
        'envi': multi_line_with_equal_split,
        'mntr': multi_line_with_tab_split,
        'srvr': multi_line_with_colon_split,
        'ruok': single_line,
     
    }
     
     
    def get_response_parser(four_word_command):
        return response_parser_repository.get(four_word_command, None)
     
     
    def talk_to_zookeeper(four_word_command, host='127.0.0.1', port=2181,  timeout=1):
        conn = socket.socket()
        conn.settimeout(timeout)
        conn.connect((host,port))
        conn.send(four_word_command)
        response_data = conn.recv(2048)
        conn.close()
        # print response_data
        parser = get_response_parser(four_word_command)
        if parser is None:
            return response_data
        return parser(response_data)
     
     
    if __name__ == '__main__':
        print 'ruok: %s ' % talk_to_zookeeper('ruok')
        print 'conf: %s ' % talk_to_zookeeper('conf')
        print 'envi: %s ' % talk_to_zookeeper('envi')
        print 'srvr: %s ' % talk_to_zookeeper('srvr')
        print 'mntr: %s ' % talk_to_zookeeper('mntr')
    

    程序执行输出:

    ruok: {'result': 'imok'} 
    conf: {'clientPort': '2181', 'minSessionTimeout': '4000', 'maxClientCnxns': '60', 'dataLogDir': '/Users/liuhailong/work/zookeeper-3.4.9/bin/./data/version-2', 'dataDir': '/Users/liuhailong/work/zookeeper-3.4.9/bin/./data/version-2', 'maxSessionTimeout': '40000', 'serverId': '0', 'tickTime': '2000'} 
    envi: {'java.io.tmpdir': '/var/folders/dz/x623731n505999d3gky1jyg00000gn/T/', 'java.class.path': '/Users/liuhailong/work/zookeeper-3.4.9/bin/../build/classes:/Users/liuhailong/work/zookeeper-3.4.9/bin/../build/lib/*.jar:/Users/liuhailong/work/zookeeper-3.4.9/bin/../lib/slf4j-log4j12-1.6.1.jar:/Users/liuhailong/work/zookeeper-3.4.9/bin/../lib/slf4j-api-1.6.1.jar:/Users/liuhailong/work/zookeeper-3.4.9/bin/../lib/netty-3.10.5.Final.jar:/Users/liuhailong/work/zookeeper-3.4.9/bin/../lib/log4j-1.2.16.jar:/Users/liuhailong/work/zookeeper-3.4.9/bin/../lib/jline-0.9.94.jar:/Users/liuhailong/work/zookeeper-3.4.9/bin/../zookeeper-3.4.9.jar:/Users/liuhailong/work/zookeeper-3.4.9/bin/../src/java/lib/*.jar:/Users/liuhailong/work/zookeeper-3.4.9/bin/../conf:', 'java.library.path': '/Users/liuhailong/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.', 'java.compiler': '<NA>', 'os.version': '10.14.4', 'os.arch': 'x86_64', 'zookeeper.version': '3.4.9-1757313, built on 08/23/2016 06:50 GMT', 'java.home': '/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre', 'user.dir': '/Users/liuhailong/work/zookeeper-3.4.9/bin', 'java.version': '1.8.0_171', 'os.name': 'Mac OS X', 'java.vendor': 'Oracle Corporation', 'host.name': '192.168.1.5', 'user.home': '/Users/liuhailong', 'user.name': 'liuhailong'} 
    srvr: {'Received': ' 104', 'Node count': ' 4', 'Outstanding': ' 0', 'Zxid': ' 0x6', 'Zookeeper version': ' 3.4.9-1757313, built on 08/23/2016 06', 'Connections': ' 1', 'Latency min/avg/max': ' 0/1/8', 'Mode': ' standalone', 'Sent': ' 103'} 
    mntr: {'zk_outstanding_requests': '0', 'zk_approximate_data_size': '27', 'zk_max_latency': '8', 'zk_avg_latency': '1', 'zk_version': '3.4.9-1757313, built on 08/23/2016 06:50 GMT', 'zk_watch_count': '0', 'zk_num_alive_connections': '1', 'zk_open_file_descriptor_count': '31', 'zk_server_state': 'standalone', 'zk_packets_sent': '104', 'zk_packets_received': '105', 'zk_min_latency': '0', 'zk_ephemerals_count': '0', 'zk_znode_count': '4', 'zk_max_file_descriptor_count': '10240'} 
    

    相关文章

      网友评论

          本文标题:使用Python执行Zookeeper四字命令

          本文链接:https://www.haomeiwen.com/subject/odhdnqtx.html