From 989470f3053c07f91b1e87a6a88ffe3b8ea9621b Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Mon, 5 Aug 2024 10:17:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B5=84=E6=BA=90=E9=87=87=E9=9B=86cpuL?= =?UTF-8?q?oadUsage=E9=99=A4=E6=95=B0=E4=B8=BA0=E5=AF=BC=E8=87=B4NaN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/monitor/model/monitor_base.go | 33 +++++++------------- src/modules/monitor/model/monitor_io.go | 30 +++++++----------- src/modules/monitor/model/monitor_network.go | 24 ++++++-------- src/modules/monitor/service/monitor.impl.go | 15 +++++---- 4 files changed, 41 insertions(+), 61 deletions(-) diff --git a/src/modules/monitor/model/monitor_base.go b/src/modules/monitor/model/monitor_base.go index 7669afa2..34e056fa 100644 --- a/src/modules/monitor/model/monitor_base.go +++ b/src/modules/monitor/model/monitor_base.go @@ -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" } diff --git a/src/modules/monitor/model/monitor_io.go b/src/modules/monitor/model/monitor_io.go index cc52b728..962bfc86 100644 --- a/src/modules/monitor/model/monitor_io.go +++ b/src/modules/monitor/model/monitor_io.go @@ -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" } diff --git a/src/modules/monitor/model/monitor_network.go b/src/modules/monitor/model/monitor_network.go index a2063f18..feed747e 100644 --- a/src/modules/monitor/model/monitor_network.go +++ b/src/modules/monitor/model/monitor_network.go @@ -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" } diff --git a/src/modules/monitor/service/monitor.impl.go b/src/modules/monitor/service/monitor.impl.go index e9de38cb..2a4d4d1b 100644 --- a/src/modules/monitor/service/monitor.impl.go +++ b/src/modules/monitor/service/monitor.impl.go @@ -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