sys.setdefaultencoding('utf-8') os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' import datetime #导入日期时间模块 from kazoo.client import KazooClient def sendmetricjson(value,ip,systag):
? ? ?stime=time.localtime(time.time()) ? ? ?stimenew=time.strftime("%Y-%m-%d %H:%M:%S",stime) ? ? ?stime=time.mktime(time.strptime(stimenew,"%Y-%m-%d %H:%M:%S")) ? ? ?send_data = [{"metric":"system.allservice","endpoint":ip,"timestamp":int(stime),"step":60,"value":value,"valueCount":value,"counterType":"GAUGE", "tags":{"sysname":"Zookeeper_运行状态","EVENT":systag}}] ? ? ?#send_data = json.dumps(send_data, ensure_ascii=False) ? ? ?send_data = json.dumps(send_data) ? ? ?print send_data ? ? ?header_dict = {"Content-Type":"application/json;charset=UTF-8"} ? ? ?requrl = "http://20.5.101.53:12731/v2/push" ? ? ?#requrl for debug ? ? ?#requrl = "http://10.4.146.10:12731/v2/push" ? ? ?s = requests.session() ? ? ?headers = { ? ? ?'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0', ? ? ?} ? ? ?print s.post(requrl, data=send_data, headers=headers).content zk=KazooClient(hosts='10.2.229.112:2181,10.2.229.113:2181,10.2.229.114:2181',timeout=2) zk.start() print zk.get_children('/') zk.create('/qazwsx',makepath=True,ephemeral=True) zk.set('/qazwsx','just do it') print zk.get_children('/') zk.stop() [root@yyjk ~/sbin/mario/Zookeeper]#python a1.py [u'consumers', u'zjtlcb', u'controller_epoch', u'testdirectory0000000028', u'testdirectory0000000027', u'testdirectory0000000029', u'isr_change_notification', u'admin', u'zookeeper', u'testdirectory', u'config', u'dcfs', u'controller', u'brokers'] [u'consumers', u'zjtlcb', u'qazwsx', u'controller_epoch', u'testdirectory0000000028', u'testdirectory0000000027', u'testdirectory0000000029', u'isr_change_notification', u'admin', u'zookeeper', u'testdirectory', u'config', u'dcfs', u'controller', u'brokers'] [root@yyjk ~/sbin/mario/Zookeeper]#python a1.py [u'consumers', u'zjtlcb', u'controller_epoch', u'testdirectory0000000028', u'testdirectory0000000027', u'testdirectory0000000029', u'isr_change_notification', u'admin', u'zookeeper', u'testdirectory', u'config', u'dcfs', u'controller', u'brokers'] [u'consumers', u'zjtlcb', u'qazwsx', u'controller_epoch', u'testdirectory0000000028', u'testdirectory0000000027', u'testdirectory0000000029', u'isr_change_notification', u'admin', u'zookeeper', u'testdirectory', u'config', u'dcfs', u'controller', u'brokers'] [root@yyjk ~/sbin/mario/Zookeeper]#python a1.py [u'consumers', u'zjtlcb', u'controller_epoch', u'testdirectory0000000028', u'testdirectory0000000027', u'testdirectory0000000029', u'isr_change_notification', u'admin', u'zookeeper', u'testdirectory', u'config', u'dcfs', u'controller', u'brokers'] [u'consumers', u'zjtlcb', u'qazwsx', u'controller_epoch', u'testdirectory0000000028', u'testdirectory0000000027', u'testdirectory0000000029', u'isr_change_notification', u'admin', u'zookeeper', u'testdirectory', u'config', u'dcfs', u'controller', u'brokers'] [root@yyjk ~/sbin/mario/Zookeeper]#
?# 创建临时节点
? ? ? ? # 注意:会话丢失、重启会话会导致zookeeper删除重启会话前创建的临时节点
? ? ? ? if not zk_client.exists('/node1/subNode2'):
? ? ? ? ? ? zk_client.create('/node1/subNode2', b'sub node2', ephemeral=True) ?? ??? ??? ? ?? ?#makepath=True是递归创建,如果不加上中间那一段,就是建立一个空的节点 ?
|