From 38c507f33add0b7fa58c7a84ee6aaf3a54076187 Mon Sep 17 00:00:00 2001 From: hanshuaikang <1758504262@qq.com> Date: Wed, 6 Sep 2023 16:51:14 +0800 Subject: [PATCH] =?UTF-8?q?bugfix:=20=E5=88=86=E6=89=B9=E6=9F=A5=E8=AF=A2g?= =?UTF-8?q?se=E7=9A=84agent=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pipeline_plugins/cmdb_ip_picker/utils.py | 41 ++++++++++++++++-------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/pipeline_plugins/cmdb_ip_picker/utils.py b/pipeline_plugins/cmdb_ip_picker/utils.py index 89ba5838bc..3a7f8ecea4 100644 --- a/pipeline_plugins/cmdb_ip_picker/utils.py +++ b/pipeline_plugins/cmdb_ip_picker/utils.py @@ -709,27 +709,42 @@ def get_bk_cloud_id_for_host(host_info, cloud_key="cloud"): def get_gse_agent_status_ipv6(bk_agent_id_list): if not bk_agent_id_list: return {} - ENV_MAP = {"PRODUCT": "prod", "STAGING": "stag"} + ENV_MAP = {"PRODUCT": "prod", "STAGING": "stage"} gse_url = settings.BK_API_URL_TMPL.format(api_name="bk-gse") get_agent_status_url = "{}/{}/api/v2/cluster/list_agent_state".format( - gse_url, ENV_MAP.get(settings.RUN_MODE, "prod") + gse_url, ENV_MAP.get(settings.RUN_MODE, "stage") ) - params = {"bk_app_code": settings.APP_CODE, "bk_app_secret": settings.SECRET_KEY, "agent_id_list": bk_agent_id_list} - resp = requests.post(url=get_agent_status_url, json=params) + def send_request(agent_ids): + params = { + "bk_app_code": settings.APP_CODE, + "bk_app_secret": settings.SECRET_KEY, + "agent_id_list": agent_ids, + } + + resp = requests.post(url=get_agent_status_url, json=params) + + if resp.status_code != 200: + raise Exception("[get_gse_agent_status_ipv6] 查询agent状态错误,返回值非200, content = {}".format(resp.content)) + try: + resp_data = resp.json() + except Exception as e: + raise Exception("[get_gse_agent_status_ipv6] 查询agent状态错误,返回值非Json, err={}".format(e)) + if resp_data["code"] != 0: + raise Exception("[get_gse_agent_status_ipv6] 查询agent状态错误,返回值非code非0, {}".format(data)) + + return resp_data.get("data", []) + + # gse 请求最大支持1000个agent_id的同时查询,所以需要把agent_id分成1000份的单元 + multi_agent_id_list = [bk_agent_id_list[i : i + 1000] for i in range(0, len(bk_agent_id_list), 1000)] - if resp.status_code != 200: - raise Exception("[get_gse_agent_status_ipv6] 查询agent状态错误,返回值非200, content = {}".format(resp.content)) - try: - data = resp.json() - except Exception as e: - raise Exception("[get_gse_agent_status_ipv6] 查询agent状态错误,返回值非Json, err={}".format(e)) - if data["code"] != 0: - raise Exception("[get_gse_agent_status_ipv6] 查询agent状态错误,返回值非code非0") + data = [] + for agent_id_list in multi_agent_id_list: + data.extend(send_request(agent_id_list)) agent_id_status_map = {} - for item in data.get("data", []): + for item in data: # esb agent 状态规则 : agent在线状态,0为不在线,1为在线 # apigw agent 状态规则: Agent当前运行状态码, -1:未知 0:初始安装 1:启动中 2:运行中 3:有损状态 4:繁忙状态 5:升级中 6:停止中 7:解除安装 # 为了前端的显示/与过滤保持一致,所有需要对状态进行转换