Authored by jack.xue

update

Showing 14 changed files with 240 additions and 23 deletions
1 -<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">  
2 - <wb-module deploy-name="monitor-service-middleware">  
3 - <wb-resource deploy-path="/" source-path="/src/main/java"/>  
4 - <wb-resource deploy-path="/" source-path="/src/main/resources"/>  
5 - </wb-module>  
6 -</project-modules>  
1 -<?xml version="1.0" encoding="UTF-8"?>  
2 -<faceted-project>  
3 - <installed facet="java" version="1.8"/>  
4 - <installed facet="jst.utility" version="1.0"/>  
5 -</faceted-project>  
1 -disabled=06target  
2 -eclipse.preferences.version=1  
  1 +package com.monitor.middleware.nginx.constant;
  2 +
  3 +import com.model.MObjectInfo;
  4 +
  5 +import java.util.concurrent.ConcurrentHashMap;
  6 +
  7 +/**
  8 + * Created by yoho on 2016/6/22.
  9 + */
  10 +public interface InterVar {
  11 +
  12 + ConcurrentHashMap<Integer, MObjectInfo> moMaps = new ConcurrentHashMap<>();
  13 +
  14 + String DBNAME = "nginx_info";
  15 +
  16 + String APIACCESSMEASURE = "api_access";
  17 +
  18 + String SERACCESSMEASURE = "service_access";
  19 +
  20 + String LUAERRORMEASURE = "lua_error";
  21 +
  22 + String CONERRORMEASURE = "con_error";
  23 +
  24 + String SIMPLE_QUERY_SQL = "select count(log_ip) from {} where (time > now() - 10m) and (log_ip = \'{}\') group by time(1m)";
  25 +
  26 + String NGINXPREFIX = "(.*)nginx(.*)";
  27 +
  28 + Integer API_LIMIT = 100;
  29 +
  30 + Integer SERVIE_LIMIT = 100;
  31 +
  32 + Integer LUA_LIMIT = 100;
  33 +
  34 + Integer CON_LIMIT = 100;
  35 +
  36 + String COMPLEX_QUERY_SQL = "select count(log_ip) from {} where (time > now() - 10m)";
  37 +
  38 +}
  1 +package com.monitor.middleware.nginx.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +/**
  6 + * Created by yoho on 2016/6/22.
  7 + */
  8 +@Data
  9 +public class CountInfo {
  10 +
  11 + InfoType type;
  12 +
  13 + int count;
  14 +
  15 + boolean isWatch;
  16 +}
  1 +package com.monitor.middleware.nginx.model;
  2 +
  3 +/**
  4 + * Created by yoho on 2016/6/22.
  5 + */
  6 +public enum InfoType {
  7 + API_NO200, SERVIE_NO200, CONNECTION_TIMOUT, LUA_ERROR
  8 +}
  1 +package com.monitor.middleware.nginx.service;
  2 +
  3 +import com.monitor.cmdb.service.IMObjectInfoService;
  4 +import com.monitor.middleware.nginx.constant.InterVar;
  5 +import com.monitor.middleware.nginx.model.InfoType;
  6 +import com.monitor.middleware.rabbitmq.component.InfluxComp;
  7 +import com.monitor.middleware.rabbitmq.model.QueryRequest;
  8 +import com.monitor.model.response.BaseResponse;
  9 +import org.influxdb.dto.QueryResult;
  10 +import org.springframework.beans.factory.annotation.Autowired;
  11 +import org.springframework.web.bind.annotation.RequestBody;
  12 +import org.springframework.web.bind.annotation.RequestMapping;
  13 +import org.springframework.web.bind.annotation.RestController;
  14 +
  15 +import java.util.HashMap;
  16 +import java.util.Map;
  17 +
  18 +/**
  19 + * Created by yoho on 2016/6/22.
  20 + */
  21 +@RestController(value = "/middleware/nginx")
  22 +public class NginxService {
  23 +
  24 + @Autowired
  25 + InfluxComp influxComp;
  26 +
  27 + @Autowired
  28 + IMObjectInfoService imObjectInfoService;
  29 +
  30 + @RequestMapping
  31 + public BaseResponse queryCount(@RequestBody QueryRequest request) {
  32 +
  33 + BaseResponse response = new BaseResponse();
  34 +
  35 + Map<InfoType, QueryResult> resultMap = new HashMap<>();
  36 +
  37 + String moIp = imObjectInfoService.queryMObjectInfo(Integer.parseInt(request.getMoId())).getMoHostIp();
  38 +
  39 + QueryResult apiResult = influxComp.doQuery(String.format(InterVar.SIMPLE_QUERY_SQL, InterVar.APIACCESSMEASURE, moIp), InterVar.DBNAME);
  40 +
  41 + resultMap.put(InfoType.API_NO200, apiResult);
  42 +
  43 + QueryResult serResult = influxComp.doQuery(String.format(InterVar.SIMPLE_QUERY_SQL, InterVar.SERACCESSMEASURE, moIp), InterVar.DBNAME);
  44 +
  45 + resultMap.put(InfoType.SERVIE_NO200, serResult);
  46 +
  47 + QueryResult luaResult = influxComp.doQuery(String.format(InterVar.SIMPLE_QUERY_SQL, InterVar.LUAERRORMEASURE, moIp), InterVar.DBNAME);
  48 +
  49 + resultMap.put(InfoType.LUA_ERROR, luaResult);
  50 +
  51 + QueryResult conResult = influxComp.doQuery(String.format(InterVar.SIMPLE_QUERY_SQL, InterVar.CONERRORMEASURE, moIp), InterVar.DBNAME);
  52 +
  53 + resultMap.put(InfoType.CONNECTION_TIMOUT, conResult);
  54 +
  55 + response.setData(resultMap);
  56 +
  57 + return response;
  58 + }
  59 +
  60 +
  61 +}
  1 +package com.monitor.middleware.nginx.task;
  2 +
  3 +/**
  4 + * Created by yoho on 2016/6/22.
  5 + */
  6 +
  7 +import com.monitor.middleware.nginx.constant.InterVar;
  8 +import com.monitor.middleware.rabbitmq.component.InfluxComp;
  9 +import org.influxdb.dto.QueryResult;
  10 +import org.springframework.beans.factory.annotation.Autowired;
  11 +import org.springframework.scheduling.annotation.EnableScheduling;
  12 +import org.springframework.scheduling.annotation.Scheduled;
  13 +import org.springframework.stereotype.Component;
  14 +
  15 +/**
  16 + * 每隔3分钟统计一次本周期内的监控特性的数量,超过阈值告警
  17 + */
  18 +@EnableScheduling
  19 +@Component
  20 +public class NginxAlarmTask {
  21 +
  22 + @Autowired
  23 + InfluxComp influxComp;
  24 +
  25 + @Scheduled(cron = "* 0/3 * * * ? ")
  26 + public void doTask()
  27 + {
  28 + //统计api总和
  29 + QueryResult apiResult = influxComp.doQuery(String.format(InterVar.COMPLEX_QUERY_SQL, InterVar.APIACCESSMEASURE), InterVar.DBNAME);
  30 +
  31 + String countStr=apiResult.getResults().get(0).getSeries().get(0).getColumns().get(1).toString();
  32 +
  33 + if(InterVar.API_LIMIT<Integer.parseInt(countStr))
  34 + {
  35 + //alarm
  36 + }
  37 + //统计service总和
  38 +
  39 + QueryResult serResult = influxComp.doQuery(String.format(InterVar.COMPLEX_QUERY_SQL, InterVar.SERACCESSMEASURE), InterVar.DBNAME);
  40 +
  41 + //统计lua总和
  42 +
  43 + QueryResult luaResult = influxComp.doQuery(String.format(InterVar.COMPLEX_QUERY_SQL, InterVar.LUAERRORMEASURE), InterVar.DBNAME);
  44 +
  45 + //统计con总和
  46 + QueryResult conResult = influxComp.doQuery(String.format(InterVar.COMPLEX_QUERY_SQL, InterVar.CONERRORMEASURE), InterVar.DBNAME);
  47 +
  48 +
  49 + }
  50 +}
  1 +package com.monitor.middleware.nginx.task;
  2 +
  3 +import com.model.MObjectInfo;
  4 +import com.model.TypeInfo;
  5 +import com.monitor.cmdb.service.IMObjectInfoService;
  6 +import com.monitor.cmdb.service.ITypeInfoService;
  7 +import com.monitor.middleware.nginx.constant.InterVar;
  8 +import org.springframework.beans.factory.annotation.Autowired;
  9 +import org.springframework.scheduling.annotation.EnableScheduling;
  10 +import org.springframework.scheduling.annotation.Scheduled;
  11 +import org.springframework.stereotype.Component;
  12 +
  13 +import java.util.ArrayList;
  14 +import java.util.List;
  15 +
  16 +/**
  17 + * Created by yoho on 2016/6/22.
  18 + */
  19 +
  20 +/*自动发现nginx监控对象
  21 +* */
  22 +@EnableScheduling
  23 +@Component
  24 +public class NginxScanTask {
  25 + @Autowired
  26 + IMObjectInfoService moService;
  27 +
  28 + @Autowired
  29 + ITypeInfoService typeService;
  30 +
  31 + //自动发现rabbit监控对象
  32 + @Scheduled(cron = "* 0/1 * * * ? ")
  33 + public void doTask() {
  34 +
  35 + List<TypeInfo> typeInfosList = typeService.queryAllTypesInfo();
  36 +
  37 + List<MObjectInfo> mObjectInfoList = new ArrayList<>();
  38 +
  39 + for (TypeInfo typeInfo : typeInfosList) {
  40 +
  41 + if (1 == typeInfo.getTypeIsLeaf() && typeInfo.getTypeName().matches(InterVar.NGINXPREFIX)) {
  42 +
  43 + mObjectInfoList.addAll(moService.queryMObjectsInfoByType(typeInfo.getTypeId()));
  44 + }
  45 + }
  46 +
  47 +
  48 + for (MObjectInfo info : mObjectInfoList) {
  49 +
  50 + InterVar.moMaps.put(info.getMoId(), info);
  51 + }
  52 + }
  53 +}
  1 +package com.monitor.middleware.nginx.task.job;
  2 +
  3 +/**
  4 + * Created by yoho on 2016/6/22.
  5 + */
  6 +public class ApiJob {
  7 +}
@@ -32,7 +32,6 @@ public class InfluxComp implements Runnable { @@ -32,7 +32,6 @@ public class InfluxComp implements Runnable {
32 32
33 private InfluxDB influxDBClient; 33 private InfluxDB influxDBClient;
34 34
35 -  
36 @PostConstruct 35 @PostConstruct
37 public void init() { 36 public void init() {
38 37
@@ -49,8 +48,6 @@ public class InfluxComp implements Runnable { @@ -49,8 +48,6 @@ public class InfluxComp implements Runnable {
49 48
50 PointView pointView = InterVar.POINT_QUEUE.poll(); 49 PointView pointView = InterVar.POINT_QUEUE.poll();
51 50
52 - influxDBClient.createDatabase(InterVar.DBNAME);  
53 -  
54 influxDBClient.write(pointView.toPoint()); 51 influxDBClient.write(pointView.toPoint());
55 52
56 } 53 }
@@ -66,9 +63,9 @@ public class InfluxComp implements Runnable { @@ -66,9 +63,9 @@ public class InfluxComp implements Runnable {
66 } 63 }
67 64
68 65
69 - public QueryResult doQuery(String command) { 66 + public QueryResult doQuery(String command,String dbName) {
70 67
71 - Query query = new Query(command, InterVar.DBNAME); 68 + Query query = new Query(command, dbName);
72 69
73 return influxDBClient.query(query); 70 return influxDBClient.query(query);
74 } 71 }
@@ -30,7 +30,7 @@ public class RabbitmqService { @@ -30,7 +30,7 @@ public class RabbitmqService {
30 BaseResponse response = new BaseResponse(); 30 BaseResponse response = new BaseResponse();
31 QueryResult result = null; 31 QueryResult result = null;
32 try { 32 try {
33 - result = this.influxComp.doQuery(String.format(InterVar.OVERVIEW_SQL, request.getMoId())); 33 + result = this.influxComp.doQuery(String.format(InterVar.OVERVIEW_SQL, request.getMoId()), InterVar.DBNAME);
34 } catch (Exception e) { 34 } catch (Exception e) {
35 DEBUG.error("Failed to query overview about mo {}", request.getMoId()); 35 DEBUG.error("Failed to query overview about mo {}", request.getMoId());
36 response.setData(e); 36 response.setData(e);
@@ -47,7 +47,7 @@ public class RabbitmqService { @@ -47,7 +47,7 @@ public class RabbitmqService {
47 BaseResponse response = new BaseResponse(); 47 BaseResponse response = new BaseResponse();
48 QueryResult result = null; 48 QueryResult result = null;
49 try { 49 try {
50 - result = this.influxComp.doQuery(String.format(InterVar.CLUSTERVIEW_SQL, request.getMoId()) + InterVar.nodesCountMaps.get(request.getMoId())); 50 + result = this.influxComp.doQuery(String.format(InterVar.CLUSTERVIEW_SQL, request.getMoId()) + InterVar.nodesCountMaps.get(request.getMoId()), InterVar.DBNAME);
51 } catch (Exception e) { 51 } catch (Exception e) {
52 DEBUG.error("Failed to query overview about mo {}", request.getMoId()); 52 DEBUG.error("Failed to query overview about mo {}", request.getMoId());
53 response.setData(e); 53 response.setData(e);
@@ -64,7 +64,7 @@ public class RabbitmqService { @@ -64,7 +64,7 @@ public class RabbitmqService {
64 BaseResponse response = new BaseResponse(); 64 BaseResponse response = new BaseResponse();
65 QueryResult result = null; 65 QueryResult result = null;
66 try { 66 try {
67 - result = this.influxComp.doQuery(String.format(InterVar.QUEUEVIEW_SQL, request.getMoId()) + InterVar.queueCountMaps.get(request.getMoId())); 67 + result = this.influxComp.doQuery(String.format(InterVar.QUEUEVIEW_SQL, request.getMoId()) + InterVar.queueCountMaps.get(request.getMoId()), InterVar.DBNAME);
68 68
69 69
70 } catch (Exception e) { 70 } catch (Exception e) {
@@ -24,7 +24,7 @@ import static com.monitor.middleware.rabbitmq.constant.InterVar.EXECUTOR_SERVICE @@ -24,7 +24,7 @@ import static com.monitor.middleware.rabbitmq.constant.InterVar.EXECUTOR_SERVICE
24 */ 24 */
25 @EnableScheduling 25 @EnableScheduling
26 @Component 26 @Component
27 -public class MonitTask { 27 +public class RabbitMonitTask {
28 28
29 @Scheduled(cron = "* 0/3 * * * ? ") 29 @Scheduled(cron = "* 0/3 * * * ? ")
30 public void doTask() { 30 public void doTask() {
@@ -20,7 +20,7 @@ import java.util.List; @@ -20,7 +20,7 @@ import java.util.List;
20 */ 20 */
21 @EnableScheduling 21 @EnableScheduling
22 @Component 22 @Component
23 -public class MoScanTask { 23 +public class RabbitScanTask {
24 24
25 @Autowired 25 @Autowired
26 IMObjectInfoService moService; 26 IMObjectInfoService moService;