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

View File

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

View File

@@ -89,6 +89,8 @@ func (r *MonitorImpl) SelectMonitorNetwork(query map[string]any) []model.Monitor
dbConn := r.db()
if query["name"] != "" {
dbConn = dbConn.Where("name = ?", query["name"])
} else {
dbConn = dbConn.Where("name = ?", "all")
}
if 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 执行资源监控
func (s *MonitorImpl) RunMonitor() {
var itemModel model.MonitorBase
itemModel.CreateTime = time.Now().UnixMilli()
var itemBase model.MonitorBase
itemBase.CreateTime = time.Now().UnixMilli()
itemBase.NeType = "#"
itemBase.NeID = "#"
totalPercent, _ := cpu.Percent(3*time.Second, false)
if len(totalPercent) == 1 {
itemModel.CPU = totalPercent[0]
itemBase.CPU = totalPercent[0]
}
cpuCount, _ := cpu.Counts(false)
loadInfo, _ := load.Avg()
itemModel.CPULoad1 = loadInfo.Load1
itemModel.CPULoad5 = loadInfo.Load5
itemModel.CPULoad15 = loadInfo.Load15
itemModel.LoadUsage = loadInfo.Load1 / (float64(cpuCount*2) * 0.75) * 100
itemBase.CPULoad1 = loadInfo.Load1
itemBase.CPULoad5 = loadInfo.Load5
itemBase.CPULoad15 = loadInfo.Load15
itemBase.LoadUsage = loadInfo.Load1 / (float64(cpuCount*2) * 0.75) * 100
memoryInfo, _ := mem.VirtualMemory()
itemModel.Memory = memoryInfo.UsedPercent
if err := s.monitorRepository.CreateMonitorBase(itemModel); err != nil {
itemBase.Memory = memoryInfo.UsedPercent
if err := s.monitorRepository.CreateMonitorBase(itemBase); err != nil {
logger.Errorf("CreateMonitorBase err: %v", err)
}
@@ -128,7 +131,10 @@ func (s *MonitorImpl) saveIODataToDB(ctx context.Context, interval float64) {
if io2.Name == io1.Name {
var itemIO model.MonitorIO
itemIO.CreateTime = timeMilli
itemIO.NeType = "#"
itemIO.NeID = "#"
itemIO.Name = io1.Name
if io2.ReadBytes != 0 && io1.ReadBytes != 0 && io2.ReadBytes > io1.ReadBytes {
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 {
var itemNet model.MonitorNetwork
itemNet.CreateTime = timeMilli
itemNet.NeType = "#"
itemNet.NeID = "#"
itemNet.Name = net1.Name
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{}
// 基本信息
if infoType == "all" || infoType == "cpu" || infoType == "memory" {
if infoType == "all" || infoType == "load" || infoType == "cpu" || infoType == "memory" {
rows := s.monitorRepository.SelectMonitorBase(map[string]any{
"startTime": startTimeMilli,
"endTime": endTimeMilli,