github.com/thomasobenaus/nomad@v0.11.1/command/agent/stats_endpoint.go (about)

     1  package agent
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  
     7  	cstructs "github.com/hashicorp/nomad/client/structs"
     8  	"github.com/hashicorp/nomad/nomad/structs"
     9  )
    10  
    11  func (s *HTTPServer) ClientStatsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    12  	// Get the requested Node ID
    13  	requestedNode := req.URL.Query().Get("node_id")
    14  
    15  	// Build the request and parse the ACL token
    16  	args := structs.NodeSpecificRequest{
    17  		NodeID: requestedNode,
    18  	}
    19  	s.parse(resp, req, &args.QueryOptions.Region, &args.QueryOptions)
    20  
    21  	// Determine the handler to use
    22  	useLocalClient, useClientRPC, useServerRPC := s.rpcHandlerForNode(requestedNode)
    23  
    24  	// Make the RPC
    25  	var reply cstructs.ClientStatsResponse
    26  	var rpcErr error
    27  	if useLocalClient {
    28  		rpcErr = s.agent.Client().ClientRPC("ClientStats.Stats", &args, &reply)
    29  	} else if useClientRPC {
    30  		rpcErr = s.agent.Client().RPC("ClientStats.Stats", &args, &reply)
    31  	} else if useServerRPC {
    32  		rpcErr = s.agent.Server().RPC("ClientStats.Stats", &args, &reply)
    33  	} else {
    34  		rpcErr = CodedError(400, "No local Node and node_id not provided")
    35  	}
    36  
    37  	if rpcErr != nil {
    38  		if structs.IsErrNoNodeConn(rpcErr) {
    39  			rpcErr = CodedError(404, rpcErr.Error())
    40  		} else if strings.Contains(rpcErr.Error(), "Unknown node") {
    41  			rpcErr = CodedError(404, rpcErr.Error())
    42  		}
    43  
    44  		return nil, rpcErr
    45  	}
    46  
    47  	return reply.HostStats, nil
    48  }