fix wrong agent status of host
This commit is contained in:
		
							parent
							
								
									4b9fddcf8d
								
							
						
					
					
						commit
						605c89222e
					
				| 
						 | 
					@ -82,17 +82,6 @@ func (h *APIHandler) enrollHost(w http.ResponseWriter, req *http.Request, ps htt
 | 
				
			||||||
			log.Error(err)
 | 
								log.Error(err)
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if hi.Source == "agent" {
 | 
					 | 
				
			||||||
			sm := common2.GetStateManager()
 | 
					 | 
				
			||||||
			ag, _  := sm.GetAgent(hostInfo.AgentID)
 | 
					 | 
				
			||||||
			err = sm.GetAgentClient().DiscoveredHost(nil, ag.GetEndpoint(), util.MapStr{
 | 
					 | 
				
			||||||
				"host_id": hostInfo.ID,
 | 
					 | 
				
			||||||
			})
 | 
					 | 
				
			||||||
			if err != nil {
 | 
					 | 
				
			||||||
				log.Error(err)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	resBody :=  util.MapStr{
 | 
						resBody :=  util.MapStr{
 | 
				
			||||||
		"success": true,
 | 
							"success": true,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -73,6 +73,7 @@ func (h *APIHandler) createInstance(w http.ResponseWriter, req *http.Request, ps
 | 
				
			||||||
		obj.IPS = res.IPS
 | 
							obj.IPS = res.IPS
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						obj.Status = common2.StatusOnline
 | 
				
			||||||
	err = orm.Create(nil, obj)
 | 
						err = orm.Create(nil, obj)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		h.WriteError(w, err.Error(), http.StatusInternalServerError)
 | 
							h.WriteError(w, err.Error(), http.StatusInternalServerError)
 | 
				
			||||||
| 
						 | 
					@ -116,13 +117,6 @@ func bindAgentToHostByIP(ag *agent.Instance) error{
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return  err
 | 
								return  err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					 | 
				
			||||||
		err = sm.GetAgentClient().DiscoveredHost(nil, ag.GetEndpoint(), util.MapStr{
 | 
					 | 
				
			||||||
			"host_id": hostInfo.ID,
 | 
					 | 
				
			||||||
		})
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return  err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -38,28 +38,6 @@ func (client *Client) GetHostInfo(ctx context.Context, agentBaseURL string) (*ho
 | 
				
			||||||
	return resBody.HostInfo, nil
 | 
						return resBody.HostInfo, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (client *Client) DiscoveredHost(ctx context.Context, agentBaseURL string, body interface{}) error {
 | 
					 | 
				
			||||||
	req := &util.Request{
 | 
					 | 
				
			||||||
		Method:  http.MethodPut,
 | 
					 | 
				
			||||||
		Url:     fmt.Sprintf("%s/host/discover", agentBaseURL),
 | 
					 | 
				
			||||||
		Context: ctx,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	reqBody, err := util.ToJSONBytes(body)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	req.Body = reqBody
 | 
					 | 
				
			||||||
	resBody := map[string]interface{}{}
 | 
					 | 
				
			||||||
	err = client.doRequest(req, &resBody)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if resBody["success"] != true {
 | 
					 | 
				
			||||||
		return fmt.Errorf("discover host callback error: %v", resBody["error"])
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (client *Client) GetElasticProcess(ctx context.Context, agentBaseURL string, agentID string)(interface{}, error) {
 | 
					func (client *Client) GetElasticProcess(ctx context.Context, agentBaseURL string, agentID string)(interface{}, error) {
 | 
				
			||||||
	req := &util.Request{
 | 
						req := &util.Request{
 | 
				
			||||||
		Method:  http.MethodGet,
 | 
							Method:  http.MethodGet,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -25,7 +25,6 @@ func RegisterClient(client ClientAPI) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
type ClientAPI interface {
 | 
					type ClientAPI interface {
 | 
				
			||||||
	GetHostInfo(ctx context.Context, agentBaseURL string) (*host.HostInfo, error)
 | 
						GetHostInfo(ctx context.Context, agentBaseURL string) (*host.HostInfo, error)
 | 
				
			||||||
	DiscoveredHost(ctx context.Context, agentBaseURL string, body interface{}) error
 | 
					 | 
				
			||||||
	GetElasticProcess(ctx context.Context, agentBaseURL string, agentID string)(interface{}, error)
 | 
						GetElasticProcess(ctx context.Context, agentBaseURL string, agentID string)(interface{}, error)
 | 
				
			||||||
	GetElasticLogFiles(ctx context.Context, agentBaseURL string, logsPath string)(interface{}, error)
 | 
						GetElasticLogFiles(ctx context.Context, agentBaseURL string, logsPath string)(interface{}, error)
 | 
				
			||||||
	GetElasticLogFileContent(ctx context.Context, agentBaseURL string, body interface{})(interface{}, error)
 | 
						GetElasticLogFileContent(ctx context.Context, agentBaseURL string, body interface{})(interface{}, error)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -70,6 +70,7 @@ func (sm *StateManager) checkAgentStatus() {
 | 
				
			||||||
	for agentID, status := range sm.agentIds {
 | 
						for agentID, status := range sm.agentIds {
 | 
				
			||||||
		if _, ok := onlineAgentIDs[agentID]; ok {
 | 
							if _, ok := onlineAgentIDs[agentID]; ok {
 | 
				
			||||||
			sm.syncSettings(agentID)
 | 
								sm.syncSettings(agentID)
 | 
				
			||||||
 | 
								host.UpdateHostAgentStatus(agentID, StatusOnline)
 | 
				
			||||||
			if status == StatusOnline {
 | 
								if status == StatusOnline {
 | 
				
			||||||
				continue
 | 
									continue
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue