def update_gse_agent_status(username, host_list: List) -> List: """更新 GSE Agent 状态信息""" gse_params = [] for info in host_list: bk_cloud_id = info.get('bk_cloud_id') or 0 gse_params.extend([{ 'plat_id': bk_cloud_id, 'bk_cloud_id': bk_cloud_id, 'ip': ip } for ip in info.get('bk_host_innerip', '').split(',')]) gse_host_status_map = { info['ip']: info for info in gse.get_agent_status(username, gse_params) } # 根据 IP 匹配更新 Agent 信息 cc_host_map = {host['bk_host_innerip']: host for host in host_list} for ips in cc_host_map: # 同主机可能存在多个 IP,任一 IP Agent 正常即可 for ip in ips.split(','): if ip not in gse_host_status_map: continue ip_status = gse_host_status_map[ip] cc_host_map[ips]['agent_alive'] = ip_status.get('bk_agent_alive') break return list(cc_host_map.values())
def get_agent_status(cls, request, ip_list): hosts = [] for info in ip_list: plat_info = info.get('bk_cloud_id') or [] plat_id = plat_info[0]['id'] if plat_info else 0 hosts.extend([{ 'plat_id': plat_id, 'bk_cloud_id': plat_id, 'ip': ip } for ip in info.get('inner_ip', '').split(',')]) return gse.get_agent_status(request.user.username, hosts)
def get_agent_status(cls, username: str, ip_list: List[Dict]) -> List[Dict]: """""" hosts = [] for info in ip_list: bk_cloud_id = info.get('bk_cloud_id') or 0 hosts.extend([{ 'plat_id': bk_cloud_id, 'bk_cloud_id': bk_cloud_id, 'ip': ip } for ip in info.get('inner_ip', '').split(',')]) return gse.get_agent_status(username, hosts)
def _get_ip_map_agent_status(self, hosts: List[Dict]) -> Dict[str, Dict]: """通过 IP 查询主机 agent 状态""" # 主机为空时,直接返回 if not hosts: return {} params = [{"ip": host["inner_ip"], "bk_cloud_id": host["bk_cloud_id"]} for host in hosts] try: agents = gse.get_agent_status(self.username, params) except Exception as e: logger.error("查询主机agent信息失败,%s", e) return {} # 如果返回状态字段缺失,则认为agent状态异常,其中0表示agent不在线 return { agent["ip"]: {"agent": agent.get("bk_agent_alive", node_constants.DEFAULT_BK_AGENT_ALIVE)} for agent in agents }
def get_agent_status(username: str, host_list: List[HostData]) -> List[Dict]: """查询主机 agent 状态 :param username: 当前请求的用户名 :param ip_list: IP 列表,用于查询主机的 agent 状态 """ hosts = [] for info in host_list: # 查询所属区域云区域 hosts.extend([{ "plat_id": info.bk_cloud_id, "bk_cloud_id": info.bk_cloud_id, "ip": ip } for ip in info.inner_ip.split(",")]) # 处理返回数据 data = gse.get_agent_status(username, hosts) return [ asdict(make_dataclass_from_dict(HostAgentData, info)) for info in data ]