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

This commit is contained in:
qiaoqinzheng 2024-02-22 20:47:07 +08:00
commit 93d78ee368
11 changed files with 146 additions and 139 deletions

View File

@ -92,6 +92,7 @@ public class StatusAspect {
/*
* 新增|修改数据源信息后 异步更新其状态信息
* 更新数据源的状态(1正常 2离线 3告警)和它对应的系统监控的HostId
* */
@Async
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysDatabaseServiceImpl.update(..)) || " +
@ -99,23 +100,52 @@ public class StatusAspect {
public void updateDatabaseStatus(JoinPoint point){
Object[] args = point.getArgs();
if (ArrayUtil.length(args) == 0) return;
String key = RedisConstant.DATABASE_STATUS;
Boolean online = null;
SysDatabase database = (SysDatabase) args[0];
String id = database.getId();
String name = database.getName();
try {
databaseService.status2Redis(database);
String token = ManageUtil.getToken();
Page<Host> hostPage = monitorAlarm.dbList(MonitorConstant.pageNo, MonitorConstant.pageSize, token).getResult();
if (ObjectUtil.isNull(hostPage) || CollUtil.isEmpty(hostPage.getRecords())) return;
if (ObjectUtil.isNull(hostPage) || CollUtil.isEmpty(hostPage.getRecords())) {
redisUtil.hset(key, id, new NameValue(name, online));
return;
}
List<Host> hosts = hostPage.getRecords();
String name = database.getName();
Host host = null;
for (Host oneHost : hosts) {
if (StrUtil.equals(name, oneHost.getCode()))
host = oneHost;
}
if (ObjectUtil.isNull(host)) return;
if (ObjectUtil.isNull(host)) {
redisUtil.hset(key, id, new NameValue(name, online));
return;
}
// 更新该数据源状态信息
String status = host.getStatus();
online = CollUtil.contains(ListUtil.toList(ServerStatus.ON.getValue(),
ServerStatus.WARN.getValue()), status);
redisUtil.hset(key, id, new NameValue(name, online));
// 更新数据源的HostId
database.setHostId(host.getHostId());
databaseService.updateById(database);
// 同步数据库监控项
Map<String, Item> itemMap = host.getItems();
if (MapUtil.isEmpty(itemMap) || CollUtil.isEmpty(itemMap.values()))
return;
Collection<Item> items = itemMap.values();
List<AlarmItem> alarmItems = new ArrayList<>();
// 只保留六个有效的监控项
List<String> itemNames = ListUtil.toList("dbMemory", "logRemainingSize", "latency",
"login", "dbSize", "dblSize", "connections");
for (Item item : items) {
if (!itemNames.contains(item.getName())) continue;
AlarmItem alarmItem = BeanUtil.copyProperties(item, AlarmItem.class);
alarmItem.setId(item.getItemId());
alarmItems.add(alarmItem);
}
alarmItemService.saveOrUpdateBatch(alarmItems);
}catch (FeignException.Unauthorized e){
ManageUtil.refreshToken();
log.warn("向运管系统查询Hosts信息异常: Token失效,已刷新Token");
@ -193,7 +223,8 @@ public class StatusAspect {
if (ObjectUtil.isNotNull(result)){
String hostId = ((Result<String>) result).getResult();
// 删除服务器相关联的监控项
alarmItemService.deleteByHostId(hostId);
if (StrUtil.isNotBlank(hostId))
alarmItemService.deleteByHostId(hostId);
}
// 删除服务器相关联的报警规则
Object[] args = point.getArgs();
@ -205,8 +236,14 @@ public class StatusAspect {
* 删除Database时 同步删除关联的报警规则
* */
@Async
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysDatabaseServiceImpl.deleteById(..))")
public void databaseDelete(JoinPoint point){
@AfterReturning(value = "execution(* org.jeecg.modules.service.impl.SysDatabaseServiceImpl.deleteById(..))", returning = "result")
public void databaseDelete(JoinPoint point, Object result){
if (ObjectUtil.isNotNull(result)){
String hostId = ((Result<String>) result).getResult();
// 删除服务器相关联的监控项
if (StrUtil.isNotBlank(hostId))
alarmItemService.deleteByHostId(hostId);
}
// 删除数据库相关联的报警规则
Object[] args = point.getArgs();
if (ArrayUtil.length(args) > 0)

View File

@ -6,7 +6,6 @@ import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.base.entity.postgre.AlarmItem;
import org.jeecg.modules.service.IAlarmItemService;
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;
@ -24,7 +23,7 @@ public class AlarmItemController extends JeecgController<AlarmItem, IAlarmItemSe
@GetMapping("alarmItems")
@ApiOperation(value = "服务器所有监控项",notes = "服务器所有监控项")
public Result<?> alarmItems(@RequestParam String sourceId){
List<AlarmItem> alarmItems = service.alarmItems(sourceId);
List<AlarmItem> alarmItems = service.alarmItemsServer(sourceId);
Map<String, String> itemIds = alarmItems.stream()
.collect(Collectors.toMap(AlarmItem::getName, AlarmItem::getId));
return Result.OK(itemIds);

View File

@ -7,5 +7,7 @@ import java.util.List;
public interface AlarmItemMapper extends BaseMapper<AlarmItem> {
List<AlarmItem> alarmItems(String sourceId);
List<AlarmItem> alarmItemsServer(String sourceId);
List<AlarmItem> alarmItemsDatabase(String sourceId);
}

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.jeecg.modules.mapper.AlarmItemMapper">
<select id="alarmItems" resultType="org.jeecg.modules.base.entity.postgre.AlarmItem">
<select id="alarmItemsServer" resultType="org.jeecg.modules.base.entity.postgre.AlarmItem">
SELECT
i.*
FROM
@ -9,4 +9,13 @@
INNER JOIN sys_server s ON i.host_id = s.host_id
WHERE s.ID = #{sourceId}
</select>
<select id="alarmItemsDatabase" resultType="org.jeecg.modules.base.entity.postgre.AlarmItem">
SELECT
i.*
FROM
alarm_item i
INNER JOIN sys_database d ON i.host_id = d.host_id
WHERE d.ID = #{sourceId}
</select>
</mapper>

View File

@ -1,7 +1,6 @@
package org.jeecg.modules.service;
import com.baomidou.mybatisplus.extension.service.IService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.base.dto.ItemDto;
import org.jeecg.modules.base.entity.postgre.AlarmItem;
@ -11,7 +10,9 @@ public interface IAlarmItemService extends IService<AlarmItem> {
void deleteByHostId(String serverId);
List<AlarmItem> alarmItems(String sourceId);
List<AlarmItem> alarmItemsServer(String sourceId);
List<AlarmItem> alarmItemsDatabase(String sourceId);
List<ItemDto> allItems(String sourceType, String sourceId);
}

View File

@ -35,7 +35,5 @@ public interface ISysDatabaseService extends IService<SysDatabase> {
void status2Redis();
void status2Redis(SysDatabase database);
String getNameById(String id);
}

View File

@ -1,45 +1,22 @@
package org.jeecg.modules.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.MonitorConstant;
import org.jeecg.common.constant.Prompt;
import org.jeecg.common.util.RedisStreamUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.base.dto.ItemDto;
import org.jeecg.modules.base.entity.monitor.Host;
import org.jeecg.modules.base.entity.monitor.Item;
import org.jeecg.modules.base.entity.postgre.AlarmItem;
import org.jeecg.modules.base.entity.postgre.AlarmItemDe;
import org.jeecg.modules.base.entity.postgre.SysServer;
import org.jeecg.modules.base.enums.SourceType;
import org.jeecg.modules.feignclient.ManageUtil;
import org.jeecg.modules.feignclient.MonitorAlarm;
import org.jeecg.modules.mapper.AlarmItemMapper;
import org.jeecg.modules.service.IAlarmItemDeService;
import org.jeecg.modules.service.IAlarmItemService;
import org.jeecg.modules.service.ISysDatabaseService;
import org.jeecg.modules.service.ISysServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static org.jeecg.modules.base.enums.SourceType.DATABASE;
import static org.jeecg.modules.base.enums.SourceType.SERVER;
@Service
@Slf4j
@ -62,8 +39,13 @@ public class AlarmItemServiceImpl extends ServiceImpl<AlarmItemMapper, AlarmItem
* 返回指定服务器的所有监控项
*/
@Override
public List<AlarmItem> alarmItems(String sourceId) {
return baseMapper.alarmItems(sourceId);
public List<AlarmItem> alarmItemsServer(String sourceId) {
return baseMapper.alarmItemsServer(sourceId);
}
@Override
public List<AlarmItem> alarmItemsDatabase(String sourceId) {
return baseMapper.alarmItemsDatabase(sourceId);
}
/*
@ -77,10 +59,13 @@ public class AlarmItemServiceImpl extends ServiceImpl<AlarmItemMapper, AlarmItem
return itemDtos;
switch (type){
case SERVER:
itemDtos = alarmItems(sourceId).stream()
itemDtos = alarmItemsServer(sourceId).stream()
.map(ItemDto::new).collect(Collectors.toList());
break;
case DATABASE:
itemDtos = alarmItemsDatabase(sourceId).stream()
.map(ItemDto::new).collect(Collectors.toList());
break;
case EMAIL:
itemDtos = alarmItemDeService.alarmItemDes(sourceType).stream()
.map(ItemDto::new).collect(Collectors.toList());

View File

@ -2,6 +2,7 @@ package org.jeecg.modules.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReUtil;
@ -24,7 +25,9 @@ import org.jeecg.modules.base.entity.monitor.Host;
import org.jeecg.modules.base.entity.monitor.Item;
import org.jeecg.modules.base.entity.postgre.SysDatabase;
import org.jeecg.modules.base.bizVo.SourceVo;
import org.jeecg.modules.base.entity.postgre.SysServer;
import org.jeecg.modules.base.enums.DbItem;
import org.jeecg.modules.base.enums.ServerStatus;
import org.jeecg.modules.entity.AlarmHistory;
import org.jeecg.modules.feignclient.ManageUtil;
import org.jeecg.modules.feignclient.MonitorAlarm;
@ -228,10 +231,12 @@ public class SysDatabaseServiceImpl extends ServiceImpl<SysDatabaseMapper, SysDa
@Override
@Transactional
public Result<?> deleteById(String id) {
SysDatabase database = getById(id);
boolean success = removeById(id);
if(success) {
delStatus(id);
return Result.OK(Prompt.DELETE_SUCC);
String hostId = ObjectUtil.isNotNull(database) ? database.getHostId() : null;
return Result.OK(Prompt.DELETE_SUCC, hostId);
}
return Result.error(Prompt.DELETE_ERR);
}
@ -363,24 +368,39 @@ public class SysDatabaseServiceImpl extends ServiceImpl<SysDatabaseMapper, SysDa
public void status2Redis() {
// 获取所有配置的数据源
List<SysDatabase> databases = list();
for (SysDatabase database : databases) {
this.status2Redis(database);
String key = RedisConstant.DATABASE_STATUS;
Map<String, Object> values = new HashMap<>();
try {
String token = ManageUtil.getToken();
List<Host> hosts = monitorAlarm.dbList(MonitorConstant.pageNo, MonitorConstant.pageSize, token).getResult().getRecords();
Map<String, Host> hostMap = hosts.stream().collect(Collectors.toMap(Host::getHostId, Host -> Host));
for (SysDatabase database : databases) {
Boolean online = null;
String hostId = database.getHostId();
String name = database.getName();
String databaseId = database.getId();
Host host = hostMap.get(hostId);
// 获取该数据源的状态并保存
if (ObjectUtil.isNotNull(host)) {
String status = host.getStatus();
online = CollUtil.contains(ListUtil.toList(ServerStatus.ON.getValue(),
ServerStatus.WARN.getValue()), status);
values.put(databaseId, new NameValue(name, online));
continue;
}
// 当前数据库不在监控数据库列表
values.put(databaseId, new NameValue(name, online));
}
redisUtil.hmset(key, values);
}catch (FeignException.Unauthorized e){
ManageUtil.refreshToken();
log.warn("向运管系统查询Hosts信息异常: Token失效,已刷新Token");
} catch (Exception e){
defaultStatus(databases);
log.error("向运管系统查询Hosts信息异常: {}", e.getMessage());
}
}
@Override
public void status2Redis(SysDatabase database) {
String statusKey = RedisConstant.DATABASE_STATUS;
String id = database.getId();
String name = database.getName();
String dbUrl = database.getDbUrl();
String dbDriver = database.getDbDriver();
String dbUsername = database.getDbUsername();
String dbPassword = database.getDbPassword();
boolean isConn = JDBCUtil.isConnection(dbUrl, dbDriver, dbUsername, dbPassword);
redisUtil.hset(statusKey, id, new NameValue(name, isConn));
}
@Override
public String getNameById(String id) {
String key = RedisConstant.DATABASE_STATUS;
@ -456,6 +476,17 @@ public class SysDatabaseServiceImpl extends ServiceImpl<SysDatabaseMapper, SysDa
return dbName;
}
private void defaultStatus(List<SysDatabase> sysDatabases){
String key = RedisConstant.DATABASE_STATUS;
Map<String, Object> values = new HashMap<>();
for (SysDatabase database: sysDatabases) {
String id = database.getId();
String name = database.getName();
values.put(id, new NameValue(name, null));
}
redisUtil.hmset(key, values);
}
/*
* 删除指定id的数据源的状态值
* */

View File

@ -189,12 +189,11 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
@Override
@Transactional
public Result<?> deleteById(String id) {
String hostId = null;
SysServer server = getById(id);
boolean success = removeById(id);
if(success) {
delStatus(id);
hostId = ObjectUtil.isNotNull(server) ? server.getHostId() : hostId;
String hostId = ObjectUtil.isNotNull(server) ? server.getHostId() : null;
return Result.OK(Prompt.DELETE_SUCC, hostId);
}
return Result.error(Prompt.DELETE_ERR);

View File

@ -1,77 +0,0 @@
package org.jeecg.modules.model;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
/**
* @Description: 用户通告阅读标记表
* @Author: jeecg-boot
* @Date: 2019-02-21
* @Version: V1.0
*/
@Data
public class AnnouncementSendModel implements Serializable {
private static final long serialVersionUID = 1L;
/**id*/
@TableId(type = IdType.ASSIGN_ID)
private String id;
/**通告id*/
private String anntId;
/**用户id*/
private String userId;
/**标题*/
private String titile;
/**内容*/
private String msgContent;
/**发布人*/
private String sender;
/**优先级L低M中H高*/
private String priority;
/**阅读状态*/
private String readFlag;
/**发布时间*/
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private java.util.Date sendTime;
/**页数*/
private Integer pageNo;
/**大小*/
private Integer pageSize;
/**
* 消息类型1:通知公告2:系统消息
*/
private String msgCategory;
/**
* 业务id
*/
private String busId;
/**
* 业务类型
*/
private String busType;
/**
* 打开方式 组件component 路由url
*/
private String openType;
/**
* 组件/路由 地址
*/
private String openPage;
/**
* 业务类型查询0.非bpm业务
*/
private String bizSource;
/**
* 摘要
*/
private String msgAbstract;
}

View File

@ -22,6 +22,7 @@ import org.jeecg.modules.base.entity.postgre.AlarmRule;
import org.jeecg.modules.base.entity.postgre.SysDatabase;
import org.jeecg.modules.base.enums.Item;
import org.jeecg.modules.feignclient.AbnormalAlarmClient;
import org.jeecg.modules.feignclient.ManageUtil;
import org.jeecg.modules.feignclient.MonitorSystem;
import org.jeecg.modules.message.SendMessage;
import org.jeecg.modules.quartz.entity.Monitor;
@ -55,6 +56,16 @@ public class DatabaseJob extends Monitor implements Job{
Set<String> keys = getRedisStreamUtil().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) {
@ -64,6 +75,8 @@ public class DatabaseJob extends Monitor implements Job{
operator = alarmRule.getOperator();
String ruleId = alarmRule.getId();
String itemId = alarmRule.getItemId();
String type = alarmRule.getItemType();
Integer itemType = StrUtil.isBlank(type) ? 0 : Integer.parseInt(type);
String silenceKey = prefixSilence + ruleId;
boolean hasKey = getRedisStreamUtil().hasKey(silenceKey);
boolean blank1 = StrUtil.isBlank(operator);
@ -75,7 +88,7 @@ public class DatabaseJob extends Monitor implements Job{
String sourceId = alarmRule.getSourceId();
String databaseName = getAlarmClient().getDatabaseName(sourceId);
// 根据监控项id选择要查询的监控项信息
/*// 根据监控项id选择要查询的监控项信息
Item item = Item.of(itemId);
if (ObjectUtil.isNull(item)) continue;
Number current = null;
@ -86,7 +99,17 @@ public class DatabaseJob extends Monitor implements Job{
// 追加的监控项...
default:
break;
}*/
// 向运管查询监控项数据
String token = ManageUtil.getToken();
Result<ItemHistory> result = getMonitorSystem().itemBack(itemId, itemType, start, end, token);
ItemHistory itemHistory = result.getResult();
if (ObjectUtil.isNull(itemHistory)){
log.warn("Database监控异常: [{}]查询监控项历史数据为空", databaseName);
continue;
}
Double current = itemHistory.getNow();
// 解析预警规则,判断是否需要报警
ObjectMapper mapper = new ObjectMapper();
Rule rule = mapper.readValue(operator, Rule.class);
@ -130,12 +153,12 @@ public class DatabaseJob extends Monitor implements Job{
/*
* 监控项-2: 测试数据源是否可以连接成功 (0:失败 1:成功)
* */
private Integer isConnection(String databaseId){
/*private Integer isConnection(String databaseId){
int res = 1;
String statusKey = RedisConstant.DATABASE_STATUS;
NameValue nameValue = (NameValue)getRedisUtil().hget(statusKey, databaseId);
if (ObjectUtil.isNull(nameValue) || ObjectUtil.isNull(nameValue.getValue()) || !nameValue.getValue())
res = 0;
return res;
}
}*/
}