Pārlūkot izejas kodu

[alerter] 阈值触发次数后告警

tomsun28 4 gadi atpakaļ
vecāks
revīzija
f87eb03454

+ 31 - 11
alerter/src/main/java/com/usthe/alert/calculate/CalculateAlarm.java

@@ -20,6 +20,7 @@ import org.springframework.context.annotation.Configuration;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * 根据告警定义规则和采集数据匹配计算告警
@@ -34,12 +35,14 @@ public class CalculateAlarm {
     private AlerterWorkerPool workerPool;
     private AlerterDataQueue dataQueue;
     private AlertDefineService alertDefineService;
+    private Map<String, Alert> triggeredAlertMap;
 
     public CalculateAlarm (AlerterProperties properties, AlerterWorkerPool workerPool,
                            AlerterDataQueue dataQueue, AlertDefineService alertDefineService) {
         this.workerPool = workerPool;
         this.dataQueue = dataQueue;
         this.alertDefineService = alertDefineService;
+        this.triggeredAlertMap = new ConcurrentHashMap<>(128);
         startCalculate();
     }
 
@@ -133,17 +136,34 @@ public class CalculateAlarm {
                             Expression expression = AviatorEvaluator.compile(expr, true);
                             Boolean match = (Boolean) expression.execute(fieldValueMap);
                             if (match) {
-                                // 阈值规则匹配,触发告警
-                                Alert alert = Alert.builder()
-                                        .monitorId(monitorId)
-                                        .priority(define.getPriority())
-                                        .status((byte) 0)
-                                        .target(app + "." + metrics + "." + define.getField())
-                                        .times(1)
-                                        // 模板中关键字匹配替换
-                                        .content(AlertTemplateUtil.render(define.getTemplate(), fieldValueMap))
-                                        .build();
-                                dataQueue.addAlertData(alert);
+                                // 阈值规则匹配,判断已触发阈值次数,触发告警
+                                String monitorAlertKey = String.valueOf(monitorId) + define.getId();
+                                Alert triggeredAlert = triggeredAlertMap.get(monitorAlertKey);
+                                if (triggeredAlert != null) {
+                                    int times = triggeredAlert.getTimes() + 1;
+                                    triggeredAlert.setTimes(times);
+                                    if (times >= define.getTimes()) {
+                                        triggeredAlertMap.remove(monitorAlertKey);
+                                        dataQueue.addAlertData(triggeredAlert);
+                                    }
+                                } else {
+                                    int times = 1;
+                                    Alert alert = Alert.builder()
+                                            .monitorId(monitorId)
+                                            .alertDefineId(define.getId())
+                                            .priority(define.getPriority())
+                                            .status((byte) 0)
+                                            .target(app + "." + metrics + "." + define.getField())
+                                            .times(times)
+                                            // 模板中关键字匹配替换
+                                            .content(AlertTemplateUtil.render(define.getTemplate(), fieldValueMap))
+                                            .build();
+                                    if (times >= define.getTimes()) {
+                                        dataQueue.addAlertData(alert);
+                                    } else {
+                                        triggeredAlertMap.put(monitorAlertKey, alert);
+                                    }
+                                }
                                 // 此优先级以下的阈值规则则忽略
                                 break;
                             }

+ 0 - 1
alerter/src/main/java/com/usthe/alert/controller/AlertsController.java

@@ -18,7 +18,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
-import javax.persistence.Id;
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.Predicate;
 import java.util.HashSet;

+ 2 - 2
manager/src/main/java/com/usthe/manager/controller/AppController.java

@@ -48,8 +48,8 @@ public class AppController {
         if (lang == null || "".equals(lang)) {
             lang = "zh-CN";
         }
-        lang = lang.equalsIgnoreCase("zh-cn")? "zh-CN" : lang;
-        lang = lang.equalsIgnoreCase("en-us")? "en-US" : lang;
+        lang = "zh-cn".equalsIgnoreCase(lang)? "zh-CN" : lang;
+        lang = "en-us".equalsIgnoreCase(lang)? "en-US" : lang;
         List<Hierarchy> appHierarchies = appService.getAllAppHierarchy(lang);
         return ResponseEntity.ok(new Message<>(appHierarchies));
     }

+ 2 - 2
manager/src/main/java/com/usthe/manager/controller/I18nController.java

@@ -38,8 +38,8 @@ public class I18nController {
         if (lang == null || "".equals(lang)) {
             lang = "zh-CN";
         }
-        lang = lang.equalsIgnoreCase("zh-cn")? "zh-CN" : lang;
-        lang = lang.equalsIgnoreCase("en-us")? "en-US" : lang;
+        lang = "zh-cn".equalsIgnoreCase(lang)? "zh-CN" : lang;
+        lang = "en-us".equalsIgnoreCase(lang)? "en-US" : lang;
         Map<String, String> i18nResource = appService.getI18nResources(lang);
         return ResponseEntity.ok(new Message<>(i18nResource));
     }

+ 22 - 1
web-app/src/app/routes/monitor/monitor-detail/monitor-detail.component.html

@@ -44,7 +44,28 @@
       </div>
       <div nz-row nzGutter="16">
         <div nz-col nzSpan="8"><p style="text-align: right">状态</p></div>
-        <div nz-col nzSpan="16"><p style="text-align: left">{{monitor?.status}}</p></div>
+        <div nz-col nzSpan="16">
+          <nz-tag *ngIf="monitor?.status == 0" nzColor="default">
+            <i nz-icon nzType="robot" nzTheme="outline"></i>
+            <span>未监控</span>
+          </nz-tag>
+          <nz-tag *ngIf="monitor?.status == 1" nzColor="success">
+            <i nz-icon nzType="smile" nzTheme="outline"></i>
+            <span>正常监控</span>
+          </nz-tag>
+          <nz-tag *ngIf="monitor?.status == 2" nzColor="warning">
+            <i nz-icon nzType="meh" nzTheme="outline"></i>
+            <span>监控不可用</span>
+          </nz-tag>
+          <nz-tag *ngIf="monitor?.status == 3" nzColor="error">
+            <i nz-icon nzType="frown" nzTheme="outline"></i>
+            <span>监控不可达</span>
+          </nz-tag>
+          <nz-tag *ngIf="monitor?.status == 4" nzColor="default">
+            <i nz-icon nzType="sync"></i>
+            <span>监控已挂起</span>
+          </nz-tag>
+        </div>
       </div>
       <div nz-row nzGutter="16">
         <div nz-col nzSpan="8"><p style="text-align: right">采集间隔</p></div>

+ 1 - 1
web-app/src/app/routes/monitor/monitor-list/monitor-list.component.html

@@ -50,7 +50,7 @@
     <th nzAlign="center" nzLeft nzWidth="60px" [(nzChecked)]="checkedAll" (nzCheckedChange)="onAllChecked($event)"></th>
     <th nzAlign="center">监控名称</th>
     <th nzAlign="center">监控状态</th>
-    <th nzAlign="center">监控主机Host</th>
+    <th nzAlign="center">监控Host</th>
     <th nzAlign="center">监控类型</th>
     <th nzAlign="center">最新修改时间</th>
     <th nzAlign="center" nzRight>操作</th>