├── README.md ├── getZookeeperInfo.py └── zbx_zookeeper_templates.xml /README.md: -------------------------------------------------------------------------------- 1 | ##Description: 2 | The ZooKeeper service can be monitored in one of two primary ways; 3 | 4 | 1) the command port through the use of 4 letter words 5 | 6 | 2) JMX. See the appropriate section for your environment/requirements. 7 | 8 | 1. zookeeper命令行模式结合telnet或者nc:http://zookeeper.apache.org/doc/r3.4.6/zookeeperAdmin.html#sc_zkCommands 9 | 10 | 2. 通过JMX监控zookeeper:http://zookeeper.apache.org/doc/r3.4.6/zookeeperJMX.html 11 | 12 | ##模板使用方法: 13 | Monitoring zookeeper remotely on zabbix server. 14 | 15 | 1.upload getZookeeperInfo.py to externalscripts folder. A chmod/chown to get execution permission is necessary. 16 | 17 | 2.import the template and linked to the host. 18 | 19 | ##Macros 20 | 21 | You don't need to modify the template if you are using the standard port to access to the zookeeper (port 2181). 22 | If you need a different zookeeper port for the monitor, you will need to modify the {$ZOOKEEPERPORT} macro. 23 | 24 | ##Triggers 25 | 26 | There are 2 trigger to detect zookeeper port issues. 27 | 28 | ##Graphs 29 | 30 | There is a graph of zookeeper received packets and sended packets. 31 | 32 | 33 | ##Contributors: 34 | 35 | * [xiaobao623 ] (https://github.com/xiaobao623) 36 | -------------------------------------------------------------------------------- /getZookeeperInfo.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | 5 | import getopt, sys 6 | from telnetlib import Telnet 7 | 8 | # default Zookeeper server to check 9 | ZookeeperServer = '127.0.0.1' 10 | ZookeeperPort = 2181 11 | ZookeeperCommand = 'mntr' 12 | ZookeeperKey = 'zk_version' 13 | 14 | # ZooKeeper Commands: The Four Letter Words 15 | # Referer: http://zookeeper.apache.org/doc/r3.4.6/zookeeperAdmin.html#sc_zkCommands 16 | 17 | CommandKey={ 18 | 'conf':['clientPort','dataDir','dataLogDir','tickTime','maxClientCnxns','minSessionTimeout','maxSessionTimeout','serverId','initLimit','syncLimit','electionAlg','electionPort','quorumPort','peerType'], 19 | 'ruok':['state'], 20 | 'mntr':['zk_version','zk_avg_latency','zk_max_latency','zk_min_latency','zk_packets_received','zk_packets_sent','zk_num_alive_connections','zk_outstanding_requests','zk_server_state','zk_znode_count','zk_watch_count','zk_ephemerals_count','zk_approximate_data_size','zk_open_file_descriptor_count','zk_max_file_descriptor_count','zk_followers','zk_synced_followers','zk_pending_syncs'] 21 | } 22 | 23 | class ZooKeeperCommands(object): 24 | #---------------------------------------------------------------------- 25 | def __init__(self,server,port,zkCommand,zkKey): 26 | self._server = server 27 | self._port = port 28 | self._zkCommand = zkCommand 29 | self._zkKey = zkKey 30 | self._value_raw = None 31 | self._value = None 32 | 33 | def zkExec(self): 34 | self._exec_command() 35 | self._parse_value() 36 | return self._value 37 | 38 | def _exec_command(self): 39 | tn = Telnet(self._server, self._port, timeout=30) 40 | # tn.read_until('login: ') 41 | # tn.write(username + '\n') 42 | # tn.read_until('password: ') 43 | # tn.write(password + '\n') 44 | # tn.read_until(finish) 45 | tn.write('%s\n' % self._zkCommand) 46 | # tn.write('conf\n') 47 | tn.write('quit\n') 48 | self._value_raw = tn.read_all() 49 | # print type(self._value_raw) 50 | # print (self._value_raw) 51 | 52 | def _parse_value(self): 53 | self._value = {} 54 | if self._zkCommand == 'mntr': 55 | for line in self._value_raw.splitlines(): 56 | parts = line.split(' ') 57 | index = parts[0] 58 | self._value[index] = parts[1] 59 | elif self._zkCommand == 'conf': 60 | for line in self._value_raw.splitlines(): 61 | parts = line.split('=') 62 | index = parts[0] 63 | self._value[index] = parts[1] 64 | elif self._zkCommand == 'ruok': 65 | 66 | if self._value_raw == 'imok': 67 | self._value['state'] = 1 68 | # print " server is running in a non-error state." 69 | else: 70 | self._value['state'] = 0 71 | # print "ruok command does not respond at all" 72 | else: 73 | "zkCommand is wrong" 74 | 75 | ########################################################################################################### 76 | 77 | def Usage(): 78 | print ''' 79 | Usage: 80 | getZookeeperInfo.py -h 127.0.0.1 -p 2181 -c -k " 81 | getZookeeperInfo.py --host 127.0.0.1 --port 2181 --zkCommand --zkKey 82 | ''' 83 | for key in CommandKey: 84 | print 'zkCommand:%s, zkKey:%s' %(key, CommandKey[key]) 85 | 86 | ########################################################################################################### 87 | 88 | def main(host, port, zkCommand, zkKey): 89 | 90 | try: 91 | options, args = getopt.getopt( sys.argv[1:], "h:p:c:k:", ["host=","port=","zkCommand=","zkKey="]); 92 | for name,value in options: 93 | if name in ('-h','--host'): 94 | host = value 95 | if name in ('-p','--port'): 96 | port = value 97 | if name in ('-c','--zkCommand'): 98 | zkCommand = value 99 | if name in ('-k','--zkKey'): 100 | zkKey = value 101 | except getopt.GetoptError, err: 102 | print str(err) 103 | Usage() 104 | sys.exit(1) 105 | 106 | 107 | data = ZooKeeperCommands(host, port, zkCommand, zkKey) 108 | items = data.zkExec() 109 | try: 110 | print items[zkKey] 111 | except: 112 | print "Not valid item." 113 | Usage() 114 | if __name__ == '__main__': 115 | main(ZookeeperServer, ZookeeperPort,ZookeeperCommand,ZookeeperKey) 116 | -------------------------------------------------------------------------------- /zbx_zookeeper_templates.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 2.0 4 | 2015-07-07T01:51:15Z 5 | 6 | 7 | Templates 8 | 9 | 10 | 11 | 813 | 814 | 815 | 816 | {Template ZooKeeper Info:getZookeeperInfo.py["-h","{HOST.CONN}","-p","{$ZOOKEEPERPORT}","-c","ruok","-k","state"].last(#3)}=0 817 | The Zookeeper is not responding in last 3 times. 818 | 819 | 1 820 | 4 821 | 822 | 0 823 | 824 | 825 | 826 | {Template ZooKeeper Info:net.tcp.service[tcp,,{$ZOOKEEPERPORT}].count(#2,0,"eq")}=2 827 | ZooKeeperport {$ZOOKEEPERPORT} is not running 828 | 829 | 0 830 | 4 831 | 832 | 0 833 | 834 | 835 | 836 | 837 | 838 | ZooKeeper Packets 839 | 900 840 | 200 841 | 0.0000 842 | 100.0000 843 | 1 844 | 1 845 | 0 846 | 1 847 | 0 848 | 0.0000 849 | 0.0000 850 | 0 851 | 0 852 | 0 853 | 0 854 | 855 | 856 | 0 857 | 1 858 | 99FF99 859 | 0 860 | 2 861 | 0 862 | 863 | Template ZooKeeper Info 864 | getZookeeperInfo.py["-h","{HOST.CONN}","-p","{$ZOOKEEPERPORT}","-c","mntr","-k","zk_packets_received"] 865 | 866 | 867 | 868 | 1 869 | 2 870 | C80000 871 | 0 872 | 2 873 | 0 874 | 875 | Template ZooKeeper Info 876 | getZookeeperInfo.py["-h","{HOST.CONN}","-p","{$ZOOKEEPERPORT}","-c","mntr","-k","zk_packets_sent"] 877 | 878 | 879 | 880 | 881 | 882 | 883 | --------------------------------------------------------------------------------