kongdeqiang
2023-02-14 18d6ac5798b9ef96077f592ccc3f94b365006e6a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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<String, Object> params, int currPage, int pageSize) throws Exception {
        PageHelper.startPage(currPage, pageSize);
        List<DbTable> list = dbTableMapper.selectByParams(params);
        PageInfo<DbTable> pageInfo = new PageInfo<DbTable>(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<String, Object> params) throws Exception {
        return dbTableMapper.countByParams(params);
    }
 
    public Long sumByParams(Map<String, Object> params) throws Exception {
        return dbTableMapper.sumByParams(params);
    }
 
    /**
     * 检查数据表监控,告警表达式,为true告警,false不告警
     *
     * @param dbTableList 数据表列表
     * @param dbInfos     数据源列表
     * @return
     */
    public void warnCheckExp(List<DbTable> dbTableList, List<DbInfo> dbInfos) {
        //组装数据源map begin
        Map<String, String> 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<DbTable> recordList) throws Exception {
        if (recordList.size() < 1) {
            return;
        }
        dbTableMapper.updateList(recordList);
    }
 
    public DbTable selectById(String id) throws Exception {
        return dbTableMapper.selectById(id);
    }
 
    public List<DbTable> selectAllByParams(Map<String, Object> params) throws Exception {
        return dbTableMapper.selectAllByParams(params);
    }
 
    /**
     * 数据表监控任务具体处理线程
     */
    public void taskThreadHandler() {
        Map<String, Object> params = new HashMap<>();
        List<DbTable> dbTablesUpdate = new ArrayList<DbTable>();
        Date date = new Date();
        Long tableCount = 0L;
        try {
            List<DbInfo> 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<DbTable> 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;
 
 
}