This commit is contained in:
2023-11-01 18:44:41 +08:00
4 changed files with 24 additions and 14 deletions

View File

@@ -25,8 +25,8 @@ type MonitorController struct {
// GET /load // GET /load
func (s *MonitorController) Load(c *gin.Context) { func (s *MonitorController) Load(c *gin.Context) {
var querys struct { var querys struct {
// 数据类型all/cpu/memory/io/network // 数据类型all/load/cpu/memory/io/network
Type string `form:"type" binding:"required,oneof=all cpu memory io network"` Type string `form:"type" binding:"required,oneof=all load cpu memory io network"`
// 开始时间 // 开始时间
StartTime int64 `form:"startTime" binding:"required"` StartTime int64 `form:"startTime" binding:"required"`
// 结束时间 // 结束时间

View File

@@ -12,9 +12,9 @@ type MonitorNetwork struct {
Up float64 `json:"up"` Up float64 `json:"up"`
// 下行 // 下行
Down float64 `json:"down"` Down float64 `json:"down"`
// 网元ID // 网元ID 本机#号
NeType string `json:"neType"` NeType string `json:"neType"`
// 网元类型 // 网元类型 本机#号
NeID string `json:"neId"` NeID string `json:"neId"`
} }

View File

@@ -89,6 +89,8 @@ func (r *MonitorImpl) SelectMonitorNetwork(query map[string]any) []model.Monitor
dbConn := r.db() dbConn := r.db()
if query["name"] != "" { if query["name"] != "" {
dbConn = dbConn.Where("name = ?", query["name"]) dbConn = dbConn.Where("name = ?", query["name"])
} else {
dbConn = dbConn.Where("name = ?", "all")
} }
if query["neType"] != "" && query["neId"] != "" { if query["neType"] != "" && query["neId"] != "" {
dbConn = dbConn.Where("ne_type = ? and ne_id = ?", query["neType"], query["neId"]) dbConn = dbConn.Where("ne_type = ? and ne_id = ?", query["neType"], query["neId"])

View File

@@ -37,23 +37,26 @@ type MonitorImpl struct {
// RunMonitor 执行资源监控 // RunMonitor 执行资源监控
func (s *MonitorImpl) RunMonitor() { func (s *MonitorImpl) RunMonitor() {
var itemModel model.MonitorBase var itemBase model.MonitorBase
itemModel.CreateTime = time.Now().UnixMilli() itemBase.CreateTime = time.Now().UnixMilli()
itemBase.NeType = "#"
itemBase.NeID = "#"
totalPercent, _ := cpu.Percent(3*time.Second, false) totalPercent, _ := cpu.Percent(3*time.Second, false)
if len(totalPercent) == 1 { if len(totalPercent) == 1 {
itemModel.CPU = totalPercent[0] itemBase.CPU = totalPercent[0]
} }
cpuCount, _ := cpu.Counts(false) cpuCount, _ := cpu.Counts(false)
loadInfo, _ := load.Avg() loadInfo, _ := load.Avg()
itemModel.CPULoad1 = loadInfo.Load1 itemBase.CPULoad1 = loadInfo.Load1
itemModel.CPULoad5 = loadInfo.Load5 itemBase.CPULoad5 = loadInfo.Load5
itemModel.CPULoad15 = loadInfo.Load15 itemBase.CPULoad15 = loadInfo.Load15
itemModel.LoadUsage = loadInfo.Load1 / (float64(cpuCount*2) * 0.75) * 100 itemBase.LoadUsage = loadInfo.Load1 / (float64(cpuCount*2) * 0.75) * 100
memoryInfo, _ := mem.VirtualMemory() memoryInfo, _ := mem.VirtualMemory()
itemModel.Memory = memoryInfo.UsedPercent itemBase.Memory = memoryInfo.UsedPercent
if err := s.monitorRepository.CreateMonitorBase(itemModel); err != nil {
if err := s.monitorRepository.CreateMonitorBase(itemBase); err != nil {
logger.Errorf("CreateMonitorBase err: %v", err) logger.Errorf("CreateMonitorBase err: %v", err)
} }
@@ -128,7 +131,10 @@ func (s *MonitorImpl) saveIODataToDB(ctx context.Context, interval float64) {
if io2.Name == io1.Name { if io2.Name == io1.Name {
var itemIO model.MonitorIO var itemIO model.MonitorIO
itemIO.CreateTime = timeMilli itemIO.CreateTime = timeMilli
itemIO.NeType = "#"
itemIO.NeID = "#"
itemIO.Name = io1.Name itemIO.Name = io1.Name
if io2.ReadBytes != 0 && io1.ReadBytes != 0 && io2.ReadBytes > io1.ReadBytes { if io2.ReadBytes != 0 && io1.ReadBytes != 0 && io2.ReadBytes > io1.ReadBytes {
itemIO.Read = int64(float64(io2.ReadBytes-io1.ReadBytes) / interval / 60) itemIO.Read = int64(float64(io2.ReadBytes-io1.ReadBytes) / interval / 60)
} }
@@ -189,6 +195,8 @@ func (s *MonitorImpl) saveNetDataToDB(ctx context.Context, interval float64) {
if net2.Name == net1.Name { if net2.Name == net1.Name {
var itemNet model.MonitorNetwork var itemNet model.MonitorNetwork
itemNet.CreateTime = timeMilli itemNet.CreateTime = timeMilli
itemNet.NeType = "#"
itemNet.NeID = "#"
itemNet.Name = net1.Name itemNet.Name = net1.Name
if net2.BytesSent != 0 && net1.BytesSent != 0 && net2.BytesSent > net1.BytesSent { if net2.BytesSent != 0 && net1.BytesSent != 0 && net2.BytesSent > net1.BytesSent {
@@ -225,7 +233,7 @@ func (s *MonitorImpl) SelectMonitorInfo(query map[string]any) map[string]any {
backDatas := map[string]any{} backDatas := map[string]any{}
// 基本信息 // 基本信息
if infoType == "all" || infoType == "cpu" || infoType == "memory" { if infoType == "all" || infoType == "load" || infoType == "cpu" || infoType == "memory" {
rows := s.monitorRepository.SelectMonitorBase(map[string]any{ rows := s.monitorRepository.SelectMonitorBase(map[string]any{
"startTime": startTimeMilli, "startTime": startTimeMilli,
"endTime": endTimeMilli, "endTime": endTimeMilli,