Authored by bblu

Merge branch 'master' into switch

Showing 36 changed files with 1083 additions and 22 deletions
... ... @@ -28,7 +28,12 @@ public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAcc
public Map<String,NewJavaApiInfoRep> getBaseDataByContext(NewJavaApiInfoReq req) {
Map<String,NewJavaApiInfoRep> map = new HashMap<>();
String sql = String.format("select count(cost),mean(cost) from service_access where context = '%s' and time > '%s' and time < '%s' group by hostAddress",req.getServiceName(),req.getStartTime(),req.getEndTime());
String sql = "";
if(req.getServiceType() == -1){
sql = String.format("select count(cost),mean(cost) from service_access where time > '%s' and time < '%s' group by hostAddress",req.getStartTime(),req.getEndTime());
}else{
sql = String.format("select count(cost),mean(cost) from service_access where context = '%s' and time > '%s' and time < '%s' group by hostAddress",req.getServiceName(),req.getStartTime(),req.getEndTime());
}
map.putAll(getDataByContext(InfluxDBContants.AWS,sql));
map.putAll(getDataByContext(InfluxDBContants.Q_CLOUD,sql));
... ... @@ -60,7 +65,13 @@ public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAcc
@Override
public Map<String,List<String>> getTimeoutInfo(NewJavaApiInfoReq req) {
Map<String,List<String>> map = new HashMap();
String sql = String.format("select ip,stack from service_access where context='%s' and cost > 200 and time > '%s' and time < '%s'",req.getServiceName(),req.getStartTime(),req.getEndTime());
String sql = "";
if(req.getServiceType() == -1){
sql = String.format("select ip,stack from service_access where cost > 200 and time > '%s' and time < '%s'",req.getStartTime(),req.getEndTime());
}else{
sql = String.format("select ip,stack from service_access where context='%s' and cost > 200 and time > '%s' and time < '%s'",req.getServiceName(),req.getStartTime(),req.getEndTime());
}
map.putAll(getTimeoutInfo(InfluxDBContants.AWS,sql));
map.putAll(getTimeoutInfo(InfluxDBContants.Q_CLOUD,sql));
return map;
... ... @@ -160,13 +171,21 @@ public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAcc
}else{
source = InfluxDBContants.AWS;
}
if(StringUtils.isNotBlank(req.getIp())){
sql = String.format("select count(cost),mean(cost) from service_access where context = '%s' and hostAddress = '%s' and time > '%s' and time < '%s' group by event",req.getServiceName(),req.getIp(),req.getStartTime(),req.getEndTime());
if(req.getServiceType() == -1){
if(StringUtils.isNotBlank(req.getIp())){
sql = String.format("select count(cost),mean(cost) from service_access where hostAddress = '%s' and time > '%s' and time < '%s' group by event,context",req.getIp(),req.getStartTime(),req.getEndTime());
}else{
sql = String.format("select count(cost),mean(cost) from service_access where time > '%s' and time < '%s' group by event,context",req.getStartTime(),req.getEndTime());
}
}else{
sql = String.format("select count(cost),mean(cost) from service_access where context = '%s' and time > '%s' and time < '%s' group by event",req.getServiceName(),req.getStartTime(),req.getEndTime());
if(StringUtils.isNotBlank(req.getIp())){
sql = String.format("select count(cost),mean(cost) from service_access where context = '%s' and hostAddress = '%s' and time > '%s' and time < '%s' group by event,context",req.getServiceName(),req.getIp(),req.getStartTime(),req.getEndTime());
}else{
sql = String.format("select count(cost),mean(cost) from service_access where context = '%s' and time > '%s' and time < '%s' group by event,context",req.getServiceName(),req.getStartTime(),req.getEndTime());
}
}
QueryResult queryResult = query(source, sql, InfluxDBContants.YOHO_EVENT);
QueryResult.Result rel = queryResult.getResults().get(0);
List<QueryResult.Series> listSeries = rel.getSeries();
... ... @@ -175,11 +194,13 @@ public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAcc
for(QueryResult.Series series : listSeries){
NewJavaApiDetailInfoRep rep = new NewJavaApiDetailInfoRep();
String apiName = series.getTags().get("event");
String serviceName = series.getTags().get("context");
Double count = (Double)series.getValues().get(0).get(1);
Double mean = (Double)series.getValues().get(0).get(2);
rep.setApiName(apiName);
rep.setTotalCount(count.intValue());
rep.setAvgCost(mean.intValue());
rep.setServiceName(serviceName);
list.add(rep);
}
... ... @@ -196,14 +217,22 @@ public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAcc
}else{
source = InfluxDBContants.AWS;
}
if(StringUtils.isNotBlank(req.getIp())){
sql = String.format("select event,stack from service_access where context='%s' and cost > 200 and time > '%s' and time < '%s' and ip = '%s' ",req.getServiceName(),req.getStartTime(),req.getEndTime(),req.getIp());
if(req.getServiceType() == -1){
if(StringUtils.isNotBlank(req.getIp())){
sql = String.format("select event,stack from service_access where context='%s' and cost > 200 and time > '%s' and time < '%s' and ip = '%s' ",req.getServiceName(),req.getStartTime(),req.getEndTime(),req.getIp());
}else{
sql = String.format("select event,stack from service_access where context='%s' and cost > 200 and time > '%s' and time < '%s'",req.getServiceName(),req.getStartTime(),req.getEndTime());
}
}else{
sql = String.format("select event,stack from service_access where context='%s' and cost > 200 and time > '%s' and time < '%s'",req.getServiceName(),req.getStartTime(),req.getEndTime());
if(StringUtils.isNotBlank(req.getIp())){
sql = String.format("select event,stack from service_access where cost > 200 and time > '%s' and time < '%s' and ip = '%s' ",req.getStartTime(),req.getEndTime(),req.getIp());
}else{
sql = String.format("select event,stack from service_access where cost > 200 and time > '%s' and time < '%s'",req.getStartTime(),req.getEndTime());
}
}
Map<String,List<String>> map = new HashMap();
QueryResult queryResult = query(source, sql, InfluxDBContants.YOHO_EVENT);
QueryResult.Result rel = queryResult.getResults().get(0);
... ...
... ... @@ -23,8 +23,13 @@ public class ServiceServerExceptionMapperImpl extends InfluxDBQuery implements S
@Override
public Map<String,List<String>> getErrorDataByContext(NewJavaApiInfoReq req) {
Map<String,List<String>> map = new HashMap();
String sql = String.format("select ip,stack from service_server_exception where context='%s' and time > '%s' and time < '%s'",req.getServiceName(),req.getStartTime(),req.getEndTime());
// String sql = "select ip,stack from service_server_exception";
String sql = "";
if(req.getServiceType() == -1){
sql = String.format("select ip,stack from service_server_exception where time > '%s' and time < '%s'",req.getStartTime(),req.getEndTime());
}else{
sql = String.format("select ip,stack from service_server_exception where context='%s' and time > '%s' and time < '%s'",req.getServiceName(),req.getStartTime(),req.getEndTime());
}
map.putAll(getDataByContext(InfluxDBContants.AWS,sql));
map.putAll(getDataByContext(InfluxDBContants.Q_CLOUD,sql));
return map;
... ... @@ -126,10 +131,18 @@ public class ServiceServerExceptionMapperImpl extends InfluxDBQuery implements S
source = InfluxDBContants.AWS;
}
if(StringUtils.isNotBlank(req.getIp())){
sql = String.format("select event,stack from service_server_exception where context='%s' and time > '%s' and time < '%s' and hostAddress = '%s'",req.getServiceName(),req.getStartTime(),req.getEndTime(),req.getIp());
if(req.getServiceType() == -1){
if(StringUtils.isNotBlank(req.getIp())){
sql = String.format("select event,stack from service_server_exception where time > '%s' and time < '%s' and hostAddress = '%s'",req.getStartTime(),req.getEndTime(),req.getIp());
}else{
sql = String.format("select event,stack from service_server_exception where time > '%s' and time < '%s'",req.getStartTime(),req.getEndTime());
}
}else{
sql = String.format("select event,stack from service_server_exception where context='%s' and time > '%s' and time < '%s'",req.getServiceName(),req.getStartTime(),req.getEndTime());
if(StringUtils.isNotBlank(req.getIp())){
sql = String.format("select event,stack from service_server_exception where context='%s' and time > '%s' and time < '%s' and hostAddress = '%s'",req.getServiceName(),req.getStartTime(),req.getEndTime(),req.getIp());
}else{
sql = String.format("select event,stack from service_server_exception where context='%s' and time > '%s' and time < '%s'",req.getServiceName(),req.getStartTime(),req.getEndTime());
}
}
QueryResult queryResult = query(source, sql, InfluxDBContants.YOMO_MONITOR);
... ...
... ... @@ -138,7 +138,22 @@ public class NewJavaApiInfoServiceImpl implements NewJavaApiInfoService {
@Override
public List<String> getIPList(NewJavaApiInfoReq req){
List<MObjectInfo> objectInfoList = mObjectInfoMapper.getTypeMosInfo(req.getServiceType());
List<MObjectInfo> objectInfoList = new ArrayList<>();
if(req.getServiceType() == -1){
List<TypeInfo> typeInfoList = mTypeInfoMapper.getChildTypesInfoByAlias("java_app");
if(CollectionUtils.isEmpty(typeInfoList)){
return null;
}
List<Integer> typeList = new ArrayList<>();
for(TypeInfo typeInfo : typeInfoList){
typeList.add(typeInfo.getTypeId());
}
objectInfoList = mObjectInfoMapper.selectDistinctHostIPByTypes(typeList);
}else{
objectInfoList = mObjectInfoMapper.getTypeMosInfo(req.getServiceType());
}
List<String> ipList = new ArrayList<>();
for(MObjectInfo mObjectInfo : objectInfoList){
... ... @@ -160,15 +175,41 @@ public class NewJavaApiInfoServiceImpl implements NewJavaApiInfoService {
//查询根据servicetype查询所有ip信息
private List<MObjectInfo> getMObjectInfoList(NewJavaApiInfoReq req){
if(req.getServiceType() == 0){
//servicetype=-1表示查询全部 0表示gateway
if(req.getServiceType() == -1){
List<TypeInfo> typeInfoList = mTypeInfoMapper.getChildTypesInfoByAlias("java_app");
if(CollectionUtils.isEmpty(typeInfoList)){
return null;
}
List<Integer> typeList = new ArrayList<>();
for(TypeInfo typeInfo : typeInfoList){
typeList.add(typeInfo.getTypeId());
}
List<MObjectInfo> objectInfoList = mObjectInfoMapper.selectMObjectsInfoByTypes(typeList);
return objectInfoList;
}
if(req.getServiceType()==0){
TypeInfo typeInfo = mTypeInfoMapper.selectTypeInfoByName(req.getServiceName());
if(typeInfo == null){
return null;
}
req.setServiceType(typeInfo.getTypeId());
}
List<MObjectInfo> objectInfoList = mObjectInfoMapper.getTypeMosInfo(req.getServiceType());
return objectInfoList;
// if(req.getServiceType() == 0){
// TypeInfo typeInfo = mTypeInfoMapper.selectTypeInfoByName(req.getServiceName());
// if(typeInfo == null){
// return null;
// }
// req.setServiceType(typeInfo.getTypeId());
// }
// List<MObjectInfo> objectInfoList = mObjectInfoMapper.getTypeMosInfo(req.getServiceType());
// return objectInfoList;
}
@Override
... ... @@ -184,6 +225,7 @@ public class NewJavaApiInfoServiceImpl implements NewJavaApiInfoService {
NewJavaApiDetailInfoRep returnRep = new NewJavaApiDetailInfoRep();
String apiName = rep.getApiName();
returnRep.setApiName(apiName);
returnRep.setServiceName(rep.getServiceName());
returnRep.setErrorInfo(exceptionMap.get(apiName));
returnRep.setTotalCount(rep.getTotalCount());
returnRep.setAvgCost(rep.getAvgCost());
... ... @@ -198,7 +240,13 @@ public class NewJavaApiInfoServiceImpl implements NewJavaApiInfoService {
@Override
public List<NewJavaApiInfoRep> queryByServiceTypeAndApiName(NewJavaApiInfoReq req) {
List<MObjectInfo> objectInfoList = getMObjectInfoList(req);
//根据serviceName查询ip列表
TypeInfo typeInfo = mTypeInfoMapper.selectTypeInfoByName(req.getServiceName());
if(typeInfo == null){
return null;
}
req.setServiceType(typeInfo.getTypeId());
List<MObjectInfo> objectInfoList = mObjectInfoMapper.getTypeMosInfo(req.getServiceType());
if(CollectionUtils.isEmpty(objectInfoList)){
return null;
}
... ...
... ... @@ -4,6 +4,7 @@ package com.monitor.model.request;
* Created by yoho on 2016/10/20.
*/
public class NewJavaApiInfoReq {
//特殊情况:-1代表全部 0代表gateway
private int serviceType;
private String serviceName;
... ...
... ... @@ -14,6 +14,8 @@ public class NewJavaApiDetailInfoRep {
private String apiName;
private String serviceName;
public Integer getTotalCount() {
return totalCount;
}
... ... @@ -61,4 +63,12 @@ public class NewJavaApiDetailInfoRep {
public void setApiName(String apiName) {
this.apiName = apiName;
}
public String getServiceName() {
return serviceName;
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
}
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>monitor-service-parent</artifactId>
<groupId>monitor-service</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>monitor-service-monit</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>monitor-service</groupId>
<artifactId>monitor-service-common</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.4.2</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
... ...
package com.monitor.monit.Job;
import com.monitor.monit.constant.MConstants;
import com.monitor.monit.model.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.Base64;
import java.util.Map;
import java.util.concurrent.Callable;
/**
* Created by yoho on 2016/5/11.
*/
public class ActionJob implements Callable {
private static final Logger DEBUG = LoggerFactory.getLogger(ActionJob.class);
private MAction actionInfo;
private ActionJob() {
}
public ActionJob(MAction actionInfo) {
this.actionInfo = actionInfo;
}
@Override
public Integer call() throws Exception {
String uri;
MMonitInfo mMonitInfo;
synchronized (actionInfo.getHostId().intern()) {
mMonitInfo = MConstants.HOSTSINFO_MAPPER.get(actionInfo.getHostId());
if (null == mMonitInfo) {
DEBUG.error("Faile to find host info with hostId {} now...", actionInfo.getHostId());
return 1;
}
uri = "http://" + mMonitInfo.getHttpInfo().getAddress() + ":" + mMonitInfo.getHttpInfo().getPort() + "/" + actionInfo.getService();
//建立httpclient 根据 host service action 操作服务
RestTemplate client = new RestTemplate();
String plainCreds = "admin:monit";
String enCreds = new String(Base64.getUrlEncoder().encode(plainCreds.getBytes("UTF-8")));
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + enCreds);
//create form data
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();
parameters.add("action", actionInfo.getAction());
HttpEntity httpEntity = new HttpEntity(parameters, headers);
ResponseEntity<String> responseEntity = client.exchange(uri, HttpMethod.POST, httpEntity, String.class);
if (HttpStatus.OK.value() != responseEntity.getStatusCode().value()) {
DEBUG.error("Failed to execute action {} in url {} with result {}", actionInfo.getService(), actionInfo.getAction(), uri, responseEntity);
return 1;
}
DEBUG.info("Success to execute action {} for url {}", actionInfo.getService(), actionInfo.getAction(), uri);
//修改缓存状态,等待更新
if (!StringUtils.equals("start", actionInfo.getAction())) {
Map<String, MServiceInfo> serviceInfoMap = mMonitInfo.getServiceInfos();
if (null != serviceInfoMap) {
MServiceInfo serviceInfo = serviceInfoMap.get(actionInfo.getService());
if (null != serviceInfo) {
serviceInfo.setServiceStatus(ServiceStatus.UNNORMAL);
}
}
}
return 0;
}
}
}
... ...
package com.monitor.monit.Job;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.monitor.monit.constant.MConstants;
import com.monitor.monit.model.*;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.Callable;
/**
* Created by yoho on 2016/5/11.
*/
public class CollectorJob implements Callable {
private final static Logger DEBUG = LoggerFactory.getLogger(CollectorJob.class);
@Getter
@Setter
private String monitorInfo;
private CollectorJob() {
}
public CollectorJob(String info) {
this.monitorInfo = info;
}
@Override
public Object call() throws Exception {
//解析xml 处理event事件
parseCollectorXml(this.monitorInfo);
return null;
}
private void parseCollectorXml(String monitorInfo) {
MMonit mMonit = null;
try {
MConstants.XMLMAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mMonit = MConstants.XMLMAPPER.readValue(monitorInfo, MMonit.class);
} catch (IOException e) {
DEBUG.error("Failed to parse xml content {} , error {}", monitorInfo, e);
mMonit = null;
}
if (null == mMonit) {
return;
}
if (null != mMonit.getEvent()) {
DEBUG.warn("host {} service {} occur event {}", mMonit.getServer().getHttpd().getAddress(), mMonit.getEvent().getService(), mMonit.getEvent().getMessage());
}
synchronized (mMonit.getId().intern()) {
MMonitInfo mMonitInfo = MConstants.HOSTSINFO_MAPPER.get(mMonit.getId());
if (null != mMonitInfo) {
//update mMonitInfo
updateMMonitInfo(mMonitInfo, mMonit);
} else {
//insert mMonitInfo
insertMMonitInfo(mMonitInfo, mMonit);
}
}
}
private void updateMMonitInfo(MMonitInfo mMonitInfo, MMonit mMonit) {
MHttpd httpInfo = mMonit.getServer().getHttpd();
if (null != httpInfo) {
mMonitInfo.setHttpInfo(httpInfo);
}
MEvent eventInfo = mMonit.getEvent();
if (null != eventInfo) {
mMonitInfo.getEvents().add(eventInfo);
}
//update service
List<MService> services = mMonit.getServices();
for (MService oneService : services) {
//update service
MServiceInfo oneServiceInfo = mMonitInfo.getServiceInfos().get(oneService.getName());
if (null == oneServiceInfo) {
oneServiceInfo = new MServiceInfo();
oneServiceInfo.setServiceName(oneService.getName());
oneServiceInfo.setSystem(false);
oneServiceInfo.setServiceStatus(ServiceStatus.UNNORMAL);
}
if (StringUtils.equals(MConstants.SYSTEMTYPE, oneService.getType())) {
oneServiceInfo.setSystem(true);
}
if (StringUtils.equals(MConstants.MONITED, oneService.getMonitor()) && StringUtils.equals(MConstants.NORMALSTATUS, oneService.getStatus())) {
oneServiceInfo.setServiceStatus(ServiceStatus.NORMAL);
} else {
oneServiceInfo.setServiceStatus(ServiceStatus.UNNORMAL);
}
mMonitInfo.getServiceInfos().put(oneServiceInfo.getServiceName(), oneServiceInfo);
}
}
private void insertMMonitInfo(MMonitInfo mMonitInfo, MMonit mMonit) {
mMonitInfo = new MMonitInfo();
mMonitInfo.setMonitorId(mMonit.getId());
updateMMonitInfo(mMonitInfo, mMonit);
MConstants.HOSTSINFO_MAPPER.put(mMonitInfo.getMonitorId(), mMonitInfo);
}
}
... ...
package com.monitor.monit.Job;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.*;
/**
* Created by yoho on 2016/5/11.
*/
public class OneJob implements Callable {
private static final Logger DEBUG = LoggerFactory.getLogger(OneJob.class);
private static final long DEFAULTIMEOUT = 10 * 1000;
private Callable jobDetail;
private long timeout;
private OneJob() {
}
public OneJob(Callable jobDetail, int timeout) {
this.jobDetail = jobDetail;
this.timeout = timeout;
}
public OneJob(Callable jobDetail) {
this.jobDetail = jobDetail;
this.timeout = DEFAULTIMEOUT;
}
@Override
public Object call() {
ExecutorService oneExecutor = Executors.newSingleThreadExecutor();
Future future = oneExecutor.submit(jobDetail);
try {
future.get(this.timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
DEBUG.error("Failed to execute job ,intrrupted error {}", e);
} catch (ExecutionException e) {
DEBUG.error("Failed to execute job ,execute error {}", e);
} catch (TimeoutException e) {
DEBUG.error("Failed to execute job ,timeout error {}", e);
}
oneExecutor.shutdownNow();
return null;
}
}
... ...
package com.monitor.monit.constant;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.monitor.monit.model.MMonitInfo;
import org.codehaus.jackson.map.ObjectMapper;
import java.util.concurrent.ConcurrentHashMap;
/**
* Created by yoho on 2016/5/11.
*/
public interface MConstants {
ObjectMapper OBJECTMAPPER = new ObjectMapper();
XmlMapper XMLMAPPER = new XmlMapper();
ConcurrentHashMap<String, MMonitInfo> HOSTSINFO_MAPPER = new ConcurrentHashMap<>();
Integer HTTPTIMEOUT = 2;
String SYSTEMTYPE = "5";
String MONITED = "1";
String NORMALSTATUS = "0";
}
... ...
package com.monitor.monit.model;
/**
* Created by yoho on 2016/5/11.
*/
public enum EAction {
start,
stop,
restart,
monitor,
unmonitor
}
... ...
package com.monitor.monit.model;
import lombok.Data;
/**
* Created by yoho on 2016/5/11.
*/
@Data
public class MAction {
private String hostId;
private String service;
private String action;
}
... ...
package com.monitor.monit.model;
import lombok.Data;
import java.util.List;
/**
* Created by yoho on 2016/5/11.
*/
@Data
public class MActions {
private List<MAction> actions;
}
... ...
package com.monitor.monit.model;
import lombok.Data;
/**
* Created by yoho on 2016/5/11.
*/
@Data
public class MEvent {
private String collected_sec;
private String service;
//服务类型 5为系统服务
private String type;
private String message;
}
... ...
package com.monitor.monit.model;
import lombok.Data;
/**
* Created by yoho on 2016/5/11.
*/
@Data
public class MHttpd {
private String address;
private String port;
private String ssl;
}
... ...
package com.monitor.monit.model;
import lombok.Data;
import java.util.List;
/**
* Created by yoho on 2016/5/11.
*/
@Data
public class MMonit {
private String id;
private MServer server;
private MPlatform platform;
private List<MService> services;
private MEvent event;
}
... ...
package com.monitor.monit.model;
import lombok.Data;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* Created by yoho on 2016/5/13.
*/
@Data
public class MMonitInfo {
private String monitorId;
private MHttpd httpInfo;
private Map<String, MServiceInfo> serviceInfos = new HashMap<String, MServiceInfo>();
private List<MEvent> events = new LinkedList<MEvent>();
}
... ...
package com.monitor.monit.model;
import lombok.Data;
/**
* Created by yoho on 2016/5/11.
*/
@Data
public class MPlatform {
private String name;
private String release;
private String version;
private String machine;
private String cpu;
private String memory;
private String swap;
}
... ...
package com.monitor.monit.model;
import lombok.Data;
/**
* Created by yoho on 2016/5/11.
*/
@Data
public class MServer {
private String localhostname;
private String controlfile;
private MHttpd httpd;
}
... ...
package com.monitor.monit.model;
import lombok.Data;
/**
* Created by yoho on 2016/5/11.
*/
@Data
public class MService {
private String name;
//5 为系统服务
private String type;
private String collected_sec;
//根据monitor状态来判断
private String status;
//monitor: 1 正常监听 非1异常监听
private String monitor;
private MSystem system;
}
... ...
package com.monitor.monit.model;
import lombok.Data;
/**
* Created by yoho on 2016/5/13.
*/
@Data
public class MServiceInfo {
private String serviceName;
private boolean isSystem = false;
private ServiceStatus serviceStatus;
}
... ...
package com.monitor.monit.model;
import lombok.Data;
/**
* Created by yoho on 2016/5/11.
*/
@Data
public class MSystem {
@Data
class MCpu {
private String user;
private String system;
private String wait;
}
@Data
class MMemory {
private String percent;
private String kilobyte;
}
@Data
class MSwap {
private String percent;
private String kilobyte;
}
private MCpu cpu;
private MMemory memory;
private MSwap swap;
}
... ...
package com.monitor.monit.model;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* Created by yoho on 2016/10/27.
*/
@Data
public class ServiceResponse {
String monitorId;
MHttpd httpInfo;
List<MServiceInfo> servicesInfo=new ArrayList<>();
List<MEvent> eventsInfo=new ArrayList<>();
}
... ...
package com.monitor.monit.model;
/**
* Created by yoho on 2016/5/13.
*/
public enum ServiceStatus {
NORMAL,
UNNORMAL
}
... ...
package com.monitor.monit.service;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
/**
* Created by yoho on 2016/5/11.
*/
public abstract class AbstractService {
protected ExecutorService service = Executors.newFixedThreadPool(20, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "ops-monitor-thread");
return t;
}
});
}
... ...
package com.monitor.monit.service;
import com.monitor.monit.Job.CollectorJob;
import com.monitor.monit.Job.OneJob;
import com.monitor.monit.constant.MConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/**
* Created by yoho on 2016/5/11.
*/
@RestController
@RequestMapping(value = "/monit")
public class SCollectorService extends AbstractService {
private static final Logger DEBUG = LoggerFactory.getLogger(SCollectorService.class);
@RequestMapping(value = "/collector", method = RequestMethod.POST)
public void doHandle(@RequestBody String info, @RequestHeader Map<String, String> headerInfo) {
DEBUG.debug("Received collector msg {} form host {}", info, headerInfo.get("Host"));
//2s timeout
service.submit(new OneJob(new CollectorJob(info), MConstants.HTTPTIMEOUT * 1000));
}
}
... ...
package com.monitor.monit.service;
import com.monitor.monit.Job.ActionJob;
import com.monitor.monit.Job.OneJob;
import com.monitor.monit.constant.MConstants;
import com.monitor.monit.model.MAction;
import com.monitor.monit.model.MActions;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
/**
* Created by yoho on 2016/5/11.
*/
@RestController
@RequestMapping("/monit/hosts")
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) {
DEBUG.info("Received actions msg {}", info);
MActions actions;
try {
actions = MConstants.OBJECTMAPPER.readValue(info, MActions.class);
} catch (IOException e) {
DEBUG.error("Failed to parse");
actions = null;
}
if (null == actions || 0 >= actions.getActions().size()) {
DEBUG.warn("Null actions found in request body, do nothing...");
return;
}
for (MAction action : actions.getActions()) {
if (null == action) {
DEBUG.warn("Null action found in actions, do nothing...");
continue;
}
//2s timeout
service.submit(new OneJob(new ActionJob(action), MConstants.HTTPTIMEOUT * 1000));
}
}
@RequestMapping(value = "/oneAction", method = RequestMethod.POST)
public void doOneActionHandle(@RequestBody MAction action) {
DEBUG.info("Received action msg {}", action);
if (null == action) {
DEBUG.warn("Null action found in actions, do nothing...");
return;
}
//2s timeout
service.submit(new OneJob(new ActionJob(action), MConstants.HTTPTIMEOUT * 1000));
}
@RequestMapping(value = "/monit/hosts/delete/{hostId}", method = RequestMethod.POST)
public void doDeleteHandle(@PathVariable("hostId") String hostId) {
DEBUG.info("Received delete host {} action.", hostId);
if (StringUtils.isNotBlank(hostId)) {
MConstants.HOSTSINFO_MAPPER.remove(hostId);
}
}
}
... ...
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 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 java.io.IOException;
import java.util.*;
/**
* Created by yoho on 2016/5/12.
* <p>
* 查询服务
*/
@RestController
@RequestMapping("/monit/service")
public class SStatuService extends AbstractService {
private static final Logger DEBUG = LoggerFactory.getLogger(SStatuService.class);
@RequestMapping(value = "/all", method = RequestMethod.GET)
public BaseResponse fetchAllService() {
BaseResponse response = new BaseResponse();
response.setData(buildServiceResponse());
return response;
}
private List<ServiceResponse> buildServiceResponse() {
List<ServiceResponse> servicesList=new ArrayList<>();
for (Map.Entry<String, MMonitInfo> entry : MConstants.HOSTSINFO_MAPPER.entrySet()) {
ServiceResponse serviceResponse=new ServiceResponse();
serviceResponse.setMonitorId(entry.getKey());
serviceResponse.setHttpInfo(entry.getValue().getHttpInfo());
List<MServiceInfo> serviceInfos=new ArrayList<>();
for (Map.Entry<String,MServiceInfo> serviceInfoEntry:entry.getValue().getServiceInfos().entrySet())
{
serviceInfos.add(serviceInfoEntry.getValue());
}
serviceResponse.setServicesInfo(serviceInfos);
serviceResponse.setEventsInfo(entry.getValue().getEvents());
servicesList.add(serviceResponse);
}
return servicesList;
}
@RequestMapping(value = "/host/{hostId}", method = RequestMethod.GET)
public String fetchHostSerivce(@PathVariable("hostId") String hostId) {
Map<String, MServiceInfo> services = null;
if (MConstants.HOSTSINFO_MAPPER.containsKey(hostId)) {
services = MConstants.HOSTSINFO_MAPPER.get(hostId).getServiceInfos();
}
if (null != services) {
try {
String content = MConstants.OBJECTMAPPER.writeValueAsString(services);
return content;
} catch (IOException e) {
DEBUG.error("Failed to parse service info...");
}
}
return null;
}
}
... ...
... ... @@ -24,5 +24,7 @@ public interface MObjectInfoMapper {
List<MObjectInfo> getTypeMosInfo(int typeId);
List<MObjectInfo> selectDistinctHostIPByTypes(List<Integer> typeIds);
/* List<MObjectInfo> getHostMosInfo(int hostId);*/
}
... ...
... ... @@ -22,4 +22,6 @@ public interface MTypeInfoMapper {
TypeInfo selectTypeInfoByName(String typeName);
List<TypeInfo> getChildTypesInfoByAlias(String alias);
}
... ...
... ... @@ -54,4 +54,18 @@
SELECT * FROM mobject_info where instr(alias, LOWER(#{alias})) &gt; 0 ORDER BY id asc
</select>
<select id="selectDistinctHostIPByTypes" resultType="com.model.MObjectInfo" resultMap="mobjectInfoMapper">
SELECT distinct host_ip FROM mobject_info
where type_id in
<foreach collection="list" open="(" close=")" separator="," item="item">
#{item}
</foreach>
order by host_ip DESC
</select>
</mapper>
\ No newline at end of file
... ...
... ... @@ -51,4 +51,12 @@
FROM type_info where alias =#{typeName} limit 1
</select>
<select id="getChildTypesInfoByAlias" resultType="com.model.TypeInfo" resultMap="typeInfoMap">
SELECT
<include refid="Base_Column_List"/>
FROM `type_info` tt WHERE tt.`parent_id` = (SELECT id FROM `type_info` t WHERE t.`alias` = #{alias})
</select>
</mapper>
\ No newline at end of file
... ...
... ... @@ -95,6 +95,11 @@
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
... ...
... ... @@ -9,16 +9,15 @@ import com.monitor.nginxsync.model.TaskInfo;
import com.monitor.nginxsync.nio.model.LogMsg;
import com.monitor.nginxsync.nio.service.LogService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.text.MessageFormat;
import java.util.logging.Logger;
/**
* Created by yoho on 2016/8/23.
... ... @@ -26,6 +25,8 @@ import java.util.logging.Logger;
@RestController
@RequestMapping(value = "/nginxsync")
public class NginxSyncService {
public static final Logger DEBUG = LoggerFactory.getLogger(NginxSyncService.class);
@Value("${nginxsync.agent}")
String agent;
... ... @@ -38,6 +39,7 @@ public class NginxSyncService {
@RequestMapping(value = "/pushtask")
public BaseResponse pushTask(@RequestBody TaskInfo taskInfo) {
DEBUG.info("Recive nginx sync task {}", taskInfo);
Preconditions.checkNotNull(taskInfo);
... ... @@ -104,7 +106,8 @@ public class NginxSyncService {
try {
restTemplate.postForObject("http://" + agent + "/handle", cmdTaskInfo, String.class);
} catch (RestClientException e) {
} catch (Exception e) {
DEBUG.error("Failed to dispatch nginx sync task {}...error {}", cmdTaskInfo, e);
response.setCode(500);
... ...
... ... @@ -66,6 +66,10 @@
<groupId>monitor-service</groupId>
<artifactId>monitor-service-compare</artifactId>
</dependency>
<!-- <dependency>
<groupId>monitor-service</groupId>
<artifactId>monitor-service-monit</artifactId>
</dependency>-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
... ...
... ... @@ -153,6 +153,11 @@
<artifactId>monitor-service-compare</artifactId>
<version>${project-version}</version>
</dependency>
<!-- <dependency>
<groupId>monitor-service</groupId>
<artifactId>monitor-service-monit</artifactId>
<version>${project-version}</version>
</dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
... ... @@ -188,6 +193,7 @@
<module>monitor-service-awstools</module>
<module>monitor-service-zabbix</module>
<module>monitor-service-compare</module>
<module>monitor-service-monit</module>
</modules>
... ...