fix: 资源采集cpuLoadUsage除数为0导致NaN
This commit is contained in:
@@ -2,28 +2,19 @@ package model
|
||||
|
||||
// MonitorBase 监控_基本信息 monitor_base
|
||||
type MonitorBase struct {
|
||||
// id
|
||||
ID int64 `json:"id" gorm:"primaryKey"`
|
||||
// 创建时间
|
||||
CreateTime int64 `json:"createTime"`
|
||||
// cpu使用率
|
||||
CPU float64 `json:"cpu"`
|
||||
// cpu平均使用率
|
||||
LoadUsage float64 `json:"loadUsage"`
|
||||
// cpu使用1分钟
|
||||
CPULoad1 float64 `json:"cpuLoad1"`
|
||||
// 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"`
|
||||
ID int64 `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
|
||||
CreateTime int64 `json:"createTime" gorm:"create_time"` // 创建时间
|
||||
CPU float64 `json:"cpu" gorm:"cpu"` // cpu使用率
|
||||
LoadUsage float64 `json:"loadUsage" gorm:"load_usage"` // cpu平均使用率
|
||||
CPULoad1 float64 `json:"cpuLoad1" gorm:"cpu_load1"` // cpu使用1分钟
|
||||
CPULoad5 float64 `json:"cpuLoad5" gorm:"cpu_load5"` // cpu使用5分钟
|
||||
CPULoad15 float64 `json:"cpuLoad15" gorm:"cpu_load15"` // cpu使用15分钟
|
||||
Memory float64 `json:"memory" gorm:"memory"` // 内存使用率
|
||||
NeType string `json:"neType" gorm:"ne_type"` // 网元类型
|
||||
NeID string `json:"neId" gorm:"ne_id"` // 网元ID
|
||||
}
|
||||
|
||||
func (MonitorBase) TableName() string {
|
||||
// TableName 表名称
|
||||
func (*MonitorBase) TableName() string {
|
||||
return "monitor_base"
|
||||
}
|
||||
|
||||
@@ -2,26 +2,18 @@ package model
|
||||
|
||||
// MonitorIO 监控_磁盘IO monitor_io
|
||||
type MonitorIO struct {
|
||||
// id
|
||||
ID int64 `json:"id" gorm:"primaryKey"`
|
||||
// 创建时间
|
||||
CreateTime int64 `json:"createTime"`
|
||||
// 磁盘名
|
||||
Name string `json:"name"`
|
||||
// 读取K
|
||||
Read int64 `json:"read"`
|
||||
// 写入K
|
||||
Write int64 `json:"write"`
|
||||
// 次数
|
||||
Count int64 `json:"count"`
|
||||
// 耗时
|
||||
Time int64 `json:"time"`
|
||||
// 网元ID
|
||||
NeType string `json:"neType"`
|
||||
// 网元类型
|
||||
NeID string `json:"neId"`
|
||||
ID int64 `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
|
||||
CreateTime int64 `json:"createTime" gorm:"create_time"` // 创建时间
|
||||
Name string `json:"name" gorm:"name"` // 磁盘名
|
||||
Read int64 `json:"read" gorm:"read"` // 读取K
|
||||
Write int64 `json:"write" gorm:"write"` // 写入K
|
||||
Count int64 `json:"count" gorm:"count"` // 读写次数
|
||||
Time int64 `json:"time" gorm:"time"` // 读写延迟
|
||||
NeType string `json:"neType" gorm:"ne_type"` // 网元类型
|
||||
NeID string `json:"neId" gorm:"ne_id"` // 网元ID
|
||||
}
|
||||
|
||||
func (MonitorIO) TableName() string {
|
||||
// TableName 表名称
|
||||
func (*MonitorIO) TableName() string {
|
||||
return "monitor_io"
|
||||
}
|
||||
|
||||
@@ -2,22 +2,16 @@ package model
|
||||
|
||||
// MonitorNetwork 监控_网络IO monitor_network
|
||||
type MonitorNetwork struct {
|
||||
// id
|
||||
ID int64 `json:"id" gorm:"primaryKey"`
|
||||
// 创建时间
|
||||
CreateTime int64 `json:"createTime"`
|
||||
// 网卡名
|
||||
Name string `json:"name"`
|
||||
// 上行
|
||||
Up float64 `json:"up"`
|
||||
// 下行
|
||||
Down float64 `json:"down"`
|
||||
// 网元ID 本机#号
|
||||
NeType string `json:"neType"`
|
||||
// 网元类型 本机#号
|
||||
NeID string `json:"neId"`
|
||||
ID int64 `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
|
||||
CreateTime int64 `json:"createTime" gorm:"create_time"` // 创建时间
|
||||
Name string `json:"name" gorm:"name"` // 网卡名
|
||||
Up float64 `json:"up" gorm:"up"` // 上行
|
||||
Down float64 `json:"down" gorm:"down"` // 下行
|
||||
NeType string `json:"neType" gorm:"ne_type"` // 网元类型
|
||||
NeID string `json:"neId" gorm:"ne_id"` // 网元ID
|
||||
}
|
||||
|
||||
func (MonitorNetwork) TableName() string {
|
||||
// TableName 表名称
|
||||
func (*MonitorNetwork) TableName() string {
|
||||
return "monitor_network"
|
||||
}
|
||||
|
||||
@@ -41,17 +41,20 @@ func (s *MonitorImpl) RunMonitor() {
|
||||
itemBase.CreateTime = time.Now().UnixMilli()
|
||||
itemBase.NeType = "#"
|
||||
itemBase.NeID = "#"
|
||||
loadInfo, _ := load.Avg()
|
||||
itemBase.CPULoad1 = loadInfo.Load1
|
||||
itemBase.CPULoad5 = loadInfo.Load5
|
||||
itemBase.CPULoad15 = loadInfo.Load15
|
||||
totalPercent, _ := cpu.Percent(3*time.Second, false)
|
||||
if len(totalPercent) == 1 {
|
||||
itemBase.CPU = totalPercent[0]
|
||||
}
|
||||
cpuCount, _ := cpu.Counts(false)
|
||||
|
||||
loadInfo, _ := load.Avg()
|
||||
itemBase.CPULoad1 = loadInfo.Load1
|
||||
itemBase.CPULoad5 = loadInfo.Load5
|
||||
itemBase.CPULoad15 = loadInfo.Load15
|
||||
itemBase.LoadUsage = loadInfo.Load1 / (float64(cpuCount*2) * 0.75) * 100
|
||||
cpuAvg := (float64(cpuCount*2) * 0.75) * 100
|
||||
itemBase.LoadUsage = 0
|
||||
if cpuAvg > 0 {
|
||||
itemBase.LoadUsage = loadInfo.Load1 / cpuAvg
|
||||
}
|
||||
|
||||
memoryInfo, _ := mem.VirtualMemory()
|
||||
itemBase.Memory = memoryInfo.UsedPercent
|
||||
|
||||
Reference in New Issue
Block a user