实时日志功能
ScriptLogController
@ApiOperation(value = "实时获取脚本日志", notes = "实时获取脚本日志")@GetMapping(CddApi.SCRIPT_LOG_RUNNING)public R<String> scriptRunningLog(@Param("logId") Integer logId,@Param("scriptId") Long scriptId,@Param("operationObj") String operationObj){String res = scriptLogService.scriptRunningLog(logId,scriptId,operationObj);return R.ok(res);}
ScriptLogServiceImpl
@Overridepublic String scriptRunningLog(Integer logId,Long scriptId, String operationObj) {//判断任务是否在运行中try {R<String> res = jobExecutorFeign.isInQueue(logId);if (res.getCode()==0){return "本次任务执行正在排队中!";}}catch (Exception e){log.error("查询任务队列状态失败!",e.getMessage());return "查询任务队列状态失败!"+e.getMessage();}List<ScriptVersion> scriptVersions = scriptVersionMapper.selectList(Wrappers.<ScriptVersion>query().eq("script_id", scriptId).eq("is_default", 1));//获取脚本信息if (scriptVersions == null || scriptVersions.size()==0) {throw new BasicBusinessException("脚本为空!");}ScriptTool scriptTool = scriptToolMapper.selectById(scriptId);if (scriptTool == null){throw new BasicBusinessException("脚本为空!");}if (scriptTool!=null && scriptTool.getIsRunningLog()==0){throw new BasicBusinessException("当前脚本不支持实时日志查看!");}Long clusterId = 0L;ScriptVersion scriptVersion = scriptVersions.get(0);String scriptCode = scriptTool.getScriptCode();//从宿主机维度获得集群idif (scriptCode.indexOf("Node")>-1){QueryWrapper<CddNode> queryWrapper = new QueryWrapper<>();queryWrapper.eq("host_name",operationObj);CddNode cddNode = nodeMapper.selectOne(queryWrapper);clusterId = cddNode.getClusterId();}else {//从pod维度获得集群idString name = "";String[] operationObjs = operationObj.split(",");if (operationObjs[0]!=null){name = operationObjs[0];//查看是否是podQueryWrapper<CddmPod> queryWrapper = new QueryWrapper<>();queryWrapper.eq("name",name);CddmPod cddmPod = podMapper.selectOne(queryWrapper);if (cddmPod!=null){name = cddmPod.getMgrName();}}//从数据库实例维度获得集群idQueryWrapper<InstanceConfig> queryWrapper = new QueryWrapper<>();queryWrapper.eq("name", name);InstanceConfig instanceConfig = instanceConfigMapper.selectOne(queryWrapper);if (instanceConfig == null) {throw new BasicBusinessException("实例为空!");}clusterId = instanceConfig.getClusterId();}//操作机try {Cluster cluster = clusterService.getById(clusterId);String scriptPath = scriptVersion.getScriptPath();String scriptName = "";if (scriptPath.indexOf("py") > -1) {scriptName = scriptPath.substring(scriptPath.lastIndexOf("/") + 1, scriptPath.lastIndexOf("py") + 2);} else {scriptName = scriptPath.substring(scriptPath.lastIndexOf("/") + 1, scriptPath.lastIndexOf("sh") + 2);}LocalDate date = LocalDate.now();DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");String dateStr = date.format(fmt);StringBuffer sb = new StringBuffer();sb.append("cat ");sb.append(CommonConstant.SCRIPT_LOG_PATH);sb.append(scriptName);sb.append("-");sb.append(dateStr);sb.append("-");sb.append(operationObj);sb.append(".");sb.append("log");log.info("cmd:{}", sb.toString());ClusterOperationVo clusterOperationVo = saltApiUtil.getAvailableoperateMachine(cluster.getOperationMachine());String result = saltApiUtil.remoteInvoke(clusterOperationVo, sb.toString(), null);// String result = saltApiUtil.remoteInvoke(cluster.getOperationMachine(), sb.toString());String aReturn = JSONObject.parseObject(result).getJSONArray("return").getJSONObject(0).getString(clusterOperationVo.getOperationName());//3、判断相等return aReturn;} catch (Exception e) {log.error("查看脚本日志文件异常:", e);}return null;}
获取数据库schema,表,用户接口
根据数据库架构,选择不同配置的账号密码连接数据库,获取相关信息。
@ApiOperation(value = "实例数据库列表接口", notes = "实例数据库列表接口")
@ResponseBody
@GetMapping(CddApi.INSTANCE_CONFIG_DATABASE)
public R<List<DataBaseVo>> getDataBases(
@RequestParam(value = "arcType", required = true, defaultValue = "") String arcType,
@RequestParam(value = "ip", required = true, defaultValue = "") String ip,
@RequestParam(value = "port", required = true, defaultValue = "") int port,
@RequestParam(value = "addr", required = false) String addr,
@RequestParam(value = "searchKey", required = false) String searchKey) {
// int port = 3306;
List<DataBaseVo> dataBaseVoList = instanceConfigService.getDataBases(arcType, ip, port, addr, searchKey);
return R.ok(dataBaseVoList);
}
@ApiOperation(value = "数据库表列表接口", notes = "容器表列表接口")
@ResponseBody
@GetMapping(CddApi.INSTANCE_CONFIG_TABLE)
public R<List<DataBaseTableVo>> getDBTableList(
@RequestParam(value = "arcType", required = true, defaultValue = "") String arcType,
@RequestParam(value = "ip", required = true, defaultValue = "") String ip,
@RequestParam(value = "port", required = true, defaultValue = "") int port,
@RequestParam(value = "addr", required = false) String addr,
@RequestParam(value = "database", required = true, defaultValue = "") String database,
@RequestParam(value = "searchKey", required = false) String searchKey) {
// int port = 3306;
List<DataBaseTableVo> dataBaseUserVoList = instanceConfigService.getDBTables(arcType, ip, port, addr, database, searchKey);
return R.ok(dataBaseUserVoList);
}
@ApiOperation(value = "数据库用户列表接口", notes = "数据库用户列表接口")
@ResponseBody
@GetMapping(CddApi.INSTANCE_CONFIG_USER)
public R<List<DataBaseUserVo>> getDBUserList(
@RequestParam(value = "arcType", required = true, defaultValue = "") String arcType,
@RequestParam(value = "ip", required = true, defaultValue = "") String ip,
@RequestParam(value = "port", required = true, defaultValue = "") int port,
@RequestParam(value = "addr", required = false) String addr,
@RequestParam(value = "searchKey", required = false) String searchKey) {
// int port = 3306;
List<DataBaseUserVo> dataBaseUserVoList = instanceConfigService.getDBUsers(arcType, ip, port, addr, searchKey);
return R.ok(dataBaseUserVoList);
}
mysql运维操作通用脚本定时任务调用
CronMysqlCommonHandler
@Component
@Slf4j
@JobHandler(value="cronMysqlCommonHandler")
public class CronMysqlCommonHandler extends IJobHandler {
@Autowired
CronMysqlCommonService cronMysqlCommonService;
@Override
public JobWrapper<String> execute(String param, Integer jobId, Integer logId) throws Exception {
log.info("开始执行定时脚本任务调度...........");
JobWrapper jobWrapper = cronMysqlCommonService.doExecute(jobId);
log.info("结束执行定时脚本任务调度...........");
return jobWrapper;
}
}
数据库分析
直接用salt调用脚本,并返回脚本执行信息
@ApiOperation(value = "MySQL故障分析", notes = "MySQL故障分析")
@SysLogAnnotation(module = LogModuleConstant.MYSQL_FAULT_ANALYSIS)
@PostMapping(CddApi.MYSQL_FAULT_ANALYSIS)
public R<String> faultAnalysis(@RequestBody @Validated FaultAnalysisDto faultAnalysisDto) {
String res = instanceConfigService.faultAnalysis(faultAnalysisDto);
return R.ok(res);
}
@ApiOperation(value = "MySQL锁等待查询", notes = "MySQL锁等待查询")
@SysLogAnnotation(module = LogModuleConstant.MYSQL_FAULT_ANALYSIS)
@PostMapping(CddApi.MYSQL_QUERY_LOCKS)
public R<String> queryLocks(@RequestBody @Validated QueryLocksDto queryLocksDto) {
String res = instanceConfigService.queryLocks(queryLocksDto);
return R.ok(res);
}
