From afbc8621eeef81a2af38f5516de3a5b6cd141d45 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Sun, 20 Mar 2016 17:30:33 -0700 Subject: [PATCH] less verbose output and show node's total resources --- command/node_status.go | 76 ++++++++++++++++++++++++------------- command/node_status_test.go | 6 --- 2 files changed, 49 insertions(+), 33 deletions(-) diff --git a/command/node_status.go b/command/node_status.go index efbbab31226..949b7bb388e 100644 --- a/command/node_status.go +++ b/command/node_status.go @@ -2,9 +2,10 @@ package command import ( "fmt" - "github.com/hashicorp/nomad/api" "sort" "strings" + + "github.com/hashicorp/nomad/api" ) type NodeStatusCommand struct { @@ -179,20 +180,6 @@ func (c *NodeStatusCommand) Run(args []string) int { return 1 } - m := node.Attributes - keys := make([]string, len(m)) - for k := range m { - keys = append(keys, k) - } - sort.Strings(keys) - - var attributes []string - for _, k := range keys { - if k != "" { - attributes = append(attributes, fmt.Sprintf("%s:%s", k, m[k])) - } - } - // Format the output basic := []string{ fmt.Sprintf("ID|%s", limit(node.ID, length)), @@ -201,19 +188,10 @@ func (c *NodeStatusCommand) Run(args []string) int { fmt.Sprintf("DC|%s", node.Datacenter), fmt.Sprintf("Drain|%v", node.Drain), fmt.Sprintf("Status|%s", node.Status), - fmt.Sprintf("Attributes|%s", strings.Join(attributes, ", ")), } - - // Dump the output c.Ui.Output(formatKV(basic)) + if !short { - allocs, err := getAllocs(client, node, length) - if err != nil { - c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err)) - return 1 - } - c.Ui.Output("\n==> Allocations") - c.Ui.Output(formatList(allocs)) resources, err := getResources(client, node) if err != nil { c.Ui.Error(fmt.Sprintf("Error querying node resources: %s", err)) @@ -221,7 +199,37 @@ func (c *NodeStatusCommand) Run(args []string) int { } c.Ui.Output("\n==> Resource Utilization") c.Ui.Output(formatList(resources)) + + allocs, err := getAllocs(client, node, length) + if err != nil { + c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err)) + return 1 + } + + if len(allocs) > 1 { + c.Ui.Output("\n==> Allocations") + c.Ui.Output(formatList(allocs)) + } + } + + if verbose { + // Print the attributes + keys := make([]string, len(node.Attributes)) + for k := range node.Attributes { + keys = append(keys, k) + } + sort.Strings(keys) + + var attributes []string + for _, k := range keys { + if k != "" { + attributes = append(attributes, fmt.Sprintf("%s|%s", k, node.Attributes[k])) + } + } + c.Ui.Output("\n==> Attributes") + c.Ui.Output(formatKV(attributes)) } + return 0 } @@ -260,9 +268,19 @@ func getAllocs(client *api.Client, node *api.Node, length int) ([]string, error) return allocs, err } +// getResources returns the resource usage of the node. func getResources(client *api.Client, node *api.Node) ([]string, error) { var resources []string var cpu, mem, disk, iops int + var totalCpu, totalMem, totalDisk, totalIops int + + // Compute the total + r := node.Resources + res := node.Reserved + totalCpu = r.CPU - res.CPU + totalMem = r.MemoryMB - res.MemoryMB + totalDisk = r.DiskMB - res.DiskMB + totalIops = r.IOPS - res.IOPS // Get list of running allocations on the node runningAllocs, err := getRunningAllocs(client, node.ID) @@ -277,11 +295,15 @@ func getResources(client *api.Client, node *api.Node) ([]string, error) { resources = make([]string, 2) resources[0] = "CPU|Memory MB|Disk MB|IOPS" - resources[1] = fmt.Sprintf("%v|%v|%v|%v", + resources[1] = fmt.Sprintf("%v/%v|%v/%v|%v/%v|%v/%v", cpu, + totalCpu, mem, + totalMem, disk, - iops) + totalDisk, + iops, + totalIops) return resources, err } diff --git a/command/node_status_test.go b/command/node_status_test.go index 14324bca46e..e4924d9c81d 100644 --- a/command/node_status_test.go +++ b/command/node_status_test.go @@ -58,9 +58,6 @@ func TestNodeStatusCommand_Run(t *testing.T) { if !strings.Contains(out, "mynode") { t.Fatalf("expect to find mynode, got: %s", out) } - if !strings.Contains(out, "Allocations") { - t.Fatalf("expected allocations, got: %s", out) - } ui.OutputWriter.Reset() // Query single node in short view @@ -83,9 +80,6 @@ func TestNodeStatusCommand_Run(t *testing.T) { if !strings.Contains(out, "mynode") { t.Fatalf("expect to find mynode, got: %s", out) } - if !strings.Contains(out, "Allocations") { - t.Fatalf("expected allocations, got: %s", out) - } if strings.Contains(out, nodeID) { t.Fatalf("expected truncated node id, got: %s", out) }