feat:Server监控

fix:修改监控项同步策略
This commit is contained in:
nieziyan 2023-09-26 19:19:01 +08:00
parent 5b2ca8213b
commit b1d989c9cf
16 changed files with 123 additions and 119 deletions

View File

@ -0,0 +1,16 @@
package org.jeecg.modules.base.dto;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.util.Date;
@Data
public class AlarmInfo {
private String alarmInfo;
private String alarmValue;
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date alarmStartDate;
}

View File

@ -13,6 +13,8 @@ public class ServerDto implements Serializable {
private String name;
private String hostId;
private boolean online;
private String serverInfo;

View File

@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.jeecg.common.system.base.entity.JeecgEntity;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
@ -13,14 +14,7 @@ import java.time.LocalDate;
@Data
@TableName(value = "sys_server")
public class SysServer implements Serializable {
/**
* id
*/
@TableId(value = "id", type = IdType.ASSIGN_ID)
private String id;
public class SysServer extends JeecgEntity implements Serializable {
/**
* 名称
*/
@ -41,33 +35,4 @@ public class SysServer implements Serializable {
@TableField("host_id")
private String hostId;
/**
* 创建时间
*/
@TableField(value = "create_time")
@JsonFormat(pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate createTime;
/**
* 创建人
*/
@TableField(value = "create_by")
private String createBy;
/**
* 修改时间
*/
@TableField(value = "update_time")
@JsonFormat(pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate updateTime;
/**
* 修改人
*/
@TableField(value = "update_by")
private String updateBy;
}

View File

@ -2,6 +2,7 @@ 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.common.system.base.controller.JeecgController;
import org.jeecg.modules.base.entity.postgre.AlarmItem;
import org.jeecg.modules.service.IAlarmItemService;
@ -11,15 +12,27 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Api(value = "监控项管理",tags = "监控项管理")
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Api(value = "监控项同步及管理",tags = "监控项同步及管理")
@RestController
@RequestMapping("alarmItem")
public class AlarmItemController extends JeecgController<AlarmItem, IAlarmItemService> {
@GetMapping("syncItem")
@ApiOperation(value = "同步监控项信息",notes = "同步监控项信息")
public boolean syncItem(@RequestParam String type,
@RequestParam String code){
return service.syncItem(type,code);
@GetMapping("syncServerItem")
@ApiOperation(value = "同步服务器监控项信息",notes = "同步服务器监控项信息")
public boolean syncServerItem(){
return service.syncServerItem();
}
@GetMapping("alarmItems")
@ApiOperation(value = "服务器所有监控项",notes = "服务器所有监控项")
public Result<?> alarmItems(@RequestParam String sourceId){
List<AlarmItem> alarmItems = service.alarmItems(sourceId);
Map<String, String> itemIds = alarmItems.stream()
.collect(Collectors.toMap(AlarmItem::getName, AlarmItem::getId));
return Result.OK(itemIds);
}
}

View File

@ -73,8 +73,16 @@ public class SysServerController {
}
@GetMapping("detailsBasicInfo")
@ApiOperation(value = "Server Monitor-Details-BasicInfo",notes = "Server Monitor-Details-BasicInfo")
@ApiOperation(value = "ServerMonitor-Details-BasicInfo",notes = "ServerMonitor-Details-BasicInfo")
public Result<?> details_BasicInfo(@RequestParam String hostId){
return sysServerService.details_BasicInfo(hostId);
}
@GetMapping("detailsAlarmInfo")
@ApiOperation(value = "ServerMonitor-Details-AlarmInfo",notes = "ServerMonitor-Details-AlarmInfo")
public Result<?> details_AlarmInfo(@RequestParam String sourceId,
@RequestParam(defaultValue = "1") Integer pageNo,
@RequestParam(defaultValue = "10") Integer pageSize){
return sysServerService.details_AlarmInfo(sourceId, pageNo, pageSize);
}
}

View File

@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("systemMonitor")
@Api("系统监控信息查询")
@Api("系统监控信息管理")
public class SystemMonitorController {
@Autowired

View File

@ -15,10 +15,10 @@ import org.springframework.web.bind.annotation.RequestParam;
public interface MonitorAlarm {
// --------------------后端专用-------------------
@GetMapping("list") // 获取指定服务器信息
Result<Servers> listBack(@RequestParam("code") String code);
@GetMapping("list") // 获取所有 服务器/数据库服务 信息
Result<Servers> listApp(@RequestParam("type") String type);
@GetMapping("list") // 获取所有在线服务器信息
@GetMapping("list") // 获取所有在线 服务器/数据库服务 信息
Result<Servers> listOnApp(@RequestParam("status") String status,
@RequestParam("type") String type);

View File

@ -2,10 +2,7 @@ package org.jeecg.modules.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.jeecg.modules.base.dto.IdCount;
import org.jeecg.modules.base.dto.ServerDto;
import org.jeecg.modules.base.dto.SourceDto;
import org.jeecg.modules.base.dto.SourceItem;
import org.jeecg.modules.base.dto.*;
import org.jeecg.modules.base.entity.postgre.SysServer;
import org.jeecg.modules.entity.AlarmHistory;
@ -18,8 +15,7 @@ public interface SysServerMapper extends BaseMapper<SysServer> {
List<ServerDto> findPage(Map<String,Object> params);
void saveOrUpdateHostId(@Param("hostId") String hostId,
@Param("sourceId") String sourceId);
List<SourceDto> pageAll(String itemName);
List<AlarmInfo> alarmInfo(String sourceId);
}

View File

@ -35,6 +35,7 @@
SELECT
s.ID,
s.NAME,
s.host_id,
s.ip_address,
COUNT (l.id) AS alarms
FROM
@ -42,7 +43,7 @@
LEFT JOIN alarm_rule r ON s.ID = r.source_id
LEFT JOIN alarm_log l ON r.ID = l.rule_id
AND l.alarm_start_date BETWEEN #{startDate} AND #{endDate}
GROUP BY s.ID, s.NAME, s.ip_address
GROUP BY s.ID, s.NAME, s.host_id, s.ip_address
<if test="pageFlag == null">
LIMIT #{pageSize} OFFSET #{pageStart}
</if>
@ -60,7 +61,15 @@
s.status DESC,
s.NAME ASC
</select>
<update id="saveOrUpdateHostId" parameterType="String">
UPDATE sys_server SET host_id = #{hostId} WHERE id = #{sourceId}
</update>
<select id="alarmInfo" resultType="org.jeecg.modules.base.dto.AlarmInfo">
SELECT
l.alarm_info,
l.alarm_value,
l.alarm_start_date
FROM
alarm_log l
INNER JOIN alarm_rule r ON l.rule_id = r.ID
WHERE r.source_id = #{sourceId}
ORDER BY l.alarm_start_date DESC
</select>
</mapper>

View File

@ -7,7 +7,7 @@ import org.jeecg.modules.base.entity.postgre.AlarmItem;
import java.util.List;
public interface IAlarmItemService extends IService<AlarmItem> {
boolean syncItem(String sourceType,String code);
boolean syncServerItem();
List<AlarmItem> alarmItems(String sourceId);
}

View File

@ -32,5 +32,5 @@ public interface ISysServerService extends IService<SysServer> {
Result<?> details_BasicInfo(String hostId);
void saveOrUpdateHostId(String sourceId, String hostId);
Result<?> details_AlarmInfo(String sourceId, Integer pageNo, Integer pageSize);
}

View File

@ -7,11 +7,13 @@ import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.MonitorConstant;
import org.jeecg.common.constant.Prompt;
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.SysServer;
import org.jeecg.modules.feignclient.MonitorAlarm;
import org.jeecg.modules.mapper.AlarmItemMapper;
import org.jeecg.modules.service.IAlarmItemService;
@ -40,42 +42,38 @@ public class AlarmItemServiceImpl extends ServiceImpl<AlarmItemMapper, AlarmItem
@Autowired
private ISysDatabaseService databaseService;
@Override
public boolean syncItem(String type,String code) {
/**
* 同步所有服务器监控项信息
*/
@Transactional
public boolean syncServerItem() {
try {
String sourceId = null;
if (SERVER.getType().equals(type)){
sourceId = serverService.getByName(code);
} else if (DATABASE.getType().equals(type)) {
}else {
return false;
// 获取所有服务器信息(不包括数据库服务)
List<Host> hosts = monitorAlarm.listApp(MonitorConstant.SERVER_APP).getResult().getRecords();
// 获取所有服务器信息
List<SysServer> servers = serverService.list();
for (SysServer server : servers) {
String name = server.getName();
String serverId = server.getId();
// 批量保存服务器对应的监控项信息
for (Host host : hosts) {
String code = host.getCode();
if (!StrUtil.equals(name, 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);
}
}
if (StrUtil.isBlank(sourceId))
return false;
Result<Servers> result = monitorAlarm.listBack(code);
List<Host> hosts = result.getResult().getRecords();
List<Item> items = new ArrayList<>();
String hostId = null;
for (Host host : hosts) {
boolean equals = host.getCode().equals(code);
if(!equals) continue;
items = ListUtil.toList(host.getItems().values());
hostId = host.getHostId();
}
if (CollUtil.isEmpty(items))
return false;
List<AlarmItem> alarmItems = new ArrayList<>();
for (Item item : items) {
AlarmItem alarmItem = BeanUtil.copyProperties(item, AlarmItem.class);
alarmItem.setId(item.getItemId());
alarmItem.setSourceId(sourceId);
alarmItems.add(alarmItem);
}
serverService.saveOrUpdateHostId(sourceId, hostId);
return saveOrUpdateBatch(alarmItems);
// 添加或更新SysServer的HostId
return serverService.updateBatchById(servers);
}catch (Exception e){
e.printStackTrace();
return false;
@ -85,7 +83,7 @@ public class AlarmItemServiceImpl extends ServiceImpl<AlarmItemMapper, AlarmItem
@Override
public List<AlarmItem> alarmItems(String sourceId) {
if (StrUtil.isBlank(sourceId))
return ListUtil.empty();
return new ArrayList<>();
LambdaQueryWrapper<AlarmItem> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(AlarmItem::getSourceId,sourceId);
return list(wrapper);

View File

@ -51,8 +51,8 @@ public class CalculateConcServiceImpl implements CalculateConcService {
String comma = SymbolConstant.COMMA;
List<AlarmAnalysisRule> analysisRules = analysisRuleService.allAnalysisRule();
Set<String> nuclideNames = analysisRules.stream()
.filter(rule -> StrUtil.isNotBlank(rule.getNuclides()))
.map(AlarmAnalysisRule::getNuclides)
.filter(StrUtil::isNotBlank)
.flatMap(nuclides -> ListUtil.toList(nuclides.split(comma)).stream())
.collect(Collectors.toSet());
Map<String,Object> params = new HashMap<>();

View File

@ -18,6 +18,7 @@ import org.jeecg.common.constant.MonitorConstant;
import org.jeecg.common.constant.Prompt;
import org.jeecg.common.util.NumUtil;
import org.jeecg.common.util.PageUtil;
import org.jeecg.modules.base.dto.AlarmInfo;
import org.jeecg.modules.base.dto.BasicInfo;
import org.jeecg.modules.base.dto.ServerDto;
import org.jeecg.modules.base.dto.SourceDto;
@ -65,12 +66,12 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
List<Host> hosts = monitorAlarm.listOnApp(MonitorConstant.STATUS_ON,
MonitorConstant.SERVER_APP).getResult().getRecords();
for (ServerDto serverDto : serverDtos) {
String name = serverDto.getName();
int alarms = serverDto.getAlarms();
String hostIdS = serverDto.getHostId();
serverDto.setAlarmRed(alarms > 0);
for (Host host : hosts) {
String code = host.getCode();
if (!StrUtil.equals(name, code)) continue;
String hostId = host.getHostId();
if (!StrUtil.equals(hostIdS, hostId)) continue;
Map<String, Item> items = host.getItems();
// cpu利用率
@ -246,9 +247,9 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
String status = host.getStatus();
boolean online = StrUtil.equals(status, MonitorConstant.STATUS_ON);
Map<String, Item> items = host.getItems();
String upTime = items.get(MonitorConstant.ITEM_RUNTIME).getLastValue();
upTime = StrUtil.isBlank(upTime) ? "--" :
NumUtil.keepStr(Long.parseLong(upTime) / 3600.0, 1) + "h";
String runTime = items.get(MonitorConstant.ITEM_RUNTIME).getLastValue();
runTime = StrUtil.isBlank(runTime) ? "--" :
NumUtil.keepStr(Long.parseLong(runTime) / 3600.0, 1) + "h";
String ramSize = items.get(MonitorConstant.ITEM_RAMSIZE).getLastValue();
ramSize = StrUtil.isBlank(ramSize) ? "--" :
NumUtil.keepStr(Double.parseDouble(ramSize.replace("MB", "")) / 1024, 1) + "GB";
@ -285,7 +286,7 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
diskUsedValue.put("xData", xData);diskUsedValue.put("yData", yData);
BasicInfo basicInfo = new BasicInfo();
basicInfo.setRunningState(online).setRunTime(upTime).setRamSize(ramSize)
basicInfo.setRunningState(online).setRunTime(runTime).setRamSize(ramSize)
.setCpuCores(cpuCores).setTotalDiskPar(totalDiskPar).setHostName(hostName)
.setOsVersion(osVersion).setNetwork(netWork).setLocation(location).setIp(ip)
.setZone(zone).setOsName(osName).setStartTime(startTime).setCpuType(cpuType)
@ -294,7 +295,12 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
}
@Override
public void saveOrUpdateHostId(String sourceId, String hostId) {
baseMapper.saveOrUpdateHostId(hostId, sourceId);
public Result<?> details_AlarmInfo(String sourceId, Integer pageNo, Integer pageSize) {
List<AlarmInfo> alarmInfos = baseMapper.alarmInfo(sourceId);
int total = alarmInfos.size();
Page<AlarmInfo> page = new Page<>(pageNo, pageSize, total);
List<AlarmInfo> records = PageUtil.page(pageNo, pageSize, alarmInfos);
page.setRecords(records);
return Result.OK(page);
}
}

View File

@ -1,7 +1,6 @@
package org.jeecg.modules.feignclient;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.base.entity.postgre.AlarmAnalysisRule;
import org.jeecg.modules.base.entity.postgre.AlarmLog;
import org.jeecg.modules.base.entity.postgre.SysEmail;
import org.springframework.cloud.openfeign.FeignClient;
@ -38,7 +37,6 @@ public interface AbnormalAlarmClient {
boolean refreshParam();
/* AlarmItemController下相关接口 */
@GetMapping("/alarmItem/syncItem")
boolean syncItem(@RequestParam String type,
@RequestParam String code);
@GetMapping("/alarmItem/syncServerItem")
boolean syncServerItem();
}

View File

@ -1,10 +1,7 @@
package org.jeecg.modules.quartz.job;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.config.mqtoken.UserTokenContext;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.base.enums.SourceType;
import org.jeecg.modules.feignclient.AbnormalAlarmClient;
import org.quartz.*;
@ -14,13 +11,9 @@ import static org.jeecg.common.util.TokenUtils.getTempToken;
* 此处的同步是指:当定时任务的执行时间大于任务的时间
* 间隔时会等待第一个任务执行完成才会走第二个任务
*/
/**
* 监控项信息更新保存
*/
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class SyncItemJob implements Job {
public class SyncServerItemJob implements Job {
private AbnormalAlarmClient alarmClient;
@ -30,7 +23,7 @@ public class SyncItemJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
init();
success = alarmClient.syncItem(SourceType.SERVER.getType(), "172.21.60.231");
success = alarmClient.syncServerItem();
}catch (Exception e){
e.printStackTrace();
success = false;