...
|
...
|
@@ -2,6 +2,7 @@ package com.monitor.other.mysqlMonitor.service; |
|
|
|
|
|
import com.monitor.influxdb.contants.InfluxDBContants;
|
|
|
import com.monitor.influxdb.mapper.MetricMapper;
|
|
|
import com.monitor.influxdb.mapper.MysqlMonitorMapper;
|
|
|
import com.monitor.other.sqlOperate.constant.SqlQueryConstant;
|
|
|
import com.response.MysqlMobjectRep;
|
|
|
import org.apache.commons.lang.StringUtils;
|
...
|
...
|
@@ -21,51 +22,72 @@ public class MysqlMonitorServiceImpl { |
|
|
@Autowired
|
|
|
private MetricMapper metricMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private MysqlMonitorMapper mysqlMonitorMapper;
|
|
|
|
|
|
Logger log = LoggerFactory.getLogger(MysqlMonitorServiceImpl.class);
|
|
|
private final String detectSQL="select 1";
|
|
|
|
|
|
private final String connectMetric="connectstatus";
|
|
|
//设置超时时间,autoReconnect超时不再重试
|
|
|
private final String param=SqlQueryConstant.DB_PARAM+"&autoReconnect=false&connectTimeout=3000&socketTimeout=6000";
|
|
|
//private final String detectSQL="show databases";
|
|
|
|
|
|
|
|
|
public void setMySQLMetric(List<MysqlMobjectRep> mysqlMobjectReps){
|
|
|
Map<String,MysqlMobjectRep> aws_MysqlMobjectRepMap=new HashMap();
|
|
|
List<String> aws_ips=new ArrayList<String>();
|
|
|
Map<String,MysqlMobjectRep> qcloud_MysqlMobjectRepMap=new HashMap<>();
|
|
|
List<String> qcloud_ips=new ArrayList<String>();
|
|
|
Map<String,MysqlMobjectRep> all_ip_port_MysqlMobjectRepMap=new HashMap();
|
|
|
Map<String,MysqlMobjectRep> aws_ip_port_MysqlMobjectRepMap=new HashMap();
|
|
|
Map<String,MysqlMobjectRep> qcloud_ip_port_MysqlMobjectRepMap=new HashMap<>();
|
|
|
for (MysqlMobjectRep mysqlMobject : mysqlMobjectReps) {
|
|
|
if("aws".equalsIgnoreCase(mysqlMobject.getCloudType())){
|
|
|
aws_MysqlMobjectRepMap.put(mysqlMobject.getMoHostIp(),mysqlMobject);
|
|
|
aws_ips.add(mysqlMobject.getMoHostIp());
|
|
|
aws_ip_port_MysqlMobjectRepMap.put(mysqlMobject.getIpAndPort(),mysqlMobject);
|
|
|
}else{
|
|
|
qcloud_MysqlMobjectRepMap.put(mysqlMobject.getMoHostIp(),mysqlMobject);
|
|
|
qcloud_ips.add(mysqlMobject.getMoHostIp());
|
|
|
qcloud_ip_port_MysqlMobjectRepMap.put(mysqlMobject.getIpAndPort(),mysqlMobject);
|
|
|
}
|
|
|
all_ip_port_MysqlMobjectRepMap.put(mysqlMobject.getIpAndPort(),mysqlMobject);
|
|
|
}
|
|
|
if(aws_ips.size()>0){
|
|
|
Map<String, Map<String, String>> maps=metricMapper.selectMysqlInfos(InfluxDBContants.METRIC_AWS,aws_ips);
|
|
|
setMetricInfo(maps,aws_MysqlMobjectRepMap);
|
|
|
if(aws_ip_port_MysqlMobjectRepMap.size()>0){
|
|
|
Map<String, Map<String, String>> maps=metricMapper.selectMysqlInfos(InfluxDBContants.METRIC_AWS,aws_ip_port_MysqlMobjectRepMap.keySet());
|
|
|
setMetricInfo(maps,aws_ip_port_MysqlMobjectRepMap);
|
|
|
}
|
|
|
|
|
|
if(qcloud_ips.size()>0){
|
|
|
Map<String, Map<String, String>> maps=metricMapper.selectMysqlInfos(InfluxDBContants.METRIC_QCLOUD,qcloud_ips);
|
|
|
setMetricInfo(maps,qcloud_MysqlMobjectRepMap);
|
|
|
if(qcloud_ip_port_MysqlMobjectRepMap.size()>0){
|
|
|
Map<String, Map<String, String>> maps=metricMapper.selectMysqlInfos(InfluxDBContants.METRIC_QCLOUD,qcloud_ip_port_MysqlMobjectRepMap.keySet());
|
|
|
setMetricInfo(maps,qcloud_ip_port_MysqlMobjectRepMap);
|
|
|
}
|
|
|
|
|
|
//连接状态,从运维自己的influx查询
|
|
|
if(all_ip_port_MysqlMobjectRepMap.size()>0){
|
|
|
Map<String, Map<String, String>> maps=mysqlMonitorMapper.selectMysqlInfos(all_ip_port_MysqlMobjectRepMap.keySet());
|
|
|
setConnectStatus(maps,all_ip_port_MysqlMobjectRepMap);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private void setMetricInfo(Map<String, Map<String, String>> maps,Map<String,MysqlMobjectRep> mysqlMobjectRepMap){
|
|
|
if(maps!=null&&maps.size()>0){
|
|
|
for(String ip:maps.keySet()){
|
|
|
MysqlMobjectRep obj = mysqlMobjectRepMap.get(ip);
|
|
|
for(String ipAndPort:maps.keySet()){
|
|
|
MysqlMobjectRep obj = mysqlMobjectRepMap.get(ipAndPort);
|
|
|
if(obj!=null){
|
|
|
obj.getMysqlMetric().changeFromInfluxQueryResultMap(maps.get(ip));
|
|
|
String status=obj.getMysqlMetric().getStatus();
|
|
|
obj.getMysqlMetric().changeFromInfluxQueryResultMap(maps.get(ipAndPort));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//运维定时任务检测数据库状态
|
|
|
private void setConnectStatus(Map<String, Map<String, String>> maps,Map<String,MysqlMobjectRep> mysqlMobjectRepMap){
|
|
|
if(maps!=null&&maps.size()>0){
|
|
|
for(String ipAndPort:maps.keySet()){
|
|
|
Map<String, String> metrics=maps.get(ipAndPort);
|
|
|
MysqlMobjectRep obj = mysqlMobjectRepMap.get(ipAndPort);
|
|
|
if(obj!=null){
|
|
|
String connect=metrics.get(connectMetric);
|
|
|
|
|
|
int statusInt=0;
|
|
|
if(StringUtils.isNotBlank(status)){
|
|
|
if(connect!=null&&StringUtils.isNotBlank(connect)){
|
|
|
try{
|
|
|
Double d=Double.parseDouble(status);
|
|
|
Double d=Double.parseDouble(connect);
|
|
|
statusInt =d.intValue();
|
|
|
//System.out.println(statusInt);
|
|
|
}catch (Exception e){
|
...
|
...
|
@@ -75,7 +97,7 @@ public class MysqlMonitorServiceImpl { |
|
|
if(1==statusInt){
|
|
|
obj.setConnectStatus("OK");
|
|
|
}else{
|
|
|
obj.setConnectStatus("——");
|
|
|
obj.setConnectStatus("Fail");
|
|
|
}
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -83,8 +105,8 @@ public class MysqlMonitorServiceImpl { |
|
|
}
|
|
|
|
|
|
|
|
|
public String detectMysqlStatus(String ipAndPort){
|
|
|
String result="FAIL";
|
|
|
public int detectMysqlStatus(String ipAndPort){
|
|
|
int result=0;
|
|
|
try {
|
|
|
Class.forName(SqlQueryConstant.DRIVE_CLASS_MYSQL);
|
|
|
} catch (ClassNotFoundException e) {
|
...
|
...
|
@@ -113,8 +135,7 @@ public class MysqlMonitorServiceImpl { |
|
|
dbNameList.add(String.valueOf(value));
|
|
|
}
|
|
|
}
|
|
|
result="OK";
|
|
|
return result;
|
|
|
result=1;
|
|
|
} catch (SQLException e) {
|
|
|
log.error("MysqlMonitorServiceImpl detectMysqlStatus 查询数据库失败",e);
|
|
|
} catch (Exception e) {
|
...
|
...
|
@@ -141,6 +162,9 @@ public class MysqlMonitorServiceImpl { |
|
|
} catch (SQLException e) {
|
|
|
log.error("MysqlMonitorServiceImpl detectMysqlStatus finally 关闭connect异常", e);
|
|
|
}
|
|
|
//把结果写到influx中
|
|
|
String[] array_ipAndPort=ipAndPort.split(":");
|
|
|
mysqlMonitorMapper.insert(array_ipAndPort[0],array_ipAndPort[1],ipAndPort,connectMetric,result);
|
|
|
}
|
|
|
return result;
|
|
|
}
|
...
|
...
|
|