package com.wgcloud.service; import cn.hutool.core.collection.CollectionUtil; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.wgcloud.entity.DbInfo; import com.wgcloud.entity.DbTable; import com.wgcloud.entity.DbTableCount; import com.wgcloud.mapper.DbTableMapper; import com.wgcloud.util.*; import com.wgcloud.util.jdbc.ConnectionUtil; import com.wgcloud.util.msg.WarnMailUtil; import com.wgcloud.util.staticvar.BatchData; import com.wgcloud.util.staticvar.StaticKeys; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletRequest; import java.util.*; /** * @version v3.3 * @ClassName:DbTableService.java * @author: http://www.wgstart.com * @date: 2021年1月16日 * @Description: DbTableService.java * @Copyright: 2019-2021 wgcloud. All rights reserved. */ @Service public class DbTableService { private static final Logger logger = LoggerFactory.getLogger(DbTableService.class); public PageInfo selectByParams(Map params, int currPage, int pageSize) throws Exception { PageHelper.startPage(currPage, pageSize); List list = dbTableMapper.selectByParams(params); PageInfo pageInfo = new PageInfo(list); return pageInfo; } public void save(DbTable DbTable) throws Exception { DbTable.setId(UUIDUtil.getUUID()); DbTable.setCreateTime(new Date()); if (!StringUtils.isEmpty(DbTable.getTableName())) { DbTable.setTableName(DbTable.getTableName().trim()); } if (!StringUtils.isEmpty(DbTable.getResultExp())) { DbTable.setResultExp(DbTable.getResultExp().trim()); } dbTableMapper.save(DbTable); } public int countByParams(Map params) throws Exception { return dbTableMapper.countByParams(params); } public Long sumByParams(Map params) throws Exception { return dbTableMapper.sumByParams(params); } /** * 检查数据表监控,告警表达式,为true告警,false不告警 * * @param dbTableList 数据表列表 * @param dbInfos 数据源列表 * @return */ public void warnCheckExp(List dbTableList, List dbInfos) { //组装数据源map begin Map dbInfoMap = new HashMap<>(); for (DbInfo dbInfo : dbInfos) { dbInfoMap.put(dbInfo.getId(), dbInfo.getAccount()); } //组装数据源map end for (DbTable dbTable : dbTableList) { try { if (StringUtils.isEmpty(dbTable.getResultExp()) || null == dbTable.getTableCount()) { continue; } Boolean result = FormatUtil.validateExpression(dbTable.getResultExp(), dbTable.getTableCount()); if (result) { dbTable.setState(StaticKeys.DOWN_STATE); Runnable runnable = () -> { WarnMailUtil.sendDbTableDown(dbTable, dbInfoMap.get(dbTable.getDbInfoId())); }; ThreadPoolUtil.executor.execute(runnable); } } catch (Exception e) { logger.error("数据表监控编译表达式错误", e); } } } @Transactional public int deleteById(String[] id) throws Exception { return dbTableMapper.deleteById(id); } @Transactional public int deleteByDbInfoId(String dbInfoId) throws Exception { if (StringUtils.isEmpty(dbInfoId)) { return 0; } return dbTableMapper.deleteByDbInfoId(dbInfoId); } public void updateById(DbTable DbTable) throws Exception { if (!StringUtils.isEmpty(DbTable.getTableName())) { DbTable.setTableName(DbTable.getTableName().trim()); } if (!StringUtils.isEmpty(DbTable.getResultExp())) { DbTable.setResultExp(DbTable.getResultExp().trim()); } dbTableMapper.updateById(DbTable); } @Transactional public void updateRecord(List recordList) throws Exception { if (recordList.size() < 1) { return; } dbTableMapper.updateList(recordList); } public DbTable selectById(String id) throws Exception { return dbTableMapper.selectById(id); } public List selectAllByParams(Map params) throws Exception { return dbTableMapper.selectAllByParams(params); } /** * 数据表监控任务具体处理线程 */ public void taskThreadHandler() { Map params = new HashMap<>(); List dbTablesUpdate = new ArrayList(); Date date = new Date(); Long tableCount = 0L; try { List dbInfos = dbInfoService.selectAllByParams(params); for (DbInfo dbInfo : dbInfos) { if (ServerBackupUtil.DB_INFO_ID_LIST.contains(dbInfo.getId())) { logger.info("此数据源由wgcloud-server-backup监测:" + dbInfo.getAliasName()); //将server-backup节点处理的数据源,server节点就不监控这些数据源了 continue; } JdbcTemplate jdbcTemplate = connectionUtil.getJdbcTemplate(dbInfo); params.put("dbInfoId", dbInfo.getId()); params.put("active", StaticKeys.ON_STATE); List dbTables = selectAllByParams(params); for (DbTable dbTable : dbTables) { if (StringUtils.isEmpty(dbTable.getWhereVal())) { continue; } tableCount = connectionUtil.queryTableCount(dbInfo, jdbcTemplate, dbTable); DbTableCount dbTableCount = new DbTableCount(); dbTableCount.setCreateTime(date); dbTableCount.setDbTableId(dbTable.getId()); dbTableCount.setTableCount(tableCount); BatchData.DBTABLE_COUNT_LIST.add(dbTableCount); dbTable.setCreateTime(date); dbTable.setTableCount(tableCount); dbTablesUpdate.add(dbTable); } //没有数据表时,只检测数据源是否可用 begin if (CollectionUtil.isEmpty(dbTables)) { connectionUtil.getJdbcTemplate(dbInfo); } //没有数据表时,只检测数据源是否可用 end } if (dbTablesUpdate.size() > 0) { warnCheckExp(dbTablesUpdate, dbInfos); updateRecord(dbTablesUpdate); } } catch (Exception e) { logger.error("数据表监控任务错误", e); logInfoService.save("数据表监控任务错误", e.toString(), StaticKeys.LOG_XTCZ); } } /** * 保存操作日志 * * @param request 获取当前登录用户 * @param action 操作标识 */ public void saveLog(HttpServletRequest request, String action, DbTable dbTable) { if (null == dbTable) { return; } logInfoService.save(HostUtil.getAccountByRequest(request).getAccount() + action + "数据表监测信息:" + dbTable.getRemark(), "数据表:" + dbTable.getRemark(), StaticKeys.LOG_XTCZ); } @Autowired private DbTableMapper dbTableMapper; @Autowired private DbInfoService dbInfoService; @Autowired private DbTableCountService dbTableCountService; @Autowired private ConnectionUtil connectionUtil; @Autowired private LogInfoService logInfoService; }