func refreshDesiredAgent() { pageInfo := &models.PageInfo{PageIndex: 1, PageSize: math.MaxInt32} agentList, _ := models.QueryAgentList(models.QueryAgentDto{}, pageInfo) if nil == agentList || 0 == len(agentList) { return } refreshAgentsMap(agentList) hostGroupAgentMap := make(map[int64]*set.SafeSet) usedHostGroupIds := set.NewSafeInt64Set() //获取Agent关联到的HostGroupId for _, agent := range agentList { relAgentGroups, _ := models.QueryRelAgentGroupList(agent.Id) if nil == relAgentGroups || 0 == len(relAgentGroups) { continue } for _, rag := range relAgentGroups { usedHostGroupIds.Add(rag.HostGroupId) nameSet, exists := hostGroupAgentMap[rag.HostGroupId] if !exists { nameSet = set.NewSafeSet() } nameSet.Add(agent.Name) hostGroupAgentMap[rag.HostGroupId] = nameSet } } //获取每个HostGroup下属Endpoint endpointList, _ := models.QueryEndpointList(models.QueryEndpointDto{}, pageInfo) if nil == endpointList || 0 == len(endpointList) { return } for _, hostGroupId := range usedHostGroupIds.Slice() { relEndpointGroupList, _ := models.QueryRelEndpointGroupList(models.QueryRelEndpointGroupDto{HostGroupId: hostGroupId}, pageInfo) if nil == relEndpointGroupList || 0 == len(relEndpointGroupList) { continue } for _, reg := range relEndpointGroupList { if "fixed" == reg.RelType && "hostname" == reg.PropName { HostAgents.Put(reg.PropValue, hostGroupAgentMap[hostGroupId].ToSlice()) } else { pattern := regexp.MustCompile(reg.PropValue) for _, endpoint := range endpointList { if "hostname" == reg.PropName { if pattern.MatchString(endpoint.Hostname) { HostAgents.Put(endpoint.Hostname, hostGroupAgentMap[hostGroupId].ToSlice()) } } else if "ip" == reg.PropValue { if pattern.MatchString(endpoint.Ip) { HostAgents.Put(endpoint.Hostname, hostGroupAgentMap[hostGroupId].ToSlice()) } } } } } } }
func (this *AgentController) Query() { jsonResult := make(map[string]interface{}) pageInfo := models.NewPageInfo(this.Ctx.Request) queryDto := models.QueryAgentDto{} if err := this.ParseForm(&queryDto); nil != err { logger.Errorln("parseForm error", err) } list, pageInfo := models.QueryAgentList(queryDto, pageInfo) jsonResult["items"] = list jsonResult["total"] = pageInfo.RowCount this.Data["json"] = &jsonResult this.ServeJSON() }