func (collector CpuCollector) Collect(vm models.VirtualMachine) (string, error) { // new current CPU counters newSchedStat, err := lookupStats(vm) if err != nil { return "", err } // get old CPU counters if oldSchedStat, exists := cpustats[vm.Ppid()]; exists { // set new stats as old stats for next run cpustats[vm.Ppid()] = newSchedStat // calculate diff between new and old counters cpu_utilisation := newSchedStat.calculateDiff(oldSchedStat) vCores := len(vm.VCpuTasks()) result := fmt.Sprintf("%d\t%.0f%%\t%.0f%%\t%.0f%%\t%.0f%%", vCores, (cpu_utilisation.Avg.Inside * 100), (cpu_utilisation.Avg.Outside * 100), (cpu_utilisation.Avg.Steal * 100), (cpu_utilisation.Avg_other * 100)) return result, nil } else { // no measurement yet // set new stats as old stats for next run cpustats[vm.Ppid()] = newSchedStat return "-", nil } }
func lookupStats(vm models.VirtualMachine) (VmCpuSchedStat, error) { cpuSchedStats := []CpuSchedStat{} for _, taskId := range vm.VCpuTasks() { cpuSchedStat, err := getCpuSchedStat(taskId) if err != nil { return VmCpuSchedStat{}, err } cpuSchedStats = append(cpuSchedStats, cpuSchedStat) } other := CpuSchedStat{} // TODO fill other //getTasks(vm) return VmCpuSchedStat{cpuSchedStats, other}, nil }