Authored by skinny.wu

Redis 增加caps以及uptime

@@ -51,6 +51,10 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService @@ -51,6 +51,10 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService
51 51
52 @Autowired 52 @Autowired
53 private SnsMobileConfig snsMobileConfig; 53 private SnsMobileConfig snsMobileConfig;
  54 +
  55 + private static Map<String, Long> lastUpTimeMap = new HashMap<>();
  56 +
  57 + private static Map<String, Long> lastRequestCountMap = new HashMap<>();
54 58
55 @Override 59 @Override
56 public void redisMonitor() { 60 public void redisMonitor() {
@@ -104,7 +108,7 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService @@ -104,7 +108,7 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService
104 List<String> ipList=null; 108 List<String> ipList=null;
105 for(MObjectInfo obj:redisProxymList){ 109 for(MObjectInfo obj:redisProxymList){
106 paramMonitor=new StringBuffer(); 110 paramMonitor=new StringBuffer();
107 - String[] ports=obj.getMoTags().split(",");; 111 + String[] ports=obj.getMoTags().split(",");
108 log.info("two port is {}",obj.getMoTags()); 112 log.info("two port is {}",obj.getMoTags());
109 String result= TelnetUtils.getResult(obj.getMoHostIp(),Integer.valueOf(ports[0])); 113 String result= TelnetUtils.getResult(obj.getMoHostIp(),Integer.valueOf(ports[0]));
110 redisMonitor = new RedisMonitor(); 114 redisMonitor = new RedisMonitor();
@@ -117,9 +121,19 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService @@ -117,9 +121,19 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService
117 } 121 }
118 //取舍成功重新设为1 122 //取舍成功重新设为1
119 JSONObject response=JSONObject.parseObject(result); 123 JSONObject response=JSONObject.parseObject(result);
  124 +
  125 + //记录代理所有request的和
  126 + long requestCount = 0;
  127 + long upTime = -1;
  128 + int redisCount = 0;
  129 +
  130 + //用ipString作为lastRequestCountMap lastUpTimeMap中的key
  131 + String ipString ="";
  132 +
120 if(null != response){ 133 if(null != response){
121 int total_connections=(Integer)response.get("total_connections"); 134 int total_connections=(Integer)response.get("total_connections");
122 int curr_connections=(Integer)response.get("curr_connections"); 135 int curr_connections=(Integer)response.get("curr_connections");
  136 + upTime = (long)response.get("uptime");
123 if(total_connections>0){ 137 if(total_connections>0){
124 //总连接数 138 //总连接数
125 paramMonitor.append("总连接数:"+total_connections+";"); 139 paramMonitor.append("总连接数:"+total_connections+";");
@@ -127,19 +141,57 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService @@ -127,19 +141,57 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService
127 if (total_connections > 0) { 141 if (total_connections > 0) {
128 paramMonitor.append("当前连接数:"+curr_connections+";"); 142 paramMonitor.append("当前连接数:"+curr_connections+";");
129 } 143 }
130 - //查看代理下的redis 144 +
  145 + //查看代理下的redis 求出所有代理request总量
131 JSONObject alpha=response.getJSONObject("alpha"); 146 JSONObject alpha=response.getJSONObject("alpha");
132 ipList=new ArrayList<String>(); 147 ipList=new ArrayList<String>();
  148 +
133 for (Map.Entry<String, Object> entry : alpha.entrySet()) { 149 for (Map.Entry<String, Object> entry : alpha.entrySet()) {
134 String key=entry.getKey(); 150 String key=entry.getKey();
135 if(key.indexOf(":")>1){ 151 if(key.indexOf(":")>1){
136 ipList.add(key+":"+obj.getMoTypeId()); 152 ipList.add(key+":"+obj.getMoTypeId());
  153 + JSONObject ipObj= alpha.getJSONObject(key);
  154 +
  155 + // 取出requests的long值
  156 + requestCount += (long)ipObj.get("requests");
  157 + redisCount ++;
137 } 158 }
138 } 159 }
  160 +
  161 + ipString = obj.getMoHostIp()+":"+ports[1];
  162 +
139 tMap.put(obj.getMoHostIp()+":"+ports[1],ipList); 163 tMap.put(obj.getMoHostIp()+":"+ports[1],ipList);
140 redisMonitor.setIsFailed(1); 164 redisMonitor.setIsFailed(1);
141 redisMonitor.setParamMonitor(paramMonitor.toString()); 165 redisMonitor.setParamMonitor(paramMonitor.toString());
142 } 166 }
  167 +
  168 + /**
  169 + * 计算caps值 (当前requestCount - lastRequestCount)/ (uptime - lastUpTime)/ 1000
  170 + * 在param_monitor字段数据中中添加 caps值 uptime值
  171 + * 将caps uptime数据存到全局map中,供下次计算使用
  172 + */
  173 + double caps = -1;
  174 +
  175 + //如果数据为0,即没有探测出数据,不记录该干扰数据
  176 + if (0 < requestCount || 0 < upTime) {
  177 +
  178 + //如果map中有该ip的数据,则计算caps值以及记录当前request跟uptime字段值到map中
  179 + if (null != lastRequestCountMap.get(ipString)
  180 + && null != lastUpTimeMap.get(ipString)) {
  181 +
  182 + long lastRequestCount = lastRequestCountMap.get(ipString);
  183 + long lastUpTime = lastUpTimeMap.get(ipString);
  184 + caps = (requestCount - lastRequestCount) * 1.0 / ((upTime - lastUpTime) * 1000) / redisCount;
  185 + }
  186 +
  187 + //将当前的uptime request数据存到map中
  188 + lastRequestCountMap.put(ipString, requestCount);
  189 + lastUpTimeMap.put(ipString, upTime);
  190 + }
  191 +
  192 + paramMonitor.append("caps:" + caps + ";");
  193 + paramMonitor.append("uptime:" + upTime + ";");
  194 +
143 }else{ 195 }else{
144 twemproxyAlarmList.add("失败:"+obj.getMoHostIp()+":"+ports[0]); 196 twemproxyAlarmList.add("失败:"+obj.getMoHostIp()+":"+ports[0]);
145 redisMonitor.setIsFailed(0); 197 redisMonitor.setIsFailed(0);