Skip to content

Commit

Permalink
command: Cleanup node-status
Browse files Browse the repository at this point in the history
  • Loading branch information
endocrimes committed Aug 9, 2019
1 parent ac26f25 commit 78e4459
Showing 1 changed file with 84 additions and 68 deletions.
152 changes: 84 additions & 68 deletions command/node_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func nodeDrivers(n *api.Node) []string {

func nodeVolumeNames(n *api.Node) []string {
var volumes []string
for name, _ := range n.HostVolumes {
for name := range n.HostVolumes {
volumes = append(volumes, name)
}

Expand Down Expand Up @@ -346,88 +346,104 @@ func (c *NodeStatusCommand) formatNode(client *api.Client, node *api.Node) int {
basic = append(basic, fmt.Sprintf("Host Volumes|%s", strings.Join(nodeVolumeNames(node), ",")))
basic = append(basic, fmt.Sprintf("Drivers|%s", strings.Join(nodeDrivers(node), ",")))
c.Ui.Output(c.Colorize().Color(formatKV(basic)))
} else {
// Get the host stats
hostStats, nodeStatsErr := client.Nodes().Stats(node.ID, nil)
if nodeStatsErr != nil {
c.Ui.Output("")
c.Ui.Error(fmt.Sprintf("error fetching node stats: %v", nodeStatsErr))
}
if hostStats != nil {
uptime := time.Duration(hostStats.Uptime * uint64(time.Second))
basic = append(basic, fmt.Sprintf("Uptime|%s", uptime.String()))
}

// Emit the volume info
if !c.verbose {
basic = append(basic, fmt.Sprintf("Host Volumes|%s", strings.Join(nodeVolumeNames(node), ",")))
// Output alloc info
if err := c.outputAllocInfo(client, node); err != nil {
c.Ui.Error(fmt.Sprintf("%s", err))
return 1
}

// Emit the driver info
if !c.verbose {
driverStatus := fmt.Sprintf("Driver Status| %s", c.outputTruncatedNodeDriverInfo(node))
basic = append(basic, driverStatus)
}
return 0
}

c.Ui.Output(c.Colorize().Color(formatKV(basic)))
// Get the host stats
hostStats, nodeStatsErr := client.Nodes().Stats(node.ID, nil)
if nodeStatsErr != nil {
c.Ui.Output("")
c.Ui.Error(fmt.Sprintf("error fetching node stats: %v", nodeStatsErr))
}
if hostStats != nil {
uptime := time.Duration(hostStats.Uptime * uint64(time.Second))
basic = append(basic, fmt.Sprintf("Uptime|%s", uptime.String()))
}

if c.verbose {
c.outputNodeVolumeInfo(node)
c.outputNodeDriverInfo(node)
}
// When we're not running in verbose mode, then also include host volumes and
// driver info in the basic output
if !c.verbose {
basic = append(basic, fmt.Sprintf("Host Volumes|%s", strings.Join(nodeVolumeNames(node), ",")))

// Emit node events
c.outputNodeStatusEvents(node)
driverStatus := fmt.Sprintf("Driver Status| %s", c.outputTruncatedNodeDriverInfo(node))
basic = append(basic, driverStatus)
}

// Get list of running allocations on the node
runningAllocs, err := getRunningAllocs(client, node.ID)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying node for running allocations: %s", err))
return 1
}
// Output the basic info
c.Ui.Output(c.Colorize().Color(formatKV(basic)))

allocatedResources := getAllocatedResources(client, runningAllocs, node)
c.Ui.Output(c.Colorize().Color("\n[bold]Allocated Resources[reset]"))
c.Ui.Output(formatList(allocatedResources))
// If we're running in verbose mode, include full host volume and driver info
if c.verbose {
c.outputNodeVolumeInfo(node)
c.outputNodeDriverInfo(node)
}

actualResources, err := getActualResources(client, runningAllocs, node)
if err == nil {
c.Ui.Output(c.Colorize().Color("\n[bold]Allocation Resource Utilization[reset]"))
c.Ui.Output(formatList(actualResources))
}
// Emit node events
c.outputNodeStatusEvents(node)

hostResources, err := getHostResources(hostStats, node)
if err != nil {
c.Ui.Output("")
c.Ui.Error(fmt.Sprintf("error fetching node stats: %v", err))
}
if err == nil {
c.Ui.Output(c.Colorize().Color("\n[bold]Host Resource Utilization[reset]"))
c.Ui.Output(formatList(hostResources))
}
// Get list of running allocations on the node
runningAllocs, err := getRunningAllocs(client, node.ID)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying node for running allocations: %s", err))
return 1
}

if err == nil && node.NodeResources != nil && len(node.NodeResources.Devices) > 0 {
c.Ui.Output(c.Colorize().Color("\n[bold]Device Resource Utilization[reset]"))
c.Ui.Output(formatList(getDeviceResourcesForNode(hostStats.DeviceStats, node)))
}
if hostStats != nil && c.stats {
c.Ui.Output(c.Colorize().Color("\n[bold]CPU Stats[reset]"))
c.printCpuStats(hostStats)
c.Ui.Output(c.Colorize().Color("\n[bold]Memory Stats[reset]"))
c.printMemoryStats(hostStats)
c.Ui.Output(c.Colorize().Color("\n[bold]Disk Stats[reset]"))
c.printDiskStats(hostStats)
if len(hostStats.DeviceStats) > 0 {
c.Ui.Output(c.Colorize().Color("\n[bold]Device Stats[reset]"))
printDeviceStats(c.Ui, hostStats.DeviceStats)
}
allocatedResources := getAllocatedResources(client, runningAllocs, node)
c.Ui.Output(c.Colorize().Color("\n[bold]Allocated Resources[reset]"))
c.Ui.Output(formatList(allocatedResources))

actualResources, err := getActualResources(client, runningAllocs, node)
if err == nil {
c.Ui.Output(c.Colorize().Color("\n[bold]Allocation Resource Utilization[reset]"))
c.Ui.Output(formatList(actualResources))
}

hostResources, err := getHostResources(hostStats, node)
if err != nil {
c.Ui.Output("")
c.Ui.Error(fmt.Sprintf("error fetching node stats: %v", err))
}
if err == nil {
c.Ui.Output(c.Colorize().Color("\n[bold]Host Resource Utilization[reset]"))
c.Ui.Output(formatList(hostResources))
}

if err == nil && node.NodeResources != nil && len(node.NodeResources.Devices) > 0 {
c.Ui.Output(c.Colorize().Color("\n[bold]Device Resource Utilization[reset]"))
c.Ui.Output(formatList(getDeviceResourcesForNode(hostStats.DeviceStats, node)))
}
if hostStats != nil && c.stats {
c.Ui.Output(c.Colorize().Color("\n[bold]CPU Stats[reset]"))
c.printCpuStats(hostStats)
c.Ui.Output(c.Colorize().Color("\n[bold]Memory Stats[reset]"))
c.printMemoryStats(hostStats)
c.Ui.Output(c.Colorize().Color("\n[bold]Disk Stats[reset]"))
c.printDiskStats(hostStats)
if len(hostStats.DeviceStats) > 0 {
c.Ui.Output(c.Colorize().Color("\n[bold]Device Stats[reset]"))
printDeviceStats(c.Ui, hostStats.DeviceStats)
}
}

if err := c.outputAllocInfo(client, node); err != nil {
c.Ui.Error(fmt.Sprintf("%s", err))
return 1
}

return 0
}

func (c *NodeStatusCommand) outputAllocInfo(client *api.Client, node *api.Node) error {
nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err))
return 1
return fmt.Errorf("Error querying node allocations: %s", err)
}

c.Ui.Output(c.Colorize().Color("\n[bold]Allocations[reset]"))
Expand All @@ -438,8 +454,8 @@ func (c *NodeStatusCommand) formatNode(client *api.Client, node *api.Node) int {
c.formatDeviceAttributes(node)
c.formatMeta(node)
}
return 0

return nil
}

func (c *NodeStatusCommand) outputTruncatedNodeDriverInfo(node *api.Node) string {
Expand Down

0 comments on commit 78e4459

Please sign in to comment.