Commit 33f2d0c1 authored by Diptanu Choudhury's avatar Diptanu Choudhury
Browse files

Added a stats api for retreiving node stats

parent 6132ccc2
No related merge requests found
Showing with 60 additions and 3 deletions
+60 -3
......@@ -46,7 +46,7 @@ func (a *Allocations) Info(allocID string, q *QueryOptions) (*Allocation, *Query
}
func (a *Allocations) Stats(alloc *Allocation, q *QueryOptions) (map[string]*TaskResourceUsage, error) {
node, _, err := a.client.Nodes().Info(alloc.NodeID, &QueryOptions{})
node, _, err := a.client.Nodes().Info(alloc.NodeID, q)
if err != nil {
return nil, err
}
......@@ -71,7 +71,7 @@ func (a *Allocations) Stats(alloc *Allocation, q *QueryOptions) (map[string]*Tas
return nil, err
}
if resp.StatusCode != 200 {
return nil, a.getErrorMsg(resp)
return nil, getErrorMsg(resp)
}
decoder := json.NewDecoder(resp.Body)
var stats map[string]*TaskResourceUsage
......@@ -81,7 +81,7 @@ func (a *Allocations) Stats(alloc *Allocation, q *QueryOptions) (map[string]*Tas
return stats, nil
}
func (a *Allocations) getErrorMsg(resp *http.Response) error {
func getErrorMsg(resp *http.Response) error {
if errMsg, err := ioutil.ReadAll(resp.Body); err == nil {
return fmt.Errorf(string(errMsg))
} else {
......
package api
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"sort"
"strconv"
)
......@@ -71,6 +75,39 @@ func (n *Nodes) ForceEvaluate(nodeID string, q *WriteOptions) (string, *WriteMet
return resp.EvalID, wm, nil
}
func (n *Nodes) Stats(nodeID string, q *QueryOptions) (*HostStats, error) {
node, _, err := n.client.Nodes().Info(nodeID, q)
if err != nil {
return nil, err
}
if node.HTTPAddr == "" {
return nil, fmt.Errorf("http addr of the node %q is running is not advertised", nodeID)
}
u := &url.URL{
Scheme: "http",
Host: node.HTTPAddr,
Path: "/v1/client/stats/",
}
req := &http.Request{
Method: "GET",
URL: u,
}
c := http.Client{}
resp, err := c.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, getErrorMsg(resp)
}
decoder := json.NewDecoder(resp.Body)
var stats *HostStats
if err := decoder.Decode(&stats); err != nil {
return nil, err
}
return stats, nil
}
// Node is used to deserialize a node entry.
type Node struct {
ID string
......@@ -90,6 +127,26 @@ type Node struct {
ModifyIndex uint64
}
// HostStats represents resource usage stats of the host running a Nomad client
type HostStats struct {
Memory *HostMemoryStats
CPU []*HostCPUStats
}
type HostMemoryStats struct {
Total uint64
Available uint64
Used uint64
Free uint64
}
type HostCPUStats struct {
CPU string
User float64
System float64
Idle float64
}
// NodeListStub is a subset of information returned during
// node list operations.
type NodeListStub struct {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment