2016-04-29 18:06:19 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *HTTPServer) StatsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if s.agent.client == nil {
|
|
|
|
return nil, clientNotRunning
|
|
|
|
}
|
2016-05-09 15:55:19 +00:00
|
|
|
cStatsReporter := s.agent.client.StatsReporter()
|
2016-04-29 18:06:19 +00:00
|
|
|
var allocID, task string
|
|
|
|
if allocID = req.URL.Query().Get("allocation"); allocID == "" {
|
2016-05-09 15:55:19 +00:00
|
|
|
return cStatsReporter.HostStats(), nil
|
2016-04-29 18:06:19 +00:00
|
|
|
}
|
2016-05-09 15:55:19 +00:00
|
|
|
allocStats := cStatsReporter.AllocStats()
|
|
|
|
arStatsReporter, ok := allocStats[allocID]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("alloc %q is not running on this client", allocID)
|
2016-05-09 14:57:26 +00:00
|
|
|
}
|
2016-04-29 18:06:19 +00:00
|
|
|
if task = req.URL.Query().Get("task"); task != "" {
|
2016-05-09 15:55:19 +00:00
|
|
|
taskStatsReporter, err := arStatsReporter.TaskStats(task)
|
2016-05-09 14:57:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return taskStatsReporter.ResourceUsage(), nil
|
|
|
|
}
|
|
|
|
res := make(map[string]interface{})
|
2016-05-09 15:55:19 +00:00
|
|
|
for task, sr := range arStatsReporter.AllocStats() {
|
2016-05-09 14:57:26 +00:00
|
|
|
res[task] = sr.ResourceUsage()
|
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
|
|
|
}
|