Authored by zhengyouwei

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

... ... @@ -18,6 +18,10 @@
<artifactId>monitor-service-common</artifactId>
</dependency>
<dependency>
<groupId>monitor-service</groupId>
<artifactId>monitor-service-cmdb</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
... ...
package com.monitor.monit.Job;
import com.model.HostInfo;
import com.monitor.cmdb.service.IHostInfoService;
import com.monitor.common.util.SpringContextUtils;
import com.monitor.monit.constant.MConstants;
import com.monitor.monit.model.*;
import com.monitor.mysql.mapper.HostInfoMapper;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
... ... @@ -66,7 +70,7 @@ public class CollectorJob implements Callable {
DEBUG.warn("host {} service {} occur event {}", mMonit.getServer().getHttpd().getAddress(), mMonit.getEvent().getService(), mMonit.getEvent().getMessage());
}
synchronized (mMonit.getId().intern()) {
synchronized (MConstants.LOCK.intern()) {
MMonitInfo mMonitInfo = MConstants.HOSTSINFO_MAPPER.get(mMonit.getId());
if (null != mMonitInfo) {
... ... @@ -76,6 +80,8 @@ public class CollectorJob implements Callable {
//insert mMonitInfo
insertMMonitInfo(mMonitInfo, mMonit);
}
updateHostInfo(mMonit);
}
}
... ... @@ -241,6 +247,8 @@ public class CollectorJob implements Callable {
if (StringUtils.equals(MConstants.SYSTEMTYPE, oneService.getType())) {
oneServiceInfo.setSystem(true);
//system 服务过滤
continue;
}
if (StringUtils.equals(MConstants.MONITED, oneService.getMonitor()) && StringUtils.equals(MConstants.NORMALSTATUS, oneService.getStatus())) {
... ... @@ -249,7 +257,6 @@ public class CollectorJob implements Callable {
oneServiceInfo.setServiceStatus(ServiceStatus.UNNORMAL);
}
mMonitInfo.getServiceInfos().put(oneServiceInfo.getServiceName(), oneServiceInfo);
}
... ... @@ -266,13 +273,31 @@ public class CollectorJob implements Callable {
}
private void updateHostInfo(MMonit monit) {
if (null == monit || null == monit.getServer() || null == monit.getServer().getHttpd()) {
return;
}
public static void main(String[] args) throws DocumentException {
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>";
String ip = monit.getServer().getHttpd().getAddress();
if (StringUtils.isBlank(ip)) {
return;
}
}
MHostInfo mHostInfo = new MHostInfo();
HostInfoMapper hostInfoMapper = SpringContextUtils.getBeanByClass(HostInfoMapper.class);
HostInfo hostInfo = hostInfoMapper.selectByHostIp(ip);
mHostInfo.setTags(hostInfo.getTags());
mHostInfo.setMonitId(monit.getId());
mHostInfo.setIp(ip);
MConstants.MHOST_Mapper.put(ip, mHostInfo);
}
}
... ...
package com.monitor.monit.constant;
import com.monitor.monit.model.MHostInfo;
import com.monitor.monit.model.MMonitInfo;
import org.codehaus.jackson.map.ObjectMapper;
... ... @@ -14,6 +15,8 @@ public interface MConstants {
ConcurrentHashMap<String, MMonitInfo> HOSTSINFO_MAPPER = new ConcurrentHashMap<>();
ConcurrentHashMap<String, MHostInfo> MHOST_Mapper = new ConcurrentHashMap<>();
Integer HTTPTIMEOUT = 2;
String SYSTEMTYPE = "5";
... ... @@ -21,4 +24,6 @@ public interface MConstants {
String MONITED = "1";
String NORMALSTATUS = "0";
String LOCK = "LOCK";
}
... ...
package com.monitor.monit.model;
import lombok.Data;
import java.util.List;
/**
* Created by yoho on 2016/11/1.
*/
@Data
public class MHostInfo {
private String ip;
private String tags;
private String monitId;
}
... ...
package com.monitor.monit.model;
import org.apache.commons.lang.StringUtils;
import java.util.Comparator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by yoho on 2016/11/1.
*/
public class MHostInfoComparator implements Comparator<MHostInfo> {
@Override
public int compare(MHostInfo o1, MHostInfo o2) {
long long1 = ipToLong((String) o1.getIp());
long long2 = ipToLong((String) o2.getIp());
if (long1 > long2) {
return 1;
} else if(long1 < long2){
return -1;
}else {
return 0;
}
}
public long ipToLong(String ipAddress) {
long result = 0;
String[] ipAddressInArray = ipAddress.split("\\.");
for (int i = 3; i >= 0; i--) {
long ip = Long.parseLong(ipAddressInArray[3 - i]);
result |= ip << (i * 8);
}
return result;
}
public static boolean isIP(String addr) {
if (addr.length() < 7 || addr.length() > 15 || StringUtils.isBlank(addr)) {
return false;
}
/**
* 判断IP格式和范围
*/
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}";
Pattern pat = Pattern.compile(rexp);
Matcher mat = pat.matcher(addr);
boolean ipAddress = mat.find();
return ipAddress;
}
}
... ...
package com.monitor.monit.model;
import lombok.Data;
/**
* Created by yoho on 2016/11/2.
*/
@Data
public class MonitRequest {
private String monitorId;
private String query;
}
... ...
... ... @@ -13,6 +13,7 @@ public abstract class AbstractService {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "ops-monitor-thread");
t.setDaemon(true);
return t;
}
});
... ...
package com.monitor.monit.service;
import com.monitor.model.response.BaseResponse;
import com.monitor.monit.Job.ActionJob;
import com.monitor.monit.Job.OneJob;
import com.monitor.monit.constant.MConstants;
... ... @@ -23,7 +24,7 @@ public class SHostService extends AbstractService {
private static final Logger DEBUG = LoggerFactory.getLogger(SHostService.class);
@RequestMapping(value = "/action", method = RequestMethod.POST)
public void doActionHandle(@RequestBody String info) {
public void doActionHandle(@RequestBody String info) {
DEBUG.info("Received actions msg {}", info);
MActions actions;
... ... @@ -54,25 +55,34 @@ public class SHostService extends AbstractService {
}
@RequestMapping(value = "/oneAction", method = RequestMethod.POST)
public void doOneActionHandle(@RequestBody MAction action) {
public BaseResponse doOneActionHandle(@RequestBody MAction action) {
DEBUG.info("Received action msg {}", action);
if (null == action) {
DEBUG.warn("Null action found in actions, do nothing...");
return;
return new BaseResponse();
}
//2s timeout
service.submit(new OneJob(new ActionJob(action), MConstants.HTTPTIMEOUT * 1000));
return new BaseResponse();
}
@RequestMapping(value = "/monit/hosts/delete/{hostId}", method = RequestMethod.POST)
public void doDeleteHandle(@PathVariable("hostId") String hostId) {
public BaseResponse doDeleteHandle(@PathVariable("hostId") String hostId) {
DEBUG.info("Received delete host {} action.", hostId);
if (StringUtils.isNotBlank(hostId)) {
String ip = MConstants.HOSTSINFO_MAPPER.get(hostId).getHttpInfo().getAddress();
MConstants.MHOST_Mapper.remove(ip);
MConstants.HOSTSINFO_MAPPER.remove(hostId);
}
return new BaseResponse();
}
}
... ...
... ... @@ -2,15 +2,11 @@ package com.monitor.monit.service;
import com.monitor.model.response.BaseResponse;
import com.monitor.monit.constant.MConstants;
import com.monitor.monit.model.MMonitInfo;
import com.monitor.monit.model.MServiceInfo;
import com.monitor.monit.model.ServiceResponse;
import com.monitor.monit.model.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.*;
... ... @@ -31,42 +27,50 @@ public class SStatuService extends AbstractService {
BaseResponse response = new BaseResponse();
response.setData(buildServiceResponse());
List<ServiceResponse> servicesList = new ArrayList<>();
return response;
for (Map.Entry<String, MMonitInfo> entry : MConstants.HOSTSINFO_MAPPER.entrySet()) {
}
ServiceResponse serviceResponse = buildServiceResponse(entry.getValue());
if (null == serviceResponse) {
continue;
}
private List<ServiceResponse> buildServiceResponse() {
List<ServiceResponse> servicesList=new ArrayList<>();
servicesList.add(serviceResponse);
}
for (Map.Entry<String, MMonitInfo> entry : MConstants.HOSTSINFO_MAPPER.entrySet()) {
response.setData(servicesList);
ServiceResponse serviceResponse=new ServiceResponse();
return response;
serviceResponse.setMonitorId(entry.getKey());
}
serviceResponse.setHttpInfo(entry.getValue().getHttpInfo());
List<MServiceInfo> serviceInfos=new ArrayList<>();
private ServiceResponse buildServiceResponse(MMonitInfo mMonitInfo) {
for (Map.Entry<String,MServiceInfo> serviceInfoEntry:entry.getValue().getServiceInfos().entrySet())
{
serviceInfos.add(serviceInfoEntry.getValue());
}
if (null == mMonitInfo) {
return null;
}
serviceResponse.setServicesInfo(serviceInfos);
ServiceResponse serviceResponse = new ServiceResponse();
serviceResponse.setEventsInfo(entry.getValue().getEvents());
serviceResponse.setMonitorId(mMonitInfo.getMonitorId());
servicesList.add(serviceResponse);
serviceResponse.setHttpInfo(mMonitInfo.getHttpInfo());
List<MServiceInfo> serviceInfos = new ArrayList<>();
for (Map.Entry<String, MServiceInfo> serviceInfoEntry : mMonitInfo.getServiceInfos().entrySet()) {
serviceInfos.add(serviceInfoEntry.getValue());
}
return servicesList;
}
serviceResponse.setServicesInfo(serviceInfos);
serviceResponse.setEventsInfo(mMonitInfo.getEvents());
return serviceResponse;
}
@RequestMapping(value = "/host/{hostId}", method = RequestMethod.GET)
... ... @@ -91,4 +95,92 @@ public class SStatuService extends AbstractService {
return null;
}
@RequestMapping(value = "/allMonit")
public BaseResponse fetchAllMonit(@RequestBody MonitRequest request) {
BaseResponse baseResponse = new BaseResponse();
List<MHostInfo> mHostInfos = new ArrayList<>();
if (StringUtils.isBlank(request.getQuery()) || StringUtils.equals(";", request.getQuery().trim())) {
mHostInfos.addAll(MConstants.MHOST_Mapper.values());
} else {
String[] querys = request.getQuery().split(";");
if (StringUtils.isBlank(querys[0])) {
mHostInfos = queryMHostByTags(querys[1]);
} else {
mHostInfos = queryMHostByIp(querys[0]);
}
}
Collections.sort(mHostInfos, new MHostInfoComparator());
baseResponse.setData(mHostInfos);
return baseResponse;
}
/* @RequestMapping(value = "/searchMonit")
public BaseResponse fetchAllMonit(MonitRequest query) {
BaseResponse baseResponse = new BaseResponse();
List<MHostInfo> mHostInfos = new ArrayList<>();
String ip = StringUtils.trim(query.getQuery());
if (MHostInfoComparator.isIP(ip)) {
//根据Ip查询
mHostInfos = queryMHostByIp(ip);
} else {
//根据tag查询
mHostInfos = queryMHostByTags(ip);
}
Collections.sort(mHostInfos, new MHostInfoComparator());
baseResponse.setData(mHostInfos);
return baseResponse;
}*/
@RequestMapping(value = "/searchService")
public BaseResponse fetchService(@RequestBody MonitRequest request) {
ServiceResponse serviceResponse = buildServiceResponse(MConstants.HOSTSINFO_MAPPER.get(request.getMonitorId()));
BaseResponse response = new BaseResponse();
if (null != serviceResponse) {
response.setData(serviceResponse);
}
return response;
}
private List<MHostInfo> queryMHostByIp(String ip) {
List<MHostInfo> mHostInfos = new ArrayList<>();
if (MConstants.MHOST_Mapper.containsKey(ip)) {
mHostInfos.add(MConstants.MHOST_Mapper.get(ip));
}
return mHostInfos;
}
private List<MHostInfo> queryMHostByTags(String ip) {
List<MHostInfo> mHostInfos = new ArrayList<>();
synchronized (MConstants.LOCK.intern()) {
for (Map.Entry<String, MHostInfo> entry : MConstants.MHOST_Mapper.entrySet()) {
String tags = entry.getValue().getTags();
//tag满足要求
if (StringUtils.isNotBlank(tags) && StringUtils.contains(StringUtils.lowerCase(tags), StringUtils.lowerCase(ip))) {
mHostInfos.add(entry.getValue());
}
}
}
return mHostInfos;
}
}
... ...