Skip to content

Commit

Permalink
Added disk usage to node status
Browse files Browse the repository at this point in the history
  • Loading branch information
diptanu committed May 22, 2016
1 parent d9ee502 commit aa11fe8
Show file tree
Hide file tree
Showing 16 changed files with 1,428 additions and 36 deletions.
17 changes: 14 additions & 3 deletions api/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ type Node struct {

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

type HostMemoryStats struct {
Expand All @@ -148,6 +149,16 @@ type HostCPUStats struct {
Idle float64
}

type HostDiskStats struct {
Device string
Mountpoint string
Size uint64
Used uint64
Available uint64
UsedPercent float64
InodesUsedPercent float64
}

// NodeListStub is a subset of information returned during
// node list operations.
type NodeListStub struct {
Expand Down
94 changes: 62 additions & 32 deletions client/stats/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package stats

import (
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"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
Memory *MemoryStats
CPU []*CPUStats
DiskStats []*DiskStats
Uptime uint64
}

// MemoryStats represnts stats related to virtual memory usage
Expand All @@ -30,6 +32,17 @@ type CPUStats struct {
Total float64
}

// DiskStats represents stats related to disk usage
type DiskStats struct {
Device string
Mountpoint string
Size uint64
Used uint64
Available uint64
UsedPercent float64
InodesUsedPercent float64
}

// HostStatsCollector collects host resource usage stats
type HostStatsCollector struct {
statsCalculator map[string]*HostCpuStatsCalculator
Expand All @@ -50,41 +63,58 @@ func NewHostStatsCollector() (*HostStatsCollector, error) {

// Collect collects stats related to resource usage of a host
func (h *HostStatsCollector) Collect() (*HostStats, error) {
memStats, err := mem.VirtualMemory()
if err != nil {
return nil, err
}
ms := &MemoryStats{
Total: memStats.Total,
Available: memStats.Available,
Used: memStats.Used,
Free: memStats.Free,
hs := &HostStats{}
if memStats, err := mem.VirtualMemory(); err == nil {
ms := &MemoryStats{
Total: memStats.Total,
Available: memStats.Available,
Used: memStats.Used,
Free: memStats.Free,
}
hs.Memory = ms
}

cpuStats, err := cpu.Times(true)
cs := make([]*CPUStats, len(cpuStats))
for idx, cpuStat := range cpuStats {
cs[idx] = &CPUStats{
CPU: cpuStat.CPU,
User: cpuStat.User,
System: cpuStat.System,
Idle: cpuStat.Idle,
}
if percentCalculator, ok := h.statsCalculator[cpuStat.CPU]; ok {
idle, user, system, total := percentCalculator.Calculate(cpuStat)
cs[idx].Idle = idle
cs[idx].System = system
cs[idx].User = user
cs[idx].Total = total
} else {
h.statsCalculator[cpuStat.CPU] = NewHostCpuStatsCalculator()
if cpuStats, err := cpu.Times(true); err == nil {
cs := make([]*CPUStats, len(cpuStats))
for idx, cpuStat := range cpuStats {
cs[idx] = &CPUStats{
CPU: cpuStat.CPU,
User: cpuStat.User,
System: cpuStat.System,
Idle: cpuStat.Idle,
}
if percentCalculator, ok := h.statsCalculator[cpuStat.CPU]; ok {
idle, user, system, total := percentCalculator.Calculate(cpuStat)
cs[idx].Idle = idle
cs[idx].System = system
cs[idx].User = user
cs[idx].Total = total
} else {
h.statsCalculator[cpuStat.CPU] = NewHostCpuStatsCalculator()
}
}
hs.CPU = cs
}

hs := &HostStats{
Memory: ms,
CPU: cs,
if partitions, err := disk.Partitions(false); err == nil {
var diskStats []*DiskStats
for _, partition := range partitions {
if usage, err := disk.Usage(partition.Mountpoint); err == nil {
ds := DiskStats{
Device: partition.Device,
Mountpoint: partition.Mountpoint,
Size: usage.Total,
Used: usage.Used,
Available: usage.Free,
UsedPercent: usage.UsedPercent,
InodesUsedPercent: usage.InodesUsedPercent,
}
diskStats = append(diskStats, &ds)
}
}
hs.DiskStats = diskStats
}

if uptime, err := host.Uptime(); err == nil {
hs.Uptime = uptime
}
Expand Down
1 change: 0 additions & 1 deletion client/task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ func (r *TaskRunner) run() {
for {
select {
case waitRes := <-r.handle.WaitCh():
r.logger.Printf("DIPTANU RETURNING FROM WAIT")
if waitRes == nil {
panic("nil wait")
}
Expand Down
17 changes: 17 additions & 0 deletions command/node_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ func (c *NodeStatusCommand) Run(args []string) int {
c.printCpuStats(hostStats)
c.Ui.Output("\n===> Node Memory Stats")
c.printMemoryStats(hostStats)
c.Ui.Output("\n===> Node Disk Stats")
c.printDiskStats(hostStats)
}

allocs, err := getAllocs(client, node, length)
Expand Down Expand Up @@ -293,6 +295,21 @@ func (c *NodeStatusCommand) printMemoryStats(hostStats *api.HostStats) {
c.Ui.Output(formatKV(memStatsAttr))
}

func (c *NodeStatusCommand) printDiskStats(hostStats *api.HostStats) {
for _, diskStat := range hostStats.DiskStats {
diskStatsAttr := make([]string, 7)
diskStatsAttr[0] = fmt.Sprintf("Device|%s", diskStat.Device)
diskStatsAttr[1] = fmt.Sprintf("MountPoint|%s", diskStat.Mountpoint)
diskStatsAttr[2] = fmt.Sprintf("Size|%s", humanize.Bytes(diskStat.Size))
diskStatsAttr[3] = fmt.Sprintf("Used|%s", humanize.Bytes(diskStat.Used))
diskStatsAttr[4] = fmt.Sprintf("Available|%s", humanize.Bytes(diskStat.Available))
diskStatsAttr[5] = fmt.Sprintf("Used Percent|%s", formatFloat64(diskStat.UsedPercent))
diskStatsAttr[6] = fmt.Sprintf("Inodes Percent|%s", formatFloat64(diskStat.InodesUsedPercent))
c.Ui.Output(formatKV(diskStatsAttr))
c.Ui.Output("")
}
}

// getRunningAllocs returns a slice of allocation id's running on the node
func getRunningAllocs(client *api.Client, nodeID string) ([]*api.Allocation, error) {
var allocs []*api.Allocation
Expand Down
60 changes: 60 additions & 0 deletions vendor/github.com/shirou/gopsutil/disk/disk.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 111 additions & 0 deletions vendor/github.com/shirou/gopsutil/disk/disk_darwin.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit aa11fe8

Please sign in to comment.