Authored by qinchao

mysql探测

... ... @@ -509,18 +509,6 @@ public class MObjectInfoCtrl {
return true;
}
/**
* 获取所有mysql host
*
* @return
*/
@RequestMapping(value = "/queryMysqlMobject")
@ResponseBody
public BaseResponse queryMysqlMobject() {
BaseResponse response = new BaseResponse();
response.setData(mobjectService.getMysqlMobject());
return response;
}
/**
* 获取所有java app服务
... ...
package com.monitor.other.mysqlMonitor.ctrl;
import com.monitor.cmdb.service.IMObjectInfoService;
import com.monitor.model.response.BaseResponse;
import com.monitor.other.mysqlMonitor.service.MysqlMonitorServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/mysqlMonitor")
public class MysqlMonitorCtrl {
@Autowired
IMObjectInfoService mobjectService;
@Autowired
MysqlMonitorServiceImpl mysqlMonitorService;
/**
* 获取所有mysql host
*
* @return
*/
@RequestMapping(value = "/queryMysqlMobject")
@ResponseBody
public BaseResponse queryMysqlMobject() {
BaseResponse response = new BaseResponse();
response.setData(mobjectService.getMysqlMobject());
return response;
}
/**
* 获取所有mysql host
*
* @return
*/
@RequestMapping(value = "/detectMysqlStatus")
@ResponseBody
public BaseResponse detectMysqlStatus(String ipAndPort) {
BaseResponse rtn=new BaseResponse();
rtn.setCode(201);
if(StringUtils.isNotBlank(ipAndPort)){
rtn.setCode(mysqlMonitorService.detectMysqlStatus(ipAndPort));
}
return rtn;
}
}
... ...
package com.monitor.other.mysqlMonitor.service;
import com.monitor.other.sqlOperate.constant.SqlQueryConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
@Service
public class MysqlMonitorServiceImpl {
Logger log = LoggerFactory.getLogger(MysqlMonitorServiceImpl.class);
private final String detectSQL="select 1";
//设置超时时间,autoReconnect超时不再重试
private final String param=SqlQueryConstant.DB_PARAM+"&autoReconnect=false&connectTimeout=3000&socketTimeout=6000";
//private final String detectSQL="show databases";
public int detectMysqlStatus(String ipAndPort){
int result=0;
try {
Class.forName(SqlQueryConstant.DRIVE_CLASS_MYSQL);
} catch (ClassNotFoundException e) {
log.error("SqlOperateServiceImpl queryDbList 找不到驱动:com.mysql.jdbc.Driver",e);
return result;
}
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection("jdbc:mysql://" + ipAndPort + "/"+param, "root", "123456");
// con = DriverManager.getConnection("jdbc:mysql://" + ipAndPort + "/"+SqlQueryConstant.DB_PARAM, SqlQueryConstant.DATA_SOURCE_USER, SqlQueryConstant.DATA_SOURCE_PWD);
st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
st.setFetchSize(SqlQueryConstant.FETCH_COUNT_FROM_MYSQL);
//st.setMaxRows(FETCH_COUNT_FROM_MYSQL);取全部数据
rs = st.executeQuery(detectSQL);
List<String> dbNameList=new ArrayList<String>();
while (rs.next()) {
//此处处理业务逻辑:获取数据
Object value=rs.getObject(1);
if(value!=null){
dbNameList.add(String.valueOf(value));
}
}
result=200;
return result;
} catch (SQLException e) {
log.error("sqlOperateServiceImpl queryDbList 查询数据库失败",e);
} catch (Exception e) {
log.error("sqlOperateServiceImpl queryDbList 查询数据库失败",e);
} finally {
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
log.error("sqlOperateServiceImpl queryDbList finally 关闭resultset异常",e);
}
try {
if(st!=null){
st.close();
}
} catch (SQLException e) {
log.error("sqlOperateServiceImpl queryDbList finally 关闭Statement异常", e);
}
try {
if(con!=null){
con.close();
}
} catch (SQLException e) {
log.error("sqlOperateServiceImpl queryDbList finally 关闭connect异常", e);
}
}
return result;
}
}
... ...
package com.monitor.other.sqlOperate.constant;
public class SqlQueryConstant {
public static final String DRIVE_CLASS_MYSQL = "com.mysql.jdbc.Driver";
public static final String DATA_SOURCE_USER="yh_ts_db";
public static final String DATA_SOURCE_PWD="Fij8631jkdx^ua870klp";
public static final int MAX_QUERY_RESULT_COUNT=30;
public static final int FETCH_COUNT_FROM_MYSQL=MAX_QUERY_RESULT_COUNT+1;
public static final String DB_PARAM="?useOldAliasMetadataBehavior=false&zeroDateTimeBehavior=convertToNull&autoReconnect=true&useUnicode=true&characterEncoding=utf-8&defaultFetchSize=" + FETCH_COUNT_FROM_MYSQL;
}
... ...
... ... @@ -11,6 +11,7 @@ import com.monitor.model.response.BaseResponse;
import com.monitor.model.response.PageResponse;
import com.monitor.mysql.mapper.SqlOperateDbSourceMapper;
import com.monitor.mysql.mapper.SqlOperateLogsMapper;
import com.monitor.other.sqlOperate.constant.SqlQueryConstant;
import com.monitor.other.sqlOperate.service.SqlOperateService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
... ... @@ -38,12 +39,6 @@ public class SqlOperateServiceImpl implements SqlOperateService {
private SqlOperateDbSourceMapper sqlOperateDbSourceMapper;
Logger log = LoggerFactory.getLogger(SqlOperateServiceImpl.class);
private final String DRIVE_CLASS_MYSQL = "com.mysql.jdbc.Driver";
private final String DATA_SOURCE_USER="yh_ts_db";
private final String DATA_SOURCE_PWD="Fij8631jkdx^ua870klp";
private final int MAX_QUERY_RESULT_COUNT=30;
private final int FETCH_COUNT_FROM_MYSQL=MAX_QUERY_RESULT_COUNT+1;
private final String DB_PARAM="?useOldAliasMetadataBehavior=false&zeroDateTimeBehavior=convertToNull&autoReconnect=true&useUnicode=true&characterEncoding=utf-8&defaultFetchSize=" + FETCH_COUNT_FROM_MYSQL;
/**
... ... @@ -75,7 +70,7 @@ public class SqlOperateServiceImpl implements SqlOperateService {
resetDbUerAndPwd(sqlOperateModel);
BaseResponse res=null;
try {
Class.forName(DRIVE_CLASS_MYSQL);
Class.forName(SqlQueryConstant.DRIVE_CLASS_MYSQL);
} catch (ClassNotFoundException e) {
log.error("SqlOperateServiceImpl queryDbList 找不到驱动:com.mysql.jdbc.Driver",e);
res=new BaseResponse("找不到驱动:com.mysql.jdbc.Driver");
... ... @@ -86,9 +81,9 @@ public class SqlOperateServiceImpl implements SqlOperateService {
Statement st = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection("jdbc:mysql://" + sqlOperateModel.getDataSourceInfo() + "/"+DB_PARAM, sqlOperateModel.getDataSourceUser(), sqlOperateModel.getDataSourcePwd());
con = DriverManager.getConnection("jdbc:mysql://" + sqlOperateModel.getDataSourceInfo() + "/"+SqlQueryConstant.DB_PARAM, sqlOperateModel.getDataSourceUser(), sqlOperateModel.getDataSourcePwd());
st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
st.setFetchSize(FETCH_COUNT_FROM_MYSQL);
st.setFetchSize(SqlQueryConstant.FETCH_COUNT_FROM_MYSQL);
//st.setMaxRows(FETCH_COUNT_FROM_MYSQL);取全部数据
rs = st.executeQuery("show databases");
List<String> dbNameList=new ArrayList<String>();
... ... @@ -163,7 +158,7 @@ public class SqlOperateServiceImpl implements SqlOperateService {
log.info("query4Table begin "+sql);
try {
Class.forName(DRIVE_CLASS_MYSQL);
Class.forName(SqlQueryConstant.DRIVE_CLASS_MYSQL);
} catch (ClassNotFoundException e) {
log.error("SqlOperateServiceImpl query4Table 找不到驱动:com.mysql.jdbc.Driver",e);
res=new BaseResponse("找不到驱动:com.mysql.jdbc.Driver");
... ... @@ -180,9 +175,9 @@ public class SqlOperateServiceImpl implements SqlOperateService {
try {
String dbUrl="jdbc:mysql://" + sqlOperateModel.getDataSourceInfo() ;
if(StringUtils.isNotBlank(sqlOperateModel.getDbInfo())){
dbUrl+= "/"+sqlOperateModel.getDbInfo()+DB_PARAM;
dbUrl+= "/"+sqlOperateModel.getDbInfo()+SqlQueryConstant.DB_PARAM;
}else{
dbUrl+= "/"+DB_PARAM;
dbUrl+= "/"+SqlQueryConstant.DB_PARAM;
}
con = DriverManager.getConnection(dbUrl, sqlOperateModel.getDataSourceUser(), sqlOperateModel.getDataSourcePwd());
... ... @@ -195,23 +190,23 @@ public class SqlOperateServiceImpl implements SqlOperateService {
}
selectFlag=true;
rtnList.put("showPi",true);
rtnList.put("pageSizePi",MAX_QUERY_RESULT_COUNT);
rtnList.put("pageSizePi",SqlQueryConstant.MAX_QUERY_RESULT_COUNT);
rtnList.put("currentPagePi",sqlOperateModel.getCurrentPage());
}else{
rtnList.put("showPi",false);
}
st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
st.setFetchSize(FETCH_COUNT_FROM_MYSQL);
st.setFetchSize(SqlQueryConstant.FETCH_COUNT_FROM_MYSQL);
//不是desc的查询需要限定数量了
//前台有了分页,不再需要设定desc的限定数量
if(selectFlag){
st.setMaxRows(MAX_QUERY_RESULT_COUNT*sqlOperateModel.getCurrentPage() +FETCH_COUNT_FROM_MYSQL);
st.setMaxRows(SqlQueryConstant.MAX_QUERY_RESULT_COUNT*sqlOperateModel.getCurrentPage() +SqlQueryConstant.FETCH_COUNT_FROM_MYSQL);
}
rs = st.executeQuery(sql);
rs.beforeFirst();
rs.relative(MAX_QUERY_RESULT_COUNT*sqlOperateModel.getCurrentPage());
rs.relative(SqlQueryConstant.MAX_QUERY_RESULT_COUNT*sqlOperateModel.getCurrentPage());
JSONArray ja_column=new JSONArray();
JSONArray ja_data=new JSONArray();
List<String> colNameList=new ArrayList<String>();
... ... @@ -262,9 +257,9 @@ public class SqlOperateServiceImpl implements SqlOperateService {
}
while (rs.next()) {
times++;
if(selectFlag&&times>MAX_QUERY_RESULT_COUNT){
if(selectFlag&&times>SqlQueryConstant.MAX_QUERY_RESULT_COUNT){
//if(times>MAX_QUERY_RESULT_COUNT){
queryDescr="数据量超过限制,本页只显示"+MAX_QUERY_RESULT_COUNT+"条";
queryDescr="数据量超过限制,本页只显示"+SqlQueryConstant.MAX_QUERY_RESULT_COUNT+"条";
hasNext="yes";
break;
}
... ... @@ -304,7 +299,7 @@ public class SqlOperateServiceImpl implements SqlOperateService {
//计算总页数
if(selectFlag){
if(count<MAX_QUERY_RESULT_COUNT&&sqlOperateModel.getCurrentPage()==0){
if(count<SqlQueryConstant.MAX_QUERY_RESULT_COUNT&&sqlOperateModel.getCurrentPage()==0){
//当查询第一页时,如果返回的数量小于游标循环的数量,则总数量=本次游标循环的数量,不需要再查询一次了
rtnList.put("totalPi",count);
}else{
... ... @@ -508,10 +503,10 @@ public class SqlOperateServiceImpl implements SqlOperateService {
private void resetDbUerAndPwd(SqlOperateModel sqlOperateModel){
if(StringUtils.isBlank(sqlOperateModel.getDataSourceUser())){
sqlOperateModel.setDataSourceUser(DATA_SOURCE_USER);
sqlOperateModel.setDataSourceUser(SqlQueryConstant.DATA_SOURCE_USER);
}
if(StringUtils.isBlank(sqlOperateModel.getDataSourcePwd())){
sqlOperateModel.setDataSourcePwd(DATA_SOURCE_PWD);
sqlOperateModel.setDataSourcePwd(SqlQueryConstant.DATA_SOURCE_PWD);
}
if(sqlOperateModel.getCurrentPage()<0){
sqlOperateModel.setCurrentPage(0);
... ...