def connect(path=None): if path is None: path=zk.source.com:2181 pykeeper.install_log_stream() client = pykeeper.ZooKeeper(path) client.connect() print path+"连接成功!" return client
def GetActiveHmaster(self): try: client = pykeeper.ZooKeeper(self.zkSvr) client.connect() hmaster = client.get(self.path) return hmaster except: pass finally: client.close()
def GetActiveNN(self,zkServer,path): try: client = pykeeper.ZooKeeper(zkServer) client.connect() ActiveNode =HAZKInfo_pb2.ActiveNodeInfo() ActiveNode.ParseFromString(client.get(path)[0]) ActiveNamenode = ActiveNode.hostname.strip() print "ActiveNamenode : %s" % (ActiveNamenode) #return ActiveNamenode except: print "%s timeout" % zkServer finally: client.close()
def GetActiveYarn(self,zkServer,yarn): try: client = pykeeper.ZooKeeper(zkServer) client.connect() path = '/yarn-leader-election/%s/ActiveBreadCrumb' % yarn ActiveRMInfo = yarn_server_resourcemanager_service_protos_pb2.ActiveRMInfoProto() ActiveRMInfo.ParseFromString(client.get(path)[0]) ActiveRMID = ActiveRMInfo.rmId.strip() print "ActiveRMID : %s" % ActiveRMID # Yarn1 if self.service == 'yarn1' and ActiveRMID == 'rm1': return "bj-dc-jn-001.tendcloud.com" elif self.service == 'yarn1' and ActiveRMID == 'rm2': return "bj-dc-jn-002.tendcloud.com" except: pass finally: client.close()
def check_zookeeper_lock(key, value, logger): zk_key = "%s/%s" % (FLAGS.zk_lockpath, key) zk = None try: pykeeper.install_log_stream() zk = pykeeper.ZooKeeper(FLAGS.zk_lockserver) zk.connect() if FLAGS.unlock: try: zk.delete(zk_key) logger.info('%s has been deleted', zk_key) except NoNodeException: logger.info('%s has been deleted.', zk_key) except: logger.error("Clean lock failed %s, %s", zk_key, traceback.format_exc()) sys.exit(0) if zk.exists(zk_key): logger.warning('%s is running, val %s', key, zk.get(zk_key)) sys.exit(1) except: logger.error("Zookeeper failed %s, %s", zk_key, traceback.format_exc()) sys.exit(0) try: try: zk.create_recursive(FLAGS.zk_lockpath, '') zk.create(zk_key, value) except: logger.error("Zookeeper lock failed %s, %s", zk_key, traceback.format_exc()) sys.exit(0) yield finally: if FLAGS.autounlock: zk.delete(zk_key)
import_json = "" zk_host = '127.0.0.1' zk_port = '2181' for op, value in opts: if op in ("-h", "--help"): help() exit(1) if op in ("-i", "--import"): import_json = value elif op in ("-z", "--zkhost"): zk_host = value elif op in ("-p", "--zkport"): zk_port = value else: help() exit(1) if not import_json: help() exit(1) pykeeper.install_log_stream() client = pykeeper.ZooKeeper(":".join([zk_host, zk_port])) client.connect() data = json.load(open(import_json), "utf-8") zk_import(client, data) zk_dump(client) pass