From b1cc3782387e0dc405effc8183a466537f56578d Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Fri, 12 May 2023 11:51:38 +0800 Subject: [PATCH] bugfix monitor status can not auto update, remove status UN_REACHABLE (#948) --- .../alert/calculate/CalculateAlarm.java | 32 +++++++------------ .../hertzbeat/alert/dao/AlertMonitorDao.java | 8 +++++ .../alert/service/impl/AlertServiceImpl.java | 2 +- .../common/constants/CommonConstants.java | 11 ------- .../alerter/impl/DbAlertStoreHandlerImpl.java | 6 +--- .../hertzbeat/manager/pojo/dto/AppCount.java | 4 --- .../service/impl/MonitorServiceImpl.java | 8 ++--- web-app/src/app/pojo/AppCount.ts | 1 - .../routes/dashboard/dashboard.component.html | 30 +++++------------ .../routes/dashboard/dashboard.component.ts | 5 --- 10 files changed, 32 insertions(+), 75 deletions(-) diff --git a/alerter/src/main/java/org/dromara/hertzbeat/alert/calculate/CalculateAlarm.java b/alerter/src/main/java/org/dromara/hertzbeat/alert/calculate/CalculateAlarm.java index 140f73e1426..c0710733fdb 100644 --- a/alerter/src/main/java/org/dromara/hertzbeat/alert/calculate/CalculateAlarm.java +++ b/alerter/src/main/java/org/dromara/hertzbeat/alert/calculate/CalculateAlarm.java @@ -59,6 +59,8 @@ public class CalculateAlarm { * key - monitorId 为监控状态可用性可达性告警 | Indicates the monitoring status availability reachability alarm */ public Map triggeredAlertMap; + + public Set unAvailableMonitors; private final AlerterWorkerPool workerPool; private final CommonDataQueue dataQueue; @@ -77,21 +79,13 @@ public CalculateAlarm (AlerterWorkerPool workerPool, CommonDataQueue dataQueue, this.alerterProperties = alerterProperties; this.bundle = ResourceBundleUtil.getBundle("alerter"); this.triggeredAlertMap = new ConcurrentHashMap<>(128); + this.unAvailableMonitors = Collections.synchronizedSet(new HashSet<>(16)); // Initialize stateAlertMap // 初始化stateAlertMap - List monitors = monitorDao.findMonitorsByStatusIn(Arrays.asList(CommonConstants.UN_AVAILABLE_CODE, - CommonConstants.UN_REACHABLE_CODE)); + List monitors = monitorDao.findMonitorsByStatus(CommonConstants.UN_AVAILABLE_CODE); if (monitors != null) { for (Monitor monitor : monitors) { - Alert.AlertBuilder alertBuilder = Alert.builder() - .priority(CommonConstants.ALERT_PRIORITY_CODE_EMERGENCY) - .status(CommonConstants.ALERT_STATUS_CODE_PENDING) - .target(CommonConstants.AVAILABILITY) - .firstTriggerTime(System.currentTimeMillis()) - .lastTriggerTime(System.currentTimeMillis()) - .nextEvalInterval(0L) - .times(0); - this.triggeredAlertMap.put(String.valueOf(monitor.getId()), alertBuilder.build()); + this.unAvailableMonitors.add(monitor.getId()); } } startCalculate(); @@ -252,17 +246,14 @@ private void handlerAvailableMetrics(long monitorId, String app, String metrics, } else { // Check whether an availability or unreachable alarm is generated before the association monitoring, and send a clear alarm to clear the monitoring status // 判断关联监控之前是否有可用性或者不可达告警,发送恢复告警进行监控状态恢复 - Alert preAlert = triggeredAlertMap.remove(String.valueOf(monitorId)); - if (preAlert != null && preAlert.getStatus() == CommonConstants.ALERT_STATUS_CODE_PENDING) { - // Sending an alarm cleared - // 发送告警恢复 + triggeredAlertMap.remove(String.valueOf(monitorId)); + boolean isRestartUnavailable = unAvailableMonitors.remove(monitorId); + if (isRestartUnavailable) { + // Sending an alarm Restore Map tags = new HashMap<>(6); tags.put(CommonConstants.TAG_MONITOR_ID, String.valueOf(monitorId)); tags.put(CommonConstants.TAG_MONITOR_APP, app); String content = this.bundle.getString("alerter.availability.resolved"); - if (CommonConstants.REACHABLE.equals(preAlert.getTarget())) { - content = this.bundle.getString("alerter.reachability.resolved"); - } long currentTimeMilli = System.currentTimeMillis(); Alert resumeAlert = Alert.builder() .tags(tags) @@ -284,7 +275,6 @@ private void handlerMonitorAvailableAlert(long monitorId, String app, CollectRep if (avaAlertDefine == null) { return; } - String monitorKey = String.valueOf(monitorId); Alert preAlert = triggeredAlertMap.get(String.valueOf(monitorId)); long currentTimeMill = System.currentTimeMillis(); Map tags = new HashMap<>(6); @@ -307,10 +297,11 @@ private void handlerMonitorAvailableAlert(long monitorId, String app, CollectRep .times(1); if (avaAlertDefine.getTimes() == null || avaAlertDefine.getTimes() <= 1) { silenceAlarm.filterSilenceAndSendData(alertBuilder.build().clone()); + unAvailableMonitors.add(monitorId); } else { alertBuilder.status(CommonConstants.ALERT_STATUS_CODE_NOT_REACH); } - triggeredAlertMap.put(monitorKey, alertBuilder.build()); + triggeredAlertMap.put(String.valueOf(monitorId), alertBuilder.build()); } else { int times = preAlert.getTimes() + 1; if (preAlert.getStatus() == CommonConstants.ALERT_STATUS_CODE_PENDING) { @@ -324,6 +315,7 @@ private void handlerMonitorAvailableAlert(long monitorId, String app, CollectRep if (times >= defineTimes) { preAlert.setStatus(CommonConstants.ALERT_STATUS_CODE_PENDING); silenceAlarm.filterSilenceAndSendData(preAlert); + unAvailableMonitors.add(monitorId); } else { preAlert.setStatus(CommonConstants.ALERT_STATUS_CODE_NOT_REACH); } diff --git a/alerter/src/main/java/org/dromara/hertzbeat/alert/dao/AlertMonitorDao.java b/alerter/src/main/java/org/dromara/hertzbeat/alert/dao/AlertMonitorDao.java index 25acb519e84..1a68f79b3be 100644 --- a/alerter/src/main/java/org/dromara/hertzbeat/alert/dao/AlertMonitorDao.java +++ b/alerter/src/main/java/org/dromara/hertzbeat/alert/dao/AlertMonitorDao.java @@ -36,5 +36,13 @@ public interface AlertMonitorDao extends JpaRepository, JpaSpecif * @return Monitor the list | 监控列表 */ List findMonitorsByStatusIn(List status); + + + /** + * Query the monitoring status of a specified monitoring state | 查询指定监控状态的监控 + * @param status 监控状态 + * @return Monitor the list | 监控列表 + */ + List findMonitorsByStatus(Byte status); } diff --git a/alerter/src/main/java/org/dromara/hertzbeat/alert/service/impl/AlertServiceImpl.java b/alerter/src/main/java/org/dromara/hertzbeat/alert/service/impl/AlertServiceImpl.java index 3132fda061a..2ee400069f5 100644 --- a/alerter/src/main/java/org/dromara/hertzbeat/alert/service/impl/AlertServiceImpl.java +++ b/alerter/src/main/java/org/dromara/hertzbeat/alert/service/impl/AlertServiceImpl.java @@ -155,7 +155,7 @@ private Alert buildAlertData(AlertReport alertReport){ .priority(alertReport.getPriority().byteValue()) .status(CommonConstants.ALERT_STATUS_CODE_PENDING) .tags(alertReport.getLabels()) - .target(CommonConstants.AVAILABLE) + .target(alertReport.getAlertName()) .times(3) .gmtCreate(LocalDateTime.ofInstant(Instant.ofEpochMilli(alertReport.getAlertTime()), ZoneId.systemDefault())) .build(); diff --git a/common/src/main/java/org/dromara/hertzbeat/common/constants/CommonConstants.java b/common/src/main/java/org/dromara/hertzbeat/common/constants/CommonConstants.java index 41c24cafd4d..ba258fccb5d 100644 --- a/common/src/main/java/org/dromara/hertzbeat/common/constants/CommonConstants.java +++ b/common/src/main/java/org/dromara/hertzbeat/common/constants/CommonConstants.java @@ -188,17 +188,6 @@ public interface CommonConstants { */ String AVAILABILITY = "availability"; - /** - * Availability Object - * 可用性对象 - */ - String AVAILABLE = "available"; - - /** - * Reachability Object可达性对象 - */ - String REACHABLE = "reachable"; - /** * Parameter Type Number * 参数类型 数字 diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/DbAlertStoreHandlerImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/DbAlertStoreHandlerImpl.java index a2f80a5ebb3..54d4d39211b 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/DbAlertStoreHandlerImpl.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/DbAlertStoreHandlerImpl.java @@ -71,14 +71,10 @@ public void store(Alert alert) { return; } if (monitor.getStatus() == CommonConstants.AVAILABLE_CODE) { - if (CommonConstants.AVAILABLE.equals(alert.getTarget())) { + if (CommonConstants.AVAILABILITY.equals(alert.getTarget())) { // Availability Alarm Need to change the monitoring status to unavailable // 可用性告警 需变更监控状态为不可用 monitorService.updateMonitorStatus(monitor.getId(), CommonConstants.UN_AVAILABLE_CODE); - } else if (CommonConstants.REACHABLE.equals(alert.getTarget())) { - // Reachability alarm The monitoring status needs to be changed to unreachable - // 可达性告警 需变更监控状态为不可达 - monitorService.updateMonitorStatus(monitor.getId(), CommonConstants.UN_REACHABLE_CODE); } } else { // If the alarm is restored, the monitoring state needs to be restored diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/pojo/dto/AppCount.java b/manager/src/main/java/org/dromara/hertzbeat/manager/pojo/dto/AppCount.java index f03a355a9e0..ee7f4dd8cff 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/pojo/dto/AppCount.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/pojo/dto/AppCount.java @@ -64,8 +64,4 @@ public AppCount(String app, byte status, Long size) { * 监控状态不可用的数量 */ private long unAvailableSize; - /** - * 监控状态不可达的数量 - */ - private long unReachableSize; } diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java index 056bde6cf6d..ea0f707f81c 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java @@ -55,7 +55,6 @@ import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; -import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; @@ -450,6 +449,7 @@ public void modifyMonitor(Monitor monitor, List params) throws RuntimeExc // Construct the collection task Job entity // 构造采集任务Job实体 Job appDefine = appService.getAppDefine(monitor.getApp()); + appDefine.setId(preMonitor.getJobId()); appDefine.setMonitorId(monitorId); appDefine.setInterval(monitor.getIntervals()); appDefine.setCyclic(true); @@ -613,9 +613,6 @@ public List getAllAppMonitorsCount() { case CommonConstants.UN_MANAGE_CODE: appCount.setUnManageSize(appCount.getUnManageSize() + item.getSize()); break; - case CommonConstants.UN_REACHABLE_CODE: - appCount.setUnReachableSize(appCount.getUnReachableSize() + item.getSize()); - break; default: break; } @@ -624,8 +621,7 @@ public List getAllAppMonitorsCount() { //Traverse the map obtained by statistics and convert it into a List result set //遍历统计得到的map,转换成List结果集 return appCountMap.values().stream().map(item -> { - item.setSize(item.getAvailableSize() + item.getUnManageSize() - + item.getUnReachableSize() + item.getUnAvailableSize()); + item.setSize(item.getAvailableSize() + item.getUnManageSize() + item.getUnAvailableSize()); try { Job job = appService.getAppDefine(item.getApp()); item.setCategory(job.getCategory()); diff --git a/web-app/src/app/pojo/AppCount.ts b/web-app/src/app/pojo/AppCount.ts index 4bb99242d88..08d3329d3e0 100644 --- a/web-app/src/app/pojo/AppCount.ts +++ b/web-app/src/app/pojo/AppCount.ts @@ -5,5 +5,4 @@ export class AppCount { availableSize: number = 0; unManageSize: number = 0; unAvailableSize: number = 0; - unReachableSize: number = 0; } diff --git a/web-app/src/app/routes/dashboard/dashboard.component.html b/web-app/src/app/routes/dashboard/dashboard.component.html index eaad995cdb0..e3f75490e39 100644 --- a/web-app/src/app/routes/dashboard/dashboard.component.html +++ b/web-app/src/app/routes/dashboard/dashboard.component.html @@ -2,14 +2,14 @@
-
+
{{ appCountService.size }}

{{ 'monitor.category.service' | i18n }}

-
+
{{ 'monitor.status.available' | i18n }} {{ appCountService.availableSize }} @@ -18,10 +18,6 @@ {{ 'monitor.status.unavailable' | i18n }} {{ appCountService.unAvailableSize }} - - {{ 'monitor.status.unreachable' | i18n }} {{ appCountService.unReachableSize }} - {{ 'monitor.status.un-manage' | i18n }} {{ appCountService.unManageSize }} @@ -32,23 +28,20 @@
-
+
{{ appCountDb.size }}

{{ 'monitor.category.db' | i18n }}

-
+
{{ 'monitor.status.available' | i18n }} {{ appCountDb.availableSize }} {{ 'monitor.status.unavailable' | i18n }} {{ appCountDb.unAvailableSize }} - - {{ 'monitor.status.unreachable' | i18n }} {{ appCountDb.unReachableSize }} - {{ 'monitor.status.un-manage' | i18n }} {{ appCountDb.unManageSize }} @@ -59,23 +52,20 @@
-
+
{{ appCountOs.size }}

{{ 'monitor.category.os' | i18n }}

-
+
{{ 'monitor.status.available' | i18n }} {{ appCountOs.availableSize }} {{ 'monitor.status.unavailable' | i18n }} {{ appCountOs.unAvailableSize }} - - {{ 'monitor.status.unreachable' | i18n }} {{ appCountOs.unReachableSize }} - {{ 'monitor.status.un-manage' | i18n }} {{ appCountOs.unManageSize }} @@ -86,14 +76,14 @@
-
+
{{ appCountMid.size }}

{{ 'monitor.category.mid' | i18n }}

-
+
{{ 'monitor.status.available' | i18n }} {{ appCountMid.availableSize }} @@ -101,10 +91,6 @@ {{ 'monitor.status.unavailable' | i18n }} {{ appCountMid.unAvailableSize }} - - {{ 'monitor.status.unreachable' | i18n }} {{ appCountMid.unReachableSize }} - {{ 'monitor.status.un-manage' | i18n }} {{ appCountMid.unManageSize }} diff --git a/web-app/src/app/routes/dashboard/dashboard.component.ts b/web-app/src/app/routes/dashboard/dashboard.component.ts index fce2f1bed2f..8f8f11f2cb6 100644 --- a/web-app/src/app/routes/dashboard/dashboard.component.ts +++ b/web-app/src/app/routes/dashboard/dashboard.component.ts @@ -292,35 +292,30 @@ export class DashboardComponent implements OnInit, OnDestroy { this.appCountService.availableSize += app.availableSize; this.appCountService.unAvailableSize += app.unAvailableSize; this.appCountService.unManageSize += app.unManageSize; - this.appCountService.unReachableSize += app.unReachableSize; break; case 'db': this.appCountDb.size += app.size; this.appCountDb.availableSize += app.availableSize; this.appCountDb.unAvailableSize += app.unAvailableSize; this.appCountDb.unManageSize += app.unManageSize; - this.appCountDb.unReachableSize += app.unReachableSize; break; case 'os': this.appCountOs.size += app.size; this.appCountOs.availableSize += app.availableSize; this.appCountOs.unAvailableSize += app.unAvailableSize; this.appCountOs.unManageSize += app.unManageSize; - this.appCountOs.unReachableSize += app.unReachableSize; break; case 'mid': this.appCountMid.size += app.size; this.appCountMid.availableSize += app.availableSize; this.appCountMid.unAvailableSize += app.unAvailableSize; this.appCountMid.unManageSize += app.unManageSize; - this.appCountMid.unReachableSize += app.unReachableSize; break; case 'custom': this.appCountCustom.size += app.size; this.appCountCustom.availableSize += app.availableSize; this.appCountCustom.unAvailableSize += app.unAvailableSize; this.appCountCustom.unManageSize += app.unManageSize; - this.appCountCustom.unReachableSize += app.unReachableSize; break; } });