Authored by FengRuwei

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

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);
}
package com.monitor.influxdb.model;
import lombok.Data;
@Data
public class RedisInfo {
private String hostIp;
private String connected_clients;//连接客户端数量
private String is_slave;
private int is_run;
private String role;//实例角色
public RedisInfo(String hostIp, String connected_clients,
String 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;
}
}
... ... @@ -3,5 +3,6 @@ package com.monitor.middleware.redis.service;
public interface IRedisMonitorService {
void redisMonitor();
}
... ...
... ... @@ -3,29 +3,28 @@ package com.monitor.middleware.redis.service.impl;
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 com.monitor.common.util.SSHRedis;
import com.monitor.influxdb.mapper.IRedisMapper;
import com.monitor.influxdb.model.RedisInfo;
import com.monitor.middleware.redis.service.IRedisMonitorService;
@Service
public class RedisMonitorServiceImpl implements IRedisMonitorService {
Logger log = LoggerFactory.getLogger(RedisMonitorServiceImpl.class);
@Autowired
IRedisMapper redisMapper;
@Override
public void redisMonitor() {
//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
StringBuffer buff = new StringBuffer();
String isSlave="";
int isRun=0;
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;");
... ... @@ -38,19 +37,16 @@ public class RedisMonitorServiceImpl implements IRedisMonitorService {
isSlave="不同步";
}
}
buff.append("角色:"+getRedisInfo(redisInfo,"role")+";");
buff.append("用量:"+getRedisInfo(redisInfo,"role")+";");
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();
}
... ...