feat(region,host): hostpinger add cpu usage percent report (#22006)

This commit is contained in:
wanyaoqi
2025-01-25 15:55:32 +08:00
committed by GitHub
parent ee67be37ba
commit d78801de92
3 changed files with 12 additions and 2 deletions
+2 -1
View File
@@ -531,7 +531,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"`
+1
View File
@@ -4718,6 +4718,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 {
@@ -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
}