diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/modules/base/entity/postgre/AlarmLog.java b/jeecg-boot-base-core/src/main/java/org/jeecg/modules/base/entity/postgre/AlarmLog.java index d916b546..f375178a 100644 --- a/jeecg-boot-base-core/src/main/java/org/jeecg/modules/base/entity/postgre/AlarmLog.java +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/modules/base/entity/postgre/AlarmLog.java @@ -47,4 +47,7 @@ public class AlarmLog implements Serializable { @TableField(value = "alarm_value") private String alarmValue; + public AlarmLog() { + this.alarmStartDate = LocalDateTime.now(); + } } diff --git a/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/controller/SystemMonitorController.java b/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/controller/SystemMonitorController.java new file mode 100644 index 00000000..bd327b1a --- /dev/null +++ b/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/controller/SystemMonitorController.java @@ -0,0 +1,60 @@ +package org.jeecg.modules.controller; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.jeecg.common.api.vo.Result; +import org.jeecg.modules.feignclient.MonitorAlarm; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("systemMonitor") +@Api("系统监控信息查询") +public class SystemMonitorController { + + @Autowired + private MonitorAlarm monitorAlarm; + + @GetMapping("list") + @ApiOperation("") + Result list(@RequestParam String code, + @RequestParam String hostId, + @RequestParam Integer pageNo, + @RequestParam Integer pageSize, + @RequestParam String status, + @RequestParam String type){ + return monitorAlarm.list(code, hostId, pageNo, pageSize, status, type); + } + + @GetMapping("queryHostDetails") + @ApiOperation("") + Result detail(@RequestParam String hostId, + @RequestParam String pageName, + @RequestParam Integer pageNo, + @RequestParam Integer pageSize, + @RequestParam String start, + @RequestParam String end){ + return monitorAlarm.detail(hostId, pageName, pageNo, pageSize, start, end); + } + + @GetMapping("queryItemHistory") + @ApiOperation("") + Result item(@RequestParam String itemId, + @RequestParam Integer itemType, + @RequestParam String start, + @RequestParam String end){ + return monitorAlarm.item(itemId, itemType, start, end); + } + + @GetMapping("queryItemHistoryData") + @ApiOperation("") + Result itemData(@RequestParam String itemId, + @RequestParam Integer itemType, + @RequestParam String start, + @RequestParam String end){ + return monitorAlarm.itemData(itemId, itemType, start, end); + } +} diff --git a/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/feignclient/MonitorAlarm.java b/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/feignclient/MonitorAlarm.java index 40c5c5dc..61cc8ecd 100644 --- a/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/feignclient/MonitorAlarm.java +++ b/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/feignclient/MonitorAlarm.java @@ -23,7 +23,7 @@ public interface MonitorAlarm { @RequestParam String start, @RequestParam String end); - // --------------------返给前端------------------- + // --------------------前端专用------------------- @GetMapping("list") Result list(@RequestParam String code, @RequestParam String hostId, @@ -32,6 +32,14 @@ public interface MonitorAlarm { @RequestParam String status, @RequestParam String type); + @GetMapping("queryHostDetails") + Result detail(@RequestParam String hostId, + @RequestParam String pageName, + @RequestParam Integer pageNo, + @RequestParam Integer pageSize, + @RequestParam String start, + @RequestParam String end); + @GetMapping("queryItemHistory") Result item(@RequestParam String itemId, @RequestParam Integer itemType, @@ -44,14 +52,6 @@ public interface MonitorAlarm { @RequestParam String start, @RequestParam String end); - @GetMapping("queryHostDetails") - Result detail(@RequestParam String hostId, - @RequestParam String pageName, - @RequestParam Integer pageNo, - @RequestParam Integer pageSize, - @RequestParam String start, - @RequestParam String end); - @GetMapping("log") Result log(@RequestParam String code, @RequestParam String deviceType, diff --git a/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/service/impl/SpectrumAnalysisServiceImpl.java b/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/service/impl/SpectrumAnalysisServiceImpl.java index d8639059..650d87c3 100644 --- a/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/service/impl/SpectrumAnalysisServiceImpl.java +++ b/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/service/impl/SpectrumAnalysisServiceImpl.java @@ -1275,31 +1275,31 @@ public class SpectrumAnalysisServiceImpl implements ISpectrumAnalysisService { if (CollectionUtils.isNotEmpty(xeDataList)){ List xe131mDataList = xeDataList.stream().filter(item -> item.getNuclideName().equals(XeNuclideName.XE_131m.getType())).collect(Collectors.toList()); GardsXeResultsSpectrum xe131m = xe131mDataList.get(0); - rlrDataValues.setXe131m_conc(String.valueOf(xe131m.getConc())); - rlrDataValues.setXe131m_uncert_conc(String.valueOf(xe131m.getConcErr())); - rlrDataValues.setXe131m_MDC(String.valueOf(xe131m.getMdc())); - rlrDataValues.setXe131m_LC(String.valueOf(xe131m.getLc())); + rlrDataValues.setXe131m_conc(NumUtil.keepStr(xe131m.getConc(), 5)); + rlrDataValues.setXe131m_uncert_conc(NumUtil.keepStr(xe131m.getConcErr(), 5)); + rlrDataValues.setXe131m_MDC(NumUtil.keepStr(xe131m.getMdc(), 5)); + rlrDataValues.setXe131m_LC(NumUtil.keepStr(xe131m.getLc(), 5)); List xe133DataList = xeDataList.stream().filter(item -> item.getNuclideName().equals(XeNuclideName.XE_133.getType())).collect(Collectors.toList()); GardsXeResultsSpectrum xe133 = xe133DataList.get(0); - rlrDataValues.setXe133_conc(String.valueOf(xe133.getConc())); - rlrDataValues.setXe133_uncert_conc(String.valueOf(xe133.getConcErr())); - rlrDataValues.setXe133_MDC(String.valueOf(xe133.getMdc())); - rlrDataValues.setXe133_LC(String.valueOf(xe133.getLc())); + rlrDataValues.setXe133_conc(NumUtil.keepStr(xe133.getConc(), 5)); + rlrDataValues.setXe133_uncert_conc(NumUtil.keepStr(xe133.getConcErr(), 5)); + rlrDataValues.setXe133_MDC(NumUtil.keepStr(xe133.getMdc(), 5)); + rlrDataValues.setXe133_LC(NumUtil.keepStr(xe133.getLc(), 5)); List xe133mDataList = xeDataList.stream().filter(item -> item.getNuclideName().equals(XeNuclideName.XE_133m.getType())).collect(Collectors.toList()); GardsXeResultsSpectrum xe133m = xe133mDataList.get(0); - rlrDataValues.setXe133m_conc(String.valueOf(xe133m.getConc())); - rlrDataValues.setXe133m_uncert_conc(String.valueOf(xe133m.getConcErr())); - rlrDataValues.setXe133m_MDC(String.valueOf(xe133m.getMdc())); - rlrDataValues.setXe133m_LC(String.valueOf(xe133m.getLc())); + rlrDataValues.setXe133m_conc(NumUtil.keepStr(xe133m.getConc(), 5)); + rlrDataValues.setXe133m_uncert_conc(NumUtil.keepStr(xe133m.getConcErr(), 5)); + rlrDataValues.setXe133m_MDC(NumUtil.keepStr(xe133m.getMdc(), 5)); + rlrDataValues.setXe133m_LC(NumUtil.keepStr(xe133m.getLc(), 5)); List xe135DataList = xeDataList.stream().filter(item -> item.getNuclideName().equals(XeNuclideName.XE_135.getType())).collect(Collectors.toList()); GardsXeResultsSpectrum xe135 = xe135DataList.get(0); - rlrDataValues.setXe135_conc(String.valueOf(xe135.getConc())); - rlrDataValues.setXe135_uncert_conc(String.valueOf(xe135.getConcErr())); - rlrDataValues.setXe135_MDC(String.valueOf(xe135.getMdc())); - rlrDataValues.setXe135_LC(String.valueOf(xe135.getLc())); + rlrDataValues.setXe135_conc(NumUtil.keepStr(xe135.getConc(), 5)); + rlrDataValues.setXe135_uncert_conc(NumUtil.keepStr(xe135.getConcErr(), 5)); + rlrDataValues.setXe135_MDC(NumUtil.keepStr(xe135.getMdc(), 5)); + rlrDataValues.setXe135_LC(NumUtil.keepStr(xe135.getLc(), 5)); } } } diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/quartz/job/SysInfoJob.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/quartz/job/SysInfoJob.java index e3ba73f3..49d432ad 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/quartz/job/SysInfoJob.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/quartz/job/SysInfoJob.java @@ -73,20 +73,24 @@ public class SysInfoJob implements Job { String operator = null; for (String ruleKey : keys) { try { + if (StrUtil.equals(RedisConstant.ANALYSIS_RULE, ruleKey)) + continue; AlarmRule alarmRule = (AlarmRule) redisStreamUtil.get(ruleKey); // 如果报警规则为空,或者在沉默周期内,跳过当前规则 operator = alarmRule.getOperator(); String ruleId = alarmRule.getId(); + String itemId = alarmRule.getItemId(); String silenceKey = prefixSilence + ruleId; boolean hasKey = redisStreamUtil.hasKey(silenceKey); - boolean blank = StrUtil.isBlank(operator); + boolean blank1 = StrUtil.isBlank(operator); + boolean blank2 = StrUtil.isBlank(itemId); - if (blank || hasKey) continue; + if (blank1 || blank2 || hasKey) continue; // 向运管查询监控项数据 - String itemId = alarmRule.getItemId(); Result result = monitorSystem.itemBack(itemId, 0, start, end); Double current = result.getResult().getNow(); + log.error("当前CPU使用率为:{}", current); // 解析预警规则,判断是否需要报警 ObjectMapper mapper = new ObjectMapper(); @@ -94,26 +98,24 @@ public class SysInfoJob implements Job { rule.setCurrent(current); boolean needWarn = parse(rule); if (needWarn){ - // 记录报警日志 - AlarmLog alarmLog = new AlarmLog(); - alarmLog.setAlarmStartDate(LocalDateTime.now()); - alarmLog.setAlarmValue(current.toString()); + // 组织要发送的预警信息 String ruleName = alarmRule.getName(); - Double threshold = rule.getThreshold(); - String message = "您设定的预警规则:"+ruleName+"," + "预警阈值为:["+ - operator+threshold+"],当前值为:"+current; - alarmLog.setAlarmInfo(message); - alarmClient.create(alarmLog); - + String message = "您设定的预警规则:"+ruleName+"," + + "预警信息为:"+ operator + ",当前值为:" + current; // 规则触发报警后,设置该规则的沉默周期(如果有) // 沉默周期失效之前,该规则不会再次被触发 Long silenceCycle = alarmRule.getSilenceCycle(); ruleSilence(silenceKey, silenceCycle); - // 发送报警信息 String groupId = alarmRule.getContactId(); String notific = alarmRule.getNotification(); sendMessage.send(message,groupId,notific); + // 记录报警日志 + AlarmLog alarmLog = new AlarmLog(); + alarmLog.setAlarmStartDate(LocalDateTime.now()); + alarmLog.setAlarmValue(current.toString()); + alarmLog.setAlarmInfo(message); + alarmClient.create(alarmLog); } } catch (JsonProcessingException e) { log.error("预警规则{}解析失败!", operator); diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/test/JobController.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/test/JobController.java new file mode 100644 index 00000000..7fef3815 --- /dev/null +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/test/JobController.java @@ -0,0 +1,178 @@ +package org.jeecg.modules.test; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.jeecg.common.api.vo.Result; +import org.jeecg.common.config.mqtoken.UserTokenContext; +import org.jeecg.common.constant.DateConstant; +import org.jeecg.common.constant.RedisConstant; +import org.jeecg.common.util.RedisStreamUtil; +import org.jeecg.common.util.SpringContextUtils; +import org.jeecg.modules.base.entity.Rule; +import org.jeecg.modules.base.entity.monitor.ItemHistory; +import org.jeecg.modules.base.entity.postgre.AlarmLog; +import org.jeecg.modules.base.entity.postgre.AlarmRule; +import org.jeecg.modules.feignclient.AbnormalAlarmClient; +import org.jeecg.modules.feignclient.MonitorSystem; +import org.jeecg.modules.message.SendMessage; +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Set; + +import static org.jeecg.common.util.TokenUtils.getTempToken; +import static org.jeecg.modules.base.enums.Op.*; + +@Slf4j +@RestController +@RequestMapping("/sys/job") +public class JobController { + + private SendMessage sendMessage; + private MonitorSystem monitorSystem; + private RedisStreamUtil redisStreamUtil; + private AbnormalAlarmClient alarmClient; + + /** + * 根据host定时查询服务器信息 + * 并向消息队列中推送信息 + * + */ + @GetMapping("start") + public void startJob(){ + init(); + + // 查询所有报警规则,根据报警规则查询监控项数据 + String pattern = RedisConstant.PREFIX_RULE; + Set keys = redisStreamUtil.keys(pattern); + if (CollUtil.isEmpty(keys)) { + return; + } + + // 时间间隔为每分钟 + LocalDateTime now = LocalDateTime.now() + .withSecond(0) + .withNano(0); + LocalDateTime beforeMin = now.minusMinutes(1); + DateTimeFormatter formatter = DateTimeFormatter + .ofPattern(DateConstant.DATE_TIME); + String start = beforeMin.format(formatter); + String end = now.format(formatter); + + String prefixSilence = RedisConstant.PREFIX_SILENCE; + String operator = null; + for (String ruleKey : keys) { + try { + if (StrUtil.equals(RedisConstant.ANALYSIS_RULE, ruleKey)) + continue; + AlarmRule alarmRule = (AlarmRule) redisStreamUtil.get(ruleKey); + // 如果报警规则为空,或者在沉默周期内,跳过当前规则 + operator = alarmRule.getOperator(); + String ruleId = alarmRule.getId(); + String itemId = alarmRule.getItemId(); + String silenceKey = prefixSilence + ruleId; + boolean hasKey = redisStreamUtil.hasKey(silenceKey); + boolean blank1 = StrUtil.isBlank(operator); + boolean blank2 = StrUtil.isBlank(itemId); + + if (blank1 || blank2 || hasKey) continue; + + // 向运管查询监控项数据 + Result result = monitorSystem.itemBack(itemId, 0, start, end); + Double current = result.getResult().getNow(); + log.error("当前CPU使用率为:{}", current); + + // 解析预警规则,判断是否需要报警 + ObjectMapper mapper = new ObjectMapper(); + Rule rule = mapper.readValue(operator, Rule.class); + rule.setCurrent(current); + boolean needWarn = parse(rule); + if (needWarn){ + // 记录报警日志 + AlarmLog alarmLog = new AlarmLog(); + alarmLog.setRuleId(ruleId); + alarmLog.setAlarmValue(StrUtil.toString(current)); + String ruleName = alarmRule.getName(); + Double threshold = rule.getThreshold(); + String message = "您设定的预警规则:"+ruleName+"," + "预警阈值为:["+ + operator+threshold+"],当前值为:"+current; + alarmLog.setAlarmInfo(message); + alarmClient.create(alarmLog); + + // 规则触发报警后,设置该规则的沉默周期(如果有) + // 沉默周期失效之前,该规则不会再次被触发 + Long silenceCycle = alarmRule.getSilenceCycle(); + ruleSilence(silenceKey, silenceCycle); + + // 发送报警信息 + String groupId = alarmRule.getContactId(); + String notific = alarmRule.getNotification(); + sendMessage.send(message,groupId,notific); + } + } catch (JsonProcessingException e) { + log.error("预警规则{}解析失败!", operator); + e.printStackTrace(); + }catch (RuntimeException e){ + e.printStackTrace(); + } + } + destroy(); + } + + private boolean parse(Rule rule){ + Double current = rule.getCurrent(); + String op = rule.getOperator(); + Double threshold = rule.getThreshold(); + + boolean cNull = ObjectUtil.isNull(current); + boolean tNull = ObjectUtil.isNull(threshold); + if (cNull || tNull) return false; + + double currentV = current; + double thresholdV = threshold; + if (EQ.getOp().equals(op)){ + return currentV == thresholdV; + } else if (GT.getOp().equals(op)) { + return currentV > thresholdV; + } else if (GE.getOp().equals(op)) { + return currentV >= thresholdV; + } else if (LT.getOp().equals(op)) { + return currentV < thresholdV; + } else if (LE.getOp().equals(op)) { + return currentV <= thresholdV; + }else { + return false; + } + } + + /* + * 规则首次触发报警后,设置该规则的沉默周期(如果有) + */ + private void ruleSilence(String silenceKey ,Long silenceCycle) { + if (ObjectUtil.isNotNull(silenceCycle)) + redisStreamUtil.set(silenceKey, silenceCycle, silenceCycle); + } + + private void init(){ + // start:生成临时Token到线程中 + UserTokenContext.setToken(getTempToken()); + sendMessage = SpringContextUtils.getBean(SendMessage.class); + monitorSystem = SpringContextUtils.getBean(MonitorSystem.class); + redisStreamUtil = SpringContextUtils.getBean(RedisStreamUtil.class); + alarmClient = SpringContextUtils.getBean(AbnormalAlarmClient.class); + } + + private void destroy(){ + // end:删除临时Token + UserTokenContext.remove(); + } +}