Skip to content

Commit

Permalink
feat(region,host): hostpinger add cpu usage percent report
Browse files Browse the repository at this point in the history
  • Loading branch information
wanyaoqi committed Jan 21, 2025
1 parent 3e96ee5 commit e781b76
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/apis/compute/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
1 change: 1 addition & 0 deletions pkg/compute/models/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion pkg/hostman/hostinfo/hostpinger/hostpinger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"time"

"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/mem"

"yunion.io/x/jsonutils"
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down

0 comments on commit e781b76

Please sign in to comment.