⚡️ 优化历史服务监控数据写入
This commit is contained in:
		
							parent
							
								
									b3203f81d0
								
							
						
					
					
						commit
						19329d0c8f
					
				@ -12,7 +12,10 @@ import (
 | 
				
			|||||||
	pb "github.com/naiba/nezha/proto"
 | 
						pb "github.com/naiba/nezha/proto"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const _CurrentStatusSize = 30 // 统计 15 分钟内的数据为当前状态
 | 
					const (
 | 
				
			||||||
 | 
						_CurrentStatusSize = 30 // 统计 15 分钟内的数据为当前状态
 | 
				
			||||||
 | 
						_StatusOk          = "良好"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var ServiceSentinelShared *ServiceSentinel
 | 
					var ServiceSentinelShared *ServiceSentinel
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -268,7 +271,7 @@ func getStateStr(percent uint64) string {
 | 
				
			|||||||
		return "无数据"
 | 
							return "无数据"
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if percent > 95 {
 | 
						if percent > 95 {
 | 
				
			||||||
		return "良好"
 | 
							return _StatusOk
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if percent > 80 {
 | 
						if percent > 80 {
 | 
				
			||||||
		return "低可用"
 | 
							return "低可用"
 | 
				
			||||||
@ -303,18 +306,13 @@ func (ss *ServiceSentinel) worker() {
 | 
				
			|||||||
			ss.serviceStatusToday[mh.MonitorID].Up++
 | 
								ss.serviceStatusToday[mh.MonitorID].Up++
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			ss.serviceStatusToday[mh.MonitorID].Down++
 | 
								ss.serviceStatusToday[mh.MonitorID].Down++
 | 
				
			||||||
 | 
								ServerLock.RLock()
 | 
				
			||||||
 | 
								log.Println("NEZHA>> 服务故障上报:", ss.monitors[mh.MonitorID].Target, "上报者:", ServerList[r.Reporter].Name, "错误信息:", mh.Data)
 | 
				
			||||||
 | 
								ServerLock.RUnlock()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		// 写入当前数据
 | 
							// 写入当前数据
 | 
				
			||||||
		ss.serviceCurrentStatusData[mh.MonitorID][ss.serviceCurrentStatusIndex[mh.MonitorID]] = mh
 | 
							ss.serviceCurrentStatusData[mh.MonitorID][ss.serviceCurrentStatusIndex[mh.MonitorID]] = mh
 | 
				
			||||||
		ss.serviceCurrentStatusIndex[mh.MonitorID]++
 | 
							ss.serviceCurrentStatusIndex[mh.MonitorID]++
 | 
				
			||||||
		// 数据持久化
 | 
					 | 
				
			||||||
		if ss.serviceCurrentStatusIndex[mh.MonitorID] == _CurrentStatusSize {
 | 
					 | 
				
			||||||
			ss.serviceCurrentStatusIndex[mh.MonitorID] = 0
 | 
					 | 
				
			||||||
			dataToSave := ss.serviceCurrentStatusData[mh.MonitorID]
 | 
					 | 
				
			||||||
			if err := DB.Create(&dataToSave).Error; err != nil {
 | 
					 | 
				
			||||||
				log.Println("NEZHA>> 服务监控数据持久化失败:", err)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		// 更新当前状态
 | 
							// 更新当前状态
 | 
				
			||||||
		ss.serviceResponseDataStoreCurrentUp[mh.MonitorID] = 0
 | 
							ss.serviceResponseDataStoreCurrentUp[mh.MonitorID] = 0
 | 
				
			||||||
		ss.serviceResponseDataStoreCurrentDown[mh.MonitorID] = 0
 | 
							ss.serviceResponseDataStoreCurrentDown[mh.MonitorID] = 0
 | 
				
			||||||
@ -332,10 +330,17 @@ func (ss *ServiceSentinel) worker() {
 | 
				
			|||||||
			upPercent = ss.serviceResponseDataStoreCurrentUp[mh.MonitorID] * 100 / (ss.serviceResponseDataStoreCurrentDown[mh.MonitorID] + ss.serviceResponseDataStoreCurrentUp[mh.MonitorID])
 | 
								upPercent = ss.serviceResponseDataStoreCurrentUp[mh.MonitorID] * 100 / (ss.serviceResponseDataStoreCurrentDown[mh.MonitorID] + ss.serviceResponseDataStoreCurrentUp[mh.MonitorID])
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		stateStr := getStateStr(upPercent)
 | 
							stateStr := getStateStr(upPercent)
 | 
				
			||||||
		if !mh.Successful {
 | 
							// 数据持久化
 | 
				
			||||||
			ServerLock.RLock()
 | 
							if ss.serviceCurrentStatusIndex[mh.MonitorID] == _CurrentStatusSize {
 | 
				
			||||||
			log.Println("NEZHA>> 服务故障上报:", ss.monitors[mh.MonitorID].Target, stateStr, "上报者:", ServerList[r.Reporter].Name, "请求输出:", mh.Data)
 | 
								ss.serviceCurrentStatusIndex[mh.MonitorID] = 0
 | 
				
			||||||
			ServerLock.RUnlock()
 | 
								if err := DB.Create(&model.MonitorHistory{
 | 
				
			||||||
 | 
									MonitorID:  mh.MonitorID,
 | 
				
			||||||
 | 
									Delay:      ss.serviceStatusToday[mh.MonitorID].Delay,
 | 
				
			||||||
 | 
									Successful: stateStr == _StatusOk,
 | 
				
			||||||
 | 
									Data:       mh.Data,
 | 
				
			||||||
 | 
								}).Error; err != nil {
 | 
				
			||||||
 | 
									log.Println("NEZHA>> 服务监控数据持久化失败:", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if stateStr == "故障" || stateStr != ss.lastStatus[mh.MonitorID] {
 | 
							if stateStr == "故障" || stateStr != ss.lastStatus[mh.MonitorID] {
 | 
				
			||||||
			ss.monitorsLock.RLock()
 | 
								ss.monitorsLock.RLock()
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user