Authored by qinchao

加gateway流量报警

... ... @@ -42,6 +42,8 @@ public class SnsMobileConfig {
private String baseMobile;
private String testMobile;
@PostConstruct
public void init(){
//大数据报警
... ... @@ -82,6 +84,12 @@ public class SnsMobileConfig {
baseMobile="";
}
//testMobile
testMobile=getUsersInfoUtil.getMobileByAlarmGroup("test");
if(testMobile==null){
testMobile="";
}
logger.info("初始化手机号码");
logger.info("JAVAAPI错误报警 javaApiError :"+javaApiError);
logger.info("JAVAAPI错误搜索组报警 javaApiSearchError :"+javaApiSearchError);
... ... @@ -89,6 +97,7 @@ public class SnsMobileConfig {
logger.info("JAVASERVICE错误报警 javaServiceMobile :"+javaServiceMobile);
logger.info("REDIS异常报警 redisMobile :"+redisMobile);
logger.info("基础组 baseMobile :"+baseMobile);
logger.info("testMobile :"+testMobile);
}
}
\ No newline at end of file
... ...
package com.monitor.influxdb.mapper;
import org.influxdb.dto.QueryResult;
/**
* Created by craig.qin on 2017/8/18.
*/
public interface GatewayAccessAllMapper {
QueryResult selectMeanValue(String influxDBName,int beforeMin,int afterMin) ;
}
... ...
package com.monitor.influxdb.mapper.impl;
import com.monitor.influxdb.InfluxDataReporter;
import com.monitor.influxdb.contants.InfluxDBContants;
import com.monitor.influxdb.mapper.GatewayAccessAllMapper;
import com.monitor.influxdb.mapper.GatewayAccessEventsMapper;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class GatewayAccessAllMapperImpl implements GatewayAccessAllMapper {
@Autowired
private InfluxDataReporter influxDataReporter;
/**
*
* @param influxDBName
* @return
*/
@Override
public QueryResult selectMeanValue(String influxDBName,int beforeMin,int afterMin) {
String command = "SELECT mean(total_count) / 10 FROM gateway_access_all WHERE time >= now() - "+beforeMin+"m ";
if(afterMin>0){
command += "and time < now() - "+afterMin +"m ";
}
return influxDataReporter.queryResult(influxDBName, command, InfluxDBContants.YOHO_EVENT_SAMPLE);
}
}
... ...
package com.monitor.other.task.someTask;
import com.monitor.common.config.SnsMobileConfig;
import com.monitor.common.service.AlarmMsgService;
import com.monitor.influxdb.contants.InfluxDBContants;
import com.monitor.influxdb.mapper.GatewayAccessAllMapper;
import com.monitor.influxdb.util.QueryResultUtil;
import com.monitor.other.dockermonitor.task.DockerMonitorTask;
import com.util.GetUsersInfoUtil;
import org.influxdb.dto.QueryResult;
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.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.text.DecimalFormat;
import java.util.List;
/**
* Created by craig.qin on 2018/4/3.
* gateway流量有巨大变化的时候,告警
*/
@Component
public class GatewayMonitorTask {
public static final Logger logger = LoggerFactory.getLogger(GatewayMonitorTask.class);
DecimalFormat df = new DecimalFormat("######0.00");
@Autowired
private GatewayAccessAllMapper gatewayAccessAllMapper;
@Autowired
private SnsMobileConfig snsMobileConfig;
@Autowired
private AlarmMsgService alarmMsgService;
//当前环境
@Value("${system.envi}")
private String env;
@Scheduled(cron = "${cron_task_gateway_monitor_aws}")
public void alarmGatewayMonitor_aws() {
if("test1".equals(env)||"test".equals(env)){
return ;
}
alarm(InfluxDBContants.AWS);
}
//@Scheduled(cron = "${cron_task_gateway_monitor_qcloud}")
public void alarmGatewayMonitor_qcloud() {
if("test1".equals(env)||"test".equals(env)){
return ;
}
alarm(InfluxDBContants.Q_CLOUD);
}
private void alarm(String influxdbName){
String mobile=snsMobileConfig.getTestMobile();
QueryResult queryResultFurther = gatewayAccessAllMapper.selectMeanValue(influxdbName,4,2);
QueryResult queryResultClose = gatewayAccessAllMapper.selectMeanValue(influxdbName,2,0);
if(queryResultClose==null||queryResultFurther==null){
//报警
alarmMsgService.sendSms("service_exception","gateway流量获取为空,influxdb :"+influxdbName,mobile);
return ;
}
Double meanValueClose=0d;
List<List<Object>> valuesList=QueryResultUtil.getValues(queryResultClose);
if(!CollectionUtils.isEmpty(valuesList)){
for(List<Object> values:valuesList){
meanValueClose=(Double)values.get(1);
}
}
Double meanValueFurther=0d;
valuesList=QueryResultUtil.getValues(queryResultFurther);
if(!CollectionUtils.isEmpty(valuesList)){
for(List<Object> values:valuesList){
meanValueFurther=(Double)values.get(1);
}
}
logger.info("GatewayMonitorTask influx is {} ,meanValueClose is {} , meanValueFurther is {}" ,influxdbName ,meanValueClose ,meanValueFurther);
if(meanValueFurther>0&&meanValueClose==0){
//报警
alarmMsgService.sendSms("service_exception","gateway流量变化异常,最近2分钟均值的0,influxdb :"+influxdbName,mobile);
return ;
}else if(meanValueFurther==0&&meanValueClose==0){
//不报警
return ;
}else{
//都大于零,比较变化幅度的绝对值和相对值
Double abs=Math.abs(meanValueFurther-meanValueClose);
Double compareAbs=Math.abs((meanValueFurther-meanValueClose)/meanValueFurther) ;
if(abs>200 || compareAbs>0.3){
//报警
alarmMsgService.sendSms("service_exception","gateway流量变化异常,最近2分钟均值变化值 "+abs+ ",变化率 "+df.format(compareAbs*100)+"% influxdb :"+influxdbName,mobile);
return ;
}
}
}
}
... ...
... ... @@ -35,6 +35,9 @@ cron_task_alarm_voice=0 0/5 * * * ?
cron_task_aplipay_voice=0 0/1 * * * ?
cron_task_gateway_monitor_aws=0 0/2 * * * ?
cron_task_gateway_monitor_qcloud=1 0/2 * * * ?
cron_task_device_active=0 0/5 * * * ?
device_active_period=60
... ...
... ... @@ -33,6 +33,9 @@ cron_task_alarm_voice=0 0/1 * * * ?
cron_task_aplipay_voice=0 0/1 * * * ?
cron_task_gateway_monitor_aws=0 0/2 * * * ?
cron_task_gateway_monitor_qcloud=1 0/2 * * * ?
cron_task_device_active=0 0/5 * * * ?
device_active_period=60
... ...
... ... @@ -42,6 +42,9 @@ cron_task_alarm_voice=0 0 0 * * ?
cron_task_aplipay_voice=0 0 0 * * ?
cron_task_gateway_monitor_aws=0 0 0 * * ?
cron_task_gateway_monitor_qcloud=0 0 0 * * ?
cron_task_device_active=0 0 0 * * ?
device_active_period=60
... ...