修改告警监控服务
This commit is contained in:
parent
bdd25ac70d
commit
724a3c620f
|
|
@ -22,6 +22,7 @@ import org.jeecg.common.constant.SymbolConstant;
|
|||
import org.jeecg.common.util.EmailUtil;
|
||||
import org.jeecg.common.util.JDBCUtil;
|
||||
import org.jeecg.common.util.RedisUtil;
|
||||
import org.jeecg.modules.Util.PrometheusUtil;
|
||||
import org.jeecg.modules.base.dto.NameValue;
|
||||
import org.jeecg.modules.base.entity.monitor.Host;
|
||||
import org.jeecg.modules.base.entity.monitor.Item;
|
||||
|
|
@ -43,10 +44,7 @@ import org.springframework.scheduling.annotation.Async;
|
|||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.jeecg.modules.base.enums.Qiye.IS;
|
||||
|
|
@ -77,15 +75,21 @@ public class StatusAspect {
|
|||
@Autowired
|
||||
private IAlarmItemService alarmItemService;
|
||||
|
||||
@Autowired
|
||||
private PrometheusUtil prometheusUtil;
|
||||
|
||||
// 新增|修改邮箱服务器信息后 异步更新其状态信息
|
||||
// 并更新一个可用的发件服务器缓存到Redis
|
||||
@Async
|
||||
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysEmailServiceImpl.create(..)) ||" +
|
||||
"execution(* org.jeecg.modules.service.impl.SysEmailServiceImpl.update(..))")
|
||||
public void updateEamilStatus(JoinPoint point){
|
||||
@AfterReturning(
|
||||
"execution(* org.jeecg.modules.service.impl.SysEmailServiceImpl.create(..)) ||" +
|
||||
"execution(* org.jeecg.modules.service.impl.SysEmailServiceImpl.update(..))")
|
||||
public void updateEamilStatus(JoinPoint point) {
|
||||
emailService.sender2Redis();
|
||||
Object[] args = point.getArgs();
|
||||
if (ArrayUtil.length(args) == 0) return;
|
||||
if (ArrayUtil.length(args) == 0) {
|
||||
return;
|
||||
}
|
||||
SysEmail email = (SysEmail) args[0];
|
||||
emailService.status2Redis(email);
|
||||
}
|
||||
|
|
@ -95,28 +99,49 @@ public class StatusAspect {
|
|||
* 更新数据源的状态(1:正常 2:离线 3:告警)和它对应的系统监控的HostId
|
||||
* */
|
||||
@Async
|
||||
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysDatabaseServiceImpl.update(..)) || " +
|
||||
"execution(* org.jeecg.modules.service.impl.SysDatabaseServiceImpl.create(..))")
|
||||
public void updateDatabaseStatus(JoinPoint point){
|
||||
@AfterReturning(
|
||||
"execution(* org.jeecg.modules.service.impl.SysDatabaseServiceImpl.update(..)) || " +
|
||||
"execution(* org.jeecg.modules.service.impl.SysDatabaseServiceImpl.create(..))")
|
||||
public void updateDatabaseStatus(JoinPoint point) {
|
||||
Object[] args = point.getArgs();
|
||||
if (ArrayUtil.length(args) == 0) return;
|
||||
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 {
|
||||
String token = ManageUtil.getToken();
|
||||
Page<Host> hostPage = monitorAlarm.dbList(MonitorConstant.pageNo, MonitorConstant.pageSize, token).getResult();
|
||||
if (ObjectUtil.isNull(hostPage) || CollUtil.isEmpty(hostPage.getRecords())) {
|
||||
//String token = ManageUtil.getToken();
|
||||
//Page<Host> hostPage = monitorAlarm.dbList(MonitorConstant.pageNo, MonitorConstant.pageSize, token).getResult();
|
||||
|
||||
|
||||
List<Host> hosts = new ArrayList<>();
|
||||
//通过prometheus获取type=db的数据 up{type="db"}
|
||||
List<PrometheusUtil.QueryResult> hostResults =
|
||||
prometheusUtil.queryInstantFull("max by (name) (up{type=\"db\"})", null);
|
||||
for (PrometheusUtil.QueryResult result : hostResults) {
|
||||
Host host = new Host();
|
||||
String hostName = result.getMetrics().get("name");
|
||||
String job = result.getMetrics().get("job");
|
||||
String instance = result.getMetrics().get("instance");
|
||||
Double status = result.getValue().getValue();
|
||||
host.setCode(job);
|
||||
host.setHostId(instance);
|
||||
host.setName(hostName);
|
||||
host.setStatus(String.valueOf(status));
|
||||
hosts.add(host);
|
||||
}
|
||||
if (ObjectUtil.isNull(hostResults) || CollUtil.isEmpty(hostResults)) {
|
||||
redisUtil.hset(key, id, new NameValue(name, online));
|
||||
return;
|
||||
}
|
||||
List<Host> hosts = hostPage.getRecords();
|
||||
Host host = null;
|
||||
for (Host oneHost : hosts) {
|
||||
if (StrUtil.equals(name, oneHost.getCode()))
|
||||
if (StrUtil.equals(name, oneHost.getCode())) {
|
||||
host = oneHost;
|
||||
}
|
||||
}
|
||||
if (ObjectUtil.isNull(host)) {
|
||||
redisUtil.hset(key, id, new NameValue(name, online));
|
||||
|
|
@ -132,24 +157,27 @@ public class StatusAspect {
|
|||
databaseService.updateById(database);
|
||||
// 同步数据库监控项
|
||||
Map<String, Item> itemMap = host.getItems();
|
||||
if (MapUtil.isEmpty(itemMap) || CollUtil.isEmpty(itemMap.values()))
|
||||
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;
|
||||
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){
|
||||
} catch (FeignException.Unauthorized e) {
|
||||
ManageUtil.refreshToken();
|
||||
log.warn("向运管系统查询Hosts信息异常: Token失效,已刷新Token");
|
||||
}catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
log.error("数据源更新状态/绑定HostId异常: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
@ -159,30 +187,50 @@ public class StatusAspect {
|
|||
* 更新服务器的状态(1:正常 2:离线 3:告警)和它对应的系统监控的HostId
|
||||
* */
|
||||
@Async
|
||||
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysServerServiceImpl.update(..)) || " +
|
||||
"execution(* org.jeecg.modules.service.impl.SysServerServiceImpl.create(..))")
|
||||
public void updateServerStatus(JoinPoint point){
|
||||
@AfterReturning(
|
||||
"execution(* org.jeecg.modules.service.impl.SysServerServiceImpl.update(..)) || " +
|
||||
"execution(* org.jeecg.modules.service.impl.SysServerServiceImpl.create(..))")
|
||||
public void updateServerStatus(JoinPoint point) {
|
||||
Object[] args = point.getArgs();
|
||||
if (ArrayUtil.length(args) == 0) return;
|
||||
if (ArrayUtil.length(args) == 0) {
|
||||
return;
|
||||
}
|
||||
String key = RedisConstant.SERVER_STATUS;
|
||||
Boolean online = null;
|
||||
SysServer server = (SysServer) args[0];
|
||||
String id = server.getId();
|
||||
String name = server.getName();
|
||||
try {
|
||||
String token = ManageUtil.getToken();
|
||||
Page<Host> hostPage = monitorAlarm.listApp(name, MonitorConstant.pageNo, MonitorConstant.pageSize, token).getResult();
|
||||
if (ObjectUtil.isNull(hostPage) || CollUtil.isEmpty(hostPage.getRecords())){
|
||||
|
||||
List<Host> hosts = new ArrayList<>();
|
||||
|
||||
List<PrometheusUtil.QueryResult> hostResults =
|
||||
prometheusUtil.queryInstantFull(
|
||||
"node_boot_time_seconds{instance=\"" + name + "\"}", null);
|
||||
|
||||
if (ObjectUtil.isNull(hostResults) || CollUtil.isEmpty(hostResults)) {
|
||||
redisUtil.hset(key, id, new NameValue(name, online));
|
||||
return;
|
||||
}
|
||||
List<Host> hosts = hostPage.getRecords();
|
||||
|
||||
for (PrometheusUtil.QueryResult result : hostResults) {
|
||||
Host host = new Host();
|
||||
String hostName = result.getMetrics().get("__name__");
|
||||
String instance = result.getMetrics().get("instance");
|
||||
Double status = 1.0;
|
||||
host.setHostId(instance);
|
||||
host.setName(hostName);
|
||||
host.setStatus(String.valueOf(status));
|
||||
hosts.add(host);
|
||||
}
|
||||
|
||||
Host host = null;
|
||||
for (Host oneHost : hosts) {
|
||||
if (StrUtil.equals(name, oneHost.getCode()))
|
||||
if (StrUtil.equals(name, oneHost.getCode())) {
|
||||
host = oneHost;
|
||||
}
|
||||
}
|
||||
if (ObjectUtil.isNull(host)){
|
||||
if (ObjectUtil.isNull(host)) {
|
||||
redisUtil.hset(key, id, new NameValue(name, online));
|
||||
return;
|
||||
}
|
||||
|
|
@ -194,10 +242,17 @@ public class StatusAspect {
|
|||
// 更新该服务器的HostId
|
||||
server.setHostId(host.getHostId());
|
||||
serverService.updateById(server);
|
||||
// 同步服务器监控项
|
||||
// 同步服务器监控项 获取所有指标
|
||||
Set<String> metric = prometheusUtil.metricNames();
|
||||
List<String> metricNames =new ArrayList<>();
|
||||
metricNames.add("instance=\"172.21.170.11:9090\"");
|
||||
long end = System.currentTimeMillis() / 1000;
|
||||
long start = end - 3600; // 过去1小时
|
||||
prometheusUtil.series(metricNames,start, end);
|
||||
Map<String, Item> itemMap = host.getItems();
|
||||
if (MapUtil.isEmpty(itemMap) || CollUtil.isEmpty(itemMap.values()))
|
||||
if (MapUtil.isEmpty(itemMap) || CollUtil.isEmpty(itemMap.values())) {
|
||||
return;
|
||||
}
|
||||
Collection<Item> items = itemMap.values();
|
||||
List<AlarmItem> alarmItems = new ArrayList<>();
|
||||
items.forEach(item -> {
|
||||
|
|
@ -206,10 +261,10 @@ public class StatusAspect {
|
|||
alarmItems.add(alarmItem);
|
||||
});
|
||||
alarmItemService.saveOrUpdateBatch(alarmItems);
|
||||
}catch (FeignException.Unauthorized e){
|
||||
} catch (FeignException.Unauthorized e) {
|
||||
ManageUtil.refreshToken();
|
||||
log.warn("向运管系统查询Hosts信息异常: Token失效,已刷新Token");
|
||||
}catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
log.error("服务器更新状态/绑定HostId/绑定监控项异常: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
@ -219,53 +274,61 @@ public class StatusAspect {
|
|||
* */
|
||||
@Async
|
||||
@AfterReturning(value = "execution(* org.jeecg.modules.service.impl.SysServerServiceImpl.deleteById(..))", returning = "result")
|
||||
public void serverDelete(JoinPoint point, Object result){
|
||||
if (ObjectUtil.isNotNull(result)){
|
||||
public void serverDelete(JoinPoint point, Object result) {
|
||||
if (ObjectUtil.isNotNull(result)) {
|
||||
String hostId = ((Result<String>) result).getResult();
|
||||
// 删除服务器相关联的监控项
|
||||
if (StrUtil.isNotBlank(hostId))
|
||||
if (StrUtil.isNotBlank(hostId)) {
|
||||
alarmItemService.deleteByHostId(hostId);
|
||||
}
|
||||
}
|
||||
// 删除服务器相关联的报警规则
|
||||
Object[] args = point.getArgs();
|
||||
if (ArrayUtil.length(args) > 0)
|
||||
if (ArrayUtil.length(args) > 0) {
|
||||
alarmRuleService.deleteBySourceId((String) args[0]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 删除Database时 同步删除关联的报警规则
|
||||
* */
|
||||
* 删除Database时 同步删除关联的报警规则
|
||||
* */
|
||||
@Async
|
||||
@AfterReturning(value = "execution(* org.jeecg.modules.service.impl.SysDatabaseServiceImpl.deleteById(..))", returning = "result")
|
||||
public void databaseDelete(JoinPoint point, Object result){
|
||||
if (ObjectUtil.isNotNull(result)){
|
||||
public void databaseDelete(JoinPoint point, Object result) {
|
||||
if (ObjectUtil.isNotNull(result)) {
|
||||
String hostId = ((Result<String>) result).getResult();
|
||||
// 删除服务器相关联的监控项
|
||||
if (StrUtil.isNotBlank(hostId))
|
||||
if (StrUtil.isNotBlank(hostId)) {
|
||||
alarmItemService.deleteByHostId(hostId);
|
||||
}
|
||||
}
|
||||
// 删除数据库相关联的报警规则
|
||||
Object[] args = point.getArgs();
|
||||
if (ArrayUtil.length(args) > 0)
|
||||
if (ArrayUtil.length(args) > 0) {
|
||||
alarmRuleService.deleteBySourceId((String) args[0]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 删除Email时 同步删除关联的报警规则
|
||||
* 同时 如果是Redis中缓存的发件服务器 清空Redis缓存
|
||||
* */
|
||||
* 删除Email时 同步删除关联的报警规则
|
||||
* 同时 如果是Redis中缓存的发件服务器 清空Redis缓存
|
||||
* */
|
||||
@Async
|
||||
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysEmailServiceImpl.deleteById(..))")
|
||||
public void emailDelete(JoinPoint point){
|
||||
public void emailDelete(JoinPoint point) {
|
||||
Object[] args = point.getArgs();
|
||||
if (ArrayUtil.length(args) > 0){
|
||||
if (ArrayUtil.length(args) > 0) {
|
||||
String emailId = (String) args[0];
|
||||
// 删除邮箱关联的报警规则
|
||||
alarmRuleService.deleteBySourceId(emailId);
|
||||
// 如果是发件服务器 清除Redis缓存
|
||||
SysEmail sender = (SysEmail) redisUtil.get(RedisConstant.EMAIL_SENDER);
|
||||
if (ObjectUtil.isNull(sender)) return;
|
||||
if (!StrUtil.equals(emailId, sender.getId())) return;
|
||||
if (ObjectUtil.isNull(sender)) {
|
||||
return;
|
||||
}
|
||||
if (!StrUtil.equals(emailId, sender.getId())) {
|
||||
return;
|
||||
}
|
||||
redisUtil.del(RedisConstant.EMAIL_SENDER);
|
||||
emailService.sender2Redis();
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user