Skip to content

Commit 74a0771

Browse files
committed
refactor the instance alert
1 parent 45b7dd1 commit 74a0771

File tree

39 files changed

+2408
-955
lines changed

39 files changed

+2408
-955
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.sohu.cache.alert.bean;
2+
3+
import com.sohu.cache.entity.InstanceInfo;
4+
import com.sohu.cache.entity.StandardStats;
5+
6+
/**
7+
* 报警基础数据
8+
* @author leifu
9+
* @Date 2017年6月16日
10+
* @Time 下午2:19:10
11+
*/
12+
public class AlertConfigBaseData {
13+
/**
14+
* 基准数据
15+
*/
16+
private StandardStats standardStats;
17+
18+
/**
19+
* 实例信息
20+
*/
21+
private InstanceInfo instanceInfo;
22+
23+
public StandardStats getStandardStats() {
24+
return standardStats;
25+
}
26+
27+
public void setStandardStats(StandardStats standardStats) {
28+
this.standardStats = standardStats;
29+
}
30+
31+
public InstanceInfo getInstanceInfo() {
32+
return instanceInfo;
33+
}
34+
35+
public void setInstanceInfo(InstanceInfo instanceInfo) {
36+
this.instanceInfo = instanceInfo;
37+
}
38+
39+
}
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
package com.sohu.cache.alert.strategy;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.Map.Entry;
6+
7+
import org.apache.commons.collections.MapUtils;
8+
import org.apache.commons.lang.math.NumberUtils;
9+
10+
import com.sohu.cache.alert.bean.AlertConfigBaseData;
11+
import com.sohu.cache.entity.InstanceAlertConfig;
12+
import com.sohu.cache.entity.InstanceAlertValueResult;
13+
import com.sohu.cache.entity.InstanceInfo;
14+
import com.sohu.cache.entity.StandardStats;
15+
import com.sohu.cache.redis.enums.InstanceAlertCompareTypeEnum;
16+
import com.sohu.cache.util.JsonUtil;
17+
18+
/**
19+
* @author leifu
20+
* @Date 2017年6月2日
21+
* @Time 下午3:24:53
22+
*/
23+
public abstract class AlertConfigStrategy {
24+
25+
protected final static String MB_STRING = "MB";
26+
protected final static String EMPTY = "";
27+
28+
/**
29+
* 检查配置
30+
*
31+
* @param instanceAlertConfig
32+
* @param alertConfigBaseData
33+
*/
34+
public abstract List<InstanceAlertValueResult> checkConfig(InstanceAlertConfig instanceAlertConfig,
35+
AlertConfigBaseData alertConfigBaseData);
36+
37+
/**
38+
* 比较long类型
39+
*
40+
* @param instanceAlertConfig 报警配置
41+
* @param currentValue 当前值
42+
* @return
43+
*/
44+
protected boolean isCompareLongRight(InstanceAlertConfig instanceAlertConfig, long currentValue) {
45+
long alertValue = NumberUtils.toLong(instanceAlertConfig.getAlertValue());
46+
int compareType = instanceAlertConfig.getCompareType();
47+
if (compareType == InstanceAlertCompareTypeEnum.LESS_THAN.getValue() && currentValue < alertValue) {
48+
return false;
49+
} else if (compareType == InstanceAlertCompareTypeEnum.MORE_THAN.getValue() && currentValue > alertValue) {
50+
return false;
51+
} else if (compareType == InstanceAlertCompareTypeEnum.EQUAL.getValue() && currentValue == alertValue) {
52+
return false;
53+
} else if (compareType == InstanceAlertCompareTypeEnum.NOT_EQUAL.getValue() && currentValue != alertValue) {
54+
return false;
55+
}
56+
return true;
57+
}
58+
59+
/**
60+
* 比较int类型
61+
*
62+
* @param instanceAlertConfig 报警配置
63+
* @param currentValue 当前值
64+
* @return
65+
*/
66+
protected boolean isCompareIntRight(InstanceAlertConfig instanceAlertConfig, int currentValue) {
67+
int alertValue = NumberUtils.toInt(instanceAlertConfig.getAlertValue());
68+
int compareType = instanceAlertConfig.getCompareType();
69+
if (compareType == InstanceAlertCompareTypeEnum.LESS_THAN.getValue() && currentValue < alertValue) {
70+
return false;
71+
} else if (compareType == InstanceAlertCompareTypeEnum.MORE_THAN.getValue() && currentValue > alertValue) {
72+
return false;
73+
} else if (compareType == InstanceAlertCompareTypeEnum.EQUAL.getValue() && currentValue == alertValue) {
74+
return false;
75+
} else if (compareType == InstanceAlertCompareTypeEnum.NOT_EQUAL.getValue() && currentValue != alertValue) {
76+
return false;
77+
}
78+
return true;
79+
}
80+
81+
/**
82+
* 比较double类型
83+
*
84+
* @param instanceAlertConfig 报警配置
85+
* @param currentValue 当前值
86+
* @return
87+
*/
88+
protected boolean isCompareDoubleRight(InstanceAlertConfig instanceAlertConfig, double currentValue) {
89+
double alertValue = NumberUtils.toDouble(instanceAlertConfig.getAlertValue());
90+
int compareType = instanceAlertConfig.getCompareType();
91+
if (compareType == InstanceAlertCompareTypeEnum.LESS_THAN.getValue() && currentValue < alertValue) {
92+
return false;
93+
} else if (compareType == InstanceAlertCompareTypeEnum.MORE_THAN.getValue() && currentValue > alertValue) {
94+
return false;
95+
} else if (compareType == InstanceAlertCompareTypeEnum.EQUAL.getValue() && currentValue == alertValue) {
96+
return false;
97+
} else if (compareType == InstanceAlertCompareTypeEnum.NOT_EQUAL.getValue() && currentValue != alertValue) {
98+
return false;
99+
}
100+
return true;
101+
}
102+
103+
/**
104+
* 比较字符串类型
105+
*
106+
* @param instanceAlertConfig 报警配置
107+
* @param currentValue 当期值
108+
* @return
109+
*/
110+
protected boolean isCompareStringRight(InstanceAlertConfig instanceAlertConfig, String currentValue) {
111+
String alertValue = instanceAlertConfig.getAlertValue();
112+
int compareType = instanceAlertConfig.getCompareType();
113+
if (compareType == InstanceAlertCompareTypeEnum.EQUAL.getValue() && currentValue.equals(alertValue)) {
114+
return false;
115+
} else if (compareType == InstanceAlertCompareTypeEnum.NOT_EQUAL.getValue()
116+
&& !currentValue.equals(alertValue)) {
117+
return false;
118+
}
119+
return true;
120+
}
121+
122+
/**
123+
* 生成instance级别报警文案
124+
* @param instanceAlertConfig
125+
* @param instanceInfo
126+
* @param currentValue
127+
* @param unit
128+
* @return
129+
*/
130+
protected String genInstanceAlertText(InstanceAlertConfig instanceAlertConfig, InstanceInfo instanceInfo,
131+
String currentValue, String unit) {
132+
String configKey = instanceAlertConfig.getAlertConfig();
133+
String configValue = instanceAlertConfig.getAlertValue();
134+
String compareTypeInfo = InstanceAlertCompareTypeEnum
135+
.getInstanceAlertCompareTypeEnum(instanceAlertConfig.getCompareType()).getInfo();
136+
StringBuilder alertText = new StringBuilder();
137+
alertText.append(instanceInfo.getHostPort());
138+
alertText.append(",报警项:").append(configKey);
139+
alertText.append("=").append(currentValue).append(unit).append(",");
140+
alertText.append(compareTypeInfo);
141+
alertText.append("报警阈值:").append(configValue).append(unit);
142+
return alertText.toString();
143+
}
144+
145+
/**
146+
* 获取全量统计项中的内容
147+
* @param redisInfo
148+
* @param attribute
149+
* @return
150+
*/
151+
protected static Object getValueFromRedisInfo(StandardStats standardStats, String attribute) {
152+
if (standardStats == null) {
153+
return null;
154+
}
155+
// 转换成Map
156+
Map<String, Object> infoMap = JsonUtil.fromJson(standardStats.getInfoJson(), Map.class);
157+
if (MapUtils.isEmpty(infoMap)) {
158+
return null;
159+
}
160+
for (Entry<String, Object> entry : infoMap.entrySet()) {
161+
Object object = entry.getValue();
162+
// 转换成Map<String, Map<String,Object>>
163+
if (!(object instanceof Map)) {
164+
continue;
165+
}
166+
Map<String, Object> sectionInfoMap = (Map<String, Object>) object;
167+
if (sectionInfoMap != null && sectionInfoMap.containsKey(attribute)) {
168+
return MapUtils.getObject(sectionInfoMap, attribute);
169+
}
170+
}
171+
return null;
172+
}
173+
174+
/**
175+
* 获取差值统计项中的内容
176+
* @param redisInfo
177+
* @param attribute
178+
* @return
179+
*/
180+
protected static Object getValueFromDiffInfo(StandardStats standardStats, String attribute) {
181+
if (standardStats == null) {
182+
return null;
183+
}
184+
Map<String, Object> diffInfoMap = JsonUtil.fromJson(standardStats.getDiffJson(), Map.class);
185+
if (MapUtils.isEmpty(diffInfoMap)) {
186+
return null;
187+
}
188+
return MapUtils.getObject(diffInfoMap, attribute);
189+
}
190+
191+
/**
192+
* 获取cluster info统计项中的内容
193+
* @param redisInfo
194+
* @param attribute
195+
* @return
196+
*/
197+
protected static Object getValueFromClusterInfo(StandardStats standardStats, String attribute) {
198+
if (standardStats == null) {
199+
return null;
200+
}
201+
Map<String, Object> clusterInfoMap = JsonUtil.fromJson(standardStats.getClusterInfoJson(), Map.class);
202+
if (MapUtils.isEmpty(clusterInfoMap)) {
203+
return null;
204+
}
205+
return MapUtils.getObject(clusterInfoMap, attribute);
206+
}
207+
208+
/**
209+
* 把字节变为兆
210+
* @param value
211+
* @return
212+
*/
213+
protected long changeByteToMB(long value) {
214+
return value / 1024 / 1024;
215+
}
216+
217+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.sohu.cache.alert.strategy;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import org.apache.commons.lang.math.NumberUtils;
7+
8+
import com.sohu.cache.alert.bean.AlertConfigBaseData;
9+
import com.sohu.cache.entity.InstanceAlertConfig;
10+
import com.sohu.cache.entity.InstanceAlertValueResult;
11+
import com.sohu.cache.entity.InstanceInfo;
12+
import com.sohu.cache.redis.enums.RedisInfoEnum;
13+
14+
/**
15+
* aof当前尺寸检测
16+
* @author leifu
17+
* @Date 2017年6月16日
18+
* @Time 下午2:34:10
19+
*/
20+
public class AofCurrentSizeAlertStrategy extends AlertConfigStrategy {
21+
22+
@Override
23+
public List<InstanceAlertValueResult> checkConfig(InstanceAlertConfig instanceAlertConfig, AlertConfigBaseData alertConfigBaseData) {
24+
Object object = getValueFromRedisInfo(alertConfigBaseData.getStandardStats(), RedisInfoEnum.aof_current_size.getValue());
25+
// 没有配置Aof
26+
if (object == null) {
27+
return null;
28+
}
29+
long aofCurrentSize = NumberUtils.toLong(object.toString());
30+
aofCurrentSize = changeByteToMB(aofCurrentSize);
31+
boolean compareRight = isCompareLongRight(instanceAlertConfig, aofCurrentSize);
32+
if (compareRight) {
33+
return null;
34+
}
35+
InstanceInfo instanceInfo = alertConfigBaseData.getInstanceInfo();
36+
return Arrays.asList(new InstanceAlertValueResult(instanceAlertConfig, instanceInfo, String.valueOf(aofCurrentSize),
37+
instanceInfo.getAppId(), MB_STRING));
38+
}
39+
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.sohu.cache.alert.strategy;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import org.apache.commons.lang.math.NumberUtils;
7+
8+
import com.sohu.cache.alert.bean.AlertConfigBaseData;
9+
import com.sohu.cache.entity.InstanceAlertConfig;
10+
import com.sohu.cache.entity.InstanceAlertValueResult;
11+
import com.sohu.cache.entity.InstanceInfo;
12+
import com.sohu.cache.redis.enums.RedisInfoEnum;
13+
14+
/**
15+
* 客户端输入缓冲区最大buffer
16+
*
17+
* @author leifu
18+
* @Date 2017年6月16日
19+
* @Time 下午2:34:10
20+
*/
21+
public class ClientBiggestInputBufAlertStrategy extends AlertConfigStrategy {
22+
@Override
23+
public List<InstanceAlertValueResult> checkConfig(InstanceAlertConfig instanceAlertConfig, AlertConfigBaseData alertConfigBaseData) {
24+
Object object = getValueFromRedisInfo(alertConfigBaseData.getStandardStats(), RedisInfoEnum.client_biggest_input_buf.getValue());
25+
if (object == null) {
26+
return null;
27+
}
28+
// 关系比对
29+
long clientBiggestInputBuf = NumberUtils.toLong(object.toString()) ;
30+
clientBiggestInputBuf = changeByteToMB(clientBiggestInputBuf);
31+
boolean compareRight = isCompareLongRight(instanceAlertConfig, clientBiggestInputBuf);
32+
if (compareRight) {
33+
return null;
34+
}
35+
InstanceInfo instanceInfo = alertConfigBaseData.getInstanceInfo();
36+
return Arrays.asList(new InstanceAlertValueResult(instanceAlertConfig, instanceInfo, String.valueOf(clientBiggestInputBuf),
37+
instanceInfo.getAppId(), MB_STRING));
38+
}
39+
40+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.sohu.cache.alert.strategy;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import org.apache.commons.lang.math.NumberUtils;
7+
8+
import com.sohu.cache.alert.bean.AlertConfigBaseData;
9+
import com.sohu.cache.entity.InstanceAlertConfig;
10+
import com.sohu.cache.entity.InstanceAlertValueResult;
11+
import com.sohu.cache.entity.InstanceInfo;
12+
import com.sohu.cache.redis.enums.RedisInfoEnum;
13+
14+
/**
15+
* 客户端输出缓冲区最大队列长度
16+
*
17+
* @author leifu
18+
* @Date 2017年6月16日
19+
* @Time 下午2:34:10
20+
*/
21+
public class ClientLongestOutputListAlertStrategy extends AlertConfigStrategy {
22+
@Override
23+
public List<InstanceAlertValueResult> checkConfig(InstanceAlertConfig instanceAlertConfig, AlertConfigBaseData alertConfigBaseData) {
24+
Object object = getValueFromRedisInfo(alertConfigBaseData.getStandardStats(), RedisInfoEnum.client_longest_output_list.getValue());
25+
if (object == null) {
26+
return null;
27+
}
28+
// 关系比对
29+
long clientLongestOutputList = NumberUtils.toLong(object.toString());
30+
boolean compareRight = isCompareLongRight(instanceAlertConfig, clientLongestOutputList);
31+
if (compareRight) {
32+
return null;
33+
}
34+
InstanceInfo instanceInfo = alertConfigBaseData.getInstanceInfo();
35+
return Arrays.asList(new InstanceAlertValueResult(instanceAlertConfig, instanceInfo, String.valueOf(clientLongestOutputList),
36+
instanceInfo.getAppId(), EMPTY));
37+
}
38+
39+
}

0 commit comments

Comments
 (0)