提交 06a97ef0 编写于 作者: Z zengqiao

FIX N9e Mon

上级 bc4dac9c
......@@ -55,6 +55,10 @@
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
......
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<String, NotifyGroup> 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<Integer> 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<Strategy> convert2StrategyList(List<N9eStrategy> n9eStrategyList) {
public static List<Strategy> convert2StrategyList(List<N9eStrategy> n9eStrategyList,
Map<String, NotifyGroup> notifyGroupMap) {
if (n9eStrategyList == null || n9eStrategyList.isEmpty()) {
return new ArrayList<>();
}
List<Strategy> 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<String, NotifyGroup> notifyGroupMap) {
if (n9eStrategy == null) {
return null;
}
if (ValidateUtils.isNull(notifyGroupMap)) {
notifyGroupMap = new HashMap<>();
}
Map<Integer, NotifyGroup> 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<String> 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));
......
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<String, NotifyGroup> 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<NotifyGroup> 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<NotifyGroup> 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<String, NotifyGroup> getNotifyGroupsFromCacheFirst() {
Map<String, NotifyGroup> notifyGroupMap = NOTIFY_GROUP_CACHE.asMap();
if (ValidateUtils.isEmptyMap(notifyGroupMap)) {
this.getNotifyGroups();
}
return NOTIFY_GROUP_CACHE.asMap();
}
private Map<String, String> buildHeader() {
Map<String, String> header = new HashMap<>(2);
header.put("Content-Type", "application/json");
......
......@@ -36,11 +36,11 @@ public class N9eStrategy {
private Integer recovery_notify;
private List<N9eStrategyAlertUpgrade> alert_upgrade = new ArrayList<>();
private N9eStrategyAlertUpgrade alert_upgrade;
private List<Integer> converge;
private List<String> notify_group;
private List<Integer> notify_group;
private List<Integer> notify_user;
......@@ -142,11 +142,11 @@ public class N9eStrategy {
this.recovery_notify = recovery_notify;
}
public List<N9eStrategyAlertUpgrade> getAlert_upgrade() {
public N9eStrategyAlertUpgrade getAlert_upgrade() {
return alert_upgrade;
}
public void setAlert_upgrade(List<N9eStrategyAlertUpgrade> 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<String> getNotify_group() {
public List<Integer> getNotify_group() {
return notify_group;
}
public void setNotify_group(List<String> notify_group) {
public void setNotify_group(List<Integer> notify_group) {
this.notify_group = notify_group;
}
......
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<Integer> users;
private List<Integer> users = new ArrayList<>();
private List<String> groups;
private List<Integer> groups = new ArrayList<>();
public Integer getDuration() {
return duration;
......@@ -39,11 +40,11 @@ public class N9eStrategyAlertUpgrade {
this.users = users;
}
public List<String> getGroups() {
public List<Integer> getGroups() {
return groups;
}
public void setGroups(List<String> groups) {
public void setGroups(List<Integer> groups) {
this.groups = groups;
}
......
......@@ -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:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册