应用场景描述
目前在公司业务中,使用ZooKeeper作为协同服务的场景并不多。但是我们会使用Codis作为Redis的集群部署方案,Codis依靠ZooKeeper来存储配置信息。所以监督动物园管理员也很重要。
两个动物园管理员监控点
系统监控
内存使用ZooKeeper应该完全在内存中运行,不能使用SWAP。Java堆大小不能超过可用内存。
使用交换会降低ZooKeeper的性能。设置vm.swappiness=0。
网络带宽占用如果你发现ZooKeeper性能下降,关注网络带宽占用和丢包,ZooKeeper一般写20%,读80%。
磁盘使用ZooKeeper数据目录使用需要注意。
磁盘I/OZooKeeper的磁盘写是异步的,所以不会有大的I/O请求。如果ZooKeeper和其他I/O密集型服务共享,就要注意磁盘I/O的情况。
动物园管理员监控
Zk_avg/min/max_latency是响应客户端请求的时间。如果这个时间超过10Tick,建议报警。
当ZooKeeper超出其处理能力时,zk_outstanding_requests排队的请求数将会增加。建议将警报阈值设置为10
Zk_packets_received客户端请求的数据包数
zk_packets_sent发送给客户列表的包数,主要是响应和通知
Zk_max_file_descriptor_count允许打开的最大文件数由ulimit控制。
Zk_open_file_descriptor_count统计打开文件的数量,当该值大于允许值的85%时报警。
模式的运行角色,如果不在集群中,则是独立的,并加入集群的跟随者或领导者
zk_followerleader角色将有这样的输出,即集合中的追随者数量。正常值应该是集合成员数减1。
zk_pending_syncsleader角色将具有此输出,以及挂起的同步的数量。
zk_znode_countznodes的数量
zk_watch_count手表的数量
Java堆大小ZooKeeper
为Zabbix编写脚本和配置文件来监控ZooKeeper。
Zabbix收集这些监控数据有两种方式。一种是通过zabbixagent单独获取每个监控项,主动监控和被动监控都可以。另一种方法是使用zabbix_sender一次性将这些监控数据全部发送给zabbix。这里我们选择第二种方式。那么一个使用zabbix_sender一次性发送所有监控数据的脚本,不可能一个一个的获取监控项目来写一个类似zabbixagent的脚本。
首先,想办法将监控项组装成一个字典,然后遍历字典,通过指定zabbix_sender的-k和-o参数发送字典中的key:value对。
echomntr|nc127.0.0.12181
这个命令可以由Python的子进程模块调用,也可以使用socket模块访问2181端口,然后发送命令获取数据。获得mntr执行的数据后,需要转换成字典数据。
也就是说,这种类型的数据需要
zk_version3.4.6-1569965, built on 02/20/2014 09:09 GMT zk_avg_latency0 zk_max_latency0 zk_min_latency0 zk_packets_received91 zk_packets_sent90 zk_num_alive_connections1 zk_outstanding_requests0 zk_server_statefollower zk_znode_count17159 zk_watch_count0 zk_ephemerals_count1 zk_approximate_data_size6666471 zk_open_file_descriptor_count27 zk_max_file_descriptor_count102400变成这样的数据。
{'zk_followers': 2, 'zk_outstanding_requests': 0, 'zk_approximate_data_size': 6666471, 'zk_packets_sent': 2089, 'zk_pending_syncs': 0, 'zk_avg_latency': 0, 'zk_version': '3.4.6-1569965, built on 02/20/2014 09:09 GMT', 'zk_watch_count': 2, 'zk_packets_received': 2090, 'zk_open_file_descriptor_count': 30, 'zk_server_ruok': 'imok', 'zk_server_state': 'leader', 'zk_synced_followers': 2, 'zk_max_latency': 28, 'zk_num_alive_connections': 2, 'zk_min_latency': 0, 'zk_ephemerals_count': 1, 'zk_znode_count': 17159, 'zk_max_file_descriptor_count': 102400}最后需要像这样使用zabbix_sender发送的数据格式。
这是密钥的名称
zookeeper.status[zk_outstanding_requests]:0 zookeeper.status[zk_approximate_data_size]:6666471 zookeeper.status[zk_packets_sent]:48 zookeeper.status[zk_avg_latency]:0 zookeeper.status[zk_version]:3.4.6-1569965, built on 02/20/2014 09:09 GMT zookeeper.status[zk_watch_count]:0 zookeeper.status[zk_packets_received]:49 zookeeper.status[zk_open_file_descriptor_count]:27 zookeeper.status[zk_server_ruok]:imok zookeeper.status[zk_server_state]:follower zookeeper.status[zk_max_latency]:0 zookeeper.status[zk_num_alive_connections]:1 zookeeper.status[zk_min_latency]:0 zookeeper.status[zk_ephemerals_count]:1 zookeeper.status[zk_znode_count]:17159 zookeeper.status[zk_max_file_descriptor_count]:102400简化代码如下:
#!/usr/bin/python import socket #from StringIO import StringIO from cStringIO import StringIO s=socket.socket() s.connect(('localhost',2181)) s.send('mntr') data_mntr=s.recv(2048) s.close() #print data_mntr h=StringIO(data_mntr) result={} zresult={} for line in h.readlines(): key,value=map(str.strip,line.split('\t')) zkey='zookeeper.status' + '[' + key + ']' zvalue=value result[key]=value zresult[zkey]=zvalue print result print '\n\n' print zresult # python test.py {'zk_outstanding_requests': '0', 'zk_approximate_data_size': '6666471', 'zk_max_latency': '0', 'zk_avg_latency': '0', 'zk_version': '3.4.6-1569965, built on 02/20/2014 09:09 GMT', 'zk_watch_count': '0', 'zk_num_alive_connections': '1', 'zk_open_file_descriptor_count': '27', 'zk_server_state': 'follower', 'zk_packets_sent': '542', 'zk_packets_received': '543', 'zk_min_latency': '0', 'zk_ephemerals_count': '1', 'zk_znode_count': '17159', 'zk_max_file_descriptor_count': '102400'} {'zookeeper.status[zk_watch_count]': '0', 'zookeeper.status[zk_avg_latency]': '0', 'zookeeper.status[zk_max_latency]': '0', 'zookeeper.status[zk_approximate_data_size]': '6666471', 'zookeeper.status[zk_server_state]': 'follower', 'zookeeper.status[zk_num_alive_connections]': '1', 'zookeeper.status[zk_min_latency]': '0', 'zookeeper.status[zk_outstanding_requests]': '0', 'zookeeper.status[zk_packets_received]': '543', 'zookeeper.status[zk_ephemerals_count]': '1', 'zookeeper.status[zk_znode_count]': '17159', 'zookeeper.status[zk_packets_sent]': '542', 'zookeeper.status[zk_open_file_descriptor_count]': '27', 'zookeeper.status[zk_max_file_descriptor_count]': '102400', 'zookeeper.status[zk_version]': '3.4.6-1569965, built on 02/20/2014 09:09 GMT'}详细代码如下:
#!/usr/bin/python """ Check Zookeeper Cluster zookeeper version should be newer than 3.4.x # echo mntr|nc 127.0.0.1 2181 zk_version3.4.6-1569965, built on 02/20/2014 09:09 GMT zk_avg_latency0 zk_max_latency4 zk_min_latency0 zk_packets_received84467 zk_packets_sent84466 zk_num_alive_connections3 zk_outstanding_requests0 zk_server_statefollower zk_znode_count17159 zk_watch_count2 zk_ephemerals_count1 zk_approximate_data_size6666471 zk_open_file_descriptor_count29 zk_max_file_descriptor_count102400 # echo ruok|nc 127.0.0.1 2181 imok """ import sys import socket import re import subprocess from StringIO import StringIO import os zabbix_sender = '/opt/app/zabbix/sbin/zabbix_sender' zabbix_conf = '/opt/app/zabbix/conf/zabbix_agentd.conf' send_to_zabbix = 1 ############# get zookeeper server status class ZooKeeperServer(object): def __init__(self, host='localhost', port='2181', timeout=1): self._address = (host, int(port)) self._timeout = timeout self._result = {} def _create_socket(self): return socket.socket() def _send_cmd(self, cmd): """ Send a 4letter word command to the server """ s = self._create_socket() s.settimeout(self._timeout) s.connect(self._address) s.send(cmd) data = s.recv(2048) s.close() return data def get_stats(self): """ Get ZooKeeper server stats as a map """ data_mntr = self._send_cmd('mntr') data_ruok = self._send_cmd('ruok') if data_mntr: result_mntr = self._parse(data_mntr) if data_ruok: result_ruok = self._parse_ruok(data_ruok) self._result = dict(result_mntr.items() + result_ruok.items()) if not self._result.has_key('zk_followers') and not self._result.has_key('zk_synced_followers') and not self._result.has_key('zk_pending_syncs'): ##### the tree metrics only exposed on leader role zookeeper server, we just set the followers' to 0 leader_only = {'zk_followers':0,'zk_synced_followers':0,'zk_pending_syncs':0} self._result = dict(result_mntr.items() + result_ruok.items() + leader_only.items() ) return self._result def _parse(self, data): """ Parse the output from the 'mntr' 4letter word command """ h = StringIO(data) result = {} for line in h.readlines(): try: key, value = self._parse_line(line) result[key] = value except ValueError: pass # ignore broken lines return result def _parse_ruok(self, data): """ Parse the output from the 'ruok' 4letter word command """ h = StringIO(data) result = {} ruok = h.readline() if ruok: result['zk_server_ruok'] = ruok return result def _parse_line(self, line): try: key, value = map(str.strip, line.split('\t')) except ValueError: raise ValueError('Found invalid line: %s' % line) if not key: raise ValueError('The key is mandatory and should not be empty') try: value = int(value) except (TypeError, ValueError): pass return key, value def get_pid(self): # ps -ef|grep java|grep zookeeper|awk '{print $2}' pidarg = '''ps -ef|grep java|grep zookeeper|grep -v grep|awk '{print $2}' ''' pidout = subprocess.Popen(pidarg,shell=True,stdout=subprocess.PIPE) pid = pidout.stdout.readline().strip('\n') return pid def send_to_zabbix(self, metric): key = "zookeeper.status[" + metric + "]" if send_to_zabbix > 0: #print key + ":" + str(self._result[metric]) try: subprocess.call([zabbix_sender, "-c", zabbix_conf, "-k", key, "-o", str(self._result[metric]) ], stdout=FNULL, stderr=FNULL, shell=False) except OSError, detail: print "Something went wrong while exectuting zabbix_sender : ", detail else: print "Simulation: the following command would be execucted :\n", zabbix_sender, "-c", zabbix_conf, "-k", key, "-o", self._result[metric], "\n" def usage(): """Display program usage""" print "\nUsage : ", sys.argv[0], " alive|all" print "Modes : \n\talive : Return pid of running zookeeper\n\tall : Send zookeeper stats as well" sys.exit(1) accepted_modes = ['alive', 'all'] if len(sys.argv) == 2 and sys.argv[1] in accepted_modes: mode = sys.argv[1] else: usage() zk = ZooKeeperServer() # print zk.get_stats() pid = zk.get_pid() if pid != "" and mode == 'all': zk.get_stats() # print zk._result FNULL = open(os.devnull, 'w') for key in zk._result: zk.send_to_zabbix(key) FNULL.close() print pid elif pid != "" and mode == "alive": print pid else: print 0Zabbix配置文件check_zookeeper.conf
UserParameter=zookeeper.status[*],/usr/bin/python /opt/app/zabbix/sbin/check_zookeeper.py $1重新启动zabbix代理服务
制作Zabbix监控ZooKeeper的模板,设置报警阈值。
模板见附件。
参考文件:
https://blog.serverdensity.com/how-to-monitor-zookeeper/
https://github.com/Apache/zookeeper/tree/trunk/src/contrib/monitoring
http://john88wang.blog.51cto.com/2165294/1708302
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)