|
|
@@ -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
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|