Authored by fanzelei

Merge branch 'master' of http://git.yoho.cn/ops/monitor-service

# Conflicts:
#	monitor-service-influxdb/src/main/java/com/monitor/influxdb/mapper/impl/RedisMapper.java
... ... @@ -58,8 +58,8 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
.addField("mobj_ip", statics.getString("mobj_ip"))
.addField("mobj_id", statics.getIntValue("mobj_id"))
.addField("mobj_port", statics.getString("mobj_port"))
.addField("start", statics.getLongValue("start"))
.addField("end", statics.getLongValue("end"))
.addField("start_time", statics.getLongValue("start"))
.addField("end_time", statics.getLongValue("end"))
.addField("is_exception", statics.getBooleanValue("is_exception"))
.time(System.currentTimeMillis() * 1000000 + random.nextInt(999999), TimeUnit.NANOSECONDS);
... ... @@ -101,8 +101,8 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
QueryResult.Series series = rel.getSeries().get(0);
Boolean is_exception = (Boolean) series.getValues().get(0).get(series.getColumns().indexOf("is_exception"));
long startTime = Math.round((Double) series.getValues().get(0).get(series.getColumns().indexOf("start")));
long endTime = Math.round((Double) series.getValues().get(0).get(series.getColumns().indexOf("end")));
long startTime = Math.round((Double) series.getValues().get(0).get(series.getColumns().indexOf("start_time")));
long endTime = Math.round((Double) series.getValues().get(0).get(series.getColumns().indexOf("end_time")));
long costTime = (endTime - startTime);
String exception = (String) series.getValues().get(0).get(series.getColumns().indexOf("exception"));
... ... @@ -119,25 +119,76 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
javaApiStaticsModel.setStatus(1);
} catch (Exception e) {
e.printStackTrace();
log.warn("select latest java api info failed ", e);
}
return javaApiStaticsModel;
}
/**
* 批量查询
*
* @param influxDBName
* @param paramList
* @return
*/
@Override
public List<JavaApiStaticsModel> selectlatestJavaApiStaticsList(String influxDBName, List<JavaApiStatusReq> paramList) {
//TODO 后期改批量查询
List<JavaApiStaticsModel> javaApiStaticsModels = new ArrayList<JavaApiStaticsModel>();
StringBuilder sb = new StringBuilder();
for (JavaApiStatusReq param : paramList) {
int api_id = param.getServiceId();
int mobj_id = param.getMObjectId();
sb.append("select api_id,mobj_id,start_time,end_time,is_exception,exception from " + InfluxDBContants.YOMO_TB_JAVAAPI + " where time > now() - 1h ");
sb.append(" and api_id=" + api_id);
sb.append(" and mobj_id=" + mobj_id);
sb.append(" order by time desc limit 1;");
}
Query query = new Query(sb.toString(), InfluxDBContants.APP_ALARM);
List<JavaApiStaticsModel> javaApiStaticsModels = new ArrayList<>();
QueryResult result = inluxDBSingle.getInfluxDBByName(InfluxDBContants.ALARM).getInfluxDB().query(query);
for (JavaApiStatusReq param : paramList) {
JavaApiStaticsModel model = selectlatestJavaApiStatics(influxDBName, param);
javaApiStaticsModels.add(model);
for (QueryResult.Result queryResult : result.getResults()) {
if (queryResult.getSeries() == null)
continue;
try {
QueryResult.Series series = queryResult.getSeries().get(0);
Boolean is_exception = (Boolean) series.getValues().get(0).get(series.getColumns().indexOf("is_exception"));
long startTime = Math.round((Double) series.getValues().get(0).get(series.getColumns().indexOf("start_time")));
long endTime = Math.round((Double) series.getValues().get(0).get(series.getColumns().indexOf("end_time")));
long costTime = (endTime - startTime);
int api_id = ((Double) series.getValues().get(0).get(series.getColumns().indexOf("api_id"))).intValue();
int mobj_id = ((Double) series.getValues().get(0).get(series.getColumns().indexOf("mobj_id"))).intValue();
String exception = (String) series.getValues().get(0).get(series.getColumns().indexOf("exception"));
JavaApiStaticsModel javaApiStaticsModel = new JavaApiStaticsModel();
javaApiStaticsModel.setServiceId(api_id);
javaApiStaticsModel.setMObjectId(mobj_id);
javaApiStaticsModel.setStatus(0);
javaApiStaticsModel.setStartTime(DateFormatUtil.parseLongToTimeStr(startTime));
javaApiStaticsModel.setEndTime(DateFormatUtil.parseLongToTimeStr(endTime));
javaApiStaticsModel.setCostTime((int) costTime);
if (is_exception == true) {
javaApiStaticsModel.setStatus(0);
javaApiStaticsModel.setException(exception);
} else
javaApiStaticsModel.setStatus(1);
javaApiStaticsModels.add(javaApiStaticsModel);
} catch (Exception e) {
log.warn("batch select latest java api info failed ", e);
}
}
return javaApiStaticsModels;
}
... ... @@ -189,8 +240,8 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
for (int i = 0; i < size; i++) {
JavaApiStaticsModel javaApiStaticsModel = new JavaApiStaticsModel();
Boolean is_exception = (Boolean) series.getValues().get(i).get(series.getColumns().indexOf("is_exception"));
long startTime = Math.round((Double) series.getValues().get(i).get(series.getColumns().indexOf("start")));
long endTime = Math.round((Double) series.getValues().get(i).get(series.getColumns().indexOf("end")));
long startTime = Math.round((Double) series.getValues().get(i).get(series.getColumns().indexOf("start_time")));
long endTime = Math.round((Double) series.getValues().get(i).get(series.getColumns().indexOf("end_time")));
long costTime = (endTime - startTime);
... ...
package com.monitor.influxdb.mapper.impl;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang.StringUtils;
import org.influxdb.dto.Point;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.monitor.common.contants.InfluxDBContants;
import com.monitor.common.util.QueryResultUtil;
import com.monitor.influxdb.InfluxDBModel;
import com.monitor.influxdb.InfluxDBQuery;
import com.monitor.influxdb.InluxDBSingle;
import com.monitor.influxdb.mapper.IRedisMapper;
import com.monitor.influxdb.model.RedisInfo;
import com.monitor.model.domain.PageBean;
@Component
public class RedisMapper extends InfluxDBQuery implements IRedisMapper {
@Autowired
InluxDBSingle inluxDBSingle;
Random random = new Random();
@Override
public void insert(RedisInfo redis) {
Point point = Point.measurement(InfluxDBContants.REDIS_ALARM)
.addField("hostIp", redis.getHostIp())
.addField("role", redis.getRole())
.addField("connected_clients", redis.getConnected_clients())
.addField("is_slave", redis.getIs_slave())
// .addField("used_memory_peak_human", redis.getUsed_memory_peak_human())
.time(System.currentTimeMillis() * 1000000 + random.nextInt(999999), TimeUnit.NANOSECONDS)
.build();
inluxDBSingle.getInfluxDBByName(InfluxDBContants.ALARM).getInfluxDB()
.write(InfluxDBContants.MIDDLEWARE_ALARM, "default", point);
}
@Override
public int selectCountByCodition(PageBean page) {
StringBuffer buffer = new StringBuffer();
buffer.append("SELECT COUNT(hostIp) FROM " + InfluxDBContants.REDIS_ALARM);
int flag=0;
String hostIp = (String) page.getParams().get("hostIp");
if(StringUtils.isNotEmpty(hostIp)){
flag=1;
buffer.append(" where hostIp='"+hostIp+"'");
}
String recordTime=(String)page.getParams().get("recordTime");
if(StringUtils.isNotBlank(recordTime)){
if(flag==1){
buffer.append(" and time>'"+recordTime+"'");
}else{
buffer.append(" where time>'"+recordTime+"'");
}
}
InfluxDBModel influxDBModel= inluxDBSingle.getInfluxDBByName(InfluxDBContants.ALARM);
QueryResult result= query(influxDBModel.getName(), buffer.toString(), InfluxDBContants.MIDDLEWARE_ALARM);
return QueryResultUtil.getCount(result);
}
@Override
public List<?> selectRedisInfosByCodition(PageBean page) {
StringBuffer buffer = new StringBuffer();
buffer.append("SELECT hostIp,role,connected_clients,used_memory,used_memory_peak_human FROM " + InfluxDBContants.REDIS_ALARM);
int flag = 0;
String hostIp = (String) page.getParams().get("hostIp");
if(StringUtils.isNotEmpty(hostIp)){
buffer.append(" where hostIp='"+hostIp+"'");
}
String recordTime=(String)page.getParams().get("recordTime");
if(StringUtils.isNotBlank(recordTime)){
if(flag==1){
buffer.append(" and time>'"+recordTime+"'");
}else{
buffer.append(" where time>'"+recordTime+"'");
}
}
buffer.append(" ORDER BY time DESC LIMIT " + page.getPageSize() + " OFFSET " + page.getStartIndex());
InfluxDBModel influxDBModel= inluxDBSingle.getInfluxDBByName(InfluxDBContants.ALARM);
QueryResult result= query(influxDBModel.getName(), buffer.toString(), InfluxDBContants.MIDDLEWARE_ALARM);
return QueryResultUtil.getValues(result);
}
}
... ...
... ... @@ -163,7 +163,7 @@ public class JavaApiClient {
public void handleResult(JavaApiStatics rep) {
//TODO 加开关 控制gateway的检测条件
//TODO 后期可以加开关 控制gateway的检测条件
//先做预处理,主要对返回结果格式为{code:200,message:".."}的响应做处理,对code非200的设置异常标志
for (IJavaApiHadnler javaApiHadnler : javaApiHadnlerList) {
if (javaApiHadnler instanceof PreProcessJavaApiHandler) {
... ...
... ... @@ -75,7 +75,7 @@ public class JavaApiStatus {
statics.setServiceType(Integer.parseInt(strKeys[1]));
statics.setErrNum(mapStaticsErr.getOrDefault(key, new AtomicInteger(0)).intValue());
multiKeyMap.put(statics.getServiceType(), statics.getServiceType(), statics);
multiKeyMap.put(statics.getCloudType(), statics.getServiceType(), statics);
rel.add(statics);
}
... ...
package com.monitor;
import com.alibaba.fastjson.JSON;
import com.monitor.influxdb.mapper.impl.JavaApiStaticsMapper;
import com.monitor.javaserver.common.JavaApiStatics;
import com.monitor.model.domain.JavaApiStaticsModel;
... ... @@ -51,19 +52,20 @@ public class JavaApiInfluxDBTest {
public void testBatchQuery() {
List<JavaApiStatusReq> listParam = new ArrayList<>();
JavaApiStatusReq param1 = new JavaApiStatusReq();
param1.setServiceId(21);
param1.setServiceId(43);
listParam.add(param1);
JavaApiStatusReq param2 = new JavaApiStatusReq();
param2.setServiceId(19);
param2.setServiceId(134);
for (int i = 0; i < 30; i++) {
JavaApiStatusReq param1 = new JavaApiStatusReq();
param1.setServiceId(19);
param1.setMObjectId(24);
listParam.add(param1);
}
listParam.add(param2);
long start = System.currentTimeMillis();
logger.info("############################################start...");
List<JavaApiStaticsModel> rel = javaApiStaticsMapper.selectlatestJavaApiStaticsList(null, listParam);
long end = System.currentTimeMillis();
logger.info("###########################################end... cost : {}", (end - start));
logger.info("############################################rel: {}", JSON.toJSON(rel));
logger.info(rel.toString());
}
... ...