Authored by wangning

update

... ... @@ -397,31 +397,48 @@ public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAcc
@Override
public List<Map> getAlarmData(Integer costThreshold){
String sql = String.format("select mean(cost) from service_access where %s and time > now() - 10s group by hostAddress,context",contextFilter);
String sql = String.format("select count(cost),mean(cost) from service_access where %s and time > now() - 10s group by hostAddress,context;select count(cost) from service_access where %s and time > now() - 10s and cost > %d group by hostAddress,context",contextFilter,contextFilter,costThreshold);
List<Map> list = new ArrayList<>();
list.addAll(getAlarmData(InfluxDBContants.AWS,sql,costThreshold));
list.addAll(getAlarmData(InfluxDBContants.Q_CLOUD,sql,costThreshold));
list.addAll(getAlarmData(InfluxDBContants.AWS,sql));
list.addAll(getAlarmData(InfluxDBContants.Q_CLOUD,sql));
return list;
}
private List<Map> getAlarmData(String source,String sql,Integer costThreshold){
private List<Map> getAlarmData(String source,String sql){
List<Map> returnList = new ArrayList<>();
QueryResult queryResult = query(source, sql, InfluxDBContants.YOHO_EVENT);
QueryResult.Result rel = queryResult.getResults().get(0);
List<QueryResult.Series> listSeries = rel.getSeries();
if(listSeries == null)
QueryResult.Result rel1 = queryResult.getResults().get(0);
QueryResult.Result rel2 = queryResult.getResults().get(1);
List<QueryResult.Series> listSeries1 = rel1.getSeries();
List<QueryResult.Series> listSeries2 = rel2.getSeries();
if(listSeries1 == null || listSeries2 == null)
return returnList;
for(QueryResult.Series series : listSeries){
String hostAddress = series.getTags().get("hostAddress");
String serviceName = series.getTags().get("context");
Double mean = (Double)series.getValues().get(0).get(1);
if(mean > costThreshold){
Map map = new HashMap();
map.put("hostAddress",hostAddress);
map.put("serviceName",serviceName);
map.put("mean",mean);
returnList.add(map);
Map<String,QueryResult.Series> map1 = new HashMap();
for(QueryResult.Series series1 : listSeries1){
String hostAddress = series1.getTags().get("hostAddress");
String serviceName = series1.getTags().get("context");
map1.put(hostAddress+"_" + serviceName,series1);
}
for(QueryResult.Series series2 : listSeries2){
String hostAddress = series2.getTags().get("hostAddress");
String serviceName = series2.getTags().get("context");
Double count2 = (Double)series2.getValues().get(0).get(1);
QueryResult.Series series1 = map1.get(hostAddress+"_" + serviceName);
if(series1 != null){
Double count1 = (Double)series1.getValues().get(0).get(1);
Double mean = (Double)series1.getValues().get(0).get(2);
Double ratio = count2 / count1;
if(ratio >= 0.95){
Map map = new HashMap();
map.put("hostAddress",hostAddress);
map.put("serviceName",serviceName);
map.put("mean",mean);
map.put("ratio",ratio);
returnList.add(map);
}
}
}
return returnList;
... ...
... ... @@ -376,13 +376,13 @@ public class NewJavaApiInfoServiceImpl implements NewJavaApiInfoService {
//获取报警阀值
Integer costThreshold = snsMobileConfig.getJavaServiceCostThreshold();
if(costThreshold == null){
costThreshold = 200;
costThreshold = 150;
}
String nowString = DateTime.now().toString("yyyy-MM-dd HH:mm:ss");
List<Map> list = serviceAccessMapper.getAlarmData(costThreshold);
//获取cpu、内存、带宽使用情况
Map vmInfoMap = vmInfoMapper.getVMInfo();
String costMsgContentTemplate = "时间:%s,服务名:%s,ip:%s,平均耗时%dms,CPU使用率:%s,可用内存/总内存(MB):%s";
String costMsgContentTemplate = "时间:%s,服务名:%s,ip:%s,超过%dms的请求占比%s,平均耗时%dms,CPU使用率:%s,可用内存/总内存(MB):%s";
String bandWidthContentTemplate = "时间:%s,ip:%s,输入带宽 :%.2fMbps,输出带宽:%.2fMbps";
for(Map map : list){
String hostAddress = (String)map.get("hostAddress");
... ... @@ -391,6 +391,7 @@ public class NewJavaApiInfoServiceImpl implements NewJavaApiInfoService {
}
String serviceName = (String)map.get("serviceName");
Integer mean = ((Double)map.get("mean")).intValue();
String ratioStr = "" + (int)(((Double)map.get("ratio")) * 100) + "%";
String cpuRate = "";
String memoryRate = "";
List vmList = (List)vmInfoMap.get(hostAddress);
... ... @@ -399,7 +400,7 @@ public class NewJavaApiInfoServiceImpl implements NewJavaApiInfoService {
memoryRate = "" + vmList.get(2) + "/" + vmList.get(3);
}
String costMsgContent = String.format(costMsgContentTemplate,nowString,serviceName,hostAddress,mean,cpuRate,memoryRate);
String costMsgContent = String.format(costMsgContentTemplate,nowString,serviceName,hostAddress,costThreshold,ratioStr,mean,cpuRate,memoryRate);
log.info("sendAlarmMsg costMsgContent is {}",costMsgContent);
boolean result = alarmMsgService.sendSms("JavaServiceAlarm",costMsgContent,snsMobileConfig.getJavaServiceMobile());
log.info("sendAlarmMsg result is {}",result);
... ...
... ... @@ -13,6 +13,6 @@ redis_exception_mobile=13515100825,15905144483,18751986615,18502542319
java_service_alarm_mobile=13515100825,15905144483,18751986615,18502542319,18614066537
java_service_alarm_flag=true
java_service_alarm_cost_threshold=200
java_service_alarm_cost_threshold=150
... ...
... ... @@ -17,6 +17,6 @@ redis_exception_mobile=15905144483
java_service_alarm_mobile=18614066537
java_service_alarm_flag=true
java_service_alarm_cost_threshold=200
java_service_alarm_cost_threshold=150
... ...