Print resource usage w/ alloc-status + node-status

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      = <none>
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
```
This commit is contained in:
Jake Champlin 2016-03-04 21:29:39 -05:00
parent 30482f1cd6
commit edd60b4fb1
2 changed files with 91 additions and 23 deletions

View File

@ -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))
}

View File

@ -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
}