2016-05-09 15:55:19 +00:00
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
2016-12-13 11:28:57 +00:00
|
|
|
"log"
|
2016-06-20 17:29:46 +00:00
|
|
|
"math"
|
2016-06-10 21:14:33 +00:00
|
|
|
"runtime"
|
2016-12-12 06:58:28 +00:00
|
|
|
"sync"
|
2016-05-27 21:15:51 +00:00
|
|
|
"time"
|
|
|
|
|
2016-05-09 15:55:19 +00:00
|
|
|
"github.com/shirou/gopsutil/cpu"
|
2016-05-22 10:46:49 +00:00
|
|
|
"github.com/shirou/gopsutil/disk"
|
2016-05-22 09:36:12 +00:00
|
|
|
"github.com/shirou/gopsutil/host"
|
2016-05-09 15:55:19 +00:00
|
|
|
"github.com/shirou/gopsutil/mem"
|
2016-06-10 21:14:33 +00:00
|
|
|
|
|
|
|
shelpers "github.com/hashicorp/nomad/helper/stats"
|
2016-05-09 15:55:19 +00:00
|
|
|
)
|
|
|
|
|
2016-05-09 16:53:00 +00:00
|
|
|
// HostStats represents resource usage stats of the host running a Nomad client
|
2016-05-09 15:55:19 +00:00
|
|
|
type HostStats struct {
|
2016-06-10 21:14:33 +00:00
|
|
|
Memory *MemoryStats
|
|
|
|
CPU []*CPUStats
|
|
|
|
DiskStats []*DiskStats
|
2016-12-16 07:54:54 +00:00
|
|
|
AllocDirStats *DiskStats
|
2016-06-10 21:14:33 +00:00
|
|
|
Uptime uint64
|
|
|
|
Timestamp int64
|
|
|
|
CPUTicksConsumed float64
|
2016-05-09 15:55:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 16:53:00 +00:00
|
|
|
// MemoryStats represnts stats related to virtual memory usage
|
2016-05-09 15:55:19 +00:00
|
|
|
type MemoryStats struct {
|
|
|
|
Total uint64
|
|
|
|
Available uint64
|
|
|
|
Used uint64
|
|
|
|
Free uint64
|
|
|
|
}
|
|
|
|
|
2016-05-09 16:53:00 +00:00
|
|
|
// CPUStats represents stats related to cpu usage
|
2016-05-09 15:55:19 +00:00
|
|
|
type CPUStats struct {
|
|
|
|
CPU string
|
|
|
|
User float64
|
|
|
|
System float64
|
|
|
|
Idle float64
|
2016-05-22 09:04:27 +00:00
|
|
|
Total float64
|
2016-05-09 15:55:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 10:46:49 +00:00
|
|
|
// DiskStats represents stats related to disk usage
|
|
|
|
type DiskStats struct {
|
|
|
|
Device string
|
|
|
|
Mountpoint string
|
|
|
|
Size uint64
|
|
|
|
Used uint64
|
|
|
|
Available uint64
|
|
|
|
UsedPercent float64
|
|
|
|
InodesUsedPercent float64
|
|
|
|
}
|
|
|
|
|
2016-12-20 01:53:11 +00:00
|
|
|
// NodeStatsCollector is an interface which is used for the puproses of mocking
|
|
|
|
// the HostStatsCollector in the tests
|
2016-12-16 07:54:54 +00:00
|
|
|
type NodeStatsCollector interface {
|
|
|
|
Collect() error
|
|
|
|
Stats() *HostStats
|
|
|
|
}
|
|
|
|
|
2016-05-22 09:04:27 +00:00
|
|
|
// HostStatsCollector collects host resource usage stats
|
|
|
|
type HostStatsCollector struct {
|
2016-06-10 21:14:33 +00:00
|
|
|
clkSpeed float64
|
|
|
|
numCores int
|
2016-05-22 09:04:27 +00:00
|
|
|
statsCalculator map[string]*HostCpuStatsCalculator
|
2016-12-13 11:28:57 +00:00
|
|
|
logger *log.Logger
|
2016-12-12 06:58:28 +00:00
|
|
|
hostStats *HostStats
|
|
|
|
hostStatsLock sync.RWMutex
|
2016-12-16 07:54:54 +00:00
|
|
|
allocDir string
|
2016-05-22 09:04:27 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 01:53:11 +00:00
|
|
|
// NewHostStatsCollector returns a HostStatsCollector. The allocDir is passed in
|
|
|
|
// so that we can present the disk related statistics for the mountpoint where
|
|
|
|
// the allocation directory lives
|
2016-12-16 07:54:54 +00:00
|
|
|
func NewHostStatsCollector(logger *log.Logger, allocDir string) *HostStatsCollector {
|
2016-06-10 21:14:33 +00:00
|
|
|
numCores := runtime.NumCPU()
|
2016-05-22 09:04:27 +00:00
|
|
|
statsCalculator := make(map[string]*HostCpuStatsCalculator)
|
2016-06-10 21:14:33 +00:00
|
|
|
collector := &HostStatsCollector{
|
|
|
|
statsCalculator: statsCalculator,
|
|
|
|
numCores: numCores,
|
2016-12-13 11:28:57 +00:00
|
|
|
logger: logger,
|
2016-12-16 07:54:54 +00:00
|
|
|
allocDir: allocDir,
|
2016-06-10 21:14:33 +00:00
|
|
|
}
|
|
|
|
return collector
|
2016-05-22 09:04:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Collect collects stats related to resource usage of a host
|
2016-12-12 06:58:28 +00:00
|
|
|
func (h *HostStatsCollector) Collect() error {
|
2016-05-27 21:15:51 +00:00
|
|
|
hs := &HostStats{Timestamp: time.Now().UTC().UnixNano()}
|
2016-08-07 06:00:00 +00:00
|
|
|
memStats, err := mem.VirtualMemory()
|
|
|
|
if err != nil {
|
2016-12-12 06:58:28 +00:00
|
|
|
return err
|
2016-08-07 06:00:00 +00:00
|
|
|
}
|
2016-08-11 01:53:33 +00:00
|
|
|
hs.Memory = &MemoryStats{
|
2016-08-07 06:00:00 +00:00
|
|
|
Total: memStats.Total,
|
|
|
|
Available: memStats.Available,
|
|
|
|
Used: memStats.Used,
|
|
|
|
Free: memStats.Free,
|
2016-05-09 15:55:19 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 21:14:33 +00:00
|
|
|
ticksConsumed := 0.0
|
2016-08-07 06:00:00 +00:00
|
|
|
cpuStats, err := cpu.Times(true)
|
|
|
|
if err != nil {
|
2016-12-12 06:58:28 +00:00
|
|
|
return err
|
2016-08-07 06:00:00 +00:00
|
|
|
}
|
|
|
|
cs := make([]*CPUStats, len(cpuStats))
|
|
|
|
for idx, cpuStat := range cpuStats {
|
|
|
|
percentCalculator, ok := h.statsCalculator[cpuStat.CPU]
|
|
|
|
if !ok {
|
|
|
|
percentCalculator = NewHostCpuStatsCalculator()
|
|
|
|
h.statsCalculator[cpuStat.CPU] = percentCalculator
|
2016-05-22 09:04:27 +00:00
|
|
|
}
|
2016-08-07 06:00:00 +00:00
|
|
|
idle, user, system, total := percentCalculator.Calculate(cpuStat)
|
|
|
|
cs[idx] = &CPUStats{
|
|
|
|
CPU: cpuStat.CPU,
|
|
|
|
User: user,
|
|
|
|
System: system,
|
|
|
|
Idle: idle,
|
|
|
|
Total: total,
|
|
|
|
}
|
|
|
|
ticksConsumed += (total / 100) * (shelpers.TotalTicksAvailable() / float64(len(cpuStats)))
|
2016-05-09 15:55:19 +00:00
|
|
|
}
|
2016-08-07 06:00:00 +00:00
|
|
|
hs.CPU = cs
|
|
|
|
hs.CPUTicksConsumed = ticksConsumed
|
2016-05-09 15:55:19 +00:00
|
|
|
|
2016-08-07 06:00:00 +00:00
|
|
|
partitions, err := disk.Partitions(false)
|
|
|
|
if err != nil {
|
2016-12-12 06:58:28 +00:00
|
|
|
return err
|
2016-08-07 06:00:00 +00:00
|
|
|
}
|
|
|
|
var diskStats []*DiskStats
|
|
|
|
for _, partition := range partitions {
|
|
|
|
usage, err := disk.Usage(partition.Mountpoint)
|
|
|
|
if err != nil {
|
2016-12-13 11:28:57 +00:00
|
|
|
h.logger.Printf("[WARN] client: error fetching host disk usage stats for %v: %v", partition.Mountpoint, err)
|
|
|
|
continue
|
2016-05-22 10:46:49 +00:00
|
|
|
}
|
2016-12-16 07:54:54 +00:00
|
|
|
ds := h.toDiskStats(usage, &partition)
|
|
|
|
diskStats = append(diskStats, ds)
|
2016-05-09 15:55:19 +00:00
|
|
|
}
|
2016-08-07 06:00:00 +00:00
|
|
|
hs.DiskStats = diskStats
|
2016-05-22 10:46:49 +00:00
|
|
|
|
2016-12-20 01:53:11 +00:00
|
|
|
// Getting the disk stats for the allocation directory
|
2016-12-16 07:54:54 +00:00
|
|
|
usage, err := disk.Usage(h.allocDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hs.AllocDirStats = h.toDiskStats(usage, nil)
|
|
|
|
|
2016-08-07 06:00:00 +00:00
|
|
|
uptime, err := host.Uptime()
|
|
|
|
if err != nil {
|
2016-12-12 06:58:28 +00:00
|
|
|
return err
|
2016-05-22 09:36:12 +00:00
|
|
|
}
|
2016-08-07 06:00:00 +00:00
|
|
|
hs.Uptime = uptime
|
|
|
|
|
2016-12-12 06:58:28 +00:00
|
|
|
h.hostStatsLock.Lock()
|
|
|
|
defer h.hostStatsLock.Unlock()
|
|
|
|
h.hostStats = hs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-16 07:54:54 +00:00
|
|
|
// Stats returns the host stats that has been collected
|
2016-12-12 06:58:28 +00:00
|
|
|
func (h *HostStatsCollector) Stats() *HostStats {
|
|
|
|
h.hostStatsLock.RLock()
|
|
|
|
defer h.hostStatsLock.RUnlock()
|
|
|
|
return h.hostStats
|
2016-05-09 15:55:19 +00:00
|
|
|
}
|
2016-05-22 09:04:27 +00:00
|
|
|
|
2016-12-16 07:54:54 +00:00
|
|
|
// toDiskStats merges UsageStat and PartitionStat to create a DiskStat
|
|
|
|
func (h *HostStatsCollector) toDiskStats(usage *disk.UsageStat, partitionStat *disk.PartitionStat) *DiskStats {
|
|
|
|
if usage == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ds := DiskStats{
|
|
|
|
Size: usage.Total,
|
|
|
|
Used: usage.Used,
|
|
|
|
Available: usage.Free,
|
|
|
|
UsedPercent: usage.UsedPercent,
|
|
|
|
InodesUsedPercent: usage.InodesUsedPercent,
|
|
|
|
}
|
|
|
|
if math.IsNaN(ds.UsedPercent) {
|
|
|
|
ds.UsedPercent = 0.0
|
|
|
|
}
|
|
|
|
if math.IsNaN(ds.InodesUsedPercent) {
|
|
|
|
ds.InodesUsedPercent = 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
if partitionStat != nil {
|
|
|
|
ds.Device = partitionStat.Device
|
|
|
|
ds.Mountpoint = partitionStat.Mountpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ds
|
|
|
|
}
|
|
|
|
|
2016-05-22 09:04:27 +00:00
|
|
|
// HostCpuStatsCalculator calculates cpu usage percentages
|
|
|
|
type HostCpuStatsCalculator struct {
|
|
|
|
prevIdle float64
|
|
|
|
prevUser float64
|
|
|
|
prevSystem float64
|
|
|
|
prevBusy float64
|
|
|
|
prevTotal float64
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHostCpuStatsCalculator returns a HostCpuStatsCalculator
|
|
|
|
func NewHostCpuStatsCalculator() *HostCpuStatsCalculator {
|
|
|
|
return &HostCpuStatsCalculator{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate calculates the current cpu usage percentages
|
|
|
|
func (h *HostCpuStatsCalculator) Calculate(times cpu.TimesStat) (idle float64, user float64, system float64, total float64) {
|
|
|
|
currentIdle := times.Idle
|
|
|
|
currentUser := times.User
|
|
|
|
currentSystem := times.System
|
|
|
|
currentTotal := times.Total()
|
|
|
|
|
|
|
|
deltaTotal := currentTotal - h.prevTotal
|
|
|
|
idle = ((currentIdle - h.prevIdle) / deltaTotal) * 100
|
|
|
|
user = ((currentUser - h.prevUser) / deltaTotal) * 100
|
|
|
|
system = ((currentSystem - h.prevSystem) / deltaTotal) * 100
|
|
|
|
|
|
|
|
currentBusy := times.User + times.System + times.Nice + times.Iowait + times.Irq +
|
|
|
|
times.Softirq + times.Steal + times.Guest + times.GuestNice + times.Stolen
|
|
|
|
|
|
|
|
total = ((currentBusy - h.prevBusy) / deltaTotal) * 100
|
|
|
|
|
|
|
|
h.prevIdle = currentIdle
|
|
|
|
h.prevUser = currentUser
|
|
|
|
h.prevSystem = currentSystem
|
|
|
|
h.prevTotal = currentTotal
|
|
|
|
h.prevBusy = currentBusy
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|