Authored by simba

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
#	monitor-service-middleware/src/main/java/com/monitor/middleware/redis/service/impl/RedisMonitorServiceImpl.java
Showing 18 changed files with 136 additions and 257 deletions
package com.monitor.cmdb.ctrl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.monitor.cmdb.service.IRedisInfoService;
import com.monitor.model.request.RedisInfoReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.model.response.PageResponse;
@Controller
@RequestMapping("redisMonitor")
public class RedisInfoCtrl {
Logger log = LoggerFactory.getLogger(RedisInfoCtrl.class);
@Autowired
private IRedisInfoService redisInfoService;
@RequestMapping("/getRedisInfo")
@ResponseBody
public BaseResponse<PageResponse<Object>> getRedisInfo(@RequestBody RedisInfoReq req){
log.info("getRedisInfo with param is {}",req);
// 查询列表
PageResponse<Object> responseBO = redisInfoService.getRedisInfo(req);
if (responseBO == null || CollectionUtils.isEmpty(responseBO.getRows())) {
return null;
}
PageResponse<Object> response = new PageResponse<Object>();
response.setCurrentPage(responseBO.getCurrentPage());
response.setRows(responseBO.getRows());
response.setPageSize(responseBO.getPageSize());
response.setTotal(responseBO.getTotal());
log.info("getRedisInfo success and total={}", response.getTotal());
return new BaseResponse<PageResponse<Object>>(response);
}
}
package com.monitor.cmdb.service;
import com.monitor.model.request.RedisInfoReq;
import com.monitor.model.response.PageResponse;
public interface IRedisInfoService {
PageResponse<Object> getRedisInfo(RedisInfoReq req);
}
package com.monitor.cmdb.service.impl;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.monitor.cmdb.service.IRedisInfoService;
import com.monitor.influxdb.mapper.IRedisMapper;
import com.monitor.model.domain.PageBean;
import com.monitor.model.request.RedisInfoReq;
import com.monitor.model.response.PageResponse;
@Service
public class RedisServiceImpl implements IRedisInfoService {
Logger log = LoggerFactory.getLogger(RedisServiceImpl.class);
@Autowired
IRedisMapper redisMapper;
@Override
public PageResponse<Object> getRedisInfo(RedisInfoReq req) {
log.debug("getRedisInfo with param is {}",req);
PageBean page = PageBean.initPageInfo(req.getCurrentPage(),req.getPageSize(), req);
// 先查询符合条件的总数量
int total = redisMapper.selectCountByCodition(page);
log.info("selectUserTotal num is {}, with param is {}", total,
req);
// 数量为0 直接返回
if (total == 0) {
// 返回初始page对象
return null;
}
// 获取列表
List<?> redis = redisMapper.selectRedisInfosByCodition(page);
if (redis==null) {
log.debug("selectRedisInfosByCodition is null with param is {}", page);
return null;
}
PageResponse<Object> response = new PageResponse<Object>();
response.setCurrentPage(req.getCurrentPage());
response.setPageSize(req.getPageSize());
response.setTotal(total);
response.setRows((List<Object>) redis);
return response;
}
}
package com.monitor.influxdb.mapper;
import java.util.List;
import com.monitor.influxdb.model.RedisInfo;
import com.monitor.model.domain.PageBean;
public interface IRedisMapper {
void insert(RedisInfo redis);
int selectCountByCodition(PageBean page);
List<?> selectRedisInfosByCodition(PageBean page);
}
... ... @@ -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,24 +119,75 @@ 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;
... ... @@ -8,15 +18,6 @@ import com.monitor.influxdb.InluxDBSingle;
import com.monitor.influxdb.mapper.IRedisMapper;
import com.monitor.influxdb.model.RedisInfo;
import com.monitor.model.domain.PageBean;
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 java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@Component
public class RedisMapper extends InfluxDBQuery implements IRedisMapper {
... ... @@ -35,7 +36,7 @@ public class RedisMapper extends InfluxDBQuery implements IRedisMapper {
.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())
// .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()
... ...
package com.monitor.influxdb.model;
import lombok.Data;
@Data
public class RedisInfo {
private String hostIp;
private String connected_clients;//连接客户端数量
private int is_slave;
private int is_run;
private String role;//实例角色
public RedisInfo(String hostIp, String connected_clients,
int is_slave, int is_run, String role) {
super();
this.hostIp = hostIp;
this.connected_clients = connected_clients;
this.is_slave = is_slave;
this.is_run = is_run;
this.role = role;
}
}
... ... @@ -20,6 +20,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
... ... @@ -62,6 +63,13 @@ public class JavaApiClient {
JavaApiStatus javaApiStatus;
@Value("${JavaApiExecutorPoolCoreSize:20}")
private int JavaApiExecutorPoolCoreSize;
@Value("${JavaApiExecutorPoolMaxSize:50}")
private int JavaApiExecutorPoolMaxSize;
//TODO 需要 线程安全 处理 不过一般不会出现并发,任务周期比较长,一般周期内任务能够完成
private Map<String, JavaApiInfo> javaApimap;
... ... @@ -73,7 +81,8 @@ public class JavaApiClient {
public void initClient() {
executorService = new ThreadPoolExecutor(10, 30, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(),
executorService = new ThreadPoolExecutor(JavaApiExecutorPoolCoreSize, JavaApiExecutorPoolMaxSize,
60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(),
new JavaApiThreadFactory());
completionService = new ExecutorCompletionService<JavaApiStatics>(executorService);
... ... @@ -163,7 +172,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);
}
... ...
... ... @@ -44,13 +44,15 @@ public class JavaApiTask implements Callable<JavaApiStatics> {
apiStatics.setHasException(false);
apiStatics.setJavaApiInfo(this.javaApiInfo);
apiStatics.setMObjectDetails(this.mObjectInfo);
//TODO JSON解析异常
try {
JSONObject req = null;
if (javaApiInfo.getApiData() != null && !(javaApiInfo.getApiData().equals("")))
req = JSON.parseObject(javaApiInfo.getApiData());
if (javaApiInfo.getApiReqMethod() == 0) {
JSONObject req = JSON.parseObject(javaApiInfo.getApiData());
jsonRep = restTemplate.getForObject(url, JSONObject.class);
} else {
JSONObject req = JSON.parseObject(javaApiInfo.getApiData());
jsonRep = restTemplate.postForObject(url, req, JSONObject.class);
}
... ...
... ... @@ -6,7 +6,7 @@
<bean id="javaapi_ConnectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
<property name="maxTotal" value="40" />
<property name="defaultMaxPerRoute" value="5" />
<property name="defaultMaxPerRoute" value="10" />
</bean>
<bean id="javaapi_httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder"
... ... @@ -21,8 +21,8 @@
<bean id="javaapi_clientHttpRequestFactory"
class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
<constructor-arg ref="javaapi_httpClient" />
<property name="connectTimeout" value="2000" />
<property name="readTimeout" value="2000" />
<property name="connectTimeout" value="800" />
<property name="readTimeout" value="1200" />
</bean>
<bean id="javaapiRestTemplate" class="org.springframework.web.client.RestTemplate">
... ...
... ... @@ -4,4 +4,5 @@ public interface IRedisMonitorService {
void redisMonitor();
}
... ...
package com.monitor.middleware.redis.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.monitor.common.util.HttpRestClient;
import com.monitor.common.util.SSHRedis;
import com.monitor.influxdb.mapper.IRedisMapper;
import com.monitor.influxdb.model.RedisInfo;
import com.monitor.middleware.redis.service.IRedisMonitorService;
import com.monitor.mysql.mapper.RedisMonitorMapper;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
import com.monitor.common.util.SSHRedis;
import com.monitor.middleware.redis.service.IRedisMonitorService;
@Service
public class RedisMonitorServiceImpl implements IRedisMonitorService {
Logger log = LoggerFactory.getLogger(RedisMonitorServiceImpl.class);
@Autowired
IRedisMapper redisMapper;
@Autowired
RedisMonitorMapper redisMonitorMapper;
@Autowired
HttpRestClient httpRestClient;
@Override
public void redisMonitor() {
/**********************************************************************
*1、处理twemproxy
***********************************************************************/
List<com.model.RedisInfo> proxyList=redisMonitorMapper.selectRedisMonitorByLevel(1);
if(CollectionUtils.isEmpty(proxyList)){
return;
}
StringBuffer paramMonitor=null;
for(com.model.RedisInfo obj:proxyList){
paramMonitor=new StringBuffer();
//JSONObject response=httpRestClient.defaultPost(obj.getHostIp()+obj.getParamMonitor(), null, JSONObject.class);
JSONObject response=httpRestClient.defaultPost("http://192.168.102.222:22222", null, JSONObject.class);
if(null != response){
int total_connections=(Integer)response.get("total_connections");
int curr_connections=(Integer)response.get("curr_connections");
if(total_connections>0){
paramMonitor.append("总连接数:"+total_connections);
}
if (total_connections > 0) {
paramMonitor.append("当前接数:"+curr_connections);
}
}
obj.setParamMonitor(paramMonitor.toString());
redisMonitorMapper.updateByPrimaryKey(obj);
}
//TODO
//1、getallredis 分配给redis的总内存通过”config get maxmemory“取出然后算出占用百分比,需要用进度条展示(领导要求)
//2、level=2 twemproxy取这个值,update更新需要取的值: "curr_connections":2,"client_eof":1959, "requests":2423810, "request_bytes":487387692, "responses":2423810, "response_bytes":20334901
//3、遍历level=3
/**********************************************************************
*2、处理Redis
***********************************************************************/
List<com.model.RedisInfo> redisList=redisMonitorMapper.selectRedisMonitorByLevel(2);
int isSlave=0;
int isRun=0;
StringBuffer buff = new StringBuffer();
String isSlave="";
String redisInfo = SSHRedis.exec("192.168.102.162", "root", "123456", 22,"sleep 20;cd /usr/bin;redis-cli -h 192.168.102.222 -p 6379 info;");
//检验主从同步
SSHRedis.exec("192.168.102.162", "root", "123456", 22,"sleep 20;cd /usr/bin;redis-cli -h 192.168.102.222 -p 6379 set test_key 'test';");
String redisInfo2 = SSHRedis.exec("192.168.102.162", "root", "123456", 22,"sleep 20;cd /usr/bin;redis-cli -h 192.168.102.222 -p 6379 get test_key;");
... ... @@ -76,22 +32,21 @@ public class RedisMonitorServiceImpl implements IRedisMonitorService {
String[] arry = redisInfo2.split("<br>");
String val = arry[0].replace("\r\n", "").trim();
if("test".equals(val)){
isSlave=1;
isSlave="同步";
}else {
isSlave=0;
isSlave="不同步";
}
}
if (StringUtils.isNotEmpty(redisInfo)) {
isRun=1;
RedisInfo redis = new RedisInfo("192.168.102.162", getRedisInfo(
redisInfo, "connected_clients"), isSlave, isRun, getRedisInfo(redisInfo,"role"));
redisMapper.insert(redis);
if(StringUtils.isEmpty(redisInfo)){
buff.append("状态:宕机;");
}else{
isRun=0;
RedisInfo redis = new RedisInfo("192.168.102.162", "", isSlave, isRun, "");
redisMapper.insert(redis);
buff.append("角色:"+getRedisInfo(redisInfo,"role")+";");
buff.append("用量:"+getRedisInfo(redisInfo,"used_memory_rss")+";");
buff.append("内存碎片比率:"+getRedisInfo(redisInfo,"mem_fragmentation_ratio")+";");
buff.append("主从同步:"+isSlave+";");
}
buff.toString();
System.out.println(buff.toString());
}
public String getRedisInfo(String redisInfo, String info) {
... ...
... ... @@ -13,8 +13,8 @@ public class RedisMonitorTask {
@Autowired
private IRedisMonitorService redisMonitorService;
//@Scheduled(fixedRate=20000)
@Scheduled(cron="0 */5 * * * ?")
@Scheduled(fixedRate=20000)
// @Scheduled(cron="0 */5 * * * ?")
public void redisMonitor(){
redisMonitorService.redisMonitor();
}
... ...
system.envi=product
#java api 执行核心线程数
JavaApiExecutorPoolCoreSize=30
#java api 执行最大线程数
JavaApiExecutorPoolMaxSize=50
\ No newline at end of file
... ...
system.envi=test
#java api 执行核心线程数
JavaApiExecutorPoolCoreSize=30
#java api 执行最大线程数
JavaApiExecutorPoolMaxSize=50
\ No newline at end of file
... ...
... ... @@ -2,7 +2,7 @@
influxdb.num=2
influxdb.name=test;alarm
influxdb.ip=http://192.168.102.162:8086;http://123.206.79.151:18086
influxdb.ip=http://192.168.102.162:8086;http://192.168.102.162:8086
influxdb.user=yoho;root
influxdb.pwd=Yoho_9646;root
influxdb.connect.timeout=15;15
... ...
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<>();
for (int i = 0; i < 30; i++) {
JavaApiStatusReq param1 = new JavaApiStatusReq();
param1.setServiceId(21);
param1.setServiceId(43);
param1.setServiceId(19);
param1.setMObjectId(24);
listParam.add(param1);
JavaApiStatusReq param2 = new JavaApiStatusReq();
param2.setServiceId(19);
param2.setServiceId(134);
}
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());
}
... ...