Authored by simba

update

@@ -11,6 +11,28 @@ import java.util.Map; @@ -11,6 +11,28 @@ import java.util.Map;
11 11
12 public class RedisCommonUtil { 12 public class RedisCommonUtil {
13 13
  14 + public static long getRedisMaxMemory(String host,int port){
  15 + Map<String,Object> result=null;
  16 + Jedis client =null;
  17 + try {
  18 + client=new Jedis(host, port);
  19 + result=new HashMap<String, Object>();
  20 + List<String> configList=client.configGet("maxmemory");
  21 + long maxmemory=0;
  22 + if(!CollectionUtils.isEmpty(configList)&&configList.size()==2){
  23 + maxmemory=Long.valueOf(configList.get(1));
  24 + }
  25 + return maxmemory;
  26 + }catch (Exception e){
  27 + return 0;
  28 + }finally {
  29 + if(client!=null){
  30 + client.close();
  31 + }
  32 + }
  33 + }
  34 +
  35 +
14 public static Map<String,Object> getRedisInfo(String host,int port){ 36 public static Map<String,Object> getRedisInfo(String host,int port){
15 Map<String,Object> result=null; 37 Map<String,Object> result=null;
16 Jedis client =null; 38 Jedis client =null;
@@ -21,12 +43,6 @@ public class RedisCommonUtil { @@ -21,12 +43,6 @@ public class RedisCommonUtil {
21 return null; 43 return null;
22 } 44 }
23 result=new HashMap<String, Object>(); 45 result=new HashMap<String, Object>();
24 - List<String> configList=client.configGet("maxmemory");  
25 - int maxmemory=0;  
26 - if(!CollectionUtils.isEmpty(configList)&&configList.size()==2){  
27 - maxmemory=Integer.valueOf(configList.get(1));  
28 - }  
29 - result.put("maxmemory",maxmemory);  
30 String[] arr = redisInfo.split("\r\n"); 46 String[] arr = redisInfo.split("\r\n");
31 for (String str : arr) { 47 for (String str : arr) {
32 if (str.startsWith("#")||str.startsWith("\r\n")) { 48 if (str.startsWith("#")||str.startsWith("\r\n")) {
@@ -96,19 +96,17 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService @@ -96,19 +96,17 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService
96 List<String> ipList=null; 96 List<String> ipList=null;
97 for(MObjectInfo obj:redisProxymList){ 97 for(MObjectInfo obj:redisProxymList){
98 paramMonitor=new StringBuffer(); 98 paramMonitor=new StringBuffer();
99 - String[] ports=null;  
100 - if(StringUtils.isBlank(obj.getMoTags())){  
101 - ports=obj.getMoTags().split(",");  
102 - } 99 + String[] ports=obj.getMoTags().split(",");;
  100 + log.info("two port is {}",obj.getMoTags());
103 String result= TelnetUtils.getResult(obj.getMoHostIp(),Integer.valueOf(ports[0])); 101 String result= TelnetUtils.getResult(obj.getMoHostIp(),Integer.valueOf(ports[0]));
104 redisMonitor = new RedisMonitor(); 102 redisMonitor = new RedisMonitor();
105 if(StringUtils.isNotBlank(result)){ 103 if(StringUtils.isNotBlank(result)){
106 paramMonitor.append("1,"); 104 paramMonitor.append("1,");
107 boolean proxyFlag=RedisCommonUtil.getRedisIsSlave(obj.getMoHostIp(),Integer.valueOf(ports[1])); 105 boolean proxyFlag=RedisCommonUtil.getRedisIsSlave(obj.getMoHostIp(),Integer.valueOf(ports[1]));
108 if(proxyFlag){ 106 if(proxyFlag){
109 - paramMonitor.append("OK,"); 107 + paramMonitor.append("探测成功;");
110 }else{ 108 }else{
111 - paramMonitor.append("ERROR,"); 109 + paramMonitor.append("探测失败;");
112 } 110 }
113 //取舍成功重新设为1 111 //取舍成功重新设为1
114 JSONObject response=JSONObject.parseObject(result); 112 JSONObject response=JSONObject.parseObject(result);
@@ -117,10 +115,10 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService @@ -117,10 +115,10 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService
117 int curr_connections=(Integer)response.get("curr_connections"); 115 int curr_connections=(Integer)response.get("curr_connections");
118 if(total_connections>0){ 116 if(total_connections>0){
119 //总连接数 117 //总连接数
120 - paramMonitor.append(total_connections+","); 118 + paramMonitor.append("总连接数:"+total_connections+";");
121 } 119 }
122 if (total_connections > 0) { 120 if (total_connections > 0) {
123 - paramMonitor.append(curr_connections+","); 121 + paramMonitor.append("当前连接数:"+curr_connections+";");
124 } 122 }
125 //查看代理下的redis 123 //查看代理下的redis
126 JSONObject alpha=response.getJSONObject("alpha"); 124 JSONObject alpha=response.getJSONObject("alpha");
@@ -131,7 +129,7 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService @@ -131,7 +129,7 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService
131 ipList.add(key+":"+obj.getMoTypeId()); 129 ipList.add(key+":"+obj.getMoTypeId());
132 } 130 }
133 } 131 }
134 - tMap.put(obj.getMoHostIp()+":"+obj.getMoTags(),ipList); 132 + tMap.put(obj.getMoHostIp()+":"+ports[1],ipList);
135 redisMonitor.setIsFailed(1); 133 redisMonitor.setIsFailed(1);
136 redisMonitor.setParamMonitor(paramMonitor.toString()); 134 redisMonitor.setParamMonitor(paramMonitor.toString());
137 } 135 }
@@ -141,7 +139,7 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService @@ -141,7 +139,7 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService
141 redisMonitor.setParamMonitor("0,"); 139 redisMonitor.setParamMonitor("0,");
142 } 140 }
143 redisMonitor.setNodeFrom(redisTweproxyMap.get(obj.getMoTypeId())); 141 redisMonitor.setNodeFrom(redisTweproxyMap.get(obj.getMoTypeId()));
144 - redisMonitor.setNodeTo(obj.getMoHostIp()+":"+obj.getMoTags()); 142 + redisMonitor.setNodeTo(obj.getMoHostIp()+":"+ports[1]);
145 redisMonitor.setLevel(1); 143 redisMonitor.setLevel(1);
146 redisMonitor.setRedisType(obj.getMoTypeId()); 144 redisMonitor.setRedisType(obj.getMoTypeId());
147 redisInfoList.add(redisMonitor); 145 redisInfoList.add(redisMonitor);
@@ -175,13 +173,18 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService @@ -175,13 +173,18 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService
175 String role=(String)result.get("role"); 173 String role=(String)result.get("role");
176 paramMonitor.append(role+","); 174 paramMonitor.append(role+",");
177 try { 175 try {
178 - BigDecimal maxmemory=BigDecimal.valueOf(Long.valueOf(result.get("maxmemory").toString()));  
179 - BigDecimal byteDang=BigDecimal.valueOf(Long.valueOf(1024*1024));  
180 - BigDecimal maxMemoryMb = maxmemory.divide(byteDang,2,4);  
181 - paramMonitor.append(maxMemoryMb+"M,");  
182 - BigDecimal used_memory=BigDecimal.valueOf(Long.valueOf(result.get("used_memory").toString()));  
183 - BigDecimal useProportion = used_memory.divide(maxmemory,2,4);  
184 - paramMonitor.append(useProportion+"%,"); 176 + long maxmemory=RedisCommonUtil.getRedisMaxMemory(ipConfig[0], Integer.valueOf(ipConfig[1]));
  177 + if(maxmemory==0){
  178 + paramMonitor.append("最大内存为0");
  179 + }else{
  180 + BigDecimal byteDang=BigDecimal.valueOf(Long.valueOf(1024*1024));
  181 + BigDecimal maxMemoryMb = BigDecimal.valueOf(maxmemory).divide(byteDang,2,4);
  182 + paramMonitor.append(maxMemoryMb+"M,");
  183 +
  184 + BigDecimal used_memory=BigDecimal.valueOf(Long.valueOf(result.get("used_memory").toString()));
  185 + BigDecimal useProportion = used_memory.divide(BigDecimal.valueOf(maxmemory),2,4);
  186 + paramMonitor.append(useProportion+"%,");
  187 + }
185 }catch (Exception e){ 188 }catch (Exception e){
186 log.error("计算Redis使用率错误",e); 189 log.error("计算Redis使用率错误",e);
187 paramMonitor.append("0.00%,"); 190 paramMonitor.append("0.00%,");
@@ -13,7 +13,7 @@ public class RedisMonitorTask { @@ -13,7 +13,7 @@ public class RedisMonitorTask {
13 private IRedisMonitorHandleService redisMonitorService; 13 private IRedisMonitorHandleService redisMonitorService;
14 14
15 //@Scheduled(fixedRate=20000) 15 //@Scheduled(fixedRate=20000)
16 - @Scheduled(cron="0 0/5 * * * ? ") 16 + @Scheduled(cron="0 0/3 * * * ? ")
17 public void redisMonitor(){ 17 public void redisMonitor(){
18 redisMonitorService.redisMonitor(); 18 redisMonitorService.redisMonitor();
19 } 19 }