Authored by jack.xue

update

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