Fixed the node status cli command

This commit is contained in:
Diptanu Choudhury 2016-05-26 01:21:39 -07:00
parent a64062d6a6
commit 37e8e0dc35
3 changed files with 17 additions and 32 deletions

View file

@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
//"io/ioutil"
"net/http"
"net/url"
"os"
@ -398,6 +399,8 @@ func parseWriteMeta(resp *http.Response, q *WriteMeta) error {
// decodeBody is used to JSON decode a body
func decodeBody(resp *http.Response, out interface{}) error {
//res, _ := ioutil.ReadAll(resp.Body)
//fmt.Fprintf(os.Stdout, "DIPTANU BODY %v", res)
dec := json.NewDecoder(resp.Body)
return dec.Decode(out)
}

View file

@ -1,12 +1,11 @@
package api
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"sort"
"strconv"
"github.com/hashicorp/go-cleanhttp"
)
// Nodes is used to query node-related API endpoints
@ -83,29 +82,18 @@ func (n *Nodes) Stats(nodeID string, q *QueryOptions) (*HostStats, error) {
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)
client, err := NewClient(&Config{
Address: fmt.Sprintf("http://%s", node.HTTPAddr),
HttpClient: cleanhttp.DefaultClient(),
})
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 {
var hostStats HostStats
if _, err := client.query("/v1/client/stats/", &hostStats, nil); err != nil {
return nil, err
}
return stats, nil
return &hostStats, nil
}
// Node is used to deserialize a node entry.

View file

@ -47,9 +47,6 @@ Node Status Options:
-allocs
Display a count of running allocations for each node.
-stats
Display the resource usage of the node.
`
return strings.TrimSpace(helpText)
}
@ -59,7 +56,7 @@ func (c *NodeStatusCommand) Synopsis() string {
}
func (c *NodeStatusCommand) Run(args []string) int {
var short, verbose, list_allocs, self, stats bool
var short, verbose, list_allocs, self bool
var hostStats *api.HostStats
flags := c.Meta.FlagSet("node-status", FlagSetClient)
@ -68,7 +65,6 @@ func (c *NodeStatusCommand) Run(args []string) int {
flags.BoolVar(&verbose, "verbose", false, "")
flags.BoolVar(&list_allocs, "allocs", false, "")
flags.BoolVar(&self, "self", false, "")
flags.BoolVar(&stats, "stats", false, "")
if err := flags.Parse(args); err != nil {
return 1
@ -202,10 +198,8 @@ func (c *NodeStatusCommand) Run(args []string) int {
return 1
}
if stats {
if hostStats, err = client.Nodes().Stats(node.ID, nil); err != nil {
c.Ui.Error(fmt.Sprintf("error fetching node resource utilization stats: %v", err))
}
if hostStats, err = client.Nodes().Stats(node.ID, nil); err != nil {
c.Ui.Error(fmt.Sprintf("error fetching node resource utilization stats: %v", err))
}
// Format the output
@ -217,7 +211,7 @@ func (c *NodeStatusCommand) Run(args []string) int {
fmt.Sprintf("Drain|%v", node.Drain),
fmt.Sprintf("Status|%s", node.Status),
}
if stats && hostStats != nil {
if hostStats != nil {
uptime := time.Duration(hostStats.Uptime * uint64(time.Second))
basic = append(basic, fmt.Sprintf("Uptime|%s", uptime.String()))
}
@ -231,7 +225,7 @@ func (c *NodeStatusCommand) Run(args []string) int {
}
c.Ui.Output("\n==> Resource Utilization")
c.Ui.Output(formatList(resources))
if stats && hostStats != nil {
if hostStats != nil {
c.Ui.Output("\n===> Node CPU Stats")
c.printCpuStats(hostStats)
c.Ui.Output("\n===> Node Memory Stats")