Skip to content

Commit

Permalink
Added uptime to node stats
Browse files Browse the repository at this point in the history
  • Loading branch information
diptanu committed May 22, 2016
1 parent 3c83fa9 commit d9ee502
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions api/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type Node struct {
type HostStats struct {
Memory *HostMemoryStats
CPU []*HostCPUStats
Uptime uint64
}

type HostMemoryStats struct {
Expand Down
5 changes: 5 additions & 0 deletions client/stats/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package stats

import (
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
)

// HostStats represents resource usage stats of the host running a Nomad client
type HostStats struct {
Memory *MemoryStats
CPU []*CPUStats
Uptime uint64
}

// MemoryStats represnts stats related to virtual memory usage
Expand Down Expand Up @@ -83,6 +85,9 @@ func (h *HostStatsCollector) Collect() (*HostStats, error) {
Memory: ms,
CPU: cs,
}
if uptime, err := host.Uptime(); err == nil {
hs.Uptime = uptime
}
return hs, nil
}

Expand Down
26 changes: 17 additions & 9 deletions command/node_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sort"
"strconv"
"strings"
"time"

"github.com/dustin/go-humanize"

Expand Down Expand Up @@ -59,6 +60,7 @@ func (c *NodeStatusCommand) Synopsis() string {

func (c *NodeStatusCommand) Run(args []string) int {
var short, verbose, list_allocs, self, stats bool
var hostStats *api.HostStats

flags := c.Meta.FlagSet("node-status", FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
Expand Down Expand Up @@ -200,6 +202,12 @@ 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))
}
}

// Format the output
basic := []string{
fmt.Sprintf("ID|%s", limit(node.ID, length)),
Expand All @@ -209,6 +217,10 @@ func (c *NodeStatusCommand) Run(args []string) int {
fmt.Sprintf("Drain|%v", node.Drain),
fmt.Sprintf("Status|%s", node.Status),
}
if stats && hostStats != nil {
uptime := time.Duration(hostStats.Uptime * uint64(time.Second))
basic = append(basic, fmt.Sprintf("Uptime|%s", uptime.String()))
}
c.Ui.Output(formatKV(basic))

if !short {
Expand All @@ -219,15 +231,11 @@ func (c *NodeStatusCommand) Run(args []string) int {
}
c.Ui.Output("\n==> Resource Utilization")
c.Ui.Output(formatList(resources))
if stats {
if hostStats, err := client.Nodes().Stats(node.ID, nil); err == nil {
c.Ui.Output("\n===> Node CPU Stats")
c.printCpuStats(hostStats)
c.Ui.Output("\n===> Node Memory Stats")
c.printMemoryStats(hostStats)
} else {
c.Ui.Output(fmt.Sprintf("error getting node stats", err))
}
if stats && hostStats != nil {
c.Ui.Output("\n===> Node CPU Stats")
c.printCpuStats(hostStats)
c.Ui.Output("\n===> Node Memory Stats")
c.printMemoryStats(hostStats)
}

allocs, err := getAllocs(client, node, length)
Expand Down

0 comments on commit d9ee502

Please sign in to comment.