using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using ProtoCSStruct; namespace World { public static class AlertSvc { static long lastAlertTimeSec = 0; public static void OnTick(long nowSecond) { //停服,不检查 if (WorldServerUtils.GetWorldServerData().m_serverStopFlag == 1) { return; } int nowOnline = 0; var gameSvrList = WorldServerUtils.GetWorldServerData().m_gameSvrInfo; foreach (var gameSvr in gameSvrList) { // 上报数据包含gatesvr, 这里只统计gamesvr if (ServerIDUtils.GetServerType(gameSvr.serverId) == (int) ServerType.Game) { nowOnline += gameSvr.onlinePlayer; } } Check5Min(nowSecond, nowOnline); CheckLastDay(nowSecond, nowOnline); } private static void Check5Min(long nowSecond, int nowOnline) { AlertData data = WorldServerUtils.GetWorldServerData().m_alertData; int oldOnline = data.OnlinePlayer; //5分钟一个区间 if (nowSecond - data.LastCheckTimeSecond >= 300) { data.LastCheckTimeSecond = nowSecond; data.OnlinePlayer = nowOnline; } if (nowOnline >= oldOnline) { return; } //人太少没意义 if (oldOnline <= 500) { return; } if ((oldOnline - nowOnline) * 100 / oldOnline > 30) { string message = string.Format("AlertSvc.Check5Min online fall, old {0} now {1}", oldOnline, nowOnline); TraceLog.Error(message); WorldServerUtils.GetApp().Alerter.AlertMsg(message, 3600); } } private static void CheckLastDay(long nowSecond, int nowOnline) { AlertData data = WorldServerUtils.GetWorldServerData().m_alertData; //跨天 if(AppTime.IsSameDay(nowSecond, data.LastDayTimeSecond, 0) == false) { data.LastDayOnline.Clear(); data.LastDayOnline = data.TodayOnline; data.TodayOnline = new Dictionary(); data.LastDayTimeSecond = nowSecond; data.LastAlertIndex = -1; return; } DateTime time = AppTime.ConvertUnixTimeToDateTime(nowSecond*1000); var span = time.TimeOfDay; //5分钟一个区间 int total5MinIndex = (int)span.TotalMinutes/5; if(data.TodayOnline.ContainsKey(total5MinIndex)) { data.TodayOnline[total5MinIndex] = nowOnline; } else { data.TodayOnline.Add(total5MinIndex, nowOnline); } //比较上个5分钟 int checkKey = total5MinIndex - 1; if(checkKey < 0) { return; } //告警过了 if(data.LastAlertIndex == checkKey) { return; } data.LastAlertIndex = checkKey; //昨天的数据 int yesTodayValue = 0; if(data.LastDayOnline.ContainsKey(checkKey)) { yesTodayValue = data.LastDayOnline[checkKey]; } else { return; } //今天的数据 int todayValue = 0; if (data.TodayOnline.ContainsKey(checkKey)) { todayValue = data.TodayOnline[checkKey]; } else { return; } if(todayValue >= yesTodayValue) { return; } //人太少没意义 if(yesTodayValue <= 300) { return; } int alertPer = 35; //礼拜一 if(time.DayOfWeek == DayOfWeek.Monday) { alertPer = 50; } //凌晨 if(time.Hour >= 1 && time.Hour <= 5) { alertPer = 50; } if ((yesTodayValue - todayValue) * 100 / yesTodayValue > alertPer) { string message = string.Format("AlertSvc.CheckLastDay online fall, old {0} now {1}", yesTodayValue, todayValue); TraceLog.Error(message); if (nowSecond > lastAlertTimeSec + 3600 * 6) { lastAlertTimeSec = nowSecond; WorldServerUtils.GetApp().Alerter.AlertMsg(message, 3600); } } } } }