Authored by zhengyouwei

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

@@ -18,6 +18,10 @@ @@ -18,6 +18,10 @@
18 <artifactId>monitor-service-common</artifactId> 18 <artifactId>monitor-service-common</artifactId>
19 </dependency> 19 </dependency>
20 <dependency> 20 <dependency>
  21 + <groupId>monitor-service</groupId>
  22 + <artifactId>monitor-service-cmdb</artifactId>
  23 + </dependency>
  24 + <dependency>
21 <groupId>org.slf4j</groupId> 25 <groupId>org.slf4j</groupId>
22 <artifactId>slf4j-api</artifactId> 26 <artifactId>slf4j-api</artifactId>
23 </dependency> 27 </dependency>
1 package com.monitor.monit.Job; 1 package com.monitor.monit.Job;
2 2
3 3
  4 +import com.model.HostInfo;
  5 +import com.monitor.cmdb.service.IHostInfoService;
  6 +import com.monitor.common.util.SpringContextUtils;
4 import com.monitor.monit.constant.MConstants; 7 import com.monitor.monit.constant.MConstants;
5 import com.monitor.monit.model.*; 8 import com.monitor.monit.model.*;
  9 +import com.monitor.mysql.mapper.HostInfoMapper;
6 import lombok.Getter; 10 import lombok.Getter;
7 import lombok.Setter; 11 import lombok.Setter;
8 -import org.apache.commons.lang3.StringUtils; 12 +import org.apache.commons.lang.StringUtils;
9 import org.dom4j.Document; 13 import org.dom4j.Document;
10 import org.dom4j.DocumentException; 14 import org.dom4j.DocumentException;
11 import org.dom4j.Element; 15 import org.dom4j.Element;
@@ -66,7 +70,7 @@ public class CollectorJob implements Callable { @@ -66,7 +70,7 @@ public class CollectorJob implements Callable {
66 DEBUG.warn("host {} service {} occur event {}", mMonit.getServer().getHttpd().getAddress(), mMonit.getEvent().getService(), mMonit.getEvent().getMessage()); 70 DEBUG.warn("host {} service {} occur event {}", mMonit.getServer().getHttpd().getAddress(), mMonit.getEvent().getService(), mMonit.getEvent().getMessage());
67 } 71 }
68 72
69 - synchronized (mMonit.getId().intern()) { 73 + synchronized (MConstants.LOCK.intern()) {
70 MMonitInfo mMonitInfo = MConstants.HOSTSINFO_MAPPER.get(mMonit.getId()); 74 MMonitInfo mMonitInfo = MConstants.HOSTSINFO_MAPPER.get(mMonit.getId());
71 75
72 if (null != mMonitInfo) { 76 if (null != mMonitInfo) {
@@ -76,6 +80,8 @@ public class CollectorJob implements Callable { @@ -76,6 +80,8 @@ public class CollectorJob implements Callable {
76 //insert mMonitInfo 80 //insert mMonitInfo
77 insertMMonitInfo(mMonitInfo, mMonit); 81 insertMMonitInfo(mMonitInfo, mMonit);
78 } 82 }
  83 +
  84 + updateHostInfo(mMonit);
79 } 85 }
80 } 86 }
81 87
@@ -241,6 +247,8 @@ public class CollectorJob implements Callable { @@ -241,6 +247,8 @@ public class CollectorJob implements Callable {
241 247
242 if (StringUtils.equals(MConstants.SYSTEMTYPE, oneService.getType())) { 248 if (StringUtils.equals(MConstants.SYSTEMTYPE, oneService.getType())) {
243 oneServiceInfo.setSystem(true); 249 oneServiceInfo.setSystem(true);
  250 + //system 服务过滤
  251 + continue;
244 } 252 }
245 253
246 if (StringUtils.equals(MConstants.MONITED, oneService.getMonitor()) && StringUtils.equals(MConstants.NORMALSTATUS, oneService.getStatus())) { 254 if (StringUtils.equals(MConstants.MONITED, oneService.getMonitor()) && StringUtils.equals(MConstants.NORMALSTATUS, oneService.getStatus())) {
@@ -249,7 +257,6 @@ public class CollectorJob implements Callable { @@ -249,7 +257,6 @@ public class CollectorJob implements Callable {
249 oneServiceInfo.setServiceStatus(ServiceStatus.UNNORMAL); 257 oneServiceInfo.setServiceStatus(ServiceStatus.UNNORMAL);
250 } 258 }
251 259
252 -  
253 mMonitInfo.getServiceInfos().put(oneServiceInfo.getServiceName(), oneServiceInfo); 260 mMonitInfo.getServiceInfos().put(oneServiceInfo.getServiceName(), oneServiceInfo);
254 } 261 }
255 262
@@ -266,13 +273,31 @@ public class CollectorJob implements Callable { @@ -266,13 +273,31 @@ public class CollectorJob implements Callable {
266 273
267 } 274 }
268 275
  276 + private void updateHostInfo(MMonit monit) {
  277 + if (null == monit || null == monit.getServer() || null == monit.getServer().getHttpd()) {
  278 + return;
  279 + }
269 280
270 - public static void main(String[] args) throws DocumentException {  
271 - String xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><monit id=\"27603e5eecd8169c0bf0b0bb327b6c04\" incarnation=\"1477636106\" version=\"5.17.1\"><server><uptime>2350</uptime><poll>30</poll><startdelay>0</startdelay><localhostname>java_76</localhostname><controlfile>/Data/local/monit/monitrc</controlfile><httpd><address>192.168.102.76</address><port>2812</port><ssl>0</ssl></httpd></server><platform><name>Linux</name><release>2.6.32-642.el6.x86_64</release><version>#1 SMP Tue May 10 17:27:01 UTC 2016</version><machine>x86_64</machine><cpu>2</cpu><memory>7514536</memory><swap>4063228</swap></platform><services><service name=\"java_76\"><type>5</type><collected_sec>1477636141</collected_sec><collected_usec>936584</collected_usec><status>0</status><status_hint>0</status_hint><monitor>0</monitor><monitormode>0</monitormode><pendingaction>0</pendingaction></service></services><servicegroups></servicegroups><event><collected_sec>1477636170</collected_sec><collected_usec>361266</collected_usec><service>java_76</service><type>5</type><id>131072</id><state>2</state><action>1</action><message><![CDATA[stop action done]]></message></event></monit>"; 281 + String ip = monit.getServer().getHttpd().getAddress();
272 282
  283 + if (StringUtils.isBlank(ip)) {
  284 + return;
  285 + }
273 286
274 - } 287 + MHostInfo mHostInfo = new MHostInfo();
275 288
  289 + HostInfoMapper hostInfoMapper = SpringContextUtils.getBeanByClass(HostInfoMapper.class);
276 290
  291 + HostInfo hostInfo = hostInfoMapper.selectByHostIp(ip);
  292 +
  293 + mHostInfo.setTags(hostInfo.getTags());
  294 +
  295 + mHostInfo.setMonitId(monit.getId());
  296 +
  297 + mHostInfo.setIp(ip);
  298 +
  299 + MConstants.MHOST_Mapper.put(ip, mHostInfo);
  300 +
  301 + }
277 } 302 }
278 303
1 package com.monitor.monit.constant; 1 package com.monitor.monit.constant;
2 2
  3 +import com.monitor.monit.model.MHostInfo;
3 import com.monitor.monit.model.MMonitInfo; 4 import com.monitor.monit.model.MMonitInfo;
4 import org.codehaus.jackson.map.ObjectMapper; 5 import org.codehaus.jackson.map.ObjectMapper;
5 6
@@ -14,6 +15,8 @@ public interface MConstants { @@ -14,6 +15,8 @@ public interface MConstants {
14 15
15 ConcurrentHashMap<String, MMonitInfo> HOSTSINFO_MAPPER = new ConcurrentHashMap<>(); 16 ConcurrentHashMap<String, MMonitInfo> HOSTSINFO_MAPPER = new ConcurrentHashMap<>();
16 17
  18 + ConcurrentHashMap<String, MHostInfo> MHOST_Mapper = new ConcurrentHashMap<>();
  19 +
17 Integer HTTPTIMEOUT = 2; 20 Integer HTTPTIMEOUT = 2;
18 21
19 String SYSTEMTYPE = "5"; 22 String SYSTEMTYPE = "5";
@@ -21,4 +24,6 @@ public interface MConstants { @@ -21,4 +24,6 @@ public interface MConstants {
21 String MONITED = "1"; 24 String MONITED = "1";
22 25
23 String NORMALSTATUS = "0"; 26 String NORMALSTATUS = "0";
  27 +
  28 + String LOCK = "LOCK";
24 } 29 }
  1 +package com.monitor.monit.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +import java.util.List;
  6 +
  7 +/**
  8 + * Created by yoho on 2016/11/1.
  9 + */
  10 +@Data
  11 +public class MHostInfo {
  12 + private String ip;
  13 +
  14 + private String tags;
  15 +
  16 + private String monitId;
  17 +}
  1 +package com.monitor.monit.model;
  2 +
  3 +import org.apache.commons.lang.StringUtils;
  4 +
  5 +import java.util.Comparator;
  6 +import java.util.regex.Matcher;
  7 +import java.util.regex.Pattern;
  8 +
  9 +/**
  10 + * Created by yoho on 2016/11/1.
  11 + */
  12 +public class MHostInfoComparator implements Comparator<MHostInfo> {
  13 +
  14 + @Override
  15 + public int compare(MHostInfo o1, MHostInfo o2) {
  16 + long long1 = ipToLong((String) o1.getIp());
  17 +
  18 + long long2 = ipToLong((String) o2.getIp());
  19 +
  20 + if (long1 > long2) {
  21 + return 1;
  22 + } else if(long1 < long2){
  23 + return -1;
  24 + }else {
  25 + return 0;
  26 + }
  27 + }
  28 +
  29 +
  30 + public long ipToLong(String ipAddress) {
  31 + long result = 0;
  32 + String[] ipAddressInArray = ipAddress.split("\\.");
  33 +
  34 + for (int i = 3; i >= 0; i--) {
  35 + long ip = Long.parseLong(ipAddressInArray[3 - i]);
  36 + result |= ip << (i * 8);
  37 + }
  38 +
  39 + return result;
  40 + }
  41 +
  42 + public static boolean isIP(String addr) {
  43 + if (addr.length() < 7 || addr.length() > 15 || StringUtils.isBlank(addr)) {
  44 + return false;
  45 + }
  46 + /**
  47 + * 判断IP格式和范围
  48 + */
  49 + String rexp = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
  50 +
  51 + Pattern pat = Pattern.compile(rexp);
  52 +
  53 + Matcher mat = pat.matcher(addr);
  54 +
  55 + boolean ipAddress = mat.find();
  56 +
  57 + return ipAddress;
  58 + }
  59 +}
  1 +package com.monitor.monit.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +/**
  6 + * Created by yoho on 2016/11/2.
  7 + */
  8 +@Data
  9 +public class MonitRequest {
  10 + private String monitorId;
  11 +
  12 + private String query;
  13 +}
@@ -13,6 +13,7 @@ public abstract class AbstractService { @@ -13,6 +13,7 @@ public abstract class AbstractService {
13 @Override 13 @Override
14 public Thread newThread(Runnable r) { 14 public Thread newThread(Runnable r) {
15 Thread t = new Thread(r, "ops-monitor-thread"); 15 Thread t = new Thread(r, "ops-monitor-thread");
  16 + t.setDaemon(true);
16 return t; 17 return t;
17 } 18 }
18 }); 19 });
1 package com.monitor.monit.service; 1 package com.monitor.monit.service;
2 2
3 3
  4 +import com.monitor.model.response.BaseResponse;
4 import com.monitor.monit.Job.ActionJob; 5 import com.monitor.monit.Job.ActionJob;
5 import com.monitor.monit.Job.OneJob; 6 import com.monitor.monit.Job.OneJob;
6 import com.monitor.monit.constant.MConstants; 7 import com.monitor.monit.constant.MConstants;
@@ -23,7 +24,7 @@ public class SHostService extends AbstractService { @@ -23,7 +24,7 @@ public class SHostService extends AbstractService {
23 private static final Logger DEBUG = LoggerFactory.getLogger(SHostService.class); 24 private static final Logger DEBUG = LoggerFactory.getLogger(SHostService.class);
24 25
25 @RequestMapping(value = "/action", method = RequestMethod.POST) 26 @RequestMapping(value = "/action", method = RequestMethod.POST)
26 - public void doActionHandle(@RequestBody String info) { 27 + public void doActionHandle(@RequestBody String info) {
27 DEBUG.info("Received actions msg {}", info); 28 DEBUG.info("Received actions msg {}", info);
28 29
29 MActions actions; 30 MActions actions;
@@ -54,25 +55,34 @@ public class SHostService extends AbstractService { @@ -54,25 +55,34 @@ public class SHostService extends AbstractService {
54 } 55 }
55 56
56 @RequestMapping(value = "/oneAction", method = RequestMethod.POST) 57 @RequestMapping(value = "/oneAction", method = RequestMethod.POST)
57 - public void doOneActionHandle(@RequestBody MAction action) { 58 + public BaseResponse doOneActionHandle(@RequestBody MAction action) {
58 DEBUG.info("Received action msg {}", action); 59 DEBUG.info("Received action msg {}", action);
59 60
60 if (null == action) { 61 if (null == action) {
61 DEBUG.warn("Null action found in actions, do nothing..."); 62 DEBUG.warn("Null action found in actions, do nothing...");
62 - return; 63 + return new BaseResponse();
63 } 64 }
64 65
65 //2s timeout 66 //2s timeout
66 service.submit(new OneJob(new ActionJob(action), MConstants.HTTPTIMEOUT * 1000)); 67 service.submit(new OneJob(new ActionJob(action), MConstants.HTTPTIMEOUT * 1000));
  68 +
  69 + return new BaseResponse();
67 } 70 }
68 71
69 @RequestMapping(value = "/monit/hosts/delete/{hostId}", method = RequestMethod.POST) 72 @RequestMapping(value = "/monit/hosts/delete/{hostId}", method = RequestMethod.POST)
70 - public void doDeleteHandle(@PathVariable("hostId") String hostId) { 73 + public BaseResponse doDeleteHandle(@PathVariable("hostId") String hostId) {
71 74
72 DEBUG.info("Received delete host {} action.", hostId); 75 DEBUG.info("Received delete host {} action.", hostId);
73 76
74 if (StringUtils.isNotBlank(hostId)) { 77 if (StringUtils.isNotBlank(hostId)) {
  78 +
  79 + String ip = MConstants.HOSTSINFO_MAPPER.get(hostId).getHttpInfo().getAddress();
  80 +
  81 + MConstants.MHOST_Mapper.remove(ip);
  82 +
75 MConstants.HOSTSINFO_MAPPER.remove(hostId); 83 MConstants.HOSTSINFO_MAPPER.remove(hostId);
76 } 84 }
  85 +
  86 + return new BaseResponse();
77 } 87 }
78 } 88 }
@@ -2,15 +2,11 @@ package com.monitor.monit.service; @@ -2,15 +2,11 @@ package com.monitor.monit.service;
2 2
3 import com.monitor.model.response.BaseResponse; 3 import com.monitor.model.response.BaseResponse;
4 import com.monitor.monit.constant.MConstants; 4 import com.monitor.monit.constant.MConstants;
5 -import com.monitor.monit.model.MMonitInfo;  
6 -import com.monitor.monit.model.MServiceInfo;  
7 -import com.monitor.monit.model.ServiceResponse; 5 +import com.monitor.monit.model.*;
  6 +import org.apache.commons.lang.StringUtils;
8 import org.slf4j.Logger; 7 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory; 8 import org.slf4j.LoggerFactory;
10 -import org.springframework.web.bind.annotation.PathVariable;  
11 -import org.springframework.web.bind.annotation.RequestMapping;  
12 -import org.springframework.web.bind.annotation.RequestMethod;  
13 -import org.springframework.web.bind.annotation.RestController; 9 +import org.springframework.web.bind.annotation.*;
14 10
15 import java.io.IOException; 11 import java.io.IOException;
16 import java.util.*; 12 import java.util.*;
@@ -31,42 +27,50 @@ public class SStatuService extends AbstractService { @@ -31,42 +27,50 @@ public class SStatuService extends AbstractService {
31 27
32 BaseResponse response = new BaseResponse(); 28 BaseResponse response = new BaseResponse();
33 29
34 - response.setData(buildServiceResponse()); 30 + List<ServiceResponse> servicesList = new ArrayList<>();
35 31
36 - return response; 32 + for (Map.Entry<String, MMonitInfo> entry : MConstants.HOSTSINFO_MAPPER.entrySet()) {
37 33
38 - } 34 + ServiceResponse serviceResponse = buildServiceResponse(entry.getValue());
39 35
  36 + if (null == serviceResponse) {
  37 + continue;
  38 + }
40 39
41 - private List<ServiceResponse> buildServiceResponse() {  
42 - List<ServiceResponse> servicesList=new ArrayList<>(); 40 + servicesList.add(serviceResponse);
  41 + }
43 42
44 - for (Map.Entry<String, MMonitInfo> entry : MConstants.HOSTSINFO_MAPPER.entrySet()) { 43 + response.setData(servicesList);
45 44
46 - ServiceResponse serviceResponse=new ServiceResponse(); 45 + return response;
47 46
48 - serviceResponse.setMonitorId(entry.getKey()); 47 + }
49 48
50 - serviceResponse.setHttpInfo(entry.getValue().getHttpInfo());  
51 49
52 - List<MServiceInfo> serviceInfos=new ArrayList<>(); 50 + private ServiceResponse buildServiceResponse(MMonitInfo mMonitInfo) {
53 51
54 - for (Map.Entry<String,MServiceInfo> serviceInfoEntry:entry.getValue().getServiceInfos().entrySet())  
55 - {  
56 - serviceInfos.add(serviceInfoEntry.getValue());  
57 - } 52 + if (null == mMonitInfo) {
  53 + return null;
  54 + }
58 55
59 - serviceResponse.setServicesInfo(serviceInfos); 56 + ServiceResponse serviceResponse = new ServiceResponse();
60 57
61 - serviceResponse.setEventsInfo(entry.getValue().getEvents()); 58 + serviceResponse.setMonitorId(mMonitInfo.getMonitorId());
62 59
63 - servicesList.add(serviceResponse); 60 + serviceResponse.setHttpInfo(mMonitInfo.getHttpInfo());
  61 +
  62 + List<MServiceInfo> serviceInfos = new ArrayList<>();
  63 +
  64 + for (Map.Entry<String, MServiceInfo> serviceInfoEntry : mMonitInfo.getServiceInfos().entrySet()) {
  65 + serviceInfos.add(serviceInfoEntry.getValue());
64 } 66 }
65 67
66 - return servicesList;  
67 - } 68 + serviceResponse.setServicesInfo(serviceInfos);
68 69
  70 + serviceResponse.setEventsInfo(mMonitInfo.getEvents());
69 71
  72 + return serviceResponse;
  73 + }
70 74
71 75
72 @RequestMapping(value = "/host/{hostId}", method = RequestMethod.GET) 76 @RequestMapping(value = "/host/{hostId}", method = RequestMethod.GET)
@@ -91,4 +95,92 @@ public class SStatuService extends AbstractService { @@ -91,4 +95,92 @@ public class SStatuService extends AbstractService {
91 return null; 95 return null;
92 } 96 }
93 97
  98 +
  99 + @RequestMapping(value = "/allMonit")
  100 + public BaseResponse fetchAllMonit(@RequestBody MonitRequest request) {
  101 +
  102 + BaseResponse baseResponse = new BaseResponse();
  103 +
  104 + List<MHostInfo> mHostInfos = new ArrayList<>();
  105 +
  106 + if (StringUtils.isBlank(request.getQuery()) || StringUtils.equals(";", request.getQuery().trim())) {
  107 + mHostInfos.addAll(MConstants.MHOST_Mapper.values());
  108 + } else {
  109 + String[] querys = request.getQuery().split(";");
  110 +
  111 + if (StringUtils.isBlank(querys[0])) {
  112 + mHostInfos = queryMHostByTags(querys[1]);
  113 + } else {
  114 + mHostInfos = queryMHostByIp(querys[0]);
  115 + }
  116 + }
  117 +
  118 + Collections.sort(mHostInfos, new MHostInfoComparator());
  119 +
  120 + baseResponse.setData(mHostInfos);
  121 +
  122 + return baseResponse;
  123 + }
  124 +
  125 + /* @RequestMapping(value = "/searchMonit")
  126 + public BaseResponse fetchAllMonit(MonitRequest query) {
  127 + BaseResponse baseResponse = new BaseResponse();
  128 +
  129 + List<MHostInfo> mHostInfos = new ArrayList<>();
  130 +
  131 + String ip = StringUtils.trim(query.getQuery());
  132 +
  133 + if (MHostInfoComparator.isIP(ip)) {
  134 + //根据Ip查询
  135 + mHostInfos = queryMHostByIp(ip);
  136 + } else {
  137 + //根据tag查询
  138 + mHostInfos = queryMHostByTags(ip);
  139 + }
  140 +
  141 + Collections.sort(mHostInfos, new MHostInfoComparator());
  142 +
  143 + baseResponse.setData(mHostInfos);
  144 +
  145 + return baseResponse;
  146 + }*/
  147 +
  148 + @RequestMapping(value = "/searchService")
  149 + public BaseResponse fetchService(@RequestBody MonitRequest request) {
  150 + ServiceResponse serviceResponse = buildServiceResponse(MConstants.HOSTSINFO_MAPPER.get(request.getMonitorId()));
  151 +
  152 + BaseResponse response = new BaseResponse();
  153 +
  154 + if (null != serviceResponse) {
  155 + response.setData(serviceResponse);
  156 + }
  157 +
  158 + return response;
  159 + }
  160 +
  161 + private List<MHostInfo> queryMHostByIp(String ip) {
  162 + List<MHostInfo> mHostInfos = new ArrayList<>();
  163 +
  164 + if (MConstants.MHOST_Mapper.containsKey(ip)) {
  165 + mHostInfos.add(MConstants.MHOST_Mapper.get(ip));
  166 + }
  167 +
  168 + return mHostInfos;
  169 + }
  170 +
  171 + private List<MHostInfo> queryMHostByTags(String ip) {
  172 + List<MHostInfo> mHostInfos = new ArrayList<>();
  173 +
  174 + synchronized (MConstants.LOCK.intern()) {
  175 + for (Map.Entry<String, MHostInfo> entry : MConstants.MHOST_Mapper.entrySet()) {
  176 + String tags = entry.getValue().getTags();
  177 + //tag满足要求
  178 + if (StringUtils.isNotBlank(tags) && StringUtils.contains(StringUtils.lowerCase(tags), StringUtils.lowerCase(ip))) {
  179 + mHostInfos.add(entry.getValue());
  180 + }
  181 + }
  182 + }
  183 + return mHostInfos;
  184 + }
  185 +
94 } 186 }