一个告警的算法。我自己也晕。
public void doJob(MonitorAlarmThreadLock monitorAlarm) {
String status = ConfigUtil.getReadonlyValue(PREWARNING_STATUS);
if ("off".equals(status)) {
return;
}
// 超过告警阀值的信息
Set<PrewarningInfo> prewarningMap = new HashSet<PrewarningInfo>();
// 根据appname取到所有需要告警的信息
List<PrewarningInfo> prewarningInfos = this
.getPrewarningInfo(monitorAlarm.getClientAppName());
try {
for (PrewarningInfo prewarningInfo : prewarningInfos) {
prewarningInfo = prewarningInteface
.initPrewarningInfo(prewarningInfo);// 初始化预警参数
// 取出预警规则
List<PrewarningRule> prewarningRuleList = prewarningInfo
.getPrewarningRuleList();
if (prewarningRuleList.size() != 0) {
boolean hasPrewarning = false;
for (PrewarningRule prewarningRule : prewarningRuleList) {
Date pStartTime = prewarningRule.getpStartTime();
if (pStartTime == null) {
// 取时间最早的一条初始化开始时间
com.jd.mc.model.LogModel lm = new com.jd.mc.model.LogModel();
lm.setAppName(monitorAlarm.getClientAppName());
lm.setServiceName(prewarningInfo.getService_name());
lm.setMethodName(prewarningInfo.getMethod_name());
lm.setTpValue(prewarningRule.getTpvalue());
com.jd.mc.model.LogModel logModel = logModelDao
.getFirstLogModel(lm);
if (logModel != null) {
prewarningRule.setpStartTime(logModel
.getStartTime());
// 结束时间为开始时间+间隔时间
Calendar cal = Calendar.getInstance();
cal.setTime(logModel.getStartTime());
cal.add(Calendar.MINUTE, prewarningRule
.getIntervalmins());
prewarningRule.setpEndTime(cal.getTime());
prewarningDao
.updatePrewarningRule(prewarningRule);
}
}
// 取到需要告警的方法列表
List<com.jd.mc.model.LogModel> logModelExtrList = prewarningInteface
.getListLogModelExtr(prewarningInfo,
prewarningRule);
// 判断是否超过告警阀值
List<com.jd.mc.model.LogModel> logModelExtrListP = prewarningInteface
.prewarningJudge(logModelExtrList,
prewarningRule);
if (logModelExtrListP.size() >= prewarningRule
.getFrequency()
&& prewarningRule.getIsperwarning() != 1) {
hasPrewarning = true;
prewarningRule.setPerwarning(true);
prewarningRule
.setLogModelExtrList(logModelExtrListP);
}
}
if (hasPrewarning) {
prewarningMap.add(prewarningInfo);
}
for (PrewarningRule prewarningRule : prewarningRuleList) {
// 取最晚的开始时间跟规则结束时间比,如果大于结束时间则更新规则的pStartTime,pEndTime
com.jd.mc.model.LogModel lm = new com.jd.mc.model.LogModel();
lm.setAppName(monitorAlarm.getClientAppName());
lm.setServiceName(prewarningInfo.getService_name());
lm.setMethodName(prewarningInfo.getMethod_name());
lm.setTpValue(prewarningRule.getTpvalue());
com.jd.mc.model.LogModel lastLogModel = logModelDao
.getLastLogModel(lm);
if (lastLogModel!=null&&lastLogModel.getStartTime().getTime() > prewarningRule
.getpEndTime().getTime()) {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(prewarningRule.getpStartTime());
cal1.add(Calendar.MINUTE, prewarningRule
.getIntervalmins());
Calendar cal2 = Calendar.getInstance();
cal2.setTime(prewarningRule.getpEndTime());
cal2.add(Calendar.MINUTE, prewarningRule
.getIntervalmins());
prewarningRule.setpStartTime(cal1.getTime());
prewarningRule.setpEndTime(cal2.getTime());
prewarningRule.setIsperwarning(0);
prewarningDao.updatePrewarningRule(prewarningRule);
}
}
}
}
} catch (Exception e) {
logger.error(this.getClass().getName() + " error", e);
}
try{
prewarningInteface.sendPerwarningMessage(prewarningMap,
monitorAlarm.getBussGrp());// 发送预警消息
} catch (Exception e) {
logger.error(this.getClass().getName() + " error", e);
}
}