Authored by linlong

update

@@ -55,10 +55,12 @@ public class MemcachedMonitorImpl { @@ -55,10 +55,12 @@ public class MemcachedMonitorImpl {
55 55
56 @Scheduled(fixedRate = 2 * 60 * 1000) 56 @Scheduled(fixedRate = 2 * 60 * 1000)
57 public void monitor() { 57 public void monitor() {
58 - //遍历所有的redis / twemproxy 58 + MemConstants.MEMCACHED_NAME_MAP.clear();
  59 +
  60 + //遍历所有的memcached
59 serversMap = getServersMap(); 61 serversMap = getServersMap();
60 - List<MObjectInfo> memcachedList = new ArrayList<>();  
61 62
  63 + List<MObjectInfo> memcachedList = new ArrayList<>();
62 StringBuffer monitorUrl = new StringBuffer(); //监控url,将所有memcached的ip port进行拼接 64 StringBuffer monitorUrl = new StringBuffer(); //监控url,将所有memcached的ip port进行拼接
63 for (Map.Entry<String, List<MObjectInfo>> entry : serversMap.entrySet()) { 65 for (Map.Entry<String, List<MObjectInfo>> entry : serversMap.entrySet()) {
64 List<MObjectInfo> info = entry.getValue(); 66 List<MObjectInfo> info = entry.getValue();
@@ -67,7 +69,7 @@ public class MemcachedMonitorImpl { @@ -67,7 +69,7 @@ public class MemcachedMonitorImpl {
67 memcachedList.add(mobjectInfo); 69 memcachedList.add(mobjectInfo);
68 String ip = mobjectInfo.getMoHostIp(); 70 String ip = mobjectInfo.getMoHostIp();
69 String port = mobjectInfo.getMoTags().split(":")[1]; 71 String port = mobjectInfo.getMoTags().split(":")[1];
70 - MemConstants.MEMCACHED_NAME_MAP.put(ip + ":" + port,mobjectInfo.getMoName()); 72 + MemConstants.MEMCACHED_NAME_MAP.put(ip + ":" + port, mobjectInfo.getMoName());
71 monitorUrl.append(ip + ":" + port + " "); 73 monitorUrl.append(ip + ":" + port + " ");
72 } 74 }
73 } 75 }
@@ -75,12 +77,16 @@ public class MemcachedMonitorImpl { @@ -75,12 +77,16 @@ public class MemcachedMonitorImpl {
75 doMonitor(monitorUrl); 77 doMonitor(monitorUrl);
76 } 78 }
77 79
  80 + /**
  81 + * 执行监控任务
  82 + * @param monitorUrl 监控url
  83 + */
78 private void doMonitor(StringBuffer monitorUrl) { 84 private void doMonitor(StringBuffer monitorUrl) {
79 Map<InetSocketAddress, Map<String, String>> result = new HashMap<>(); 85 Map<InetSocketAddress, Map<String, String>> result = new HashMap<>();
80 try { 86 try {
81 MemcachedClient client = new XMemcachedClient(AddrUtil.getAddresses(monitorUrl.toString().trim())); 87 MemcachedClient client = new XMemcachedClient(AddrUtil.getAddresses(monitorUrl.toString().trim()));
82 result = client.getStats(); 88 result = client.getStats();
83 - LOGGER.debug("MemcachedMonitorImpl.doMonitor:get memcached status is {}",result); 89 + LOGGER.debug("MemcachedMonitorImpl.doMonitor:get memcached status is {}", result);
84 client.shutdown(); 90 client.shutdown();
85 } catch (IOException e) { 91 } catch (IOException e) {
86 LOGGER.error("MemcachedMonitorImpl.doMonitor:creatr/shutdown memcached client occurs Exception: ", e.getMessage()); 92 LOGGER.error("MemcachedMonitorImpl.doMonitor:creatr/shutdown memcached client occurs Exception: ", e.getMessage());
@@ -95,13 +101,23 @@ public class MemcachedMonitorImpl { @@ -95,13 +101,23 @@ public class MemcachedMonitorImpl {
95 LOGGER.error("MemcachedMonitorImpl.doMonitor:get memcached status occurs Exception: ", e.getMessage()); 101 LOGGER.error("MemcachedMonitorImpl.doMonitor:get memcached status occurs Exception: ", e.getMessage());
96 this.getAlarmMsgService().sendSms("Memcached", "Can not get Memcached status" + monitorUrl.toString() + " .", this.getSnsMobileConfig().getBaseMobile()); 102 this.getAlarmMsgService().sendSms("Memcached", "Can not get Memcached status" + monitorUrl.toString() + " .", this.getSnsMobileConfig().getBaseMobile());
97 } 103 }
98 - checkAllStatus(monitorUrl.toString().trim(),result);  
99 - if(!CollectionUtils.isEmpty(result)){  
100 - for(Map.Entry<InetSocketAddress, Map<String, String>> entry : result.entrySet()){ 104 + //校验是否所有memcached都返回状态,对未返回状态的memcached短信告警
  105 + checkAllStatus(monitorUrl.toString().trim(), result);
  106 +
  107 + processAllStatus(result);
  108 + }
  109 +
  110 + /**
  111 + * 处理memcached的返回结果,缓存到本地的 MemConstants.MEMCACHED_INFO_MAP
  112 + * @param result
  113 + */
  114 + private void processAllStatus(Map<InetSocketAddress, Map<String, String>> result) {
  115 + if (!CollectionUtils.isEmpty(result)) {
  116 + for (Map.Entry<InetSocketAddress, Map<String, String>> entry : result.entrySet()) {
101 InetSocketAddress address = entry.getKey(); 117 InetSocketAddress address = entry.getKey();
102 Map<String, String> value = entry.getValue(); 118 Map<String, String> value = entry.getValue();
103 - String url = address.getAddress()+":"+address.getPort();  
104 - url = url.substring(1,url.length()); 119 + String url = address.getAddress() + ":" + address.getPort();
  120 + url = url.substring(1, url.length());
105 MemcachedInfo info = new MemcachedInfo(); 121 MemcachedInfo info = new MemcachedInfo();
106 122
107 //每秒请求数 123 //每秒请求数
@@ -111,10 +127,10 @@ public class MemcachedMonitorImpl { @@ -111,10 +127,10 @@ public class MemcachedMonitorImpl {
111 info.setOldSet(cmdSet); 127 info.setOldSet(cmdSet);
112 if (MemConstants.MEMCACHED_INFO_MAP.containsKey(url)) { 128 if (MemConstants.MEMCACHED_INFO_MAP.containsKey(url)) {
113 long oldGet = MemConstants.MEMCACHED_INFO_MAP.get(url).getOldGet(); 129 long oldGet = MemConstants.MEMCACHED_INFO_MAP.get(url).getOldGet();
114 - info.setCmdGet((cmdGet-oldGet)/120.0); 130 + info.setCmdGet((cmdGet - oldGet) / 120.0);
115 131
116 long oldSet = MemConstants.MEMCACHED_INFO_MAP.get(url).getOldSet(); 132 long oldSet = MemConstants.MEMCACHED_INFO_MAP.get(url).getOldSet();
117 - info.setCmdSet((cmdSet-oldSet)/120.0); 133 + info.setCmdSet((cmdSet - oldSet) / 120.0);
118 } 134 }
119 135
120 info.setCurConnections(Integer.parseInt(value.get("curr_connections"))); 136 info.setCurConnections(Integer.parseInt(value.get("curr_connections")));
@@ -124,46 +140,49 @@ public class MemcachedMonitorImpl { @@ -124,46 +140,49 @@ public class MemcachedMonitorImpl {
124 long limit_maxbytes = Long.parseLong(value.get("limit_maxbytes")); 140 long limit_maxbytes = Long.parseLong(value.get("limit_maxbytes"));
125 long bytes = Long.parseLong(value.get("bytes")); 141 long bytes = Long.parseLong(value.get("bytes"));
126 long uptime = Long.parseLong(value.get("uptime")); 142 long uptime = Long.parseLong(value.get("uptime"));
127 - LOGGER.info(url+" monitor info: com_get is {},cmd_set is {},get_hit is {},get_misses is {},limit_maxbytes is {},bytes is {},uptime is {}",cmdGet,cmdSet,get_hit,get_misses,limit_maxbytes,bytes,uptime);  
128 - if((get_hit+get_misses)==0){ 143 + LOGGER.debug(url + " monitor info: com_get is {},cmd_set is {},get_hit is {},get_misses is {},limit_maxbytes is {},bytes is {},uptime is {}", cmdGet, cmdSet, get_hit, get_misses, limit_maxbytes, bytes, uptime);
  144 + if ((get_hit + get_misses) == 0) {
129 info.setHitRate("100.0%"); 145 info.setHitRate("100.0%");
130 - }else{  
131 - info.setHitRate(new DecimalFormat("0.00").format((get_hit*100.0/(get_hit+get_misses)))+"%"); 146 + } else {
  147 + info.setHitRate(new DecimalFormat("0.00").format((get_hit * 100.0 / (get_hit + get_misses))) + "%");
132 } 148 }
133 - info.setMaxMemory(new DecimalFormat("0.00").format(limit_maxbytes*1.0/1024/1024/1024)+" G");  
134 - info.setUpTime(uptime/(60*60*24)+" Days");  
135 - info.setUseMemory(new DecimalFormat("0.00").format(bytes*1.0/1024/1024/1024)+" G");  
136 - MemConstants.MEMCACHED_INFO_MAP.put(url,info); 149 + info.setMaxMemory(new DecimalFormat("0.00").format(limit_maxbytes * 1.0 / 1024 / 1024 / 1024) + " G");
  150 + info.setUpTime(uptime / (60 * 60 * 24) + " Days");
  151 + info.setUseMemory(new DecimalFormat("0.00").format(bytes * 1.0 / 1024 / 1024 / 1024) + " G");
  152 + MemConstants.MEMCACHED_INFO_MAP.put(url, info);
137 } 153 }
138 } 154 }
139 -  
140 } 155 }
141 156
142 /** 157 /**
143 * 检查monitorUrl的地址是否都有返回状态 158 * 检查monitorUrl的地址是否都有返回状态
  159 + *
144 * @param monitorUrl 160 * @param monitorUrl
145 * @param result 161 * @param result
146 */ 162 */
147 private void checkAllStatus(String monitorUrl, Map<InetSocketAddress, Map<String, String>> result) { 163 private void checkAllStatus(String monitorUrl, Map<InetSocketAddress, Map<String, String>> result) {
148 String[] monitorUrls = monitorUrl.split(" "); 164 String[] monitorUrls = monitorUrl.split(" ");
149 Set<String> monitorSet = new HashSet(Arrays.asList(monitorUrls)); 165 Set<String> monitorSet = new HashSet(Arrays.asList(monitorUrls));
150 - for(String s:monitorSet ){  
151 - s="/"+s; 166 + Set<InetSocketAddress> monitorUrlSet = new HashSet<>();
  167 + for (String s : monitorSet) {
  168 + String[] ipPort = s.split(":");
  169 + String ip = ipPort[0];
  170 + int port = Integer.parseInt(ipPort[1]);
  171 + monitorUrlSet.add(new InetSocketAddress(ip, port));
152 } 172 }
153 //如果监控url的地址 多于返回的状态条数,则检查哪个url未得到状态并告警 173 //如果监控url的地址 多于返回的状态条数,则检查哪个url未得到状态并告警
154 - if(!CollectionUtils.isEmpty(result)&&monitorUrls.length>result.size()){ 174 + if (!CollectionUtils.isEmpty(result) && monitorUrls.length > result.size()) {
155 Set<InetSocketAddress> statusSet = result.keySet(); 175 Set<InetSocketAddress> statusSet = result.keySet();
156 - monitorSet.removeAll(statusSet); 176 + monitorUrlSet.removeAll(statusSet);
157 StringBuffer failUrl = new StringBuffer(); 177 StringBuffer failUrl = new StringBuffer();
158 - for(String s:monitorSet){  
159 - failUrl.append(s+" "); 178 + for (InetSocketAddress address: monitorUrlSet) {
  179 + failUrl.append(address.toString()+" ");
160 } 180 }
161 this.getAlarmMsgService().sendSms("Memcached", "Failed to get Memcached status" + failUrl.toString() + " .", this.getSnsMobileConfig().getBaseMobile()); 181 this.getAlarmMsgService().sendSms("Memcached", "Failed to get Memcached status" + failUrl.toString() + " .", this.getSnsMobileConfig().getBaseMobile());
162 182
163 } 183 }
164 } 184 }
165 185
166 -  
167 private Map<String, List<MObjectInfo>> getServersMap() { 186 private Map<String, List<MObjectInfo>> getServersMap() {
168 187
169 Map<String, List<MObjectInfo>> serversMap = new HashMap<>(); 188 Map<String, List<MObjectInfo>> serversMap = new HashMap<>();
@@ -184,7 +203,6 @@ public class MemcachedMonitorImpl { @@ -184,7 +203,6 @@ public class MemcachedMonitorImpl {
184 203
185 List<TypeInfo> childTypeList = typeService.queryChildTypesInfo(parentTypeInfo.getTypeId()); 204 List<TypeInfo> childTypeList = typeService.queryChildTypesInfo(parentTypeInfo.getTypeId());
186 205
187 -  
188 for (TypeInfo typeInfo : childTypeList) { 206 for (TypeInfo typeInfo : childTypeList) {
189 List<MObjectInfo> mObjectInfos = mobjectService.queryMObjectsInfoByType(typeInfo.getTypeId()); 207 List<MObjectInfo> mObjectInfos = mobjectService.queryMObjectsInfoByType(typeInfo.getTypeId());
190 208