实时日志功能

ScriptLogController

  1. @ApiOperation(value = "实时获取脚本日志", notes = "实时获取脚本日志")
  2. @GetMapping(CddApi.SCRIPT_LOG_RUNNING)
  3. public R<String> scriptRunningLog(
  4. @Param("logId") Integer logId,
  5. @Param("scriptId") Long scriptId,
  6. @Param("operationObj") String operationObj){
  7. String res = scriptLogService.scriptRunningLog(logId,scriptId,operationObj);
  8. return R.ok(res);
  9. }

ScriptLogServiceImpl

  1. @Override
  2. public String scriptRunningLog(Integer logId,Long scriptId, String operationObj) {
  3. //判断任务是否在运行中
  4. try {
  5. R<String> res = jobExecutorFeign.isInQueue(logId);
  6. if (res.getCode()==0){
  7. return "本次任务执行正在排队中!";
  8. }
  9. }catch (Exception e){
  10. log.error("查询任务队列状态失败!",e.getMessage());
  11. return "查询任务队列状态失败!"+e.getMessage();
  12. }
  13. List<ScriptVersion> scriptVersions = scriptVersionMapper.selectList(Wrappers.<ScriptVersion>query().eq("script_id", scriptId)
  14. .eq("is_default", 1));
  15. //获取脚本信息
  16. if (scriptVersions == null || scriptVersions.size()==0) {
  17. throw new BasicBusinessException("脚本为空!");
  18. }
  19. ScriptTool scriptTool = scriptToolMapper.selectById(scriptId);
  20. if (scriptTool == null){
  21. throw new BasicBusinessException("脚本为空!");
  22. }
  23. if (scriptTool!=null && scriptTool.getIsRunningLog()==0){
  24. throw new BasicBusinessException("当前脚本不支持实时日志查看!");
  25. }
  26. Long clusterId = 0L;
  27. ScriptVersion scriptVersion = scriptVersions.get(0);
  28. String scriptCode = scriptTool.getScriptCode();
  29. //从宿主机维度获得集群id
  30. if (scriptCode.indexOf("Node")>-1){
  31. QueryWrapper<CddNode> queryWrapper = new QueryWrapper<>();
  32. queryWrapper.eq("host_name",operationObj);
  33. CddNode cddNode = nodeMapper.selectOne(queryWrapper);
  34. clusterId = cddNode.getClusterId();
  35. }else {
  36. //从pod维度获得集群id
  37. String name = "";
  38. String[] operationObjs = operationObj.split(",");
  39. if (operationObjs[0]!=null){
  40. name = operationObjs[0];
  41. //查看是否是pod
  42. QueryWrapper<CddmPod> queryWrapper = new QueryWrapper<>();
  43. queryWrapper.eq("name",name);
  44. CddmPod cddmPod = podMapper.selectOne(queryWrapper);
  45. if (cddmPod!=null){
  46. name = cddmPod.getMgrName();
  47. }
  48. }
  49. //从数据库实例维度获得集群id
  50. QueryWrapper<InstanceConfig> queryWrapper = new QueryWrapper<>();
  51. queryWrapper.eq("name", name);
  52. InstanceConfig instanceConfig = instanceConfigMapper.selectOne(queryWrapper);
  53. if (instanceConfig == null) {
  54. throw new BasicBusinessException("实例为空!");
  55. }
  56. clusterId = instanceConfig.getClusterId();
  57. }
  58. //操作机
  59. try {
  60. Cluster cluster = clusterService.getById(clusterId);
  61. String scriptPath = scriptVersion.getScriptPath();
  62. String scriptName = "";
  63. if (scriptPath.indexOf("py") > -1) {
  64. scriptName = scriptPath.substring(scriptPath.lastIndexOf("/") + 1, scriptPath.lastIndexOf("py") + 2);
  65. } else {
  66. scriptName = scriptPath.substring(scriptPath.lastIndexOf("/") + 1, scriptPath.lastIndexOf("sh") + 2);
  67. }
  68. LocalDate date = LocalDate.now();
  69. DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  70. String dateStr = date.format(fmt);
  71. StringBuffer sb = new StringBuffer();
  72. sb.append("cat ");
  73. sb.append(CommonConstant.SCRIPT_LOG_PATH);
  74. sb.append(scriptName);
  75. sb.append("-");
  76. sb.append(dateStr);
  77. sb.append("-");
  78. sb.append(operationObj);
  79. sb.append(".");
  80. sb.append("log");
  81. log.info("cmd:{}", sb.toString());
  82. ClusterOperationVo clusterOperationVo = saltApiUtil.getAvailableoperateMachine(cluster.getOperationMachine());
  83. String result = saltApiUtil.remoteInvoke(clusterOperationVo, sb.toString(), null);
  84. // String result = saltApiUtil.remoteInvoke(cluster.getOperationMachine(), sb.toString());
  85. String aReturn = JSONObject.parseObject(result).getJSONArray("return").getJSONObject(0).getString(clusterOperationVo.getOperationName());
  86. //3、判断相等
  87. return aReturn;
  88. } catch (Exception e) {
  89. log.error("查看脚本日志文件异常:", e);
  90. }
  91. return null;
  92. }

获取数据库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;
    }
}

任务配置参考“MySQL跨集群健康检查任务”

数据库分析

直接用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);
}