|
|
|
|
@@ -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,
|
|
|
|
|
|