Merge branch 'master' into switch
Showing
36 changed files
with
1083 additions
and
22 deletions
monitor-service-influxdb/src/main/java/com/monitor/influxdb/mapper/impl/ServiceAccessMapperImpl.java
@@ -28,7 +28,12 @@ public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAcc | @@ -28,7 +28,12 @@ public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAcc | ||
28 | public Map<String,NewJavaApiInfoRep> getBaseDataByContext(NewJavaApiInfoReq req) { | 28 | public Map<String,NewJavaApiInfoRep> getBaseDataByContext(NewJavaApiInfoReq req) { |
29 | 29 | ||
30 | Map<String,NewJavaApiInfoRep> map = new HashMap<>(); | 30 | Map<String,NewJavaApiInfoRep> map = new HashMap<>(); |
31 | - 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()); | 31 | + String sql = ""; |
32 | + if(req.getServiceType() == -1){ | ||
33 | + sql = String.format("select count(cost),mean(cost) from service_access where time > '%s' and time < '%s' group by hostAddress",req.getStartTime(),req.getEndTime()); | ||
34 | + }else{ | ||
35 | + 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()); | ||
36 | + } | ||
32 | 37 | ||
33 | map.putAll(getDataByContext(InfluxDBContants.AWS,sql)); | 38 | map.putAll(getDataByContext(InfluxDBContants.AWS,sql)); |
34 | map.putAll(getDataByContext(InfluxDBContants.Q_CLOUD,sql)); | 39 | map.putAll(getDataByContext(InfluxDBContants.Q_CLOUD,sql)); |
@@ -60,7 +65,13 @@ public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAcc | @@ -60,7 +65,13 @@ public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAcc | ||
60 | @Override | 65 | @Override |
61 | public Map<String,List<String>> getTimeoutInfo(NewJavaApiInfoReq req) { | 66 | public Map<String,List<String>> getTimeoutInfo(NewJavaApiInfoReq req) { |
62 | Map<String,List<String>> map = new HashMap(); | 67 | Map<String,List<String>> map = new HashMap(); |
63 | - 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()); | 68 | + String sql = ""; |
69 | + if(req.getServiceType() == -1){ | ||
70 | + sql = String.format("select ip,stack from service_access where cost > 200 and time > '%s' and time < '%s'",req.getStartTime(),req.getEndTime()); | ||
71 | + }else{ | ||
72 | + 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()); | ||
73 | + } | ||
74 | + | ||
64 | map.putAll(getTimeoutInfo(InfluxDBContants.AWS,sql)); | 75 | map.putAll(getTimeoutInfo(InfluxDBContants.AWS,sql)); |
65 | map.putAll(getTimeoutInfo(InfluxDBContants.Q_CLOUD,sql)); | 76 | map.putAll(getTimeoutInfo(InfluxDBContants.Q_CLOUD,sql)); |
66 | return map; | 77 | return map; |
@@ -160,13 +171,21 @@ public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAcc | @@ -160,13 +171,21 @@ public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAcc | ||
160 | }else{ | 171 | }else{ |
161 | source = InfluxDBContants.AWS; | 172 | source = InfluxDBContants.AWS; |
162 | } | 173 | } |
163 | - | ||
164 | - if(StringUtils.isNotBlank(req.getIp())){ | ||
165 | - 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()); | 174 | + if(req.getServiceType() == -1){ |
175 | + if(StringUtils.isNotBlank(req.getIp())){ | ||
176 | + 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()); | ||
177 | + }else{ | ||
178 | + 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()); | ||
179 | + } | ||
166 | }else{ | 180 | }else{ |
167 | - 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()); | 181 | + if(StringUtils.isNotBlank(req.getIp())){ |
182 | + 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()); | ||
183 | + }else{ | ||
184 | + 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()); | ||
185 | + } | ||
168 | } | 186 | } |
169 | 187 | ||
188 | + | ||
170 | QueryResult queryResult = query(source, sql, InfluxDBContants.YOHO_EVENT); | 189 | QueryResult queryResult = query(source, sql, InfluxDBContants.YOHO_EVENT); |
171 | QueryResult.Result rel = queryResult.getResults().get(0); | 190 | QueryResult.Result rel = queryResult.getResults().get(0); |
172 | List<QueryResult.Series> listSeries = rel.getSeries(); | 191 | List<QueryResult.Series> listSeries = rel.getSeries(); |
@@ -175,11 +194,13 @@ public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAcc | @@ -175,11 +194,13 @@ public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAcc | ||
175 | for(QueryResult.Series series : listSeries){ | 194 | for(QueryResult.Series series : listSeries){ |
176 | NewJavaApiDetailInfoRep rep = new NewJavaApiDetailInfoRep(); | 195 | NewJavaApiDetailInfoRep rep = new NewJavaApiDetailInfoRep(); |
177 | String apiName = series.getTags().get("event"); | 196 | String apiName = series.getTags().get("event"); |
197 | + String serviceName = series.getTags().get("context"); | ||
178 | Double count = (Double)series.getValues().get(0).get(1); | 198 | Double count = (Double)series.getValues().get(0).get(1); |
179 | Double mean = (Double)series.getValues().get(0).get(2); | 199 | Double mean = (Double)series.getValues().get(0).get(2); |
180 | rep.setApiName(apiName); | 200 | rep.setApiName(apiName); |
181 | rep.setTotalCount(count.intValue()); | 201 | rep.setTotalCount(count.intValue()); |
182 | rep.setAvgCost(mean.intValue()); | 202 | rep.setAvgCost(mean.intValue()); |
203 | + rep.setServiceName(serviceName); | ||
183 | list.add(rep); | 204 | list.add(rep); |
184 | } | 205 | } |
185 | 206 | ||
@@ -196,14 +217,22 @@ public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAcc | @@ -196,14 +217,22 @@ public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAcc | ||
196 | }else{ | 217 | }else{ |
197 | source = InfluxDBContants.AWS; | 218 | source = InfluxDBContants.AWS; |
198 | } | 219 | } |
199 | - | ||
200 | - if(StringUtils.isNotBlank(req.getIp())){ | ||
201 | - 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()); | 220 | + if(req.getServiceType() == -1){ |
221 | + if(StringUtils.isNotBlank(req.getIp())){ | ||
222 | + 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()); | ||
223 | + }else{ | ||
224 | + 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()); | ||
225 | + } | ||
202 | }else{ | 226 | }else{ |
203 | - 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()); | 227 | + if(StringUtils.isNotBlank(req.getIp())){ |
228 | + 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()); | ||
229 | + }else{ | ||
230 | + sql = String.format("select event,stack from service_access where cost > 200 and time > '%s' and time < '%s'",req.getStartTime(),req.getEndTime()); | ||
231 | + } | ||
204 | } | 232 | } |
205 | 233 | ||
206 | 234 | ||
235 | + | ||
207 | Map<String,List<String>> map = new HashMap(); | 236 | Map<String,List<String>> map = new HashMap(); |
208 | QueryResult queryResult = query(source, sql, InfluxDBContants.YOHO_EVENT); | 237 | QueryResult queryResult = query(source, sql, InfluxDBContants.YOHO_EVENT); |
209 | QueryResult.Result rel = queryResult.getResults().get(0); | 238 | QueryResult.Result rel = queryResult.getResults().get(0); |
@@ -23,8 +23,13 @@ public class ServiceServerExceptionMapperImpl extends InfluxDBQuery implements S | @@ -23,8 +23,13 @@ public class ServiceServerExceptionMapperImpl extends InfluxDBQuery implements S | ||
23 | @Override | 23 | @Override |
24 | public Map<String,List<String>> getErrorDataByContext(NewJavaApiInfoReq req) { | 24 | public Map<String,List<String>> getErrorDataByContext(NewJavaApiInfoReq req) { |
25 | Map<String,List<String>> map = new HashMap(); | 25 | Map<String,List<String>> map = new HashMap(); |
26 | - 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()); | ||
27 | -// String sql = "select ip,stack from service_server_exception"; | 26 | + String sql = ""; |
27 | + if(req.getServiceType() == -1){ | ||
28 | + sql = String.format("select ip,stack from service_server_exception where time > '%s' and time < '%s'",req.getStartTime(),req.getEndTime()); | ||
29 | + }else{ | ||
30 | + 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()); | ||
31 | + } | ||
32 | + | ||
28 | map.putAll(getDataByContext(InfluxDBContants.AWS,sql)); | 33 | map.putAll(getDataByContext(InfluxDBContants.AWS,sql)); |
29 | map.putAll(getDataByContext(InfluxDBContants.Q_CLOUD,sql)); | 34 | map.putAll(getDataByContext(InfluxDBContants.Q_CLOUD,sql)); |
30 | return map; | 35 | return map; |
@@ -126,10 +131,18 @@ public class ServiceServerExceptionMapperImpl extends InfluxDBQuery implements S | @@ -126,10 +131,18 @@ public class ServiceServerExceptionMapperImpl extends InfluxDBQuery implements S | ||
126 | source = InfluxDBContants.AWS; | 131 | source = InfluxDBContants.AWS; |
127 | } | 132 | } |
128 | 133 | ||
129 | - if(StringUtils.isNotBlank(req.getIp())){ | ||
130 | - 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()); | 134 | + if(req.getServiceType() == -1){ |
135 | + if(StringUtils.isNotBlank(req.getIp())){ | ||
136 | + 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()); | ||
137 | + }else{ | ||
138 | + sql = String.format("select event,stack from service_server_exception where time > '%s' and time < '%s'",req.getStartTime(),req.getEndTime()); | ||
139 | + } | ||
131 | }else{ | 140 | }else{ |
132 | - 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()); | 141 | + if(StringUtils.isNotBlank(req.getIp())){ |
142 | + 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()); | ||
143 | + }else{ | ||
144 | + 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()); | ||
145 | + } | ||
133 | } | 146 | } |
134 | 147 | ||
135 | QueryResult queryResult = query(source, sql, InfluxDBContants.YOMO_MONITOR); | 148 | QueryResult queryResult = query(source, sql, InfluxDBContants.YOMO_MONITOR); |
@@ -138,7 +138,22 @@ public class NewJavaApiInfoServiceImpl implements NewJavaApiInfoService { | @@ -138,7 +138,22 @@ public class NewJavaApiInfoServiceImpl implements NewJavaApiInfoService { | ||
138 | 138 | ||
139 | @Override | 139 | @Override |
140 | public List<String> getIPList(NewJavaApiInfoReq req){ | 140 | public List<String> getIPList(NewJavaApiInfoReq req){ |
141 | - List<MObjectInfo> objectInfoList = mObjectInfoMapper.getTypeMosInfo(req.getServiceType()); | 141 | + List<MObjectInfo> objectInfoList = new ArrayList<>(); |
142 | + if(req.getServiceType() == -1){ | ||
143 | + List<TypeInfo> typeInfoList = mTypeInfoMapper.getChildTypesInfoByAlias("java_app"); | ||
144 | + if(CollectionUtils.isEmpty(typeInfoList)){ | ||
145 | + return null; | ||
146 | + } | ||
147 | + | ||
148 | + List<Integer> typeList = new ArrayList<>(); | ||
149 | + for(TypeInfo typeInfo : typeInfoList){ | ||
150 | + typeList.add(typeInfo.getTypeId()); | ||
151 | + } | ||
152 | + objectInfoList = mObjectInfoMapper.selectDistinctHostIPByTypes(typeList); | ||
153 | + }else{ | ||
154 | + objectInfoList = mObjectInfoMapper.getTypeMosInfo(req.getServiceType()); | ||
155 | + } | ||
156 | + | ||
142 | 157 | ||
143 | List<String> ipList = new ArrayList<>(); | 158 | List<String> ipList = new ArrayList<>(); |
144 | for(MObjectInfo mObjectInfo : objectInfoList){ | 159 | for(MObjectInfo mObjectInfo : objectInfoList){ |
@@ -160,15 +175,41 @@ public class NewJavaApiInfoServiceImpl implements NewJavaApiInfoService { | @@ -160,15 +175,41 @@ public class NewJavaApiInfoServiceImpl implements NewJavaApiInfoService { | ||
160 | 175 | ||
161 | //查询根据servicetype查询所有ip信息 | 176 | //查询根据servicetype查询所有ip信息 |
162 | private List<MObjectInfo> getMObjectInfoList(NewJavaApiInfoReq req){ | 177 | private List<MObjectInfo> getMObjectInfoList(NewJavaApiInfoReq req){ |
163 | - if(req.getServiceType() == 0){ | 178 | + //servicetype=-1表示查询全部 0表示gateway |
179 | + if(req.getServiceType() == -1){ | ||
180 | + List<TypeInfo> typeInfoList = mTypeInfoMapper.getChildTypesInfoByAlias("java_app"); | ||
181 | + if(CollectionUtils.isEmpty(typeInfoList)){ | ||
182 | + return null; | ||
183 | + } | ||
184 | + | ||
185 | + List<Integer> typeList = new ArrayList<>(); | ||
186 | + for(TypeInfo typeInfo : typeInfoList){ | ||
187 | + typeList.add(typeInfo.getTypeId()); | ||
188 | + } | ||
189 | + List<MObjectInfo> objectInfoList = mObjectInfoMapper.selectMObjectsInfoByTypes(typeList); | ||
190 | + return objectInfoList; | ||
191 | + } | ||
192 | + | ||
193 | + if(req.getServiceType()==0){ | ||
164 | TypeInfo typeInfo = mTypeInfoMapper.selectTypeInfoByName(req.getServiceName()); | 194 | TypeInfo typeInfo = mTypeInfoMapper.selectTypeInfoByName(req.getServiceName()); |
165 | if(typeInfo == null){ | 195 | if(typeInfo == null){ |
166 | return null; | 196 | return null; |
167 | } | 197 | } |
168 | req.setServiceType(typeInfo.getTypeId()); | 198 | req.setServiceType(typeInfo.getTypeId()); |
169 | } | 199 | } |
200 | + | ||
170 | List<MObjectInfo> objectInfoList = mObjectInfoMapper.getTypeMosInfo(req.getServiceType()); | 201 | List<MObjectInfo> objectInfoList = mObjectInfoMapper.getTypeMosInfo(req.getServiceType()); |
171 | return objectInfoList; | 202 | return objectInfoList; |
203 | + | ||
204 | +// if(req.getServiceType() == 0){ | ||
205 | +// TypeInfo typeInfo = mTypeInfoMapper.selectTypeInfoByName(req.getServiceName()); | ||
206 | +// if(typeInfo == null){ | ||
207 | +// return null; | ||
208 | +// } | ||
209 | +// req.setServiceType(typeInfo.getTypeId()); | ||
210 | +// } | ||
211 | +// List<MObjectInfo> objectInfoList = mObjectInfoMapper.getTypeMosInfo(req.getServiceType()); | ||
212 | +// return objectInfoList; | ||
172 | } | 213 | } |
173 | 214 | ||
174 | @Override | 215 | @Override |
@@ -184,6 +225,7 @@ public class NewJavaApiInfoServiceImpl implements NewJavaApiInfoService { | @@ -184,6 +225,7 @@ public class NewJavaApiInfoServiceImpl implements NewJavaApiInfoService { | ||
184 | NewJavaApiDetailInfoRep returnRep = new NewJavaApiDetailInfoRep(); | 225 | NewJavaApiDetailInfoRep returnRep = new NewJavaApiDetailInfoRep(); |
185 | String apiName = rep.getApiName(); | 226 | String apiName = rep.getApiName(); |
186 | returnRep.setApiName(apiName); | 227 | returnRep.setApiName(apiName); |
228 | + returnRep.setServiceName(rep.getServiceName()); | ||
187 | returnRep.setErrorInfo(exceptionMap.get(apiName)); | 229 | returnRep.setErrorInfo(exceptionMap.get(apiName)); |
188 | returnRep.setTotalCount(rep.getTotalCount()); | 230 | returnRep.setTotalCount(rep.getTotalCount()); |
189 | returnRep.setAvgCost(rep.getAvgCost()); | 231 | returnRep.setAvgCost(rep.getAvgCost()); |
@@ -198,7 +240,13 @@ public class NewJavaApiInfoServiceImpl implements NewJavaApiInfoService { | @@ -198,7 +240,13 @@ public class NewJavaApiInfoServiceImpl implements NewJavaApiInfoService { | ||
198 | 240 | ||
199 | @Override | 241 | @Override |
200 | public List<NewJavaApiInfoRep> queryByServiceTypeAndApiName(NewJavaApiInfoReq req) { | 242 | public List<NewJavaApiInfoRep> queryByServiceTypeAndApiName(NewJavaApiInfoReq req) { |
201 | - List<MObjectInfo> objectInfoList = getMObjectInfoList(req); | 243 | + //根据serviceName查询ip列表 |
244 | + TypeInfo typeInfo = mTypeInfoMapper.selectTypeInfoByName(req.getServiceName()); | ||
245 | + if(typeInfo == null){ | ||
246 | + return null; | ||
247 | + } | ||
248 | + req.setServiceType(typeInfo.getTypeId()); | ||
249 | + List<MObjectInfo> objectInfoList = mObjectInfoMapper.getTypeMosInfo(req.getServiceType()); | ||
202 | if(CollectionUtils.isEmpty(objectInfoList)){ | 250 | if(CollectionUtils.isEmpty(objectInfoList)){ |
203 | return null; | 251 | return null; |
204 | } | 252 | } |
@@ -4,6 +4,7 @@ package com.monitor.model.request; | @@ -4,6 +4,7 @@ package com.monitor.model.request; | ||
4 | * Created by yoho on 2016/10/20. | 4 | * Created by yoho on 2016/10/20. |
5 | */ | 5 | */ |
6 | public class NewJavaApiInfoReq { | 6 | public class NewJavaApiInfoReq { |
7 | + //特殊情况:-1代表全部 0代表gateway | ||
7 | private int serviceType; | 8 | private int serviceType; |
8 | 9 | ||
9 | private String serviceName; | 10 | private String serviceName; |
@@ -14,6 +14,8 @@ public class NewJavaApiDetailInfoRep { | @@ -14,6 +14,8 @@ public class NewJavaApiDetailInfoRep { | ||
14 | 14 | ||
15 | private String apiName; | 15 | private String apiName; |
16 | 16 | ||
17 | + private String serviceName; | ||
18 | + | ||
17 | public Integer getTotalCount() { | 19 | public Integer getTotalCount() { |
18 | return totalCount; | 20 | return totalCount; |
19 | } | 21 | } |
@@ -61,4 +63,12 @@ public class NewJavaApiDetailInfoRep { | @@ -61,4 +63,12 @@ public class NewJavaApiDetailInfoRep { | ||
61 | public void setApiName(String apiName) { | 63 | public void setApiName(String apiName) { |
62 | this.apiName = apiName; | 64 | this.apiName = apiName; |
63 | } | 65 | } |
66 | + | ||
67 | + public String getServiceName() { | ||
68 | + return serviceName; | ||
69 | + } | ||
70 | + | ||
71 | + public void setServiceName(String serviceName) { | ||
72 | + this.serviceName = serviceName; | ||
73 | + } | ||
64 | } | 74 | } |
monitor-service-monit/pom.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
3 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
4 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
5 | + <parent> | ||
6 | + <artifactId>monitor-service-parent</artifactId> | ||
7 | + <groupId>monitor-service</groupId> | ||
8 | + <version>1.0-SNAPSHOT</version> | ||
9 | + </parent> | ||
10 | + <modelVersion>4.0.0</modelVersion> | ||
11 | + | ||
12 | + <artifactId>monitor-service-monit</artifactId> | ||
13 | + <version>1.0-SNAPSHOT</version> | ||
14 | + | ||
15 | + <dependencies> | ||
16 | + <dependency> | ||
17 | + <groupId>monitor-service</groupId> | ||
18 | + <artifactId>monitor-service-common</artifactId> | ||
19 | + </dependency> | ||
20 | + <dependency> | ||
21 | + <groupId>org.slf4j</groupId> | ||
22 | + <artifactId>slf4j-api</artifactId> | ||
23 | + </dependency> | ||
24 | + <dependency> | ||
25 | + <groupId>org.slf4j</groupId> | ||
26 | + <artifactId>jcl-over-slf4j</artifactId> | ||
27 | + </dependency> | ||
28 | + <dependency> | ||
29 | + <groupId>ch.qos.logback</groupId> | ||
30 | + <artifactId>logback-classic</artifactId> | ||
31 | + <scope>runtime</scope> | ||
32 | + </dependency> | ||
33 | + <dependency> | ||
34 | + <groupId>junit</groupId> | ||
35 | + <artifactId>junit</artifactId> | ||
36 | + <version>3.8.1</version> | ||
37 | + <scope>test</scope> | ||
38 | + </dependency> | ||
39 | + | ||
40 | + <dependency> | ||
41 | + <groupId>javax.servlet</groupId> | ||
42 | + <artifactId>javax.servlet-api</artifactId> | ||
43 | + </dependency> | ||
44 | + | ||
45 | + <dependency> | ||
46 | + <groupId>org.springframework</groupId> | ||
47 | + <artifactId>spring-core</artifactId> | ||
48 | + </dependency> | ||
49 | + | ||
50 | + <dependency> | ||
51 | + <groupId>org.springframework</groupId> | ||
52 | + <artifactId>spring-webmvc</artifactId> | ||
53 | + </dependency> | ||
54 | + | ||
55 | + <dependency> | ||
56 | + <groupId>org.springframework</groupId> | ||
57 | + <artifactId>spring-web</artifactId> | ||
58 | + </dependency> | ||
59 | + | ||
60 | + <dependency> | ||
61 | + <groupId>org.projectlombok</groupId> | ||
62 | + <artifactId>lombok</artifactId> | ||
63 | + <version>1.16.6</version> | ||
64 | + </dependency> | ||
65 | + | ||
66 | + <dependency> | ||
67 | + <groupId>org.apache.commons</groupId> | ||
68 | + <artifactId>commons-lang3</artifactId> | ||
69 | + </dependency> | ||
70 | + | ||
71 | + <dependency> | ||
72 | + <groupId>org.codehaus.jackson</groupId> | ||
73 | + <artifactId>jackson-mapper-asl</artifactId> | ||
74 | + <version>1.9.13</version> | ||
75 | + </dependency> | ||
76 | + <dependency> | ||
77 | + <groupId>org.codehaus.jackson</groupId> | ||
78 | + <artifactId>jackson-core-asl</artifactId> | ||
79 | + <version>1.9.13</version> | ||
80 | + </dependency> | ||
81 | + | ||
82 | + <dependency> | ||
83 | + <groupId>com.fasterxml.jackson.dataformat</groupId> | ||
84 | + <artifactId>jackson-dataformat-xml</artifactId> | ||
85 | + <version>2.4.2</version> | ||
86 | + </dependency> | ||
87 | + </dependencies> | ||
88 | + | ||
89 | +</project> |
1 | +package com.monitor.monit.Job; | ||
2 | + | ||
3 | +import com.monitor.monit.constant.MConstants; | ||
4 | +import com.monitor.monit.model.*; | ||
5 | +import org.apache.commons.lang.StringUtils; | ||
6 | +import org.slf4j.Logger; | ||
7 | +import org.slf4j.LoggerFactory; | ||
8 | +import org.springframework.http.*; | ||
9 | +import org.springframework.util.LinkedMultiValueMap; | ||
10 | +import org.springframework.util.MultiValueMap; | ||
11 | +import org.springframework.web.client.RestTemplate; | ||
12 | + | ||
13 | +import java.util.Base64; | ||
14 | +import java.util.Map; | ||
15 | +import java.util.concurrent.Callable; | ||
16 | + | ||
17 | +/** | ||
18 | + * Created by yoho on 2016/5/11. | ||
19 | + */ | ||
20 | +public class ActionJob implements Callable { | ||
21 | + | ||
22 | + private static final Logger DEBUG = LoggerFactory.getLogger(ActionJob.class); | ||
23 | + | ||
24 | + private MAction actionInfo; | ||
25 | + | ||
26 | + private ActionJob() { | ||
27 | + | ||
28 | + } | ||
29 | + | ||
30 | + public ActionJob(MAction actionInfo) { | ||
31 | + this.actionInfo = actionInfo; | ||
32 | + } | ||
33 | + | ||
34 | + @Override | ||
35 | + public Integer call() throws Exception { | ||
36 | + | ||
37 | + String uri; | ||
38 | + | ||
39 | + MMonitInfo mMonitInfo; | ||
40 | + | ||
41 | + synchronized (actionInfo.getHostId().intern()) { | ||
42 | + mMonitInfo = MConstants.HOSTSINFO_MAPPER.get(actionInfo.getHostId()); | ||
43 | + if (null == mMonitInfo) { | ||
44 | + DEBUG.error("Faile to find host info with hostId {} now...", actionInfo.getHostId()); | ||
45 | + | ||
46 | + return 1; | ||
47 | + } | ||
48 | + uri = "http://" + mMonitInfo.getHttpInfo().getAddress() + ":" + mMonitInfo.getHttpInfo().getPort() + "/" + actionInfo.getService(); | ||
49 | + | ||
50 | + | ||
51 | + //建立httpclient 根据 host service action 操作服务 | ||
52 | + RestTemplate client = new RestTemplate(); | ||
53 | + | ||
54 | + String plainCreds = "admin:monit"; | ||
55 | + | ||
56 | + String enCreds = new String(Base64.getUrlEncoder().encode(plainCreds.getBytes("UTF-8"))); | ||
57 | + | ||
58 | + HttpHeaders headers = new HttpHeaders(); | ||
59 | + | ||
60 | + headers.add("Authorization", "Basic " + enCreds); | ||
61 | + | ||
62 | + //create form data | ||
63 | + MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>(); | ||
64 | + | ||
65 | + parameters.add("action", actionInfo.getAction()); | ||
66 | + | ||
67 | + HttpEntity httpEntity = new HttpEntity(parameters, headers); | ||
68 | + | ||
69 | + ResponseEntity<String> responseEntity = client.exchange(uri, HttpMethod.POST, httpEntity, String.class); | ||
70 | + | ||
71 | + if (HttpStatus.OK.value() != responseEntity.getStatusCode().value()) { | ||
72 | + | ||
73 | + DEBUG.error("Failed to execute action {} in url {} with result {}", actionInfo.getService(), actionInfo.getAction(), uri, responseEntity); | ||
74 | + | ||
75 | + return 1; | ||
76 | + } | ||
77 | + | ||
78 | + DEBUG.info("Success to execute action {} for url {}", actionInfo.getService(), actionInfo.getAction(), uri); | ||
79 | + | ||
80 | + //修改缓存状态,等待更新 | ||
81 | + if (!StringUtils.equals("start", actionInfo.getAction())) { | ||
82 | + Map<String, MServiceInfo> serviceInfoMap = mMonitInfo.getServiceInfos(); | ||
83 | + if (null != serviceInfoMap) { | ||
84 | + MServiceInfo serviceInfo = serviceInfoMap.get(actionInfo.getService()); | ||
85 | + if (null != serviceInfo) { | ||
86 | + serviceInfo.setServiceStatus(ServiceStatus.UNNORMAL); | ||
87 | + } | ||
88 | + } | ||
89 | + } | ||
90 | + return 0; | ||
91 | + } | ||
92 | + } | ||
93 | + | ||
94 | +} |
1 | +package com.monitor.monit.Job; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.databind.DeserializationFeature; | ||
4 | + | ||
5 | +import com.monitor.monit.constant.MConstants; | ||
6 | +import com.monitor.monit.model.*; | ||
7 | +import lombok.Getter; | ||
8 | +import lombok.Setter; | ||
9 | +import org.apache.commons.lang3.StringUtils; | ||
10 | +import org.slf4j.Logger; | ||
11 | +import org.slf4j.LoggerFactory; | ||
12 | + | ||
13 | +import java.io.IOException; | ||
14 | +import java.util.List; | ||
15 | +import java.util.concurrent.Callable; | ||
16 | + | ||
17 | +/** | ||
18 | + * Created by yoho on 2016/5/11. | ||
19 | + */ | ||
20 | + | ||
21 | +public class CollectorJob implements Callable { | ||
22 | + | ||
23 | + private final static Logger DEBUG = LoggerFactory.getLogger(CollectorJob.class); | ||
24 | + | ||
25 | + @Getter | ||
26 | + @Setter | ||
27 | + private String monitorInfo; | ||
28 | + | ||
29 | + private CollectorJob() { | ||
30 | + | ||
31 | + } | ||
32 | + | ||
33 | + public CollectorJob(String info) { | ||
34 | + this.monitorInfo = info; | ||
35 | + } | ||
36 | + | ||
37 | + @Override | ||
38 | + public Object call() throws Exception { | ||
39 | + //解析xml 处理event事件 | ||
40 | + parseCollectorXml(this.monitorInfo); | ||
41 | + | ||
42 | + return null; | ||
43 | + } | ||
44 | + | ||
45 | + private void parseCollectorXml(String monitorInfo) { | ||
46 | + MMonit mMonit = null; | ||
47 | + try { | ||
48 | + MConstants.XMLMAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
49 | + | ||
50 | + mMonit = MConstants.XMLMAPPER.readValue(monitorInfo, MMonit.class); | ||
51 | + } catch (IOException e) { | ||
52 | + DEBUG.error("Failed to parse xml content {} , error {}", monitorInfo, e); | ||
53 | + | ||
54 | + mMonit = null; | ||
55 | + } | ||
56 | + | ||
57 | + if (null == mMonit) { | ||
58 | + return; | ||
59 | + } | ||
60 | + | ||
61 | + if (null != mMonit.getEvent()) { | ||
62 | + DEBUG.warn("host {} service {} occur event {}", mMonit.getServer().getHttpd().getAddress(), mMonit.getEvent().getService(), mMonit.getEvent().getMessage()); | ||
63 | + } | ||
64 | + | ||
65 | + synchronized (mMonit.getId().intern()) { | ||
66 | + MMonitInfo mMonitInfo = MConstants.HOSTSINFO_MAPPER.get(mMonit.getId()); | ||
67 | + | ||
68 | + if (null != mMonitInfo) { | ||
69 | + //update mMonitInfo | ||
70 | + updateMMonitInfo(mMonitInfo, mMonit); | ||
71 | + } else { | ||
72 | + //insert mMonitInfo | ||
73 | + insertMMonitInfo(mMonitInfo, mMonit); | ||
74 | + } | ||
75 | + } | ||
76 | + } | ||
77 | + | ||
78 | + private void updateMMonitInfo(MMonitInfo mMonitInfo, MMonit mMonit) { | ||
79 | + | ||
80 | + MHttpd httpInfo = mMonit.getServer().getHttpd(); | ||
81 | + | ||
82 | + if (null != httpInfo) { | ||
83 | + mMonitInfo.setHttpInfo(httpInfo); | ||
84 | + } | ||
85 | + | ||
86 | + MEvent eventInfo = mMonit.getEvent(); | ||
87 | + | ||
88 | + if (null != eventInfo) { | ||
89 | + mMonitInfo.getEvents().add(eventInfo); | ||
90 | + } | ||
91 | + | ||
92 | + //update service | ||
93 | + List<MService> services = mMonit.getServices(); | ||
94 | + | ||
95 | + for (MService oneService : services) { | ||
96 | + | ||
97 | + //update service | ||
98 | + MServiceInfo oneServiceInfo = mMonitInfo.getServiceInfos().get(oneService.getName()); | ||
99 | + | ||
100 | + if (null == oneServiceInfo) { | ||
101 | + oneServiceInfo = new MServiceInfo(); | ||
102 | + | ||
103 | + oneServiceInfo.setServiceName(oneService.getName()); | ||
104 | + | ||
105 | + oneServiceInfo.setSystem(false); | ||
106 | + | ||
107 | + oneServiceInfo.setServiceStatus(ServiceStatus.UNNORMAL); | ||
108 | + } | ||
109 | + | ||
110 | + if (StringUtils.equals(MConstants.SYSTEMTYPE, oneService.getType())) { | ||
111 | + oneServiceInfo.setSystem(true); | ||
112 | + } | ||
113 | + | ||
114 | + if (StringUtils.equals(MConstants.MONITED, oneService.getMonitor()) && StringUtils.equals(MConstants.NORMALSTATUS, oneService.getStatus())) { | ||
115 | + oneServiceInfo.setServiceStatus(ServiceStatus.NORMAL); | ||
116 | + } else { | ||
117 | + oneServiceInfo.setServiceStatus(ServiceStatus.UNNORMAL); | ||
118 | + } | ||
119 | + | ||
120 | + | ||
121 | + mMonitInfo.getServiceInfos().put(oneServiceInfo.getServiceName(), oneServiceInfo); | ||
122 | + } | ||
123 | + | ||
124 | + } | ||
125 | + | ||
126 | + private void insertMMonitInfo(MMonitInfo mMonitInfo, MMonit mMonit) { | ||
127 | + mMonitInfo = new MMonitInfo(); | ||
128 | + | ||
129 | + mMonitInfo.setMonitorId(mMonit.getId()); | ||
130 | + | ||
131 | + updateMMonitInfo(mMonitInfo, mMonit); | ||
132 | + | ||
133 | + MConstants.HOSTSINFO_MAPPER.put(mMonitInfo.getMonitorId(), mMonitInfo); | ||
134 | + | ||
135 | + } | ||
136 | +} | ||
137 | + |
1 | +package com.monitor.monit.Job; | ||
2 | + | ||
3 | +import org.slf4j.Logger; | ||
4 | +import org.slf4j.LoggerFactory; | ||
5 | +import java.util.concurrent.*; | ||
6 | + | ||
7 | +/** | ||
8 | + * Created by yoho on 2016/5/11. | ||
9 | + */ | ||
10 | +public class OneJob implements Callable { | ||
11 | + private static final Logger DEBUG = LoggerFactory.getLogger(OneJob.class); | ||
12 | + | ||
13 | + private static final long DEFAULTIMEOUT = 10 * 1000; | ||
14 | + | ||
15 | + private Callable jobDetail; | ||
16 | + | ||
17 | + private long timeout; | ||
18 | + | ||
19 | + private OneJob() { | ||
20 | + | ||
21 | + } | ||
22 | + | ||
23 | + public OneJob(Callable jobDetail, int timeout) { | ||
24 | + this.jobDetail = jobDetail; | ||
25 | + | ||
26 | + this.timeout = timeout; | ||
27 | + } | ||
28 | + | ||
29 | + public OneJob(Callable jobDetail) { | ||
30 | + this.jobDetail = jobDetail; | ||
31 | + | ||
32 | + this.timeout = DEFAULTIMEOUT; | ||
33 | + } | ||
34 | + | ||
35 | + @Override | ||
36 | + public Object call() { | ||
37 | + ExecutorService oneExecutor = Executors.newSingleThreadExecutor(); | ||
38 | + | ||
39 | + Future future = oneExecutor.submit(jobDetail); | ||
40 | + | ||
41 | + try { | ||
42 | + | ||
43 | + future.get(this.timeout, TimeUnit.MILLISECONDS); | ||
44 | + | ||
45 | + } catch (InterruptedException e) { | ||
46 | + DEBUG.error("Failed to execute job ,intrrupted error {}", e); | ||
47 | + } catch (ExecutionException e) { | ||
48 | + DEBUG.error("Failed to execute job ,execute error {}", e); | ||
49 | + } catch (TimeoutException e) { | ||
50 | + DEBUG.error("Failed to execute job ,timeout error {}", e); | ||
51 | + } | ||
52 | + | ||
53 | + oneExecutor.shutdownNow(); | ||
54 | + | ||
55 | + return null; | ||
56 | + } | ||
57 | +} |
1 | +package com.monitor.monit.constant; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.dataformat.xml.XmlMapper; | ||
4 | +import com.monitor.monit.model.MMonitInfo; | ||
5 | +import org.codehaus.jackson.map.ObjectMapper; | ||
6 | + | ||
7 | +import java.util.concurrent.ConcurrentHashMap; | ||
8 | + | ||
9 | +/** | ||
10 | + * Created by yoho on 2016/5/11. | ||
11 | + */ | ||
12 | +public interface MConstants { | ||
13 | + | ||
14 | + ObjectMapper OBJECTMAPPER = new ObjectMapper(); | ||
15 | + | ||
16 | + XmlMapper XMLMAPPER = new XmlMapper(); | ||
17 | + | ||
18 | + ConcurrentHashMap<String, MMonitInfo> HOSTSINFO_MAPPER = new ConcurrentHashMap<>(); | ||
19 | + | ||
20 | + Integer HTTPTIMEOUT = 2; | ||
21 | + | ||
22 | + String SYSTEMTYPE = "5"; | ||
23 | + | ||
24 | + String MONITED = "1"; | ||
25 | + | ||
26 | + String NORMALSTATUS = "0"; | ||
27 | + | ||
28 | +} |
1 | +package com.monitor.monit.model; | ||
2 | + | ||
3 | +import lombok.Data; | ||
4 | + | ||
5 | +/** | ||
6 | + * Created by yoho on 2016/5/11. | ||
7 | + */ | ||
8 | +@Data | ||
9 | +public class MEvent { | ||
10 | + | ||
11 | + private String collected_sec; | ||
12 | + | ||
13 | + private String service; | ||
14 | + | ||
15 | + //服务类型 5为系统服务 | ||
16 | + private String type; | ||
17 | + | ||
18 | + private String message; | ||
19 | +} |
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/5/11. | ||
9 | + */ | ||
10 | + | ||
11 | +@Data | ||
12 | +public class MMonit { | ||
13 | + | ||
14 | + private String id; | ||
15 | + | ||
16 | + private MServer server; | ||
17 | + | ||
18 | + private MPlatform platform; | ||
19 | + | ||
20 | + private List<MService> services; | ||
21 | + | ||
22 | + private MEvent event; | ||
23 | + | ||
24 | +} |
1 | +package com.monitor.monit.model; | ||
2 | + | ||
3 | +import lombok.Data; | ||
4 | + | ||
5 | +import java.util.HashMap; | ||
6 | +import java.util.LinkedList; | ||
7 | +import java.util.List; | ||
8 | +import java.util.Map; | ||
9 | + | ||
10 | +/** | ||
11 | + * Created by yoho on 2016/5/13. | ||
12 | + */ | ||
13 | +@Data | ||
14 | +public class MMonitInfo { | ||
15 | + private String monitorId; | ||
16 | + | ||
17 | + private MHttpd httpInfo; | ||
18 | + | ||
19 | + private Map<String, MServiceInfo> serviceInfos = new HashMap<String, MServiceInfo>(); | ||
20 | + | ||
21 | + private List<MEvent> events = new LinkedList<MEvent>(); | ||
22 | +} |
1 | +package com.monitor.monit.model; | ||
2 | + | ||
3 | +import lombok.Data; | ||
4 | + | ||
5 | +/** | ||
6 | + * Created by yoho on 2016/5/11. | ||
7 | + */ | ||
8 | +@Data | ||
9 | +public class MPlatform { | ||
10 | + | ||
11 | + private String name; | ||
12 | + | ||
13 | + private String release; | ||
14 | + | ||
15 | + private String version; | ||
16 | + | ||
17 | + private String machine; | ||
18 | + | ||
19 | + private String cpu; | ||
20 | + | ||
21 | + private String memory; | ||
22 | + | ||
23 | + private String swap; | ||
24 | +} |
1 | +package com.monitor.monit.model; | ||
2 | + | ||
3 | +import lombok.Data; | ||
4 | + | ||
5 | +/** | ||
6 | + * Created by yoho on 2016/5/11. | ||
7 | + */ | ||
8 | +@Data | ||
9 | +public class MService { | ||
10 | + | ||
11 | + private String name; | ||
12 | + | ||
13 | + //5 为系统服务 | ||
14 | + private String type; | ||
15 | + | ||
16 | + private String collected_sec; | ||
17 | + | ||
18 | + //根据monitor状态来判断 | ||
19 | + private String status; | ||
20 | + | ||
21 | + //monitor: 1 正常监听 非1异常监听 | ||
22 | + private String monitor; | ||
23 | + | ||
24 | + private MSystem system; | ||
25 | + | ||
26 | + | ||
27 | +} |
1 | +package com.monitor.monit.model; | ||
2 | + | ||
3 | +import lombok.Data; | ||
4 | + | ||
5 | +/** | ||
6 | + * Created by yoho on 2016/5/11. | ||
7 | + */ | ||
8 | +@Data | ||
9 | +public class MSystem { | ||
10 | + | ||
11 | + @Data | ||
12 | + class MCpu { | ||
13 | + private String user; | ||
14 | + | ||
15 | + private String system; | ||
16 | + | ||
17 | + private String wait; | ||
18 | + } | ||
19 | + | ||
20 | + @Data | ||
21 | + class MMemory { | ||
22 | + private String percent; | ||
23 | + | ||
24 | + private String kilobyte; | ||
25 | + } | ||
26 | + | ||
27 | + @Data | ||
28 | + class MSwap { | ||
29 | + private String percent; | ||
30 | + | ||
31 | + private String kilobyte; | ||
32 | + } | ||
33 | + | ||
34 | + private MCpu cpu; | ||
35 | + | ||
36 | + private MMemory memory; | ||
37 | + | ||
38 | + private MSwap swap; | ||
39 | +} | ||
40 | + | ||
41 | + | ||
42 | + |
1 | +package com.monitor.monit.model; | ||
2 | + | ||
3 | +import lombok.Data; | ||
4 | + | ||
5 | +import java.util.ArrayList; | ||
6 | +import java.util.List; | ||
7 | + | ||
8 | +/** | ||
9 | + * Created by yoho on 2016/10/27. | ||
10 | + */ | ||
11 | +@Data | ||
12 | +public class ServiceResponse { | ||
13 | + String monitorId; | ||
14 | + | ||
15 | + MHttpd httpInfo; | ||
16 | + | ||
17 | + List<MServiceInfo> servicesInfo=new ArrayList<>(); | ||
18 | + | ||
19 | + List<MEvent> eventsInfo=new ArrayList<>(); | ||
20 | + | ||
21 | +} | ||
22 | + | ||
23 | + |
1 | +package com.monitor.monit.service; | ||
2 | + | ||
3 | +import java.util.concurrent.ExecutorService; | ||
4 | +import java.util.concurrent.Executors; | ||
5 | +import java.util.concurrent.ThreadFactory; | ||
6 | + | ||
7 | +/** | ||
8 | + * Created by yoho on 2016/5/11. | ||
9 | + */ | ||
10 | +public abstract class AbstractService { | ||
11 | + | ||
12 | + protected ExecutorService service = Executors.newFixedThreadPool(20, new ThreadFactory() { | ||
13 | + @Override | ||
14 | + public Thread newThread(Runnable r) { | ||
15 | + Thread t = new Thread(r, "ops-monitor-thread"); | ||
16 | + return t; | ||
17 | + } | ||
18 | + }); | ||
19 | +} |
1 | +package com.monitor.monit.service; | ||
2 | + | ||
3 | + | ||
4 | +import com.monitor.monit.Job.CollectorJob; | ||
5 | +import com.monitor.monit.Job.OneJob; | ||
6 | +import com.monitor.monit.constant.MConstants; | ||
7 | +import org.slf4j.Logger; | ||
8 | +import org.slf4j.LoggerFactory; | ||
9 | +import org.springframework.web.bind.annotation.*; | ||
10 | + | ||
11 | +import java.util.Map; | ||
12 | + | ||
13 | + | ||
14 | +/** | ||
15 | + * Created by yoho on 2016/5/11. | ||
16 | + */ | ||
17 | +@RestController | ||
18 | +@RequestMapping(value = "/monit") | ||
19 | +public class SCollectorService extends AbstractService { | ||
20 | + private static final Logger DEBUG = LoggerFactory.getLogger(SCollectorService.class); | ||
21 | + | ||
22 | + @RequestMapping(value = "/collector", method = RequestMethod.POST) | ||
23 | + public void doHandle(@RequestBody String info, @RequestHeader Map<String, String> headerInfo) { | ||
24 | + | ||
25 | + DEBUG.debug("Received collector msg {} form host {}", info, headerInfo.get("Host")); | ||
26 | + | ||
27 | + //2s timeout | ||
28 | + service.submit(new OneJob(new CollectorJob(info), MConstants.HTTPTIMEOUT * 1000)); | ||
29 | + | ||
30 | + } | ||
31 | +} |
1 | +package com.monitor.monit.service; | ||
2 | + | ||
3 | + | ||
4 | +import com.monitor.monit.Job.ActionJob; | ||
5 | +import com.monitor.monit.Job.OneJob; | ||
6 | +import com.monitor.monit.constant.MConstants; | ||
7 | +import com.monitor.monit.model.MAction; | ||
8 | +import com.monitor.monit.model.MActions; | ||
9 | +import org.apache.commons.lang3.StringUtils; | ||
10 | +import org.slf4j.Logger; | ||
11 | +import org.slf4j.LoggerFactory; | ||
12 | +import org.springframework.web.bind.annotation.*; | ||
13 | + | ||
14 | +import java.io.IOException; | ||
15 | + | ||
16 | +/** | ||
17 | + * Created by yoho on 2016/5/11. | ||
18 | + */ | ||
19 | +@RestController | ||
20 | +@RequestMapping("/monit/hosts") | ||
21 | +public class SHostService extends AbstractService { | ||
22 | + | ||
23 | + private static final Logger DEBUG = LoggerFactory.getLogger(SHostService.class); | ||
24 | + | ||
25 | + @RequestMapping(value = "/action", method = RequestMethod.POST) | ||
26 | + public void doActionHandle(@RequestBody String info) { | ||
27 | + DEBUG.info("Received actions msg {}", info); | ||
28 | + | ||
29 | + MActions actions; | ||
30 | + | ||
31 | + try { | ||
32 | + actions = MConstants.OBJECTMAPPER.readValue(info, MActions.class); | ||
33 | + | ||
34 | + } catch (IOException e) { | ||
35 | + DEBUG.error("Failed to parse"); | ||
36 | + | ||
37 | + actions = null; | ||
38 | + } | ||
39 | + if (null == actions || 0 >= actions.getActions().size()) { | ||
40 | + DEBUG.warn("Null actions found in request body, do nothing..."); | ||
41 | + return; | ||
42 | + } | ||
43 | + | ||
44 | + for (MAction action : actions.getActions()) { | ||
45 | + | ||
46 | + if (null == action) { | ||
47 | + DEBUG.warn("Null action found in actions, do nothing..."); | ||
48 | + continue; | ||
49 | + } | ||
50 | + | ||
51 | + //2s timeout | ||
52 | + service.submit(new OneJob(new ActionJob(action), MConstants.HTTPTIMEOUT * 1000)); | ||
53 | + } | ||
54 | + } | ||
55 | + | ||
56 | + @RequestMapping(value = "/oneAction", method = RequestMethod.POST) | ||
57 | + public void doOneActionHandle(@RequestBody MAction action) { | ||
58 | + DEBUG.info("Received action msg {}", action); | ||
59 | + | ||
60 | + if (null == action) { | ||
61 | + DEBUG.warn("Null action found in actions, do nothing..."); | ||
62 | + return; | ||
63 | + } | ||
64 | + | ||
65 | + //2s timeout | ||
66 | + service.submit(new OneJob(new ActionJob(action), MConstants.HTTPTIMEOUT * 1000)); | ||
67 | + } | ||
68 | + | ||
69 | + @RequestMapping(value = "/monit/hosts/delete/{hostId}", method = RequestMethod.POST) | ||
70 | + public void doDeleteHandle(@PathVariable("hostId") String hostId) { | ||
71 | + | ||
72 | + DEBUG.info("Received delete host {} action.", hostId); | ||
73 | + | ||
74 | + if (StringUtils.isNotBlank(hostId)) { | ||
75 | + MConstants.HOSTSINFO_MAPPER.remove(hostId); | ||
76 | + } | ||
77 | + } | ||
78 | +} |
1 | +package com.monitor.monit.service; | ||
2 | + | ||
3 | +import com.monitor.model.response.BaseResponse; | ||
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; | ||
8 | +import org.slf4j.Logger; | ||
9 | +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; | ||
14 | + | ||
15 | +import java.io.IOException; | ||
16 | +import java.util.*; | ||
17 | + | ||
18 | + | ||
19 | +/** | ||
20 | + * Created by yoho on 2016/5/12. | ||
21 | + * <p> | ||
22 | + * 查询服务 | ||
23 | + */ | ||
24 | +@RestController | ||
25 | +@RequestMapping("/monit/service") | ||
26 | +public class SStatuService extends AbstractService { | ||
27 | + private static final Logger DEBUG = LoggerFactory.getLogger(SStatuService.class); | ||
28 | + | ||
29 | + @RequestMapping(value = "/all", method = RequestMethod.GET) | ||
30 | + public BaseResponse fetchAllService() { | ||
31 | + | ||
32 | + BaseResponse response = new BaseResponse(); | ||
33 | + | ||
34 | + response.setData(buildServiceResponse()); | ||
35 | + | ||
36 | + return response; | ||
37 | + | ||
38 | + } | ||
39 | + | ||
40 | + | ||
41 | + private List<ServiceResponse> buildServiceResponse() { | ||
42 | + List<ServiceResponse> servicesList=new ArrayList<>(); | ||
43 | + | ||
44 | + for (Map.Entry<String, MMonitInfo> entry : MConstants.HOSTSINFO_MAPPER.entrySet()) { | ||
45 | + | ||
46 | + ServiceResponse serviceResponse=new ServiceResponse(); | ||
47 | + | ||
48 | + serviceResponse.setMonitorId(entry.getKey()); | ||
49 | + | ||
50 | + serviceResponse.setHttpInfo(entry.getValue().getHttpInfo()); | ||
51 | + | ||
52 | + List<MServiceInfo> serviceInfos=new ArrayList<>(); | ||
53 | + | ||
54 | + for (Map.Entry<String,MServiceInfo> serviceInfoEntry:entry.getValue().getServiceInfos().entrySet()) | ||
55 | + { | ||
56 | + serviceInfos.add(serviceInfoEntry.getValue()); | ||
57 | + } | ||
58 | + | ||
59 | + serviceResponse.setServicesInfo(serviceInfos); | ||
60 | + | ||
61 | + serviceResponse.setEventsInfo(entry.getValue().getEvents()); | ||
62 | + | ||
63 | + servicesList.add(serviceResponse); | ||
64 | + } | ||
65 | + | ||
66 | + return servicesList; | ||
67 | + } | ||
68 | + | ||
69 | + | ||
70 | + | ||
71 | + | ||
72 | + @RequestMapping(value = "/host/{hostId}", method = RequestMethod.GET) | ||
73 | + public String fetchHostSerivce(@PathVariable("hostId") String hostId) { | ||
74 | + | ||
75 | + Map<String, MServiceInfo> services = null; | ||
76 | + | ||
77 | + if (MConstants.HOSTSINFO_MAPPER.containsKey(hostId)) { | ||
78 | + services = MConstants.HOSTSINFO_MAPPER.get(hostId).getServiceInfos(); | ||
79 | + } | ||
80 | + | ||
81 | + if (null != services) { | ||
82 | + try { | ||
83 | + String content = MConstants.OBJECTMAPPER.writeValueAsString(services); | ||
84 | + | ||
85 | + return content; | ||
86 | + | ||
87 | + } catch (IOException e) { | ||
88 | + DEBUG.error("Failed to parse service info..."); | ||
89 | + } | ||
90 | + } | ||
91 | + return null; | ||
92 | + } | ||
93 | + | ||
94 | +} |
@@ -24,5 +24,7 @@ public interface MObjectInfoMapper { | @@ -24,5 +24,7 @@ public interface MObjectInfoMapper { | ||
24 | 24 | ||
25 | List<MObjectInfo> getTypeMosInfo(int typeId); | 25 | List<MObjectInfo> getTypeMosInfo(int typeId); |
26 | 26 | ||
27 | + List<MObjectInfo> selectDistinctHostIPByTypes(List<Integer> typeIds); | ||
28 | + | ||
27 | /* List<MObjectInfo> getHostMosInfo(int hostId);*/ | 29 | /* List<MObjectInfo> getHostMosInfo(int hostId);*/ |
28 | } | 30 | } |
@@ -54,4 +54,18 @@ | @@ -54,4 +54,18 @@ | ||
54 | SELECT * FROM mobject_info where instr(alias, LOWER(#{alias})) > 0 ORDER BY id asc | 54 | SELECT * FROM mobject_info where instr(alias, LOWER(#{alias})) > 0 ORDER BY id asc |
55 | </select> | 55 | </select> |
56 | 56 | ||
57 | + | ||
58 | + | ||
59 | + <select id="selectDistinctHostIPByTypes" resultType="com.model.MObjectInfo" resultMap="mobjectInfoMapper"> | ||
60 | + SELECT distinct host_ip FROM mobject_info | ||
61 | + | ||
62 | + where type_id in | ||
63 | + | ||
64 | + <foreach collection="list" open="(" close=")" separator="," item="item"> | ||
65 | + #{item} | ||
66 | + </foreach> | ||
67 | + | ||
68 | + order by host_ip DESC | ||
69 | + </select> | ||
70 | + | ||
57 | </mapper> | 71 | </mapper> |
@@ -51,4 +51,12 @@ | @@ -51,4 +51,12 @@ | ||
51 | FROM type_info where alias =#{typeName} limit 1 | 51 | FROM type_info where alias =#{typeName} limit 1 |
52 | </select> | 52 | </select> |
53 | 53 | ||
54 | + | ||
55 | + | ||
56 | + <select id="getChildTypesInfoByAlias" resultType="com.model.TypeInfo" resultMap="typeInfoMap"> | ||
57 | + SELECT | ||
58 | + <include refid="Base_Column_List"/> | ||
59 | + FROM `type_info` tt WHERE tt.`parent_id` = (SELECT id FROM `type_info` t WHERE t.`alias` = #{alias}) | ||
60 | + </select> | ||
61 | + | ||
54 | </mapper> | 62 | </mapper> |
@@ -95,6 +95,11 @@ | @@ -95,6 +95,11 @@ | ||
95 | <groupId>commons-collections</groupId> | 95 | <groupId>commons-collections</groupId> |
96 | <artifactId>commons-collections</artifactId> | 96 | <artifactId>commons-collections</artifactId> |
97 | </dependency> | 97 | </dependency> |
98 | + <dependency> | ||
99 | + <groupId>com.fasterxml.jackson.core</groupId> | ||
100 | + <artifactId>jackson-databind</artifactId> | ||
101 | + <version>2.8.3</version> | ||
102 | + </dependency> | ||
98 | </dependencies> | 103 | </dependencies> |
99 | 104 | ||
100 | </project> | 105 | </project> |
@@ -9,16 +9,15 @@ import com.monitor.nginxsync.model.TaskInfo; | @@ -9,16 +9,15 @@ import com.monitor.nginxsync.model.TaskInfo; | ||
9 | import com.monitor.nginxsync.nio.model.LogMsg; | 9 | import com.monitor.nginxsync.nio.model.LogMsg; |
10 | import com.monitor.nginxsync.nio.service.LogService; | 10 | import com.monitor.nginxsync.nio.service.LogService; |
11 | import org.apache.commons.lang.StringUtils; | 11 | import org.apache.commons.lang.StringUtils; |
12 | +import org.slf4j.Logger; | ||
13 | +import org.slf4j.LoggerFactory; | ||
12 | import org.springframework.beans.factory.annotation.Autowired; | 14 | import org.springframework.beans.factory.annotation.Autowired; |
13 | import org.springframework.beans.factory.annotation.Value; | 15 | import org.springframework.beans.factory.annotation.Value; |
14 | import org.springframework.web.bind.annotation.RequestBody; | 16 | import org.springframework.web.bind.annotation.RequestBody; |
15 | import org.springframework.web.bind.annotation.RequestMapping; | 17 | import org.springframework.web.bind.annotation.RequestMapping; |
16 | import org.springframework.web.bind.annotation.RestController; | 18 | import org.springframework.web.bind.annotation.RestController; |
17 | -import org.springframework.web.client.RestClientException; | ||
18 | import org.springframework.web.client.RestTemplate; | 19 | import org.springframework.web.client.RestTemplate; |
19 | 20 | ||
20 | -import java.text.MessageFormat; | ||
21 | -import java.util.logging.Logger; | ||
22 | 21 | ||
23 | /** | 22 | /** |
24 | * Created by yoho on 2016/8/23. | 23 | * Created by yoho on 2016/8/23. |
@@ -26,6 +25,8 @@ import java.util.logging.Logger; | @@ -26,6 +25,8 @@ import java.util.logging.Logger; | ||
26 | @RestController | 25 | @RestController |
27 | @RequestMapping(value = "/nginxsync") | 26 | @RequestMapping(value = "/nginxsync") |
28 | public class NginxSyncService { | 27 | public class NginxSyncService { |
28 | + public static final Logger DEBUG = LoggerFactory.getLogger(NginxSyncService.class); | ||
29 | + | ||
29 | 30 | ||
30 | @Value("${nginxsync.agent}") | 31 | @Value("${nginxsync.agent}") |
31 | String agent; | 32 | String agent; |
@@ -38,6 +39,7 @@ public class NginxSyncService { | @@ -38,6 +39,7 @@ public class NginxSyncService { | ||
38 | 39 | ||
39 | @RequestMapping(value = "/pushtask") | 40 | @RequestMapping(value = "/pushtask") |
40 | public BaseResponse pushTask(@RequestBody TaskInfo taskInfo) { | 41 | public BaseResponse pushTask(@RequestBody TaskInfo taskInfo) { |
42 | + DEBUG.info("Recive nginx sync task {}", taskInfo); | ||
41 | 43 | ||
42 | Preconditions.checkNotNull(taskInfo); | 44 | Preconditions.checkNotNull(taskInfo); |
43 | 45 | ||
@@ -104,7 +106,8 @@ public class NginxSyncService { | @@ -104,7 +106,8 @@ public class NginxSyncService { | ||
104 | try { | 106 | try { |
105 | restTemplate.postForObject("http://" + agent + "/handle", cmdTaskInfo, String.class); | 107 | restTemplate.postForObject("http://" + agent + "/handle", cmdTaskInfo, String.class); |
106 | 108 | ||
107 | - } catch (RestClientException e) { | 109 | + } catch (Exception e) { |
110 | + DEBUG.error("Failed to dispatch nginx sync task {}...error {}", cmdTaskInfo, e); | ||
108 | 111 | ||
109 | response.setCode(500); | 112 | response.setCode(500); |
110 | 113 |
@@ -66,6 +66,10 @@ | @@ -66,6 +66,10 @@ | ||
66 | <groupId>monitor-service</groupId> | 66 | <groupId>monitor-service</groupId> |
67 | <artifactId>monitor-service-compare</artifactId> | 67 | <artifactId>monitor-service-compare</artifactId> |
68 | </dependency> | 68 | </dependency> |
69 | +<!-- <dependency> | ||
70 | + <groupId>monitor-service</groupId> | ||
71 | + <artifactId>monitor-service-monit</artifactId> | ||
72 | + </dependency>--> | ||
69 | <dependency> | 73 | <dependency> |
70 | <groupId>junit</groupId> | 74 | <groupId>junit</groupId> |
71 | <artifactId>junit</artifactId> | 75 | <artifactId>junit</artifactId> |
@@ -153,6 +153,11 @@ | @@ -153,6 +153,11 @@ | ||
153 | <artifactId>monitor-service-compare</artifactId> | 153 | <artifactId>monitor-service-compare</artifactId> |
154 | <version>${project-version}</version> | 154 | <version>${project-version}</version> |
155 | </dependency> | 155 | </dependency> |
156 | +<!-- <dependency> | ||
157 | + <groupId>monitor-service</groupId> | ||
158 | + <artifactId>monitor-service-monit</artifactId> | ||
159 | + <version>${project-version}</version> | ||
160 | + </dependency>--> | ||
156 | <dependency> | 161 | <dependency> |
157 | <groupId>org.projectlombok</groupId> | 162 | <groupId>org.projectlombok</groupId> |
158 | <artifactId>lombok</artifactId> | 163 | <artifactId>lombok</artifactId> |
@@ -188,6 +193,7 @@ | @@ -188,6 +193,7 @@ | ||
188 | <module>monitor-service-awstools</module> | 193 | <module>monitor-service-awstools</module> |
189 | <module>monitor-service-zabbix</module> | 194 | <module>monitor-service-zabbix</module> |
190 | <module>monitor-service-compare</module> | 195 | <module>monitor-service-compare</module> |
196 | + <module>monitor-service-monit</module> | ||
191 | </modules> | 197 | </modules> |
192 | 198 | ||
193 | 199 |
-
Please register or login to post a comment