Merge remote-tracking branch 'origin/station' into station

This commit is contained in:
qiaoqinzheng 2023-09-15 10:12:47 +08:00
commit ed7493423b
6 changed files with 282 additions and 39 deletions

View File

@ -47,4 +47,7 @@ public class AlarmLog implements Serializable {
@TableField(value = "alarm_value") @TableField(value = "alarm_value")
private String alarmValue; private String alarmValue;
public AlarmLog() {
this.alarmStartDate = LocalDateTime.now();
}
} }

View File

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

View File

@ -23,7 +23,7 @@ public interface MonitorAlarm {
@RequestParam String start, @RequestParam String start,
@RequestParam String end); @RequestParam String end);
// --------------------返给前端------------------- // --------------------前端专用-------------------
@GetMapping("list") @GetMapping("list")
Result<?> list(@RequestParam String code, Result<?> list(@RequestParam String code,
@RequestParam String hostId, @RequestParam String hostId,
@ -32,6 +32,14 @@ public interface MonitorAlarm {
@RequestParam String status, @RequestParam String status,
@RequestParam String type); @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") @GetMapping("queryItemHistory")
Result<?> item(@RequestParam String itemId, Result<?> item(@RequestParam String itemId,
@RequestParam Integer itemType, @RequestParam Integer itemType,
@ -44,14 +52,6 @@ public interface MonitorAlarm {
@RequestParam String start, @RequestParam String start,
@RequestParam String end); @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") @GetMapping("log")
Result<?> log(@RequestParam String code, Result<?> log(@RequestParam String code,
@RequestParam String deviceType, @RequestParam String deviceType,

View File

@ -1275,31 +1275,31 @@ public class SpectrumAnalysisServiceImpl implements ISpectrumAnalysisService {
if (CollectionUtils.isNotEmpty(xeDataList)){ if (CollectionUtils.isNotEmpty(xeDataList)){
List<GardsXeResultsSpectrum> xe131mDataList = xeDataList.stream().filter(item -> item.getNuclideName().equals(XeNuclideName.XE_131m.getType())).collect(Collectors.toList()); List<GardsXeResultsSpectrum> xe131mDataList = xeDataList.stream().filter(item -> item.getNuclideName().equals(XeNuclideName.XE_131m.getType())).collect(Collectors.toList());
GardsXeResultsSpectrum xe131m = xe131mDataList.get(0); GardsXeResultsSpectrum xe131m = xe131mDataList.get(0);
rlrDataValues.setXe131m_conc(String.valueOf(xe131m.getConc())); rlrDataValues.setXe131m_conc(NumUtil.keepStr(xe131m.getConc(), 5));
rlrDataValues.setXe131m_uncert_conc(String.valueOf(xe131m.getConcErr())); rlrDataValues.setXe131m_uncert_conc(NumUtil.keepStr(xe131m.getConcErr(), 5));
rlrDataValues.setXe131m_MDC(String.valueOf(xe131m.getMdc())); rlrDataValues.setXe131m_MDC(NumUtil.keepStr(xe131m.getMdc(), 5));
rlrDataValues.setXe131m_LC(String.valueOf(xe131m.getLc())); rlrDataValues.setXe131m_LC(NumUtil.keepStr(xe131m.getLc(), 5));
List<GardsXeResultsSpectrum> xe133DataList = xeDataList.stream().filter(item -> item.getNuclideName().equals(XeNuclideName.XE_133.getType())).collect(Collectors.toList()); List<GardsXeResultsSpectrum> xe133DataList = xeDataList.stream().filter(item -> item.getNuclideName().equals(XeNuclideName.XE_133.getType())).collect(Collectors.toList());
GardsXeResultsSpectrum xe133 = xe133DataList.get(0); GardsXeResultsSpectrum xe133 = xe133DataList.get(0);
rlrDataValues.setXe133_conc(String.valueOf(xe133.getConc())); rlrDataValues.setXe133_conc(NumUtil.keepStr(xe133.getConc(), 5));
rlrDataValues.setXe133_uncert_conc(String.valueOf(xe133.getConcErr())); rlrDataValues.setXe133_uncert_conc(NumUtil.keepStr(xe133.getConcErr(), 5));
rlrDataValues.setXe133_MDC(String.valueOf(xe133.getMdc())); rlrDataValues.setXe133_MDC(NumUtil.keepStr(xe133.getMdc(), 5));
rlrDataValues.setXe133_LC(String.valueOf(xe133.getLc())); rlrDataValues.setXe133_LC(NumUtil.keepStr(xe133.getLc(), 5));
List<GardsXeResultsSpectrum> xe133mDataList = xeDataList.stream().filter(item -> item.getNuclideName().equals(XeNuclideName.XE_133m.getType())).collect(Collectors.toList()); List<GardsXeResultsSpectrum> xe133mDataList = xeDataList.stream().filter(item -> item.getNuclideName().equals(XeNuclideName.XE_133m.getType())).collect(Collectors.toList());
GardsXeResultsSpectrum xe133m = xe133mDataList.get(0); GardsXeResultsSpectrum xe133m = xe133mDataList.get(0);
rlrDataValues.setXe133m_conc(String.valueOf(xe133m.getConc())); rlrDataValues.setXe133m_conc(NumUtil.keepStr(xe133m.getConc(), 5));
rlrDataValues.setXe133m_uncert_conc(String.valueOf(xe133m.getConcErr())); rlrDataValues.setXe133m_uncert_conc(NumUtil.keepStr(xe133m.getConcErr(), 5));
rlrDataValues.setXe133m_MDC(String.valueOf(xe133m.getMdc())); rlrDataValues.setXe133m_MDC(NumUtil.keepStr(xe133m.getMdc(), 5));
rlrDataValues.setXe133m_LC(String.valueOf(xe133m.getLc())); rlrDataValues.setXe133m_LC(NumUtil.keepStr(xe133m.getLc(), 5));
List<GardsXeResultsSpectrum> xe135DataList = xeDataList.stream().filter(item -> item.getNuclideName().equals(XeNuclideName.XE_135.getType())).collect(Collectors.toList()); List<GardsXeResultsSpectrum> xe135DataList = xeDataList.stream().filter(item -> item.getNuclideName().equals(XeNuclideName.XE_135.getType())).collect(Collectors.toList());
GardsXeResultsSpectrum xe135 = xe135DataList.get(0); GardsXeResultsSpectrum xe135 = xe135DataList.get(0);
rlrDataValues.setXe135_conc(String.valueOf(xe135.getConc())); rlrDataValues.setXe135_conc(NumUtil.keepStr(xe135.getConc(), 5));
rlrDataValues.setXe135_uncert_conc(String.valueOf(xe135.getConcErr())); rlrDataValues.setXe135_uncert_conc(NumUtil.keepStr(xe135.getConcErr(), 5));
rlrDataValues.setXe135_MDC(String.valueOf(xe135.getMdc())); rlrDataValues.setXe135_MDC(NumUtil.keepStr(xe135.getMdc(), 5));
rlrDataValues.setXe135_LC(String.valueOf(xe135.getLc())); rlrDataValues.setXe135_LC(NumUtil.keepStr(xe135.getLc(), 5));
} }
} }
} }

View File

@ -73,20 +73,24 @@ public class SysInfoJob implements Job {
String operator = null; String operator = null;
for (String ruleKey : keys) { for (String ruleKey : keys) {
try { try {
if (StrUtil.equals(RedisConstant.ANALYSIS_RULE, ruleKey))
continue;
AlarmRule alarmRule = (AlarmRule) redisStreamUtil.get(ruleKey); AlarmRule alarmRule = (AlarmRule) redisStreamUtil.get(ruleKey);
// 如果报警规则为空,或者在沉默周期内,跳过当前规则 // 如果报警规则为空,或者在沉默周期内,跳过当前规则
operator = alarmRule.getOperator(); operator = alarmRule.getOperator();
String ruleId = alarmRule.getId(); String ruleId = alarmRule.getId();
String itemId = alarmRule.getItemId();
String silenceKey = prefixSilence + ruleId; String silenceKey = prefixSilence + ruleId;
boolean hasKey = redisStreamUtil.hasKey(silenceKey); 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<ItemHistory> result = monitorSystem.itemBack(itemId, 0, start, end); Result<ItemHistory> result = monitorSystem.itemBack(itemId, 0, start, end);
Double current = result.getResult().getNow(); Double current = result.getResult().getNow();
log.error("当前CPU使用率为:{}", current);
// 解析预警规则,判断是否需要报警 // 解析预警规则,判断是否需要报警
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
@ -94,26 +98,24 @@ public class SysInfoJob implements Job {
rule.setCurrent(current); rule.setCurrent(current);
boolean needWarn = parse(rule); boolean needWarn = parse(rule);
if (needWarn){ if (needWarn){
// 记录报警日志 // 组织要发送的预警信息
AlarmLog alarmLog = new AlarmLog();
alarmLog.setAlarmStartDate(LocalDateTime.now());
alarmLog.setAlarmValue(current.toString());
String ruleName = alarmRule.getName(); String ruleName = alarmRule.getName();
Double threshold = rule.getThreshold(); String message = "您设定的预警规则:"+ruleName+"," +
String message = "您设定的预警规则:"+ruleName+"," + "预警阈值为:["+ "预警信息为:"+ operator + ",当前值为:" + current;
operator+threshold+"],当前值为:"+current;
alarmLog.setAlarmInfo(message);
alarmClient.create(alarmLog);
// 规则触发报警后,设置该规则的沉默周期(如果有) // 规则触发报警后,设置该规则的沉默周期(如果有)
// 沉默周期失效之前,该规则不会再次被触发 // 沉默周期失效之前,该规则不会再次被触发
Long silenceCycle = alarmRule.getSilenceCycle(); Long silenceCycle = alarmRule.getSilenceCycle();
ruleSilence(silenceKey, silenceCycle); ruleSilence(silenceKey, silenceCycle);
// 发送报警信息 // 发送报警信息
String groupId = alarmRule.getContactId(); String groupId = alarmRule.getContactId();
String notific = alarmRule.getNotification(); String notific = alarmRule.getNotification();
sendMessage.send(message,groupId,notific); 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) { } catch (JsonProcessingException e) {
log.error("预警规则{}解析失败!", operator); log.error("预警规则{}解析失败!", operator);

View File

@ -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<String> 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<ItemHistory> 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();
}
}