diff --git a/pkg/apis/compute/host.go b/pkg/apis/compute/host.go index 094d2c68a9b..ca4b063215d 100644 --- a/pkg/apis/compute/host.go +++ b/pkg/apis/compute/host.go @@ -553,7 +553,8 @@ type SHostStorageStat struct { type SHostPingInput struct { WithData bool `json:"with_data"` - MemoryUsedMb int `json:"memory_used_mb"` + MemoryUsedMb int `json:"memory_used_mb"` + CpuUsagePercent float64 `json:"cpu_usage_percent"` RootPartitionUsedCapacityMb int `json:"root_partition_used_capacity_mb"` diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 87abfeb4858..931a5797426 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -4917,6 +4917,7 @@ func (hh *SHost) PerformPing(ctx context.Context, userCred mcclient.TokenCredent } hh.SetMetadata(ctx, "root_partition_used_capacity_mb", input.RootPartitionUsedCapacityMb, userCred) hh.SetMetadata(ctx, "memory_used_mb", input.MemoryUsedMb, userCred) + hh.SetMetadata(ctx, "cpu_usage_percent", input.CpuUsagePercent, userCred) guests, _ := hh.GetGuests() for _, guest := range guests { diff --git a/pkg/hostman/hostinfo/hostpinger/hostpinger.go b/pkg/hostman/hostinfo/hostpinger/hostpinger.go index 2c499b37259..0faecee2291 100644 --- a/pkg/hostman/hostinfo/hostpinger/hostpinger.go +++ b/pkg/hostman/hostinfo/hostpinger/hostpinger.go @@ -18,6 +18,7 @@ import ( "context" "time" + "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/mem" "yunion.io/x/jsonutils" @@ -110,6 +111,7 @@ func (p *SHostPingTask) payload() api.SHostPingInput { p.lastStatAt = now data = storageman.GatherHostStorageStats(p.masterHostStorages) data.WithData = true + data.QgaRunningGuestIds = guestman.GetGuestManager().GetQgaRunningGuests() info, err := mem.VirtualMemory() if err != nil { return data @@ -118,7 +120,13 @@ func (p *SHostPingTask) payload() api.SHostPingInput { memFree := int(info.Available / 1024 / 1024) memUsed := memTotal - memFree data.MemoryUsedMb = memUsed - data.QgaRunningGuestIds = guestman.GetGuestManager().GetQgaRunningGuests() + cpuUsage, err := cpu.Percent(time.Second, false) + if err != nil { + return data + } + if len(cpuUsage) > 0 { + data.CpuUsagePercent = cpuUsage[0] + } return data }