func (s *stats) GatherStats(percent bool) { s.SysUptime = systemstat.GetUptime() s.ProcUptime = time.Since(s.startTime).Seconds() s.SysMemK = systemstat.GetMemSample() s.LoadAverage = systemstat.GetLoadAvgSample() s.LastCPUSample = s.CurCPUSample s.CurCPUSample = systemstat.GetCPUSample() if s.sysCPUSampled { // we need 2 samples to get an average s.SysCPUAvg = systemstat.GetCPUAverage(s.LastCPUSample, s.CurCPUSample) } // we have at least one sample, subsequent rounds will give us an average s.sysCPUSampled = true s.ProcMemUsedPct = 100 * float64(s.CurProcCPUSample.ProcMemUsedK) / float64(s.SysMemK.MemTotal) s.LastProcCPUSample = s.CurProcCPUSample s.CurProcCPUSample = systemstat.GetProcCPUSample() if s.procCPUSampled { s.ProcCPUAvg = systemstat.GetProcCPUAverage(s.LastProcCPUSample, s.CurProcCPUSample, s.ProcUptime) } s.procCPUSampled = true }
// This example shows how easy it is to get memory information func Example_simple() { sample = systemstat.GetMemSample() fmt.Println("Total available RAM in kb:", sample.MemTotal, "k total") fmt.Println("Used RAM in kb:", sample.MemUsed, "k used") fmt.Println("Free RAM in kb:", sample.MemFree, "k free") fmt.Printf("The output is similar to, but somewhat different than:\n\ttop -n1 | grep Mem:\n") }
func getSystemMonitorSample() MonitorSample { cpuSample := systemstat.GetCPUSample() idle := float64(cpuSample.Idle) total := float64(cpuSample.Total) memSample := systemstat.GetMemSample() var stat MonitorSample stat.Name = "system" stat.CpuPct = (total - idle) * 100.0 / total stat.MemTotal = memSample.MemTotal stat.MemUsed = memSample.MemUsed return stat }