From b21834cd8df455b18ef4b68714b8f820471e7e31 Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Fri, 10 Nov 2023 10:23:28 +0800 Subject: [PATCH] bugfix webhook post body error and alarm recover exception (#1327) --- .../hertzbeat/alert/calculate/CalculateAlarm.java | 10 +++++++--- .../collect/telnet/TelnetCollectImplTest.java | 5 ++--- .../common/entity/manager/NoticeTemplate.java | 1 + .../impl/WebHookAlertNotifyHandlerImpl.java | 1 + .../manager/service/impl/MonitorServiceImpl.java | 9 ++++----- .../service/impl/NoticeConfigServiceImpl.java | 14 +++++++++++--- 6 files changed, 26 insertions(+), 14 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 50cf9b22836..03a6c84c22a 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 @@ -51,8 +51,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; -import static org.dromara.hertzbeat.common.constants.CommonConstants.ALERT_STATUS_CODE_PENDING; -import static org.dromara.hertzbeat.common.constants.CommonConstants.ALERT_STATUS_CODE_SOLVED; +import static org.dromara.hertzbeat.common.constants.CommonConstants.*; /** * Calculate alarms based on the alarm definition rules and collected data @@ -101,7 +100,12 @@ public CalculateAlarm(AlerterWorkerPool workerPool, CommonDataQueue dataQueue, List monitors = monitorDao.findMonitorsByStatus(CommonConstants.UN_AVAILABLE_CODE); if (monitors != null) { for (Monitor monitor : monitors) { - this.notRecoveredAlertMap.put(monitor.getId() + CommonConstants.AVAILABILITY, Alert.builder().build()); + HashMap tags = new HashMap<>(8); + tags.put(TAG_MONITOR_ID, String.valueOf(monitor.getId())); + tags.put(TAG_MONITOR_NAME, monitor.getName()); + tags.put(TAG_MONITOR_APP, monitor.getApp()); + this.notRecoveredAlertMap.put(monitor.getId() + CommonConstants.AVAILABILITY, + Alert.builder().tags(tags).target(AVAILABILITY).status(UN_AVAILABLE_CODE).build()); } } startCalculate(); diff --git a/collector/src/test/java/org/dromara/hertzbeat/collector/collect/telnet/TelnetCollectImplTest.java b/collector/src/test/java/org/dromara/hertzbeat/collector/collect/telnet/TelnetCollectImplTest.java index c403d6b076c..3bb2fd1d206 100644 --- a/collector/src/test/java/org/dromara/hertzbeat/collector/collect/telnet/TelnetCollectImplTest.java +++ b/collector/src/test/java/org/dromara/hertzbeat/collector/collect/telnet/TelnetCollectImplTest.java @@ -29,8 +29,7 @@ * Test case for {@link TelnetCollectImpl} */ class TelnetCollectImplTest { - - @Test + void telnet() { TelnetClient telnetClient = null; try { @@ -51,4 +50,4 @@ void telnet() { } } } -} \ No newline at end of file +} diff --git a/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/NoticeTemplate.java b/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/NoticeTemplate.java index 077f3d44e1b..4c76d90bb20 100644 --- a/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/NoticeTemplate.java +++ b/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/NoticeTemplate.java @@ -91,6 +91,7 @@ public class NoticeTemplate { "${triggerTimeLabel} : ${triggerTime}\n" + "${contentLabel} : ${content}", accessMode = READ_WRITE) @Length(max = 60000) + @Lob @NotBlank private String content; diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImpl.java index 95402fe4991..8d56744285b 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImpl.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/WebHookAlertNotifyHandlerImpl.java @@ -43,6 +43,7 @@ public void send(NoticeReceiver receiver, NoticeTemplate noticeTemplate, Alert a HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); String webhookJson = renderContent(noticeTemplate, alert); + webhookJson = webhookJson.replace(",\n }", "\n }"); HttpEntity alertHttpEntity = new HttpEntity<>(webhookJson, headers); ResponseEntity entity = restTemplate.postForEntity(receiver.getHookUrl(), alertHttpEntity, String.class); if (entity.getStatusCode().value() < HttpStatus.BAD_REQUEST.value()) { 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 8855dd84fc5..6f1b84b1eeb 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 @@ -18,7 +18,6 @@ package org.dromara.hertzbeat.manager.service.impl; import com.fasterxml.jackson.core.type.TypeReference; -import com.google.gson.Gson; import lombok.extern.slf4j.Slf4j; import org.dromara.hertzbeat.alert.dao.AlertDefineBindDao; import org.dromara.hertzbeat.common.constants.CommonConstants; @@ -85,8 +84,6 @@ public class MonitorServiceImpl implements MonitorService { public static final String PATTERN_HTTP = "(?i)http://"; public static final String PATTERN_HTTPS = "(?i)https://"; - private static final Gson GSON = new Gson(); - @Autowired private AppService appService; @@ -734,8 +731,10 @@ public void copyMonitors(List ids) { monitorOpt.ifPresentOrElse(monitor -> { // deep copy original monitor to achieve persist in JPA - Monitor newMonitor = GSON.fromJson(GSON.toJson(monitor), Monitor.class); - copyMonitor(newMonitor, params); + Monitor newMonitor = JsonUtil.fromJson(JsonUtil.toJson(monitor), Monitor.class); + if (newMonitor != null) { + copyMonitor(newMonitor, params); + } }, () -> log.warn("can not find the monitor for id :{}", id)); }); } diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/NoticeConfigServiceImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/NoticeConfigServiceImpl.java index 6129a5dd7ad..fb9a67393c5 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/NoticeConfigServiceImpl.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/NoticeConfigServiceImpl.java @@ -62,7 +62,7 @@ public class NoticeConfigServiceImpl implements NoticeConfigService, CommandLine private static final String ALERT_TEST_TARGET = "Test Target"; - private static final String ALERT_TEST_CONTENT = "test send msg! \n This is the test data. It is proved that it can be received successfully"; + private static final String ALERT_TEST_CONTENT = "test send msg! \\n This is the test data. It is proved that it can be received successfully"; private static final Map PRESET_TEMPLATE = new HashMap<>(16); @@ -244,13 +244,21 @@ public NoticeTemplate getDefaultNoticeTemplateByType(Byte type) { @Override public boolean sendTestMsg(NoticeReceiver noticeReceiver) { + Map tags = new HashMap<>(8); + tags.put(CommonConstants.TAG_MONITOR_ID, "100"); + tags.put(CommonConstants.TAG_MONITOR_NAME, "100Name"); + tags.put(CommonConstants.TAG_THRESHOLD_ID, "200"); Alert alert = new Alert(); + alert.setTags(tags); + alert.setId(100L); alert.setTarget(ALERT_TEST_TARGET); + alert.setPriority(CommonConstants.ALERT_PRIORITY_CODE_CRITICAL); alert.setContent(ALERT_TEST_CONTENT); - alert.setTriggerTimes(1); + alert.setAlertDefineId(200L); + alert.setTimes(2); + alert.setStatus((byte) 0); alert.setFirstAlarmTime(System.currentTimeMillis()); alert.setLastAlarmTime(System.currentTimeMillis()); - alert.setPriority(CommonConstants.ALERT_PRIORITY_CODE_CRITICAL); return dispatcherAlarm.sendNoticeMsg(noticeReceiver, null, alert); }