From 35410e1487f4adf9d1288d398fdd17f073e84a81 Mon Sep 17 00:00:00 2001 From: Jake Champlin Date: Fri, 4 Mar 2016 21:29:39 -0500 Subject: [PATCH 1/2] Print resource usage w/ alloc-status + node-status MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When alloc-status is called, in it's long form only, print the resource utilization for that single allocation. When node-status is called, in it's long form only, print the TOTAL resource utilization that is occurring on that single node. Nomad Alloc Status: ``` % nomad alloc-status 195d3bf2 ID = 195d3bf2 Eval ID = c917e3ee Name = example.cache[1] Node ID = 1b2520a7 Job ID = example Client Status = running Evaluated Nodes = 1 Filtered Nodes = 0 Exhausted Nodes = 0 Allocation Time = 17.73µs Failures = 0 ==> Task "redis" is "running" Recent Events: Time Type Description 04/03/16 21:20:45 EST Started Task started by client 04/03/16 21:20:42 EST Received Task received by client ==> Status Allocation "195d3bf2" status "running" (0/1 nodes filtered) * Score "1b2520a7-6714-e78d-a8f7-68467dda6db7.binpack" = 1.209464 * Score "1b2520a7-6714-e78d-a8f7-68467dda6db7.job-anti-affinity" = -10.000000 ==> Resources CPU MemoryMB DiskMB IOPS 500 256 300 0 ``` Nomad Node Status: ``` % nomad node-status 57b3a55a ID = 57b3a55a Name = biscuits Class = DC = dc1 Drain = false Status = ready Attributes = arch:amd64, cpu.frequency:3753.458875, cpu.modelname:Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz, cpu.numcores:8, cpu.totalcompute:30027.671000, driver.docker:1, driver.docker.version:1.10.2, driver.exec:1, driver.raw_exec:1, hostname:biscuits, kernel.name:linux, kernel.version:4.4.0-9-generic, memory.totalbytes:25208934400, os.name:ubuntu, os.version:16.04, unique.cgroup.mountpoint:/sys/fs/cgroup, unique.network.ip-address:127.0.0.1, unique.storage.bytesfree:219781419008, unique.storage.bytestotal:246059892736, unique.storage.volume:/dev/sdb3 ==> Allocations ID Eval ID Job ID Task Group Desired Status Client Status 2c236883 aa11aca8 example cache run running 32f6e3d6 aa11aca8 example cache run running ==> Resource Utilization CPU MemoryMB DiskMB IOPS 1000 512 600 0 ``` --- command/alloc_status.go | 17 ++++++++ command/node_status.go | 97 +++++++++++++++++++++++++++++++---------- 2 files changed, 91 insertions(+), 23 deletions(-) diff --git a/command/alloc_status.go b/command/alloc_status.go index 34a5a2b96..faa0677dd 100644 --- a/command/alloc_status.go +++ b/command/alloc_status.go @@ -148,6 +148,11 @@ func (c *AllocStatusCommand) Run(args []string) int { c.Ui.Output("\n==> Status") dumpAllocStatus(c.Ui, alloc, length) + if !short { + c.Ui.Output("\n==> Resources") + c.taskResources(alloc) + } + return 0 } @@ -258,3 +263,15 @@ func (c *AllocStatusCommand) sortedTaskStateIterator(m map[string]*api.TaskState close(output) return output } + +// taskResources prints out the tasks current resource usage +func (c *AllocStatusCommand) taskResources(alloc *api.Allocation) { + resources := make([]string, 2) + resources[0] = "CPU|MemoryMB|DiskMB|IOPS" + resources[1] = fmt.Sprintf("%v|%v|%v|%v", + alloc.Resources.CPU, + alloc.Resources.MemoryMB, + alloc.Resources.DiskMB, + alloc.Resources.IOPS) + c.Ui.Output(formatList(resources)) +} diff --git a/command/node_status.go b/command/node_status.go index da30defaa..561563823 100644 --- a/command/node_status.go +++ b/command/node_status.go @@ -102,7 +102,7 @@ func (c *NodeStatusCommand) Run(args []string) int { } for i, node := range nodes { if list_allocs { - numAllocs, err := getRunningAllocs(client, node) + numAllocs, err := getNumRunningAllocs(client, node) if err != nil { c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err)) return 1 @@ -207,39 +207,29 @@ func (c *NodeStatusCommand) Run(args []string) int { fmt.Sprintf("Attributes|%s", strings.Join(attributes, ", ")), } - var allocs []string + // Dump the output + c.Ui.Output(formatKV(basic)) if !short { - // Query the node allocations - nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil) + allocs, err := getAllocs(client, node, length) if err != nil { c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err)) return 1 } - - // Format the allocations - allocs = make([]string, len(nodeAllocs)+1) - allocs[0] = "ID|Eval ID|Job ID|Task Group|Desired Status|Client Status" - for i, alloc := range nodeAllocs { - allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s", - limit(alloc.ID, length), - limit(alloc.EvalID, length), - alloc.JobID, - alloc.TaskGroup, - alloc.DesiredStatus, - alloc.ClientStatus) - } - } - - // Dump the output - c.Ui.Output(formatKV(basic)) - if !short { 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)) + return 1 + } + c.Ui.Output("\n==> Resource Utilization") + c.Ui.Output(formatList(resources)) } return 0 } -func getRunningAllocs(client *api.Client, node *api.NodeListStub) (int, error) { +// getNumRunningAllocs fetches the number of running allocations on the node +func getNumRunningAllocs(client *api.Client, node *api.NodeListStub) (int, error) { // Fetch number of running allocations per node numAllocs := 0 nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil) @@ -250,3 +240,64 @@ func getRunningAllocs(client *api.Client, node *api.NodeListStub) (int, error) { } return numAllocs, err } + +// getRunningAllocs returns a slice of allocation id's running on the node +func getRunningAllocs(client *api.Client, node *api.Node) ([]*api.Allocation, error) { + var allocs []*api.Allocation + + // Query the node allocations + nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil) + // Filter list to only running allocations + for _, alloc := range nodeAllocs { + if alloc.ClientStatus == "running" { + allocs = append(allocs, alloc) + } + } + return allocs, err +} + +// getAllocs returns information about every running allocation on the node +func getAllocs(client *api.Client, node *api.Node, length int) ([]string, error) { + var allocs []string + // Query the node allocations + nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil) + // Format the allocations + allocs = make([]string, len(nodeAllocs)+1) + allocs[0] = "ID|Eval ID|Job ID|Task Group|Desired Status|Client Status" + for i, alloc := range nodeAllocs { + allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s", + limit(alloc.ID, length), + limit(alloc.EvalID, length), + alloc.JobID, + alloc.TaskGroup, + alloc.DesiredStatus, + alloc.ClientStatus) + } + return allocs, err +} + +func getResources(client *api.Client, node *api.Node) ([]string, error) { + var resources []string + var cpu, mem, disk, iops int + + // Get list of running allocations on the node + runningAllocs, err := getRunningAllocs(client, node) + + // Get Resources + for _, alloc := range runningAllocs { + cpu += alloc.Resources.CPU + mem += alloc.Resources.MemoryMB + disk += alloc.Resources.DiskMB + iops += alloc.Resources.IOPS + } + + resources = make([]string, 2) + resources[0] = "CPU|MemoryMB|DiskMB|IOPS" + resources[1] = fmt.Sprintf("%v|%v|%v|%v", + cpu, + mem, + disk, + iops) + + return resources, err +} From 9ed3150f6e4e492d858360e92bb01fcbaf4e5208 Mon Sep 17 00:00:00 2001 From: Jake Champlin Date: Fri, 4 Mar 2016 22:14:57 -0500 Subject: [PATCH 2/2] Cleanup style, and make cleaner --- command/alloc_status.go | 2 +- command/node_status.go | 25 ++++++------------------- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/command/alloc_status.go b/command/alloc_status.go index faa0677dd..7bb1761ae 100644 --- a/command/alloc_status.go +++ b/command/alloc_status.go @@ -267,7 +267,7 @@ func (c *AllocStatusCommand) sortedTaskStateIterator(m map[string]*api.TaskState // taskResources prints out the tasks current resource usage func (c *AllocStatusCommand) taskResources(alloc *api.Allocation) { resources := make([]string, 2) - resources[0] = "CPU|MemoryMB|DiskMB|IOPS" + resources[0] = "CPU|Memory MB|Disk MB|IOPS" resources[1] = fmt.Sprintf("%v|%v|%v|%v", alloc.Resources.CPU, alloc.Resources.MemoryMB, diff --git a/command/node_status.go b/command/node_status.go index 561563823..164db8678 100644 --- a/command/node_status.go +++ b/command/node_status.go @@ -102,7 +102,7 @@ func (c *NodeStatusCommand) Run(args []string) int { } for i, node := range nodes { if list_allocs { - numAllocs, err := getNumRunningAllocs(client, node) + numAllocs, err := getRunningAllocs(client, node.ID) if err != nil { c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err)) return 1 @@ -114,7 +114,7 @@ func (c *NodeStatusCommand) Run(args []string) int { node.NodeClass, node.Drain, node.Status, - numAllocs) + len(numAllocs)) } else { out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s", limit(node.ID, length), @@ -228,25 +228,12 @@ func (c *NodeStatusCommand) Run(args []string) int { return 0 } -// getNumRunningAllocs fetches the number of running allocations on the node -func getNumRunningAllocs(client *api.Client, node *api.NodeListStub) (int, error) { - // Fetch number of running allocations per node - numAllocs := 0 - nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil) - for _, alloc := range nodeAllocs { - if alloc.ClientStatus == "running" { - numAllocs += 1 - } - } - return numAllocs, err -} - // getRunningAllocs returns a slice of allocation id's running on the node -func getRunningAllocs(client *api.Client, node *api.Node) ([]*api.Allocation, error) { +func getRunningAllocs(client *api.Client, nodeID string) ([]*api.Allocation, error) { var allocs []*api.Allocation // Query the node allocations - nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil) + nodeAllocs, _, err := client.Nodes().Allocations(nodeID, nil) // Filter list to only running allocations for _, alloc := range nodeAllocs { if alloc.ClientStatus == "running" { @@ -281,7 +268,7 @@ func getResources(client *api.Client, node *api.Node) ([]string, error) { var cpu, mem, disk, iops int // Get list of running allocations on the node - runningAllocs, err := getRunningAllocs(client, node) + runningAllocs, err := getRunningAllocs(client, node.ID) // Get Resources for _, alloc := range runningAllocs { @@ -292,7 +279,7 @@ func getResources(client *api.Client, node *api.Node) ([]string, error) { } resources = make([]string, 2) - resources[0] = "CPU|MemoryMB|DiskMB|IOPS" + resources[0] = "CPU|Memory MB|Disk MB|IOPS" resources[1] = fmt.Sprintf("%v|%v|%v|%v", cpu, mem,