一个告警的算法

 

一个告警的算法。我自己也晕。

 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);
  }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值