func Stat() KV { stat := u.ReadFile("/proc/stat") stat = append(stat, fmt.Sprintf("timestamp %d", u.Timestamp())) m := make(KV, len(stat)+1) for _, str := range stat { fields := strings.Fields(str) if len(fields) >= 2 { m[fields[0]] = u.Atoi(fields[1:]) } } return m }
func Cpu() CpuStat { s := Stat() cores := -1 for key, _ := range s { if strings.HasPrefix(key, "cpu") { cores++ } } cpu := s["cpu"] return CpuStat{ User: cpu[0], Nice: cpu[1], System: cpu[2], Idle: cpu[3], Iowait: cpu[4], Irq: cpu[5], Softirq: cpu[6], Timestamp: u.Timestamp(), Cores: cores, } }
func CpuInfo() []KV { file, err := ioutil.ReadFile("/proc/cpuinfo") if err != nil { panic(err) } cpus := strings.Split(u.Trim(string(file)), "\n\n") var maps = make([]KV, len(cpus)) for id, cpu := range cpus { cpuinfo := append(strings.Split(cpu, "\n"), fmt.Sprintf("timestamp:%d", u.Timestamp())) var cpuMap = make(KV, len(cpuinfo)) for _, str := range cpuinfo { chunks := strings.Split(str, ":") if len(chunks) == 2 { cpuMap[u.Trim(strings.ToLower(chunks[0]))] = u.Trim(chunks[1]) } } maps[id] = cpuMap } return maps }