feat:1.监控项与服务器绑定 2.优化监控数据查询
This commit is contained in:
parent
cbf75297c3
commit
200698ff03
|
@ -2,6 +2,10 @@ package org.jeecg.common.constant;
|
|||
|
||||
public interface MonitorConstant {
|
||||
|
||||
Integer pageNo = 1;
|
||||
|
||||
Integer pageSize = 99;
|
||||
|
||||
// 监控项名称
|
||||
String ITEM_CPUUSED = "cpuUtilization";
|
||||
String ITEM_SWAPUSED = "swapUtilization";
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
package org.jeecg.modules.base.entity.monitor;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class Servers implements Serializable {
|
||||
|
||||
private List<Host> records;
|
||||
}
|
|
@ -1,14 +1,19 @@
|
|||
package org.jeecg.modules.aspect;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.netease.qiye.qiyeopenplatform.sdk.QiyeOpenPlatSDK;
|
||||
import feign.FeignException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.common.constant.MonitorConstant;
|
||||
import org.jeecg.common.constant.RedisConstant;
|
||||
import org.jeecg.common.constant.SymbolConstant;
|
||||
|
@ -17,7 +22,8 @@ import org.jeecg.common.util.JDBCUtil;
|
|||
import org.jeecg.common.util.RedisUtil;
|
||||
import org.jeecg.modules.base.dto.NameValue;
|
||||
import org.jeecg.modules.base.entity.monitor.Host;
|
||||
import org.jeecg.modules.base.entity.monitor.Servers;
|
||||
import org.jeecg.modules.base.entity.monitor.Item;
|
||||
import org.jeecg.modules.base.entity.postgre.AlarmItem;
|
||||
import org.jeecg.modules.base.entity.postgre.SysDatabase;
|
||||
import org.jeecg.modules.base.entity.postgre.SysEmail;
|
||||
import org.jeecg.modules.base.entity.postgre.SysServer;
|
||||
|
@ -28,15 +34,15 @@ import org.jeecg.modules.qiyeEmail.base.InstanceSDK;
|
|||
import org.jeecg.modules.qiyeEmail.base.RParam;
|
||||
import org.jeecg.modules.qiyeEmail.base.dto.AccountInfo;
|
||||
import org.jeecg.modules.qiyeEmail.service.Account;
|
||||
import org.jeecg.modules.service.IAlarmRuleService;
|
||||
import org.jeecg.modules.service.ISysDatabaseService;
|
||||
import org.jeecg.modules.service.ISysEmailService;
|
||||
import org.jeecg.modules.service.ISysServerService;
|
||||
import org.jeecg.modules.service.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
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 static org.jeecg.modules.base.enums.Qiye.IS;
|
||||
|
@ -64,6 +70,9 @@ public class StatusAspect {
|
|||
@Autowired
|
||||
private IAlarmRuleService alarmRuleService;
|
||||
|
||||
@Autowired
|
||||
private IAlarmItemService alarmItemService;
|
||||
|
||||
// 新增|修改邮箱服务器信息后 异步更新其状态信息
|
||||
@Async
|
||||
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysEmailServiceImpl.update(..)) || " +
|
||||
|
@ -106,20 +115,43 @@ public class StatusAspect {
|
|||
String ipAddress = server.getIpAddress();
|
||||
try {
|
||||
String token = ManageUtil.getToken();
|
||||
Servers servers = monitorAlarm.listApp(ipAddress, MonitorConstant.SERVER_APP, token).getResult();
|
||||
// 获取所有监控主机信息
|
||||
List<Host> hosts = servers.getRecords();
|
||||
for (Host host : hosts) {
|
||||
String code = host.getCode();
|
||||
if (!StrUtil.equals(ipAddress, code))
|
||||
continue;
|
||||
server.setHostId(host.getHostId());
|
||||
status = host.getStatus();
|
||||
Page<Host> hostPage = monitorAlarm.listApp(ipAddress, MonitorConstant.SERVER_APP,
|
||||
MonitorConstant.pageNo, MonitorConstant.pageSize, token).getResult();
|
||||
if (ObjectUtil.isNull(hostPage) || CollUtil.isEmpty(hostPage.getRecords())){
|
||||
redisUtil.hset(key, id, new NameValue(name, status));
|
||||
return;
|
||||
}
|
||||
List<Host> hosts = hostPage.getRecords();
|
||||
Host host = null;
|
||||
for (Host oneHost : hosts) {
|
||||
String code = oneHost.getCode();
|
||||
if (StrUtil.equals(ipAddress, code))
|
||||
host = oneHost;
|
||||
}
|
||||
if (ObjectUtil.isNull(host)){
|
||||
redisUtil.hset(key, id, new NameValue(name, status));
|
||||
return;
|
||||
}
|
||||
status = host.getStatus();
|
||||
String hostId = host.getHostId();
|
||||
server.setHostId(hostId);
|
||||
// 更新该服务器状态信息
|
||||
redisUtil.hset(key, id, new NameValue(name, status));
|
||||
// 更新该服务器的HostId
|
||||
serverService.updateById(server);
|
||||
// 同步服务器监控项
|
||||
Page<Item> itemPage = monitorAlarm.allItems(hostId,
|
||||
MonitorConstant.pageNo, MonitorConstant.pageSize, token).getResult();
|
||||
if (ObjectUtil.isNull(itemPage) || CollUtil.isEmpty(itemPage.getRecords()))
|
||||
return;
|
||||
List<Item> items = itemPage.getRecords();
|
||||
List<AlarmItem> alarmItems = new ArrayList<>();
|
||||
items.forEach(item -> {
|
||||
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");
|
||||
|
@ -129,15 +161,32 @@ public class StatusAspect {
|
|||
}
|
||||
|
||||
/*
|
||||
* 删除Email|Database|Server时 同步删除Redis和数据库中相关联的预警规则
|
||||
* */
|
||||
* Server时 同步删除数据库中相关联的监控项&报警规则
|
||||
* */
|
||||
@Async
|
||||
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysServerServiceImpl.deleteById(..)) ||" +
|
||||
"execution(* org.jeecg.modules.service.impl.SysEmailServiceImpl.deleteById(..)) || " +
|
||||
"execution(* org.jeecg.modules.service.impl.SysDatabaseServiceImpl.deleteById(..))")
|
||||
public void deleteRules(JoinPoint point){
|
||||
@AfterReturning(value = "execution(* org.jeecg.modules.service.impl.SysServerServiceImpl.deleteById(..))", returning = "result")
|
||||
public void deleteItems(JoinPoint point, Object result){
|
||||
if (ObjectUtil.isNotNull(result)){
|
||||
String hostId = ((Result<String>) result).getResult();
|
||||
// 删除服务器相关联的监控项
|
||||
alarmItemService.deleteByHostId(hostId);
|
||||
}
|
||||
// 删除服务器相关联的报警规则
|
||||
Object[] args = point.getArgs();
|
||||
if (ArrayUtil.length(args) > 0)
|
||||
alarmRuleService.deleteBySourceId((String) args[0]);
|
||||
}
|
||||
|
||||
/*
|
||||
* 删除Email|Database时 同步删除Redis和数据库中相关联的预警规则
|
||||
* */
|
||||
@Async
|
||||
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysEmailServiceImpl.deleteById(..)) || " +
|
||||
"execution(* org.jeecg.modules.service.impl.SysDatabaseServiceImpl.deleteById(..))")
|
||||
public void deleteRules(JoinPoint point){
|
||||
Object[] args = point.getArgs();
|
||||
// 删除数据库和邮箱相关联的报警规则
|
||||
if (ArrayUtil.length(args) > 0)
|
||||
alarmRuleService.deleteBySourceId((String) args[0]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,19 @@
|
|||
package org.jeecg.modules.feignclient;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import kotlin.reflect.jvm.internal.impl.descriptors.Visibilities;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.modules.base.dto.LoginResult;
|
||||
import org.jeecg.modules.base.dto.LoginVo;
|
||||
import org.jeecg.modules.base.entity.monitor.Host;
|
||||
import org.jeecg.modules.base.entity.monitor.Item;
|
||||
import org.jeecg.modules.base.entity.monitor.ItemHistory;
|
||||
import org.jeecg.modules.base.entity.monitor.Servers;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@FeignClient(name = "monitorAlarm", url = "${monitor.url}")
|
||||
public interface MonitorAlarm {
|
||||
|
@ -20,18 +24,30 @@ public interface MonitorAlarm {
|
|||
|
||||
// --------------------后端专用-------------------
|
||||
@GetMapping("/omms/device/monitor/list") // 获取所有 服务器/数据库服务 信息
|
||||
Result<Servers> listApp(@RequestParam("type") String type,
|
||||
@RequestHeader("X-Access-Token") String token);
|
||||
Result<Page<Host>> listApp(@RequestParam("type") String type,
|
||||
@RequestParam("pageNo") Integer pageNo,
|
||||
@RequestParam("pageSize") Integer pageSize,
|
||||
@RequestHeader("X-Access-Token") String token);
|
||||
|
||||
@GetMapping("/omms/device/monitor/list") // 获取所有 服务器/数据库服务 信息
|
||||
Result<Servers> listApp(@RequestParam("code") String code,
|
||||
@RequestParam("type") String type,
|
||||
@RequestHeader("X-Access-Token") String token);
|
||||
Result<Page<Host>> listApp(@RequestParam("code") String code,
|
||||
@RequestParam("type") String type,
|
||||
@RequestParam("pageNo") Integer pageNo,
|
||||
@RequestParam("pageSize") Integer pageSize,
|
||||
@RequestHeader("X-Access-Token") String token);
|
||||
|
||||
@GetMapping("/omms/device/monitor/list") // 获取所有在线 服务器/数据库服务 信息
|
||||
Result<Servers> listOnApp(@RequestParam("status") String status,
|
||||
@RequestParam("type") String type,
|
||||
@RequestHeader("X-Access-Token") String token);
|
||||
Result<Page<Host>> listOnApp(@RequestParam("status") String status,
|
||||
@RequestParam("type") String type,
|
||||
@RequestParam("pageNo") Integer pageNo,
|
||||
@RequestParam("pageSize") Integer pageSize,
|
||||
@RequestHeader("X-Access-Token") String token);
|
||||
|
||||
@GetMapping("/omms/device/monitor/queryAllItems")
|
||||
Result<Page<Item>> allItems(@RequestParam("hostId") String hostId,
|
||||
@RequestParam("pageNo") Integer pageNo,
|
||||
@RequestParam("pageSize") Integer pageSize,
|
||||
@RequestHeader("X-Access-Token") String token);
|
||||
|
||||
@GetMapping("/omms/device/monitor/queryHostDetails") // 获取服务器摘要信息
|
||||
Result<Host> summary(@RequestParam("hostId") String hostId,
|
||||
|
@ -98,7 +114,7 @@ public interface MonitorAlarm {
|
|||
@RequestParam String status,
|
||||
@RequestHeader("X-Access-Token") String token);
|
||||
|
||||
// 数据库相关
|
||||
// -------------------------------------------数据库相关-----------------------------------------------------
|
||||
@GetMapping("/omms/monitor/db/item/detail")
|
||||
Result<?> dbDetail(@RequestParam("hostId") String hostId,
|
||||
@RequestHeader("X-Access-Token") String token);
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
<?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
|
||||
i.*
|
||||
|
|
|
@ -10,6 +10,8 @@ import java.util.List;
|
|||
public interface IAlarmItemService extends IService<AlarmItem> {
|
||||
boolean syncServerItem();
|
||||
|
||||
void deleteByHostId(String serverId);
|
||||
|
||||
List<AlarmItem> alarmItems(String sourceId);
|
||||
|
||||
List<ItemDto> allItems(String sourceType, String sourceId);
|
||||
|
|
|
@ -18,7 +18,6 @@ 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.monitor.Servers;
|
||||
import org.jeecg.modules.base.entity.postgre.AlarmItem;
|
||||
import org.jeecg.modules.base.entity.postgre.AlarmItemDe;
|
||||
import org.jeecg.modules.base.entity.postgre.SysServer;
|
||||
|
@ -55,50 +54,13 @@ public class AlarmItemServiceImpl extends ServiceImpl<AlarmItemMapper, AlarmItem
|
|||
@Autowired
|
||||
private IAlarmItemDeService alarmItemDeService;
|
||||
|
||||
/**
|
||||
* 同步所有服务器监控项信息
|
||||
*/
|
||||
/*@Transactional
|
||||
public boolean syncServerItem() {
|
||||
try {
|
||||
// 获取所有服务器信息(不包括数据库服务)
|
||||
List<Host> hosts = monitorAlarm.listApp(MonitorConstant.SERVER_APP).getResult().getRecords();
|
||||
// 获取所有服务器信息
|
||||
List<SysServer> servers = serverService.list();
|
||||
for (SysServer server : servers) {
|
||||
String ipAddress = server.getIpAddress();
|
||||
String serverId = server.getId();
|
||||
// 批量保存服务器对应的监控项信息
|
||||
for (Host host : hosts) {
|
||||
String code = host.getCode();
|
||||
if (!StrUtil.equals(ipAddress, code))
|
||||
continue;
|
||||
server.setHostId(host.getHostId());
|
||||
List<Item> items = ListUtil.toList(host.getItems().values());
|
||||
List<AlarmItem> alarmItems = new ArrayList<>();
|
||||
for (Item item : items) {
|
||||
AlarmItem alarmItem = BeanUtil.copyProperties(item, AlarmItem.class);
|
||||
alarmItem.setId(item.getItemId());
|
||||
alarmItem.setSourceId(serverId);
|
||||
alarmItems.add(alarmItem);
|
||||
}
|
||||
saveOrUpdateBatch(alarmItems);
|
||||
}
|
||||
}
|
||||
// 添加或更新SysServer的HostId
|
||||
return serverService.updateBatchById(servers);
|
||||
}catch (Exception e){
|
||||
log.error("向运管系统同步Server监控项信息异常: {}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}*/
|
||||
|
||||
@Transactional
|
||||
public boolean syncServerItem() {
|
||||
try {
|
||||
// 获取所有监控服务器信息(不包括数据库服务)
|
||||
String token = ManageUtil.getToken();
|
||||
List<Host> hosts = monitorAlarm.listApp(MonitorConstant.SERVER_APP, token).getResult().getRecords();
|
||||
List<Host> hosts = monitorAlarm.listApp(MonitorConstant.SERVER_APP,
|
||||
MonitorConstant.pageNo, MonitorConstant.pageSize, token).getResult().getRecords();
|
||||
// 获取所有服务器信息
|
||||
List<SysServer> servers = serverService.list();
|
||||
List<String> hostIds = servers.stream().map(SysServer::getHostId).filter(StrUtil::isNotBlank)
|
||||
|
@ -124,6 +86,13 @@ public class AlarmItemServiceImpl extends ServiceImpl<AlarmItemMapper, AlarmItem
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteByHostId(String hostId) {
|
||||
LambdaQueryWrapper<AlarmItem> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(AlarmItem::getHostId, hostId);
|
||||
remove(wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param sourceId
|
||||
*
|
||||
|
|
|
@ -27,7 +27,6 @@ import org.jeecg.common.util.SpringContextUtils;
|
|||
import org.jeecg.modules.base.dto.*;
|
||||
import org.jeecg.modules.base.entity.monitor.Host;
|
||||
import org.jeecg.modules.base.entity.monitor.Item;
|
||||
import org.jeecg.modules.base.entity.monitor.Servers;
|
||||
import org.jeecg.modules.base.entity.postgre.SysServer;
|
||||
import org.jeecg.modules.base.bizVo.SourceVo;
|
||||
import org.jeecg.modules.base.enums.ServerStatus;
|
||||
|
@ -78,8 +77,8 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
|
|||
List<Host> hosts = new ArrayList<>();
|
||||
try {
|
||||
String token = ManageUtil.getToken();
|
||||
hosts = monitorAlarm.listOnApp(ServerStatus.ON.getValue(),
|
||||
MonitorConstant.SERVER_APP, token).getResult().getRecords();
|
||||
hosts = monitorAlarm.listOnApp(ServerStatus.ON.getValue(), MonitorConstant.SERVER_APP,
|
||||
MonitorConstant.pageNo, MonitorConstant.pageSize, token).getResult().getRecords();
|
||||
}catch (FeignException.Unauthorized e){
|
||||
ManageUtil.refreshToken();
|
||||
log.warn("向运管系统查询Hosts信息异常: Token失效,已刷新Token");
|
||||
|
@ -187,10 +186,13 @@ 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);
|
||||
return Result.OK(Prompt.DELETE_SUCC);
|
||||
hostId = ObjectUtil.isNotNull(server) ? server.getHostId() : hostId;
|
||||
return Result.OK(Prompt.DELETE_SUCC, hostId);
|
||||
}
|
||||
return Result.error(Prompt.DELETE_ERR);
|
||||
}
|
||||
|
@ -355,7 +357,8 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
|
|||
Map<String, Object> values = new HashMap<>();
|
||||
try {
|
||||
String token = ManageUtil.getToken();
|
||||
List<Host> hosts = monitorAlarm.listApp(MonitorConstant.SERVER_APP, token).getResult().getRecords();
|
||||
List<Host> hosts = monitorAlarm.listApp(MonitorConstant.SERVER_APP,
|
||||
MonitorConstant.pageNo, MonitorConstant.pageSize, token).getResult().getRecords();
|
||||
Map<String, Host> hostMap = hosts.stream().collect(Collectors.toMap(Host::getHostId, Host -> Host));
|
||||
for (SysServer server : sysServers) {
|
||||
String hostId = server.getHostId();
|
||||
|
|
Loading…
Reference in New Issue
Block a user