Exemple #1
0
def ajax_clients(request):
    """
    ajax获取客户端数据
    :param request:
    :return:
    """
    if request.method == "POST":
        data = request.POST
        service = data.get("service", None)
        results = []
        if service and len(service) > 0:
            h = settings.ZK_HARPC
            clients = h.get_clients(service=service)
            for client in clients:
                client_name = safe_list_get(client, 0, None)
                isOperate = safe_list_get(client, 1, False)

                client_info_dict = h.get_service_child_info(
                    service=service, type=settings.ZK_CLIENTS, child=client_name
                )

                results.append(
                    [
                        client_name,
                        client_info_dict.get("name", None),
                        client_info_dict.get("owner", None),
                        client_info_dict.get("protocol", None),
                        client_info_dict.get("ctime", None),
                        isOperate,
                    ]
                )

        return JsonResponse({"data": results}, safe=False)
Exemple #2
0
def ajax_service(request):
    """
    ajax获取serivce数据
    :param request:
    :return:
    """
    if request.method == "POST":
        results = []
        h = settings.ZK_HARPC
        services = h.get_services()
        for service in services:
            service_name = safe_list_get(service, 0, None)
            isOperate = safe_list_get(service, 1, False)

            servers = h.get_servers(service_name)
            clients = h.get_clients(service_name)

            server_list = []
            client_list = []

            for server in servers:
                server_list.append(safe_list_get(server, 0, None))

            for client in clients:
                client_list.append(safe_list_get(client, 0, None))

            results.append([service_name, ",".join(server_list), ",".join(client_list), isOperate])

    return JsonResponse({"data": results}, safe=False)
Exemple #3
0
def ajax_servers(request):
    """
    获取server数据
    :param request:
    :return:
    """
    if request.method == "POST":
        data = request.POST
        service = data.get("service", None)
        results = []
        if service and len(service) > 0:
            h = settings.ZK_HARPC
            servers = h.get_servers(service=service)
            for server in servers:
                server_name = safe_list_get(server, 0, None)
                isOperate = safe_list_get(server, 1, False)

                server_info_dict = h.get_service_child_info(
                    service=service, type=settings.ZK_SERVERS, child=server_name
                )

                results.append(
                    [
                        server_name,
                        server_info_dict.get("name", None),
                        server_info_dict.get("owner", None),
                        server_info_dict.get("protocol", None),
                        server_info_dict.get("ctime", None),
                        isOperate,
                    ]
                )

        return JsonResponse({"data": results}, safe=False)
Exemple #4
0
def ajax_clients(request):
    """
    ajax获取客户端数据
    :param request:
    :return:
    """
    if request.method == 'POST':
        data = request.POST
        service = data.get('service', None)
        results = []
        if service and len(service) > 0:
            h = settings.ZK_HARPC
            clients = h.get_clients(service=service)
            for client in clients:
                client_name = safe_list_get(client, 0, None)
                isOperate = safe_list_get(client, 1, False)

                client_info_dict = h.get_service_child_info(
                    service=service,
                    type=settings.ZK_CLIENTS,
                    child=client_name)

                results.append([
                    client_name,
                    client_info_dict.get('name', None),
                    client_info_dict.get('owner', None),
                    client_info_dict.get('protocol', None),
                    client_info_dict.get('ctime', None), isOperate
                ])

        return JsonResponse({'data': results}, safe=False)
Exemple #5
0
def ajax_service(request):
    """
    ajax获取serivce数据
    :param request:
    :return:
    """
    if request.method == "POST":
        results = []
        h = settings.ZK_HARPC
        services = h.get_services()
        for service in services:
            service_name = safe_list_get(service, 0, None)
            isOperate = safe_list_get(service, 1, False)

            servers = h.get_servers(service_name)
            clients = h.get_clients(service_name)

            server_list = []
            client_list = []

            for server in servers:
                server_list.append(safe_list_get(server, 0, None))

            for client in clients:
                client_list.append(safe_list_get(client, 0, None))

            results.append([
                service_name, ",".join(server_list), ",".join(client_list),
                isOperate
            ])

    return JsonResponse({'data': results}, safe=False)
Exemple #6
0
def ajax_servers(request):
    """
    获取server数据
    :param request:
    :return:
    """
    if request.method == 'POST':
        data = request.POST
        service = data.get('service', None)
        results = []
        if service and len(service) > 0:
            h = settings.ZK_HARPC
            servers = h.get_servers(service=service)
            for server in servers:
                server_name = safe_list_get(server, 0, None)
                isOperate = safe_list_get(server, 1, False)

                server_info_dict = h.get_service_child_info(
                    service=service,
                    type=settings.ZK_SERVERS,
                    child=server_name)

                results.append([
                    server_name,
                    server_info_dict.get('name', None),
                    server_info_dict.get('owner', None),
                    server_info_dict.get('protocol', None),
                    server_info_dict.get('ctime', None), isOperate
                ])

        return JsonResponse({'data': results}, safe=False)
Exemple #7
0
    def load_zk_resource(self):
        logger.info('get service from zookeeper')
        # 如果系统节点数过多请注释该条不启用监听器实时更新节点信息,以避免ZK性能受到影响
        # service_list = self.zk.get_children(path=self.zk_root, watch=self.watch_services)
        service_list = self.zk.get_children(path=self.zk_root)
        services = {}
        for service in service_list:

            acl = False
            try:
                result = self.get_acls("%s/%s/%s" % (self.zk_root, service, self.zk_servers))
                acls = safe_list_get(result, 0, [])
                if self.zk_harpc_acl in acls:
                    acl = True
            except Exception, e:
                acl = False

            servers = {}
            # 如果系统节点数过多请注释该条不启用监听器实时更新节点信息,以避免ZK性能受到影响
            # if self.zk.exists(path="%s/%s/%s" % (self.zk_root,service,self.zk_servers),watch=self.watch_servers_exists):
            if self.zk.exists(path="%s/%s/%s" % (self.zk_root, service, self.zk_servers)):
                # 如果系统节点数过多请注释该条不启用监听器实时更新节点信息,以避免ZK性能受到影响
                # server_list= self.zk.get_children(path="%s/%s/%s/" % (self.zk_root, service, self.zk_servers),watch=self.watch_servers)
                server_list = self.zk.get_children(path="%s/%s/%s/" % (self.zk_root, service, self.zk_servers))
                for server in server_list:
                    server_data_dict = {}
                    server_data = self.zk.get(path="%s/%s/%s/%s" % (self.zk_root, service, self.zk_servers, server))
                    try:
                        server_data_str = safe_list_get(server_data, 0, "{}")
                        server_data_dict = json.loads(server_data_str)
                    except Exception, e:
                        logger.error(
                            'manage.ZKWrap.load_zk_resource getdata:' + service + "/" + server + "/" + getExceptionDesc(
                                e))
                        pass
                    # if len(server_data_str) > 2:
                    #     server_data_dict = eval(server_data_str)
                    server_node_stat = safe_list_get(server_data, 1, None)
                    if server_node_stat:
                        server_data_dict['ctime'] = time.strftime("%Y-%m-%d %H:%M:%S",
                                                                  time.localtime(int(server_node_stat.ctime / 1000)))
                    server_acl = False
                    result = self.get_acls("%s/%s/%s/%s" % (self.zk_root, service, self.zk_servers, server))
                    acls = safe_list_get(result, 0, [])
                    if self.zk_harpc_acl in acls:
                        server_acl = True

                    servers[server] = {"data": server_data_dict, "acl": server_acl}
Exemple #8
0
    def get_clients_total(self,service):
        """
        获取一个clients的合计信息
        :param service:
        :return:
        """
        statistics_series = settings.ZK_STATISTICS_SERIES
        results = {"avgtime":0,'mintime':0,"maxtime":0,"qps":0.0,"success":0,"failure":0}

        try:
            clients = self.resource[service][self.zk_clients]
            for key in clients:
                try:
                    result = self.zk.get(path="%s/%s/%s/%s/%s/" % (self.zk_root, service, self.zk_statistics, self.zk_clients, key))
                    total = {}
                    try:
                        result_str = safe_list_get(result,0,"{}")
                        result_dict = json.loads(result_str)
                        total = result_dict['total']
                    except Exception,e:
                        logger.error('manage.harpc.get_clients_total getdata:' + getExceptionDesc(e))
                        pass
                    for statistics_serie in statistics_series:
                        serie_name = statistics_serie['name']
                        serie_value = total.get(serie_name,0)
                        results[serie_name] = results.get(serie_name,0)+serie_value
                except Exception,e:
                    continue
Exemple #9
0
    def watch_services(self, event):
        logger.info("services is change:" + str(event))
        new_service_list = self.zk.get_children(path="/%s" % (self.zk_root), watch=self.watch_services)
        new_service = set(new_service_list)
        old_service = set(self.resource.keys())
        add_services = new_service - old_service
        del_services = old_service - new_service

        for add_service in add_services:
            acl = False
            try:
                result = self.get_acls("%s/%s/%s" % (self.zk_root, add_service, self.zk_servers))
                acls = safe_list_get(result, 0, [])
                if self.zk_harpc_acl in acls:
                    acl = True
            except Exception, e:
                acl = False

            servers = {}
            if self.zk.exists(path="%s/%s/%s" % (self.zk_root, add_service, self.zk_servers),
                              watch=self.watch_servers_exists):
                server_list = self.zk.get_children(path="%s/%s/%s/" % (self.zk_root, add_service, self.zk_servers),
                                                   watch=self.watch_servers)
                for server in server_list:
                    server_data_dict = {}
                    server_data = self.zk.get(path="%s/%s/%s/%s" % (self.zk_root, add_service, self.zk_servers, server))
                    try:
                        server_data_str = safe_list_get(server_data, 0, "{}")
                        server_data_dict = json.loads(server_data_str)
                    except Exception, e:
                        logger.error(
                            'manage.ZKWrap.watch_services getdata:' + add_service + "/" + server + ":" + getExceptionDesc(
                                e))
                        pass

                    server_node_stat = safe_list_get(server_data, 1, None)
                    if server_node_stat:
                        server_data_dict['ctime'] = time.strftime("%Y-%m-%d %H:%M:%S",
                                                                  time.localtime(int(server_node_stat.ctime / 1000)))

                    server_acl = False
                    result = self.get_acls("%s/%s/%s/%s" % (self.zk_root, add_service, self.zk_servers, server))
                    acls = safe_list_get(result, 0, [])
                    if self.zk_harpc_acl in acls:
                        server_acl = True

                    servers[server] = {"data": server_data_dict, "acl": server_acl}
Exemple #10
0
    def watch_clients(self, event):
        logger.info("clients is change:" + str(event))
        type = event.type
        path = event.path
        service = path.split("/")[-2]
        new_client_list = self.zk.get_children(path=path, watch=self.watch_clients)
        if type == 'CHILD':
            new_clients = set(new_client_list)
            old_clients = set(self.resource[service][self.zk_clients].keys())

            add_clients = new_clients - old_clients
            del_clients = old_clients - new_clients

            for add_client in add_clients:
                client_data_dict = {}
                client_data = self.zk.get(path="%s/%s/%s/%s" % (self.zk_root, service, self.zk_clients, add_client))
                try:
                    client_data_str = safe_list_get(client_data, 0, "{}")
                    client_data_dict = json.loads(client_data_str)
                except Exception, e:
                    logger.error(
                        'manage.ZKWrap.watch_clients getdata:' + service + "/" + add_client + ":" + getExceptionDesc(e))
                    pass

                client_node_stat = safe_list_get(client_data, 1, None)
                if client_node_stat:
                    client_data_dict['ctime'] = time.strftime("%Y-%m-%d %H:%M:%S",
                                                              time.localtime(int(client_node_stat.ctime / 1000)))

                client_acl = False
                result = self.get_acls("%s/%s/%s/%s" % (self.zk_root, service, self.zk_clients, add_client))
                acls = safe_list_get(result, 0, [])
                if self.zk_harpc_acl in acls:
                    client_acl = True

                self.lock.acquire()
                self.resource[service][self.zk_clients][add_client] = {"data": client_data_dict, "acl": client_acl}
                self.lock.release()

            for del_client in del_clients:
                self.lock.acquire()
                del self.resource[service][self.zk_clients][del_client]
                self.lock.release()
Exemple #11
0
 def get_statistics(self, service, key, type, start=0, end=0):
     """
     获取日志
     :param service:
     :param key:
     :param type:
     :param start:
     :param end:
     :return:
     """
     tables = []
     series = []
     statistics_series = settings.ZK_STATISTICS_SERIES
     total = {"avgtime":0,'mintime':0,"maxtime":0,"qps":0.0,"success":0,"failure":0}
     if service and len(service) > 0 and key and len(key) > 0 and type in (self.zk_servers, self.zk_clients):
         try:
             result = self.zk.get("%s/%s/%s/%s/%s/" % (self.zk_root, service, self.zk_statistics, type, key))
             detail_list = {}
             try:
                 result_str = safe_list_get(result,0,"{}")
                 result_dict = json.loads(result_str)
                 detail_list = result_dict['detail']
                 total = result_dict['total']
             except Exception,e:
                 logger.error('manage.harpc.get_statistics getdata:' + getExceptionDesc(e))
                 pass
             for detail in detail_list:
                 date = detail.get("time")
                 if start > 0 and end > 0 and start > int(date) or end < int(date):
                     continue
                 table = [date]
                 for statistics_serie in statistics_series:
                     statistics_serie_name = statistics_serie.get('name', None)
                     value = detail.get(statistics_serie_name, 0)
                     unit = statistics_serie.get('unit', 0)
                     table.append(value)
                     date_struct = time.strptime(SafeString(date), "%Y%m%d%H%M%S")
                     serie_dict = {'name': statistics_serie_name, 'data': [
                                 [date_struct.tm_year, date_struct.tm_mon - 1, date_struct.tm_mday, date_struct.tm_hour,
                                 date_struct.tm_min, date_struct.tm_sec, value]], 'yAxis': unit}
                     for serie in series:
                         if serie.get('name', None) == statistics_serie_name:
                             serie.get('data', []).append(
                                  [date_struct.tm_year, date_struct.tm_mon - 1, date_struct.tm_mday,
                                   date_struct.tm_hour, date_struct.tm_min, date_struct.tm_sec, value])
                             break
                     else:
                          series.append(serie_dict)
                 tables.append(table)
         except Exception,e:
             logger.error('manage.harpc.get_statistics:' + getExceptionDesc(e))
             pass
Exemple #12
0
    def watch_clients_exists(self, event):
        logger.info("watch_clients is change:" + str(event))
        type = event.type
        path = event.path
        service = path.split("/")[-2]
        if self.zk.exists(path=path, watch=self.watch_clients_exists):
            if type == 'CREATED':
                clients = {}
                client_list = self.zk.get_children(path="%s/%s/%s/" % (self.zk_root, service, self.zk_clients),
                                                   watch=self.watch_clients)
                for client in client_list:
                    client_data_dict = {}
                    client_data = self.zk.get(path="%s/%s/%s/%s" % (self.zk_root, service, self.zk_clients, client))
                    try:
                        client_data_str = safe_list_get(client_data, 0, "{}")
                        client_data_dict = json.loads(client_data_str)
                    except Exception, e:
                        logger.error(
                            'manage.ZKWrap.watch_clients_exists getdata:' + service + "/" + client + ":" + getExceptionDesc(
                                e))
                        pass

                    client_node_stat = safe_list_get(client_data, 1, None)
                    if client_node_stat:
                        client_data_dict['ctime'] = time.strftime("%Y-%m-%d %H:%M:%S",
                                                                  time.localtime(int(client_node_stat.ctime / 1000)))

                    client_acl = False
                    result = self.get_acls("%s/%s/%s/%s" % (self.zk_root, service, self.zk_clients, client))
                    acls = safe_list_get(result, 0, [])
                    if self.zk_harpc_acl in acls:
                        client_acl = True

                    clients[client] = {"data": client_data_dict, "acl": client_acl}

                self.lock.acquire()
                self.resource[service][self.zk_clients] = clients
                self.lock.release()
Exemple #13
0
def ajax_clients(request):
    """
    ajax获取客户端数据
    :param request:
    :return:
    """
    if request.method == 'POST':
        data = request.POST
        service = data.get('service', None)
        results = []
        if service and len(service) > 0:
            clients = ZK_HARPC.get_clients(service=service)
            for client in clients:
                client_name = safe_list_get(client, 0, None)
                isOperate = safe_list_get(client, 1, False)

                client_info_dict = ZK_HARPC.get_service_child_info(service=service, type=settings.ZK_CLIENTS,
                                                            child=client_name)

                results.append([client_name, client_info_dict.get('name', None), client_info_dict.get('owner', None),
                                client_info_dict.get('protocol', None), client_info_dict.get('ctime', None), isOperate])

        return JsonResponse({'data': results}, safe=False)
Exemple #14
0
def ajax_servers(request):
    """
    获取server数据
    :param request:
    :return:
    """
    if request.method == 'POST':
        data = request.POST
        service = data.get('service', None)
        results = []
        if service and len(service) > 0:
            servers = ZK_HARPC.get_servers(service=service)
            for server in servers:
                server_name = safe_list_get(server, 0, None)
                isOperate = safe_list_get(server, 1, False)

                server_info_dict = ZK_HARPC.get_service_child_info(service=service, type=settings.ZK_SERVERS,
                                                            child=server_name)

                results.append([server_name, server_info_dict.get('name', None), server_info_dict.get('owner', None),
                                server_info_dict.get('protocol', None), server_info_dict.get('ctime', None), isOperate])

        return JsonResponse({'data': results}, safe=False)
Exemple #15
0
def ajax_update_server(request):
    """
    跟新一个节点(废弃的方法)
    :param request:
    :return:
    """
    if request.method == 'POST':
        data = request.POST
        service = data.get('service', None)
        servers = data.get('servers', None)

        if (not service
                or not servers) or not (len(service) > 0 and len(servers) > 0):
            return JsonResponse({
                "rc": 0,
                'msg': 'service/servers不得为空'
            },
                                safe=False)

        if not ZK_HARPC.get_service_acl(service):
            return JsonResponse({"rc": 0, 'msg': '您没有操作的权限'}, safe=False)

        server_old_list = []
        server_new_list = servers.split(",")

        servers_olds = ZK_HARPC.get_servers(service)
        for servers_old in servers_olds:
            server_old_name = safe_list_get(servers_old, 0, None)
            if server_old_name:
                server_old_list.append(server_old_name)

        add_servers = [i for i in server_new_list if i not in server_old_list]
        del_servers = [i for i in server_old_list if i not in server_new_list]

        create_result = ZK_HARPC.create_servers(service=service,
                                                servers=add_servers)
        delete_result = ZK_HARPC.delete_servers(service=service,
                                                servers=del_servers)

        if create_result[0] and delete_result[0]:
            return JsonResponse({"rc": 0, 'msg': '更新成功'}, safe=False)
        else:
            return JsonResponse({"rc": 1, 'msg': '跟新失败'}, safe=False)
Exemple #16
0
def ajax_update_server(request):
    """
    跟新一个节点(废弃的方法)
    :param request:
    :return:
    """
    if request.method == "POST":
        data = request.POST
        service = data.get("service", None)
        servers = data.get("servers", None)

        if (not service or not servers) or not (len(service) > 0 and len(servers) > 0):
            return JsonResponse({"rc": 0, "msg": "service/servers不得为空"}, safe=False)

        h = settings.ZK_HARPC
        if not h.get_service_acl(service):
            return JsonResponse({"rc": 0, "msg": "您没有操作的权限"}, safe=False)

        server_old_list = []
        server_new_list = servers.split(",")

        servers_olds = h.get_servers(service)
        for servers_old in servers_olds:
            server_old_name = safe_list_get(servers_old, 0, None)
            if server_old_name:
                server_old_list.append(server_old_name)

        add_servers = [i for i in server_new_list if i not in server_old_list]
        del_servers = [i for i in server_old_list if i not in server_new_list]

        create_result = h.create_servers(service=service, servers=add_servers)
        delete_result = h.delete_servers(service=service, servers=del_servers)

        if create_result[0] and delete_result[0]:
            return JsonResponse({"rc": 0, "msg": "更新成功"}, safe=False)
        else:
            return JsonResponse({"rc": 1, "msg": "跟新失败"}, safe=False)
Exemple #17
0
def ajax_update_server(request):
    """
    跟新一个节点(废弃的方法)
    :param request:
    :return:
    """
    if request.method == 'POST':
        data = request.POST
        service = data.get('service', None)
        servers = data.get('servers', None)

        if (not service or not servers) or not (len(service) > 0 and len(servers) > 0):
            return JsonResponse({"rc": 0, 'msg': 'service/servers不得为空'}, safe=False)

        if not ZK_HARPC.get_service_acl(service):
            return JsonResponse({"rc": 0, 'msg': '您没有操作的权限'}, safe=False)

        server_old_list = []
        server_new_list = servers.split(",")

        servers_olds = ZK_HARPC.get_servers(service)
        for servers_old in servers_olds:
            server_old_name = safe_list_get(servers_old, 0, None)
            if server_old_name:
                server_old_list.append(server_old_name)

        add_servers = [i for i in server_new_list if i not in server_old_list]
        del_servers = [i for i in server_old_list if i not in server_new_list]

        create_result = ZK_HARPC.create_servers(service=service, servers=add_servers)
        delete_result = ZK_HARPC.delete_servers(service=service, servers=del_servers)

        if create_result[0] and delete_result[0]:
            return JsonResponse({"rc": 0, 'msg': '更新成功'}, safe=False)
        else:
            return JsonResponse({"rc": 1, 'msg': '跟新失败'}, safe=False)
Exemple #18
0
                        server_acl = True

                    servers[server] = {"data": server_data_dict, "acl": server_acl}

            clients = {}
            # 如果系统节点数过多请注释该条不启用监听器实时更新节点信息,以避免ZK性能受到影响
            # if self.zk.exists(path="%s/%s/%s" % (self.zk_root,service,self.zk_clients),watch=self.watch_clients_exists):
            if self.zk.exists(path="%s/%s/%s" % (self.zk_root, service, self.zk_clients)):
                # 如果系统节点数过多请注释该条不启用监听器实时更新节点信息,以避免ZK性能受到影响
                # client_list = self.zk.get_children(path="%s/%s/%s/" % (self.zk_root, service, self.zk_clients),watch=self.watch_clients)
                client_list = self.zk.get_children(path="%s/%s/%s/" % (self.zk_root, service, self.zk_clients))
                for client in client_list:
                    client_data_dict = {}
                    client_data = self.zk.get(path="%s/%s/%s/%s" % (self.zk_root, service, self.zk_clients, client))
                    try:
                        client_data_str = safe_list_get(client_data, 0, "{}")
                        client_data_dict = json.loads(client_data_str)
                    except Exception, e:
                        logger.error(
                            'manage.ZKWrap.load_zk_resource getdata:' + service + "/" + client + ":" + getExceptionDesc(
                                e))
                        pass
                    client_node_stat = safe_list_get(client_data, 1, None)
                    if client_node_stat:
                        client_data_dict['ctime'] = time.strftime("%Y-%m-%d %H:%M:%S",
                                                                  time.localtime(int(client_node_stat.ctime / 1000)))
                    client_acl = False
                    result = self.get_acls("%s/%s/%s/%s" % (self.zk_root, service, self.zk_clients, client))
                    acls = safe_list_get(result, 0, [])
                    if self.zk_harpc_acl in acls:
                        client_acl = True