2016-04-29 18:06:19 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2016-05-18 05:11:25 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client"
|
2016-04-29 18:06:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *HTTPServer) StatsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if s.agent.client == nil {
|
|
|
|
return nil, clientNotRunning
|
|
|
|
}
|
2016-05-09 19:24:03 +00:00
|
|
|
|
2016-05-18 05:11:25 +00:00
|
|
|
var tsRequest bool
|
|
|
|
|
|
|
|
// Check if the user has requested time series
|
|
|
|
if qp := req.URL.Query().Get("ts"); qp != "" {
|
|
|
|
if tsReq, err := strconv.ParseBool(qp); err == nil {
|
|
|
|
tsRequest = tsReq
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 19:24:03 +00:00
|
|
|
clientStats := s.agent.client.StatsReporter()
|
|
|
|
|
|
|
|
// Return the host stats if alloc ID is not present
|
2016-04-29 18:06:19 +00:00
|
|
|
var allocID, task string
|
|
|
|
if allocID = req.URL.Query().Get("allocation"); allocID == "" {
|
2016-05-09 19:24:03 +00:00
|
|
|
return clientStats.HostStats(), nil
|
2016-04-29 18:06:19 +00:00
|
|
|
}
|
2016-05-09 19:24:03 +00:00
|
|
|
|
|
|
|
// Check if the allocation is running on the node
|
|
|
|
allocStats, ok := clientStats.AllocStats()[allocID]
|
2016-05-09 15:55:19 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("alloc %q is not running on this client", allocID)
|
2016-05-09 14:57:26 +00:00
|
|
|
}
|
2016-05-09 19:24:03 +00:00
|
|
|
|
|
|
|
// Return the resource usage of the task if the task name is specified
|
2016-04-29 18:06:19 +00:00
|
|
|
if task = req.URL.Query().Get("task"); task != "" {
|
2016-05-09 19:24:03 +00:00
|
|
|
taskStats, err := allocStats.TaskStats(task)
|
2016-05-09 14:57:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-18 05:11:25 +00:00
|
|
|
return s.getStats(tsRequest, taskStats), nil
|
2016-05-09 14:57:26 +00:00
|
|
|
}
|
2016-05-09 19:24:03 +00:00
|
|
|
|
|
|
|
// Return the resource usage of all the tasks in an allocation if task name
|
|
|
|
// is not specified
|
2016-05-09 14:57:26 +00:00
|
|
|
res := make(map[string]interface{})
|
2016-05-09 19:24:03 +00:00
|
|
|
for task, taskStats := range allocStats.AllocStats() {
|
2016-05-18 05:11:25 +00:00
|
|
|
res[task] = s.getStats(tsRequest, taskStats)
|
2016-04-29 18:06:19 +00:00
|
|
|
}
|
2016-05-09 14:57:26 +00:00
|
|
|
return res, nil
|
2016-04-29 18:06:19 +00:00
|
|
|
}
|
2016-05-18 05:11:25 +00:00
|
|
|
|
|
|
|
func (s *HTTPServer) getStats(tsRequest bool, taskStats client.TaskStatsReporter) interface{} {
|
|
|
|
if tsRequest {
|
|
|
|
return taskStats.ResourceUsageTS()
|
|
|
|
}
|
|
|
|
return taskStats.ResourceUsage()
|
|
|
|
}
|