[alerter,manager] 告警信息入库,监控状态变更联动
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
package com.usthe.manager.component.alerter;
|
||||
|
||||
import com.usthe.alert.AlerterDataQueue;
|
||||
import com.usthe.alert.AlerterWorkerPool;
|
||||
import com.usthe.alert.pojo.entity.Alert;
|
||||
import com.usthe.alert.service.AlertService;
|
||||
import com.usthe.common.util.CommonConstants;
|
||||
import com.usthe.manager.pojo.entity.Monitor;
|
||||
import com.usthe.manager.service.MonitorService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 告警信息入库分发
|
||||
* @author tom
|
||||
* @date 2021/12/10 12:58
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class DispatchAlarm {
|
||||
|
||||
private AlerterWorkerPool workerPool;
|
||||
private AlerterDataQueue dataQueue;
|
||||
private AlertService alertService;
|
||||
private MonitorService monitorService;
|
||||
|
||||
public DispatchAlarm(AlerterWorkerPool workerPool, AlerterDataQueue dataQueue,
|
||||
AlertService alertService, MonitorService monitorService) {
|
||||
this.workerPool = workerPool;
|
||||
this.dataQueue = dataQueue;
|
||||
this.alertService = alertService;
|
||||
this.monitorService = monitorService;
|
||||
startDispatch();
|
||||
}
|
||||
|
||||
private void startDispatch() {
|
||||
Runnable runnable = () -> {
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
try {
|
||||
Alert alert = dataQueue.pollAlertData();
|
||||
if (alert != null) {
|
||||
// 判断告警类型入库
|
||||
storeAlertData(alert);
|
||||
// 通知分发
|
||||
sendAlertDataListener(alert);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
};
|
||||
workerPool.executeJob(runnable);
|
||||
workerPool.executeJob(runnable);
|
||||
workerPool.executeJob(runnable);
|
||||
}
|
||||
|
||||
private void storeAlertData(Alert alert) {
|
||||
// todo 过滤重复告警 使用 告警持续时间参数-duration 这个时间段的相同重复告警应该被过滤
|
||||
// todo 使用缓存不直接操作库
|
||||
Monitor monitor = monitorService.getMonitor(alert.getMonitorId());
|
||||
if (monitor == null) {
|
||||
log.warn("Dispatch alarm the monitorId: {} not existed, ignored.", alert.getMonitorId());
|
||||
return;
|
||||
}
|
||||
alert.setMonitorName(monitor.getName());
|
||||
if (monitor.getStatus() == CommonConstants.UN_MANAGE_CODE) {
|
||||
// 当监控未管理时 忽略静默其告警信息
|
||||
return;
|
||||
}
|
||||
if (monitor.getStatus() != CommonConstants.UN_AVAILABLE_CODE
|
||||
&& monitor.getStatus() != CommonConstants.UN_REACHABLE_CODE) {
|
||||
if (CommonConstants.AVAILABLE.equals(alert.getTarget())) {
|
||||
// 可用性告警 需变更监控状态为不可用
|
||||
monitorService.updateMonitorStatus(monitor.getId(), CommonConstants.UN_AVAILABLE_CODE);
|
||||
} else if (CommonConstants.REACHABLE.equals(alert.getTarget())) {
|
||||
// 可达性告警 需变更监控状态为不可达
|
||||
monitorService.updateMonitorStatus(monitor.getId(), CommonConstants.UN_REACHABLE_CODE);
|
||||
}
|
||||
}
|
||||
// 告警落库
|
||||
alertService.addAlert(alert);
|
||||
}
|
||||
|
||||
private void sendAlertDataListener(Alert alert) {
|
||||
// todo 转发配置的邮件 微信 webhook
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -66,7 +66,7 @@ public class MonitorController {
|
||||
public ResponseEntity<Message<MonitorDto>> getMonitor(
|
||||
@ApiParam(value = "监控ID", example = "6565463543") @PathVariable("id") long id) {
|
||||
// 获取监控信息
|
||||
MonitorDto monitorDto = monitorService.getMonitor(id);
|
||||
MonitorDto monitorDto = monitorService.getMonitorDto(id);
|
||||
Message.MessageBuilder<MonitorDto> messageBuilder = Message.builder();
|
||||
if (monitorDto == null) {
|
||||
messageBuilder.code(MONITOR_NOT_EXIST_CODE).msg("Monitor not exist.");
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.usthe.manager.pojo.entity.Monitor;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -36,4 +37,12 @@ public interface MonitorDao extends JpaRepository<Monitor, Long>, JpaSpecificati
|
||||
*/
|
||||
@Query("select new com.usthe.manager.pojo.dto.AppCount(mo.app, COUNT(mo.id)) from Monitor mo group by mo.app")
|
||||
List<AppCount> findAppsCount();
|
||||
|
||||
/**
|
||||
* 更新指定监控的状态
|
||||
* @param id 监控ID
|
||||
* @param status 监控状态
|
||||
*/
|
||||
@Query("update Monitor set status = :status where id = :id")
|
||||
void updateMonitorStatus(@Param(value = "id") Long id, @Param(value = "status") byte status);
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ public interface MonitorService {
|
||||
* @return MonitorDto
|
||||
* @throws RuntimeException 查询过程中异常抛出
|
||||
*/
|
||||
MonitorDto getMonitor(long id) throws RuntimeException;
|
||||
MonitorDto getMonitorDto(long id) throws RuntimeException;
|
||||
|
||||
/**
|
||||
* 动态条件查询
|
||||
@@ -100,4 +100,18 @@ public interface MonitorService {
|
||||
* @return 监控类别与监控数量映射
|
||||
*/
|
||||
List<AppCount> getAllAppMonitorsCount();
|
||||
|
||||
/**
|
||||
* 查询监控
|
||||
* @param monitorId 监控ID
|
||||
* @return 监控信息
|
||||
*/
|
||||
Monitor getMonitor(Long monitorId);
|
||||
|
||||
/**
|
||||
* 更新指定监控的状态
|
||||
* @param monitorId 监控ID
|
||||
* @param status 监控状态
|
||||
*/
|
||||
void updateMonitorStatus(Long monitorId, byte status);
|
||||
}
|
||||
|
||||
@@ -264,7 +264,7 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public MonitorDto getMonitor(long id) throws RuntimeException {
|
||||
public MonitorDto getMonitorDto(long id) throws RuntimeException {
|
||||
Optional<Monitor> monitorOptional = monitorDao.findById(id);
|
||||
if (monitorOptional.isPresent()) {
|
||||
Monitor monitor = monitorOptional.get();
|
||||
@@ -335,4 +335,14 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
return monitorDao.findAppsCount();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Monitor getMonitor(Long monitorId) {
|
||||
return monitorDao.findById(monitorId).orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateMonitorStatus(Long monitorId, byte status) {
|
||||
monitorDao.updateMonitorStatus(monitorId, status);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user