func getAllTimes() []cpu.TimesStat { cnt := []cpu.TimesStat{} cnt1, _ := cpu.Times(true) cnt2, _ := cpu.Times(false) cnt = append(cnt, cnt1...) cnt = append(cnt, cnt2...) return cnt }
// GetSystem gets statistics about the system func GetSystem() *api.SystemStats { status := new(api.SystemStats) if load, err := load.Avg(); err == nil { status.Load = &api.SystemStats_Loadstats{ Load1: float32(load.Load1), Load5: float32(load.Load5), Load15: float32(load.Load15), } } if cpu, err := cpu.Times(false); err == nil && len(cpu) == 1 { status.Cpu = &api.SystemStats_CPUStats{ User: float32(cpu[0].User), System: float32(cpu[0].System), Idle: float32(cpu[0].Idle), } } if mem, err := mem.VirtualMemory(); err == nil { status.Memory = &api.SystemStats_MemoryStats{ Total: mem.Total, Available: mem.Available, Used: mem.Used, } } return status }
// Collect collects stats related to resource usage of a host func (h *HostStatsCollector) Collect() (*HostStats, error) { hs := &HostStats{Timestamp: time.Now().UTC().UnixNano()} if memStats, err := mem.VirtualMemory(); err == nil { ms := &MemoryStats{ Total: memStats.Total, Available: memStats.Available, Used: memStats.Used, Free: memStats.Free, } hs.Memory = ms } 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, } percentCalculator, ok := h.statsCalculator[cpuStat.CPU] if !ok { percentCalculator = NewHostCpuStatsCalculator() h.statsCalculator[cpuStat.CPU] = percentCalculator } idle, user, system, total := percentCalculator.Calculate(cpuStat) cs[idx].Idle = idle cs[idx].System = system cs[idx].User = user cs[idx].Total = total } hs.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 } return hs, nil }
func (s *systemPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.TimesStat, error) { var cpuTimes []cpu.TimesStat if perCPU { if perCPUTimes, err := cpu.Times(true); err == nil { cpuTimes = append(cpuTimes, perCPUTimes...) } else { return nil, err } } if totalCPU { if totalCPUTimes, err := cpu.Times(false); err == nil { cpuTimes = append(cpuTimes, totalCPUTimes...) } else { return nil, err } } return cpuTimes, nil }
// CPUUsage - return a map with CPU usage stats func CPUUsage() CPUUsageStruct { cpuTimes1, err := cpu.Times(false) if err != nil { log.Errorf("error getting CPU info: %s", err) } c := CPUUsageStruct{} for i, lastCts := range cpuTimes1 { lastTotal := totalCPUTime(lastCts) time.Sleep(1 * time.Second) cpuTimes2, _ := cpu.Times(false) cts := cpuTimes2[i] total := totalCPUTime(cts) totalDelta := total - lastTotal system := 100 * (cts.System - lastCts.System) / totalDelta nice := 100 * (cts.Nice - lastCts.Nice) / totalDelta user := 100 * (cts.User - lastCts.User) / totalDelta idle := 100 * (cts.Idle - lastCts.Idle) / totalDelta iowait := 100 * (cts.Iowait - lastCts.Iowait) / totalDelta steal := 100 * (cts.Steal - lastCts.Steal) / totalDelta systemPercent, _ := util.FloatDecimalPoint(system, 2) nicePercent, _ := util.FloatDecimalPoint(nice, 2) userPercent, _ := util.FloatDecimalPoint(user, 2) idlePercent, _ := util.FloatDecimalPoint(idle, 2) iowaitPercent, _ := util.FloatDecimalPoint(iowait, 2) stealPercent, _ := util.FloatDecimalPoint(steal, 2) c = CPUUsageStruct{ User: userPercent, Idle: idlePercent, Nice: nicePercent, Steal: stealPercent, System: systemPercent, IOWait: iowaitPercent, } } return c }
func GetCpuStats() string { v, err := cpu.Times(false) format := "user=%f system=%f idle=%f nice=%f iowait=%f irq=%f soft_irq=%f steal=%f guest=%f guest_nice=%f stolen=%f" if err == nil { return fmt.Sprintf(format, v[0].User, v[0].System, v[0].Idle, v[0].Nice, v[0].Iowait, v[0].Irq, v[0].Softirq, v[0].Steal, v[0].Guest, v[0].GuestNice, v[0].Stolen) } else { return fmt.Sprintf(format, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) } }
func (this *guard) Start() { interval := time.Minute refreshTicker := time.NewTicker(interval) defer refreshTicker.Stop() alarmTicker := time.NewTicker(interval * 10) defer alarmTicker.Stop() refresh := func() { if v, err := cpu.Times(false); err == nil { this.cpuStat = v[0] } this.win.PushBack(this.cpuStat.User) } for { select { case <-this.gw.shutdownCh: return case <-this.refreshCh: refresh() case <-refreshTicker.C: refresh() case <-alarmTicker.C: loadTooHigh := true for _, load := range this.win.Slice() { if load < 0.9 { loadTooHigh = false break } } if loadTooHigh { // TODO in high load, should trigger elastic scaling event // send alarm email, sms } } } }
func CurrentProcStat() (*ProcStat, error) { stats, err := cpu.Times(false) if err != nil { log.Println(err) return nil, err } stat := stats[0] cu := &CpuUsage{ User: stat.User, Nice: stat.Nice, System: stat.System, Idle: stat.Idle, Iowait: stat.Iowait, Irq: stat.Irq, SoftIrq: stat.Softirq, Steal: stat.Steal, Guest: stat.Guest, Total: stat.User + stat.Nice + stat.System + stat.Idle + stat.Irq + stat.Softirq + stat.Steal + stat.Guest, } ps := &ProcStat{Cpus: make([]*CpuUsage, 1)} ps.Cpu = cu return ps, nil }
func cpuTimes(nss []core.Namespace) ([]plugin.MetricType, error) { // gather metrics per each cpu timesCPUs, err := cpu.Times(true) if err != nil { return nil, err } // gather accumulated metrics for all cpus timesAll, err := cpu.Times(false) if err != nil { return nil, err } results := []plugin.MetricType{} for _, ns := range nss { // set requested metric name from last namespace element metricName := ns.Element(len(ns) - 1).Value // check if requested metric is dynamic (requesting metrics for all cpu ids) if ns[3].Value == "*" { for _, timesCPU := range timesCPUs { // prepare namespace copy to update value // this will allow to keep namespace as dynamic (name != "") dyn := make([]core.NamespaceElement, len(ns)) copy(dyn, ns) dyn[3].Value = timesCPU.CPU // get requested metric value val, err := getCPUTimeValue(×CPU, metricName) if err != nil { return nil, err } metric := plugin.MetricType{ Namespace_: dyn, Data_: val, Timestamp_: time.Now(), Unit_: cpuLabels[metricName].unit, } results = append(results, metric) } } else { timeStats := append(timesAll, timesCPUs...) // find stats for interface name or all cpus timeStat := findCPUTimeStat(timeStats, ns[3].Value) if timeStat == nil { return nil, fmt.Errorf("Requested cpu id %s not found", ns[3].Value) } // get requested metric value from struct val, err := getCPUTimeValue(timeStat, metricName) if err != nil { return nil, err } metric := plugin.MetricType{ Namespace_: ns, Data_: val, Timestamp_: time.Now(), Unit_: cpuLabels[metricName].unit, } results = append(results, metric) } } return results, nil }
// Collect collects stats related to resource usage of a host func (h *HostStatsCollector) Collect() (*HostStats, error) { hs := &HostStats{Timestamp: time.Now().UTC().UnixNano()} memStats, err := mem.VirtualMemory() if err != nil { return nil, err } hs.Memory = &MemoryStats{ Total: memStats.Total, Available: memStats.Available, Used: memStats.Used, Free: memStats.Free, } ticksConsumed := 0.0 cpuStats, err := cpu.Times(true) if err != nil { return nil, err } 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 } 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))) } hs.CPU = cs hs.CPUTicksConsumed = ticksConsumed partitions, err := disk.Partitions(false) if err != nil { return nil, err } var diskStats []*DiskStats for _, partition := range partitions { usage, err := disk.Usage(partition.Mountpoint) if err != nil { return nil, err } ds := DiskStats{ Device: partition.Device, Mountpoint: partition.Mountpoint, 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 } diskStats = append(diskStats, &ds) } hs.DiskStats = diskStats uptime, err := host.Uptime() if err != nil { return nil, err } hs.Uptime = uptime return hs, nil }