fix: 资源采集cpuLoadUsage除数为0导致NaN

This commit is contained in:
TsMask
2024-08-05 10:17:20 +08:00
parent 3dafcbb699
commit 989470f305
4 changed files with 41 additions and 61 deletions

View File

@@ -2,28 +2,19 @@ package model
// MonitorBase 监控_基本信息 monitor_base // MonitorBase 监控_基本信息 monitor_base
type MonitorBase struct { type MonitorBase struct {
// id ID int64 `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
ID int64 `json:"id" gorm:"primaryKey"` CreateTime int64 `json:"createTime" gorm:"create_time"` // 创建时间
// 创建时间 CPU float64 `json:"cpu" gorm:"cpu"` // cpu使用率
CreateTime int64 `json:"createTime"` LoadUsage float64 `json:"loadUsage" gorm:"load_usage"` // cpu平均使用率
// cpu使用 CPULoad1 float64 `json:"cpuLoad1" gorm:"cpu_load1"` // cpu使用1分钟
CPU float64 `json:"cpu"` CPULoad5 float64 `json:"cpuLoad5" gorm:"cpu_load5"` // cpu使用5分钟
// cpu平均使用率 CPULoad15 float64 `json:"cpuLoad15" gorm:"cpu_load15"` // cpu使用15分钟
LoadUsage float64 `json:"loadUsage"` Memory float64 `json:"memory" gorm:"memory"` // 内存使用率
// cpu使用1分钟 NeType string `json:"neType" gorm:"ne_type"` // 网元类型
CPULoad1 float64 `json:"cpuLoad1"` NeID string `json:"neId" gorm:"ne_id"` // 网元ID
// cpu使用5分钟
CPULoad5 float64 `json:"cpuLoad5"`
// cpu使用15分钟
CPULoad15 float64 `json:"cpuLoad15"`
// 内存使用率
Memory float64 `json:"memory"`
// 网元ID
NeType string `json:"neType"`
// 网元类型
NeID string `json:"neId"`
} }
func (MonitorBase) TableName() string { // TableName 表名称
func (*MonitorBase) TableName() string {
return "monitor_base" return "monitor_base"
} }

View File

@@ -2,26 +2,18 @@ package model
// MonitorIO 监控_磁盘IO monitor_io // MonitorIO 监控_磁盘IO monitor_io
type MonitorIO struct { type MonitorIO struct {
// id ID int64 `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
ID int64 `json:"id" gorm:"primaryKey"` CreateTime int64 `json:"createTime" gorm:"create_time"` // 创建时间
// 创建时间 Name string `json:"name" gorm:"name"` // 磁盘名
CreateTime int64 `json:"createTime"` Read int64 `json:"read" gorm:"read"` // 读取K
// 磁盘名 Write int64 `json:"write" gorm:"write"` // 写入K
Name string `json:"name"` Count int64 `json:"count" gorm:"count"` // 读写次数
// 读取K Time int64 `json:"time" gorm:"time"` // 读写延迟
Read int64 `json:"read"` NeType string `json:"neType" gorm:"ne_type"` // 网元类型
// 写入K NeID string `json:"neId" gorm:"ne_id"` // 网元ID
Write int64 `json:"write"`
// 次数
Count int64 `json:"count"`
// 耗时
Time int64 `json:"time"`
// 网元ID
NeType string `json:"neType"`
// 网元类型
NeID string `json:"neId"`
} }
func (MonitorIO) TableName() string { // TableName 表名称
func (*MonitorIO) TableName() string {
return "monitor_io" return "monitor_io"
} }

View File

@@ -2,22 +2,16 @@ package model
// MonitorNetwork 监控_网络IO monitor_network // MonitorNetwork 监控_网络IO monitor_network
type MonitorNetwork struct { type MonitorNetwork struct {
// id ID int64 `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
ID int64 `json:"id" gorm:"primaryKey"` CreateTime int64 `json:"createTime" gorm:"create_time"` // 创建时间
// 创建时间 Name string `json:"name" gorm:"name"` // 网卡名
CreateTime int64 `json:"createTime"` Up float64 `json:"up" gorm:"up"` // 上行
// 网卡名 Down float64 `json:"down" gorm:"down"` // 下行
Name string `json:"name"` NeType string `json:"neType" gorm:"ne_type"` // 网元类型
// 上行 NeID string `json:"neId" gorm:"ne_id"` // 网元ID
Up float64 `json:"up"`
// 下行
Down float64 `json:"down"`
// 网元ID 本机#号
NeType string `json:"neType"`
// 网元类型 本机#号
NeID string `json:"neId"`
} }
func (MonitorNetwork) TableName() string { // TableName 表名称
func (*MonitorNetwork) TableName() string {
return "monitor_network" return "monitor_network"
} }

View File

@@ -41,17 +41,20 @@ func (s *MonitorImpl) RunMonitor() {
itemBase.CreateTime = time.Now().UnixMilli() itemBase.CreateTime = time.Now().UnixMilli()
itemBase.NeType = "#" itemBase.NeType = "#"
itemBase.NeID = "#" itemBase.NeID = "#"
loadInfo, _ := load.Avg()
itemBase.CPULoad1 = loadInfo.Load1
itemBase.CPULoad5 = loadInfo.Load5
itemBase.CPULoad15 = loadInfo.Load15
totalPercent, _ := cpu.Percent(3*time.Second, false) totalPercent, _ := cpu.Percent(3*time.Second, false)
if len(totalPercent) == 1 { if len(totalPercent) == 1 {
itemBase.CPU = totalPercent[0] itemBase.CPU = totalPercent[0]
} }
cpuCount, _ := cpu.Counts(false) cpuCount, _ := cpu.Counts(false)
cpuAvg := (float64(cpuCount*2) * 0.75) * 100
loadInfo, _ := load.Avg() itemBase.LoadUsage = 0
itemBase.CPULoad1 = loadInfo.Load1 if cpuAvg > 0 {
itemBase.CPULoad5 = loadInfo.Load5 itemBase.LoadUsage = loadInfo.Load1 / cpuAvg
itemBase.CPULoad15 = loadInfo.Load15 }
itemBase.LoadUsage = loadInfo.Load1 / (float64(cpuCount*2) * 0.75) * 100
memoryInfo, _ := mem.VirtualMemory() memoryInfo, _ := mem.VirtualMemory()
itemBase.Memory = memoryInfo.UsedPercent itemBase.Memory = memoryInfo.UsedPercent