Authored by qinchao

redis监控-20180320-v2

... ... @@ -4,6 +4,7 @@ import com.monitor.middleware.redis.model.RedisInfo;
import com.monitor.middleware.redis.model.TwemproxyInfo;
import redis.clients.jedis.JedisPool;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
... ... @@ -15,7 +16,7 @@ public interface Constants {
Map<String, RedisInfo> REDIS_INFO_MAP = new ConcurrentHashMap<>();
//key: "ip:port"
Map<String, TwemproxyInfo> TWEMPROXY_INFO_MAP = new ConcurrentHashMap<>();
Map<String, Map<String,TwemproxyInfo>> TWEMPROXY_INFO_MAP = new ConcurrentHashMap<>();
Map<String, JedisPool> JEDIS_POOL_MAP = new ConcurrentHashMap<>();
... ...
... ... @@ -44,11 +44,12 @@ public class RedisCtrl {
List<TwemproxyVo> twemproxyVOList = new ArrayList<>();
try {
for (Map.Entry<String, TwemproxyInfo> entry : Constants.TWEMPROXY_INFO_MAP.entrySet()) {
TwemproxyVo twemproxyVO = buildTwemproxyVO(entry.getKey(), entry.getValue());
twemproxyVOList.add(twemproxyVO);
for (Map.Entry<String, Map<String,TwemproxyInfo>> entry : Constants.TWEMPROXY_INFO_MAP.entrySet()) {
Map<String,TwemproxyInfo> tmpMap=entry.getValue();
for(TwemproxyInfo tmp:tmpMap.values()){
TwemproxyVo twemproxyVO = buildTwemproxyVO(entry.getKey(), tmp);
twemproxyVOList.add(twemproxyVO);
}
}
} catch (Exception e) {
... ... @@ -135,26 +136,35 @@ public class RedisCtrl {
List<RedisVo> redisInfoList = new ArrayList<>();
try {
TwemproxyInfo twemproxyInfo = Constants.TWEMPROXY_INFO_MAP.get(twemproxy);
Map<String,TwemproxyInfo> twemproxyInfoMap = Constants.TWEMPROXY_INFO_MAP.get(twemproxy);
DEBUG.info("Query twemproxyinfo {} by key {}", twemproxyInfo, twemproxy);
DEBUG.info("Query twemproxyinfo {} by key {}", twemproxyInfoMap, twemproxyInfoMap);
Iterator<String> relationRedis=twemproxyInfo.getRelationRedis().iterator();
if(twemproxyInfoMap!=null&&twemproxyInfoMap.size()>0){
for(String key:twemproxyInfoMap.keySet()){
TwemproxyInfo twemproxyInfo=twemproxyInfoMap.get(key);
Iterator<String> relationRedis=twemproxyInfo.getRelationRedis().iterator();
while (relationRedis.hasNext()) {
while (relationRedis.hasNext()) {
String redisUrl=relationRedis.next();
String redisUrl=relationRedis.next();
RedisInfo redisInfo = Constants.REDIS_INFO_MAP.get(redisUrl);
RedisInfo redisInfo = Constants.REDIS_INFO_MAP.get(redisUrl);
if (null != redisInfo) {
DEBUG.info("Twemproxy {} contains redis {} ", twemproxy, redisInfo);
if (null != redisInfo) {
DEBUG.info("Twemproxy {} contains redis {} ", twemproxy, redisInfo);
RedisVo redisVo = buildRedisVo(redisInfo);
RedisVo redisVo = buildRedisVo(redisInfo);
redisInfoList.add(redisVo);
redisInfoList.add(redisVo);
}
}
break;
}
}
} catch (Exception e) {
DEBUG.error("Failed to query twemproxy info , erro {}", e);
}
... ...
... ... @@ -4,6 +4,8 @@ import com.model.MObjectInfo;
import com.model.TypeInfo;
import com.monitor.cmdb.service.IMObjectInfoService;
import com.monitor.cmdb.service.ITypeInfoService;
import com.monitor.middleware.redis.constants.Constants;
import com.monitor.middleware.redis.model.TwemproxyInfo;
import com.monitor.middleware.redis.task.OneTask;
import lombok.Getter;
import org.apache.commons.lang.StringUtils;
... ... @@ -49,6 +51,41 @@ public class RedisMonitService {
return;
}
if( Constants.TWEMPROXY_INFO_MAP.size()>0){
Set<String> nowkeys=redisMap.keySet();
for(String task_name:Constants.TWEMPROXY_INFO_MAP.keySet()){
if(nowkeys.contains(task_name)){
//ip减少了
Set<String> nowUrl=new HashSet<>();
for(MObjectInfo mo:redisMap.get(task_name)){
Map<String, String> tags = mo.getMoTagsMap();
String jedisHost = mo.getMoHostIp();
String tagPort = tags.get("port");
int jedisPort = (StringUtils.isBlank(tagPort) ? 6379 : Integer.valueOf(tagPort));
String jedisUrl = jedisHost + ":" + jedisPort;
nowUrl.add(jedisUrl);
}
Map<String,TwemproxyInfo> map =Constants.TWEMPROXY_INFO_MAP.get(task_name);
if(!map.isEmpty()){
for(String key:map.keySet()){
if(!nowUrl.contains(key)){
map.remove(key);
}
}
}
}else {
Constants.TWEMPROXY_INFO_MAP.remove(task_name);
}
}
}
ExecutorService executorService = Executors.newFixedThreadPool(redisMap.size());
for (Map.Entry<String, List<MObjectInfo>> entry : redisMap.entrySet()) {
... ...
... ... @@ -9,9 +9,7 @@ import com.monitor.middleware.redis.model.TwemproxyInfo;
import org.apache.commons.lang.StringUtils;
import redis.clients.jedis.Jedis;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.*;
/**
* Created by yoho on 2016/11/9.
... ... @@ -40,8 +38,10 @@ public class OneTmpJob extends OneJob {
if (StringUtils.equals(this.getJedisHost(), STATICIP)) {
//存储
TwemproxyInfo twemproxyInfo = handleStaticIP();
Constants.TWEMPROXY_INFO_MAP.put(key, twemproxyInfo);
if(!Constants.TWEMPROXY_INFO_MAP.containsKey(key)){
Constants.TWEMPROXY_INFO_MAP.put(key,new HashMap<>());
}
Constants.TWEMPROXY_INFO_MAP.get(key).put(this.getJedisUrl(),twemproxyInfo);
return;
}
... ... @@ -57,7 +57,10 @@ public class OneTmpJob extends OneJob {
tmpInfo = handleConnected(jedis);
}
//存储
Constants.TWEMPROXY_INFO_MAP.put(key, tmpInfo);
if(!Constants.TWEMPROXY_INFO_MAP.containsKey(key)){
Constants.TWEMPROXY_INFO_MAP.put(key,new HashMap<>());
}
Constants.TWEMPROXY_INFO_MAP.get(key).put(this.getJedisUrl(),tmpInfo);
DEBUG.info("End to do twemproxy {} task...time {}", this.getJedisUrl(), System.currentTimeMillis());
... ...