diff --git a/kafka-manager-extends/kafka-manager-monitor/pom.xml b/kafka-manager-extends/kafka-manager-monitor/pom.xml index 4def3e6670fe9d79d8e5690ab8d37964c757906d..cba5366a8490ab3d8aad2fe70cd5b826bb872bd7 100644 --- a/kafka-manager-extends/kafka-manager-monitor/pom.xml +++ b/kafka-manager-extends/kafka-manager-monitor/pom.xml @@ -55,6 +55,10 @@ commons-lang commons-lang + + com.github.ben-manes.caffeine + caffeine + org.springframework diff --git a/kafka-manager-extends/kafka-manager-monitor/src/main/java/com/xiaojukeji/kafka/manager/monitor/component/n9e/N9eConverter.java b/kafka-manager-extends/kafka-manager-monitor/src/main/java/com/xiaojukeji/kafka/manager/monitor/component/n9e/N9eConverter.java index 543af302fe8005fc3a8ebd116fc51dd54bc63eeb..7735caf85ec58bbd7e71ba147bde94087140dcc7 100644 --- a/kafka-manager-extends/kafka-manager-monitor/src/main/java/com/xiaojukeji/kafka/manager/monitor/component/n9e/N9eConverter.java +++ b/kafka-manager-extends/kafka-manager-monitor/src/main/java/com/xiaojukeji/kafka/manager/monitor/component/n9e/N9eConverter.java @@ -1,12 +1,11 @@ package com.xiaojukeji.kafka.manager.monitor.component.n9e; import com.xiaojukeji.kafka.manager.common.utils.ListUtils; +import com.xiaojukeji.kafka.manager.common.utils.ValidateUtils; import com.xiaojukeji.kafka.manager.monitor.common.entry.*; import com.xiaojukeji.kafka.manager.monitor.component.n9e.entry.*; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.*; /** * @author zengqiao @@ -31,13 +30,20 @@ public class N9eConverter { return n9ePointList; } - public static N9eStrategy convert2N9eStrategy(Strategy strategy, Integer monitorN9eNid) { + public static N9eStrategy convert2N9eStrategy(Strategy strategy, + Integer monitorN9eNid, + Map notifyGroupMap) { if (strategy == null) { return null; } + if (ValidateUtils.isNull(notifyGroupMap)) { + notifyGroupMap = new HashMap<>(); + } N9eStrategy n9eStrategy = new N9eStrategy(); - n9eStrategy.setId(strategy.getId().intValue()); + if (!ValidateUtils.isNull(strategy.getId())) { + n9eStrategy.setId(strategy.getId().intValue()); + } n9eStrategy.setCategory(1); n9eStrategy.setName(strategy.getName()); n9eStrategy.setNid(monitorN9eNid); @@ -72,7 +78,17 @@ public class N9eConverter { StrategyAction strategyAction = strategy.getStrategyActionList().get(0); n9eStrategy.setConverge(ListUtils.string2IntList(strategyAction.getConverge())); - n9eStrategy.setNotify_group(ListUtils.string2StrList(strategyAction.getNotifyGroup())); + + List notifyGroups = new ArrayList<>(); + for (String name: ListUtils.string2StrList(strategyAction.getNotifyGroup())) { + NotifyGroup notifyGroup = notifyGroupMap.get(name); + if (ValidateUtils.isNull(notifyGroup)) { + continue; + } + notifyGroups.add(notifyGroup.getId().intValue()); + } + n9eStrategy.setNotify_group(notifyGroups); + n9eStrategy.setNotify_user(new ArrayList<>()); n9eStrategy.setCallback(strategyAction.getCallback()); n9eStrategy.setEnable_stime("00:00"); @@ -80,26 +96,36 @@ public class N9eConverter { n9eStrategy.setEnable_days_of_week(ListUtils.string2IntList(strategy.getPeriodDaysOfWeek())); n9eStrategy.setNeed_upgrade(0); - n9eStrategy.setAlert_upgrade(new ArrayList<>()); + n9eStrategy.setAlert_upgrade(new N9eStrategyAlertUpgrade()); return n9eStrategy; } - public static List convert2StrategyList(List n9eStrategyList) { + public static List convert2StrategyList(List n9eStrategyList, + Map notifyGroupMap) { if (n9eStrategyList == null || n9eStrategyList.isEmpty()) { return new ArrayList<>(); } List strategyList = new ArrayList<>(); for (N9eStrategy n9eStrategy: n9eStrategyList) { - strategyList.add(convert2Strategy(n9eStrategy)); + strategyList.add(convert2Strategy(n9eStrategy, notifyGroupMap)); } return strategyList; } - public static Strategy convert2Strategy(N9eStrategy n9eStrategy) { + public static Strategy convert2Strategy(N9eStrategy n9eStrategy, Map notifyGroupMap) { if (n9eStrategy == null) { return null; } + if (ValidateUtils.isNull(notifyGroupMap)) { + notifyGroupMap = new HashMap<>(); + } + + Map newNotifyGroupMap = new HashMap<>(notifyGroupMap.size()); + for (NotifyGroup notifyGroup: notifyGroupMap.values()) { + newNotifyGroupMap.put(notifyGroup.getId().intValue(), notifyGroup); + } + Strategy strategy = new Strategy(); strategy.setId(n9eStrategy.getId().longValue()); strategy.setName(n9eStrategy.getName()); @@ -130,7 +156,17 @@ public class N9eConverter { strategy.setStrategyFilterList(strategyFilterList); StrategyAction strategyAction = new StrategyAction(); - strategyAction.setNotifyGroup(ListUtils.strList2String(n9eStrategy.getNotify_group())); + + List notifyGroups = new ArrayList<>(); + for (Integer id: n9eStrategy.getNotify_group()) { + NotifyGroup notifyGroup = newNotifyGroupMap.get(id); + if (ValidateUtils.isNull(notifyGroup)) { + continue; + } + notifyGroups.add(notifyGroup.getName()); + } + strategyAction.setNotifyGroup(ListUtils.strList2String(notifyGroups)); + strategyAction.setConverge(ListUtils.intList2String(n9eStrategy.getConverge())); strategyAction.setCallback(n9eStrategy.getCallback()); strategy.setStrategyActionList(Arrays.asList(strategyAction)); diff --git a/kafka-manager-extends/kafka-manager-monitor/src/main/java/com/xiaojukeji/kafka/manager/monitor/component/n9e/N9eService.java b/kafka-manager-extends/kafka-manager-monitor/src/main/java/com/xiaojukeji/kafka/manager/monitor/component/n9e/N9eService.java index 9dc8d4e575773afa31b19b0554341769989ceddc..2d4b041ce6593093f53f5f598e074dcb56ff6983 100644 --- a/kafka-manager-extends/kafka-manager-monitor/src/main/java/com/xiaojukeji/kafka/manager/monitor/component/n9e/N9eService.java +++ b/kafka-manager-extends/kafka-manager-monitor/src/main/java/com/xiaojukeji/kafka/manager/monitor/component/n9e/N9eService.java @@ -1,6 +1,8 @@ package com.xiaojukeji.kafka.manager.monitor.component.n9e; import com.alibaba.fastjson.JSON; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; import com.xiaojukeji.kafka.manager.common.utils.HttpUtils; import com.xiaojukeji.kafka.manager.common.utils.ValidateUtils; import com.xiaojukeji.kafka.manager.monitor.component.AbstractMonitorService; @@ -14,6 +16,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.util.*; +import java.util.concurrent.TimeUnit; /** * 夜莺 @@ -36,6 +39,13 @@ public class N9eService extends AbstractMonitorService { @Value("${monitor.n9e.sink.base-url}") private String monitorN9eSinkBaseUrl; + @Value("${monitor.n9e.rdb.base-url}") + private String monitorN9eRdbBaseUrl; + + private static final Cache NOTIFY_GROUP_CACHE = Caffeine.newBuilder() + .maximumSize(100000) + .expireAfterWrite(60, TimeUnit.MINUTES).build(); + /** * 告警策略 */ @@ -75,7 +85,7 @@ public class N9eService extends AbstractMonitorService { /** * 告警组 */ - private static final String ALL_NOTIFY_GROUP_URL = "/api/rdb/teams/all"; + private static final String ALL_NOTIFY_GROUP_URL = "/api/rdb/teams/all?limit=10000"; /** * 监控策略的增删改查 @@ -86,7 +96,7 @@ public class N9eService extends AbstractMonitorService { try { response = HttpUtils.postForString( monitorN9eMonBaseUrl + STRATEGY_ADD_URL, - JSON.toJSONString(N9eConverter.convert2N9eStrategy(strategy, monitorN9eNid)), + JSON.toJSONString(N9eConverter.convert2N9eStrategy(strategy, monitorN9eNid, getNotifyGroupsFromCacheFirst())), buildHeader() ); N9eResult n9eResult = JSON.parseObject(response, N9eResult.class); @@ -94,7 +104,7 @@ public class N9eService extends AbstractMonitorService { LOGGER.error("create strategy failed, strategy:{} response:{}.", strategy, response); return null; } - return (Integer) n9eResult.getDat(); + return JSON.parseObject(JSON.toJSONString(n9eResult.getDat())).getInteger("id"); } catch (Exception e) { LOGGER.error("create strategy failed, strategy:{} response:{}.", strategy, response, e); } @@ -131,7 +141,7 @@ public class N9eService extends AbstractMonitorService { try { response = HttpUtils.putForString( monitorN9eMonBaseUrl + STRATEGY_MODIFY_URL, - JSON.toJSONString(N9eConverter.convert2N9eStrategy(strategy, monitorN9eNid)), + JSON.toJSONString(N9eConverter.convert2N9eStrategy(strategy, monitorN9eNid, getNotifyGroupsFromCacheFirst())), buildHeader() ); N9eResult n9eResult = JSON.parseObject(response, N9eResult.class); @@ -159,7 +169,7 @@ public class N9eService extends AbstractMonitorService { LOGGER.error("get monitor strategies failed, response:{}.", response); return new ArrayList<>(); } - return N9eConverter.convert2StrategyList(JSON.parseArray(JSON.toJSONString(n9eResult.getDat()), N9eStrategy.class)); + return N9eConverter.convert2StrategyList(JSON.parseArray(JSON.toJSONString(n9eResult.getDat()), N9eStrategy.class), getNotifyGroupsFromCacheFirst()); } catch (Exception e) { LOGGER.error("get monitor strategies failed, response:{}.", response, e); } @@ -178,7 +188,7 @@ public class N9eService extends AbstractMonitorService { LOGGER.error("get monitor strategy failed, response:{}.", response); return null; } - return N9eConverter.convert2Strategy(JSON.parseObject(JSON.toJSONString(n9eResult.getDat()), N9eStrategy.class)); + return N9eConverter.convert2Strategy(JSON.parseObject(JSON.toJSONString(n9eResult.getDat()), N9eStrategy.class), getNotifyGroupsFromCacheFirst()); } catch (Exception e) { LOGGER.error("get monitor strategy failed, response:{}.", response, e); } @@ -254,19 +264,34 @@ public class N9eService extends AbstractMonitorService { public List getNotifyGroups() { String response = null; try { - response = HttpUtils.get(monitorN9eMonBaseUrl + ALL_NOTIFY_GROUP_URL, new HashMap<>(0), buildHeader()); + response = HttpUtils.get(monitorN9eRdbBaseUrl + ALL_NOTIFY_GROUP_URL, new HashMap<>(0), buildHeader()); N9eResult n9eResult = JSON.parseObject(response, N9eResult.class); if (!ValidateUtils.isBlank(n9eResult.getErr())) { LOGGER.error("get notify group failed, response:{}.", response); return new ArrayList<>(); } - return N9eConverter.convert2NotifyGroupList(JSON.parseObject(JSON.toJSONString(n9eResult.getDat()), N9eNotifyGroup.class)); + List notifyGroupList = N9eConverter.convert2NotifyGroupList(JSON.parseObject(JSON.toJSONString(n9eResult.getDat()), N9eNotifyGroup.class)); + if (ValidateUtils.isEmptyList(notifyGroupList)) { + return new ArrayList<>(); + } + for (NotifyGroup notifyGroup: notifyGroupList) { + NOTIFY_GROUP_CACHE.put(notifyGroup.getName(), notifyGroup); + } + return notifyGroupList; } catch (Exception e) { LOGGER.error("get notify group failed, response:{}.", response, e); } return new ArrayList<>(); } + private Map getNotifyGroupsFromCacheFirst() { + Map notifyGroupMap = NOTIFY_GROUP_CACHE.asMap(); + if (ValidateUtils.isEmptyMap(notifyGroupMap)) { + this.getNotifyGroups(); + } + return NOTIFY_GROUP_CACHE.asMap(); + } + private Map buildHeader() { Map header = new HashMap<>(2); header.put("Content-Type", "application/json"); diff --git a/kafka-manager-extends/kafka-manager-monitor/src/main/java/com/xiaojukeji/kafka/manager/monitor/component/n9e/entry/N9eStrategy.java b/kafka-manager-extends/kafka-manager-monitor/src/main/java/com/xiaojukeji/kafka/manager/monitor/component/n9e/entry/N9eStrategy.java index 420b825cf2c21167c3b3b344517f331b15adf1ac..de91383f7db487bd03b5ada952b067d5468b32a9 100644 --- a/kafka-manager-extends/kafka-manager-monitor/src/main/java/com/xiaojukeji/kafka/manager/monitor/component/n9e/entry/N9eStrategy.java +++ b/kafka-manager-extends/kafka-manager-monitor/src/main/java/com/xiaojukeji/kafka/manager/monitor/component/n9e/entry/N9eStrategy.java @@ -36,11 +36,11 @@ public class N9eStrategy { private Integer recovery_notify; - private List alert_upgrade = new ArrayList<>(); + private N9eStrategyAlertUpgrade alert_upgrade; private List converge; - private List notify_group; + private List notify_group; private List notify_user; @@ -142,11 +142,11 @@ public class N9eStrategy { this.recovery_notify = recovery_notify; } - public List getAlert_upgrade() { + public N9eStrategyAlertUpgrade getAlert_upgrade() { return alert_upgrade; } - public void setAlert_upgrade(List alert_upgrade) { + public void setAlert_upgrade(N9eStrategyAlertUpgrade alert_upgrade) { this.alert_upgrade = alert_upgrade; } @@ -158,11 +158,11 @@ public class N9eStrategy { this.converge = converge; } - public List getNotify_group() { + public List getNotify_group() { return notify_group; } - public void setNotify_group(List notify_group) { + public void setNotify_group(List notify_group) { this.notify_group = notify_group; } diff --git a/kafka-manager-extends/kafka-manager-monitor/src/main/java/com/xiaojukeji/kafka/manager/monitor/component/n9e/entry/N9eStrategyAlertUpgrade.java b/kafka-manager-extends/kafka-manager-monitor/src/main/java/com/xiaojukeji/kafka/manager/monitor/component/n9e/entry/N9eStrategyAlertUpgrade.java index 594c4762c7e53d87d05eba3aa0e52c84a1735d22..e255b6f5971d048c7a43c9aaddb4379460f414aa 100644 --- a/kafka-manager-extends/kafka-manager-monitor/src/main/java/com/xiaojukeji/kafka/manager/monitor/component/n9e/entry/N9eStrategyAlertUpgrade.java +++ b/kafka-manager-extends/kafka-manager-monitor/src/main/java/com/xiaojukeji/kafka/manager/monitor/component/n9e/entry/N9eStrategyAlertUpgrade.java @@ -1,5 +1,6 @@ package com.xiaojukeji.kafka.manager.monitor.component.n9e.entry; +import java.util.ArrayList; import java.util.List; /** @@ -7,13 +8,13 @@ import java.util.List; * @date 20/10/19 */ public class N9eStrategyAlertUpgrade { - private Integer duration; + private Integer duration = 60; - private Integer level; + private Integer level = 1; - private List users; + private List users = new ArrayList<>(); - private List groups; + private List groups = new ArrayList<>(); public Integer getDuration() { return duration; @@ -39,11 +40,11 @@ public class N9eStrategyAlertUpgrade { this.users = users; } - public List getGroups() { + public List getGroups() { return groups; } - public void setGroups(List groups) { + public void setGroups(List groups) { this.groups = groups; } diff --git a/kafka-manager-web/src/main/resources/application.yml b/kafka-manager-web/src/main/resources/application.yml index c6ea7d43432639cbf546e522a2822807b067489e..0fb0f5a7d6112b04c9987b43561bb77ab9b5929b 100644 --- a/kafka-manager-web/src/main/resources/application.yml +++ b/kafka-manager-web/src/main/resources/application.yml @@ -49,11 +49,13 @@ monitor: enabled: false n9e: nid: 2 - user-token: 1234567890 + user-token: 123456 mon: base-url: http://127.0.0.1:8032 sink: - base-url: http://127.0.0.1:8008 + base-url: http://127.0.0.1:8006 + rdb: + base-url: http://127.0.0.1:80 notify: kafka: