有時候?qū)τ谝恍┳缘絲ookeeper的java服務(wù)屡立,因開發(fā)無法給出相應(yīng)的端口做監(jiān)控,所以想通過監(jiān)控工具對其進行監(jiān)控比較困難搀军,這里我們可以通過python獲取zookeeper的節(jié)點來判斷膨俐。
安裝zk:
wget http://ftp.cuhk.edu.hk/pub/packages/apache.org/zookeeper/zookeeper-3.3.6/zookeeper-3.3.6.tar.gz
tar zxf zookeeper-3.3.6.tar.gz
安裝zookeeper的c客戶端
cd zookeeper-3.3.6/src/c
make && make install
啟動zk,并且測試是否生效:
./cli_mt localhost:2181
Watcher SESSION_EVENT state = CONNECTED_STATE
Got a new session id: 0x23f9d77d3fe0001
安裝python客戶端
wget https://pypi.python.org/packages/14/38/a761465ab0a154405c11f7e5d6e81edf6e84584e114e152fddd340f7d6d3/zkpython-0.4.2.tar.gz
cd zkpython-0.4.2
python3 setup.py install
[root@centos3 zkpython-0.4.2]# python3
Python 3.4.7 (default, Sep 5 2017, 17:40:43)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import zookeeper
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libzookeeper_mt.so.2: cannot open shared object file: No such file or directory
出現(xiàn)這個錯誤請在/etc/profile里添加如下變量: export LD_LIBRARY_PATH=/usr/local/lib
[root@centos3 liujiangbu]# python3
Python 3.4.7 (default, Sep 5 2017, 17:40:43)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>from kazoo.client import KazooClient
vim zk.py
#!/usr/bin/python3
import logging
from time import sleep
from kazoo.client import KazooClient
from kazoo.client import KazooState
zk = KazooClient('127.0.0.1:2181')
zk.start()
#判斷zk客戶端是否與server連接
def my_listener():
if zk.state == "LOST":
print("1111")# Register somewhere that the session was lost
elif zk.state == "SUSPENDED":
print("222222")
# Handle being disconnected from Zookeeper
else:
# Handle being connected/reconnected to Zookeeper
print("6666")
# print log to console
#logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
#zk = KazooClient('127.0.0.1:2181')
#zk.start()
def children_callback(children):
print('****',children)
children = zk.get_children('/zookeeper', children_callback)
my_listener()
zk.create('/zookeeper/goodboy12364567777')
#zk.delete('/zookeeper/555555')
while True: sleep(1)
節(jié)點監(jiān)視功能解析:
>>> def children_callback(children):
... print('****',children)
...
>>> children = zk.get_children('/zookeeper/goodboy', children_callback)
>>> zk.create('/zookeeper/goodboy/12364567777333')
**** WatchedEvent(type='CHILD', state='CONNECTED', path='/zookeeper/goodboy')
**** WatchedEvent(type='CHILD', state='CONNECTED', path='/zookeeper/goodboy')
'/zookeeper/goodboy/12364567777333'
不過上面的 方法是一次性調(diào)用的,適用于節(jié)點長期穩(wěn)定不怎么變化的,對于節(jié)點經(jīng)常變更的使用于下面的方法:
>>> '/zookeeper/goodboy/12364567777333'
'/zookeeper/goodboy/12364567777333'
>>> @zk.ChildrenWatch("/zookeeper/goodboy")
... def watch_children(children):
... print("Children are now: %s" % children)
...
Children are now: ['12364567777333']
這個是動態(tài)手機節(jié)點變化的赏胚,當我在這個節(jié)點下再創(chuàng)建一個node衫冻,它會立馬返回/zookeeper/goodboy下面的節(jié)點列表:
>>> @zk.ChildrenWatch("/zookeeper/goodboy")Children are now: ['wwww12364567777', '12364567777333']
使用
from kazoo.client import KazooClient
zk = KazooClient(hosts='127.0.0.1:2181')
zk.start() //沒有異常的話就意味著連接上zookeeper
children = zk.get_children('/')
zk.stop()
獲取節(jié)點信息
zk.get_children("/zookeeper")
['goodboy', 'goodboy123', 'goodboy1', 'goodboy12', 'quota']
zk.get('/zookeeper/goodboy')
(b'', ZnodeStat(czxid=14, mzxid=14, ctime=1505749749490, mtime=1505749749490, version=0, cversion=0, aversion=0, ephemeralOwner=0, dataLength=0, numChildren=0, pzxid=14))
zk.state
'CONNECTED'