list) {
114 | String title = monitor.getSchemaName() + "," + monitor.getDingTitle() + "[" + list.size() + "]";
115 | if (monitor.getShowCount() == null) {
116 | monitor.setShowCount(10);
117 | }
118 | if (list.size() > monitor.getShowCount()) {
119 | title = title + "[展示前" + monitor.getShowCount() + "个]";
120 | list = list.subList(0, monitor.getShowCount());
121 | }
122 | String dingAt = monitor.getDingAt();
123 | boolean atAll = ALL.equalsIgnoreCase(dingAt);
124 | if (!atAll && StringUtils.isNoneBlank(dingAt)) {
125 | String[] atMobiles = dingAt.split(",");
126 | title = title + ",@" + String.join(",@", atMobiles);
127 | }
128 | if (atAll) {
129 | title = title + ",@all";
130 | }
131 | StringBuilder content = new StringBuilder();
132 | for (String s : list) {
133 | content.append(s).append("\n");
134 | }
135 | FeishuTalkHelper.sendTextMsg(title, content.toString(), monitor.getDingToken());
136 | }
137 |
138 | @Override
139 | protected MonitorTypeEnum monitorType() {
140 | return MonitorTypeEnum.SQL;
141 | }
142 | }
143 |
--------------------------------------------------------------------------------
/src/main/java/me/ctf/lm/config/ExceptionHandlers.java:
--------------------------------------------------------------------------------
1 | package me.ctf.lm.config;
2 |
3 | import me.ctf.lm.dto.MapResult;
4 | import me.ctf.lm.util.BizException;
5 | import org.springframework.validation.BindException;
6 | import org.springframework.validation.BindingResult;
7 | import org.springframework.validation.ObjectError;
8 | import org.springframework.web.bind.MethodArgumentNotValidException;
9 | import org.springframework.web.bind.annotation.ExceptionHandler;
10 | import org.springframework.web.bind.annotation.RestControllerAdvice;
11 |
12 | /**
13 | * @author: chentiefeng[chentiefeng@linzikg.com]
14 | * @create: 2019-12-12 14:35
15 | */
16 | @RestControllerAdvice
17 | public class ExceptionHandlers {
18 | /**
19 | * json 格式错误验证
20 | *
21 | * @param ex
22 | * @return
23 | */
24 | @ExceptionHandler(value = MethodArgumentNotValidException.class)
25 | public MapResult errorHandler(MethodArgumentNotValidException ex) {
26 | StringBuilder errorMsg = new StringBuilder();
27 | BindingResult re = ex.getBindingResult();
28 | for (ObjectError error : re.getAllErrors()) {
29 | errorMsg.append(error.getDefaultMessage()).append(",");
30 | }
31 | errorMsg.delete(errorMsg.length() - 1, errorMsg.length());
32 | return MapResult.error(errorMsg.toString());
33 | }
34 |
35 |
36 | /**
37 | * 表单异常
38 | *
39 | * @param ex
40 | * @return
41 | */
42 | @ExceptionHandler(value = BindException.class)
43 | public MapResult errorHandler(BindException ex) {
44 | BindingResult result = ex.getBindingResult();
45 | StringBuilder errorMsg = new StringBuilder();
46 | for (ObjectError error : result.getAllErrors()) {
47 | errorMsg.append(error.getDefaultMessage()).append(",");
48 | }
49 | errorMsg.delete(errorMsg.length() - 1, errorMsg.length());
50 | return MapResult.error(errorMsg.toString());
51 | }
52 |
53 | /**
54 | * 表单异常
55 | *
56 | * @param ex
57 | * @return
58 | */
59 | @ExceptionHandler(value = BizException.class)
60 | public MapResult errorHandler(BizException ex) {
61 | return MapResult.error(ex.getCode(), ex.getMsg());
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/main/java/me/ctf/lm/config/JacksonConfiguration.java:
--------------------------------------------------------------------------------
1 | package me.ctf.lm.config;
2 |
3 | import com.fasterxml.jackson.core.JsonParser;
4 | import com.fasterxml.jackson.databind.DeserializationFeature;
5 | import com.fasterxml.jackson.databind.ObjectMapper;
6 | import com.fasterxml.jackson.databind.SerializationFeature;
7 | import org.springframework.boot.autoconfigure.AutoConfigureBefore;
8 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
9 | import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
10 | import org.springframework.context.annotation.Bean;
11 | import org.springframework.context.annotation.Configuration;
12 | import org.springframework.context.annotation.Primary;
13 | import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
14 |
15 | import java.text.SimpleDateFormat;
16 | import java.time.ZoneId;
17 | import java.util.Locale;
18 | import java.util.TimeZone;
19 |
20 | /**
21 | * @author chentiefeng
22 | * @date 2020-05-18 23:19
23 | */
24 | @Configuration
25 | @ConditionalOnClass(ObjectMapper.class)
26 | @AutoConfigureBefore(JacksonAutoConfiguration.class)
27 | public class JacksonConfiguration {
28 |
29 | @Primary
30 | @Bean
31 | public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
32 | builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
33 | //创建ObjectMapper
34 | ObjectMapper objectMapper = builder.createXmlMapper(false).build();
35 | //设置地点为中国
36 | objectMapper.setLocale(Locale.getDefault());
37 | //去掉默认的时间戳格式
38 | objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
39 | //设置为中国上海时区
40 | objectMapper.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
41 | //序列化时,日期的统一格式
42 | objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA));
43 | //失败处理
44 | objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
45 | objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
46 | //单引号处理
47 | objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
48 | //反序列化时,属性不存在的兼容处理
49 | objectMapper.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
50 | //日期格式化
51 | objectMapper.findAndRegisterModules();
52 | objectMapper.registerModule(new JavaTimeModule());
53 | return objectMapper;
54 | }
55 |
56 | }
57 |
58 |
--------------------------------------------------------------------------------
/src/main/java/me/ctf/lm/config/JavaTimeModule.java:
--------------------------------------------------------------------------------
1 | package me.ctf.lm.config;
2 |
3 | import com.fasterxml.jackson.databind.module.SimpleModule;
4 | import com.fasterxml.jackson.datatype.jsr310.PackageVersion;
5 | import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
6 | import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
7 | import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
8 | import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
9 | import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
10 | import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
11 |
12 | import java.time.LocalDate;
13 | import java.time.LocalDateTime;
14 | import java.time.LocalTime;
15 | import java.time.format.DateTimeFormatter;
16 |
17 | /**
18 | * @author chentiefeng
19 | * @date 2020-05-18 23:22
20 | */
21 | public class JavaTimeModule extends SimpleModule {
22 |
23 | public JavaTimeModule() {
24 | super(PackageVersion.VERSION);
25 | this.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
26 | this.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
27 | this.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
28 | this.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
29 | this.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
30 | this.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
31 | }
32 |
33 | }
34 |
35 |
--------------------------------------------------------------------------------
/src/main/java/me/ctf/lm/config/MonitorConfig.java:
--------------------------------------------------------------------------------
1 | package me.ctf.lm.config;
2 |
3 | import lombok.Data;
4 | import org.springframework.boot.context.properties.ConfigurationProperties;
5 | import org.springframework.context.annotation.Configuration;
6 |
7 | /**
8 | * @author: chentiefeng[chentiefeng@linzikg.com]
9 | * @create: 2019-12-20 11:17
10 | */
11 | @Data
12 | @Configuration
13 | @ConfigurationProperties(prefix = "monitor")
14 | public class MonitorConfig {
15 | /**
16 | * 主机状态
17 | */
18 | private String hostState;
19 | /**
20 | * 是否集群
21 | */
22 | private Boolean cluster;
23 | /**
24 | * 主机检查时间,分钟
25 | */
26 | private Integer duration;
27 | /**
28 | * 分发任务模式
29 | */
30 | private String distributedLockType;
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/me/ctf/lm/config/MybatisPlusConfig.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2016-2019 人人开源 All rights reserved.
3 | *
4 | * https://www.renren.io
5 | *
6 | * 版权所有,侵权必究!
7 | */
8 |
9 | package me.ctf.lm.config;
10 |
11 | import com.baomidou.mybatisplus.annotation.DbType;
12 | import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
13 | import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
14 | import org.springframework.context.annotation.Bean;
15 | import org.springframework.context.annotation.Configuration;
16 |
17 | /**
18 | * mybatis-plus配置
19 | *
20 | * @author Mark sunlightcs@gmail.com
21 | */
22 | @Configuration
23 | public class MybatisPlusConfig {
24 |
25 | /**
26 | * 分页插件
27 | */
28 | @Bean
29 | public MybatisPlusInterceptor mybatisPlusInterceptor() {
30 | MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
31 | interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
32 | return interceptor;
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/me/ctf/lm/config/ValidateConfig.java:
--------------------------------------------------------------------------------
1 | package me.ctf.lm.config;
2 |
3 | import org.hibernate.validator.HibernateValidator;
4 | import org.springframework.context.annotation.Bean;
5 | import org.springframework.context.annotation.Configuration;
6 |
7 | import javax.validation.Validation;
8 | import javax.validation.Validator;
9 | import javax.validation.ValidatorFactory;
10 |
11 | /**
12 | * 验证配置
13 | *
14 | * @author: chentiefeng[chentiefeng@linzikg.com]
15 | * @create: 2019-12-12 14:34
16 | */
17 | @Configuration
18 | public class ValidateConfig {
19 | @Bean
20 | public Validator validator() {
21 | ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
22 | .configure()
23 | .addProperty("hibernate.validator.fail_fast", "true")
24 | .buildValidatorFactory();
25 | return validatorFactory.getValidator();
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/me/ctf/lm/controller/LiteMonitorController.java:
--------------------------------------------------------------------------------
1 | package me.ctf.lm.controller;
2 |
3 | import lombok.extern.slf4j.Slf4j;
4 | import me.ctf.lm.dto.MapResult;
5 | import me.ctf.lm.entity.MonitorConfigEntity;
6 | import me.ctf.lm.enums.FrequencyEnum;
7 | import me.ctf.lm.schedule.ScheduleCmdExecutor;
8 | import me.ctf.lm.service.LiteMonitorConfigService;
9 | import org.springframework.web.bind.annotation.*;
10 |
11 | import javax.annotation.Resource;
12 | import java.util.*;
13 |
14 | /**
15 | * @author: chentiefeng[chentiefeng@linzikg.com]
16 | * @create: 2019-12-18 09:23
17 | */
18 | @RestController
19 | @RequestMapping("/liteMonitor")
20 | @Slf4j
21 | public class LiteMonitorController {
22 |
23 | @Resource
24 | private LiteMonitorConfigService liteMonitorConfigService;
25 |
26 | /**
27 | * 分页查询
28 | *
29 | * @return
30 | */
31 | @GetMapping("/page")
32 | public MapResult page(@RequestParam Map params) {
33 | try {
34 | return MapResult.ok().put("page", liteMonitorConfigService.queryPage(params));
35 | } catch (Exception e) {
36 | log.error(e.getMessage(), e);
37 | return MapResult.error(e.getMessage());
38 | }
39 | }
40 |
41 | /**
42 | * 执行
43 | *
44 | * @param ids
45 | * @return
46 | */
47 | @PostMapping("/execute")
48 | public MapResult execute(@RequestBody Long[] ids) {
49 | ScheduleCmdExecutor.execute(ids);
50 | return MapResult.ok();
51 | }
52 |
53 | /**
54 | * 保存
55 | *
56 | * @param monitor
57 | * @return
58 | */
59 | @PostMapping("/save")
60 | public MapResult save(@RequestBody MonitorConfigEntity monitor) {
61 | liteMonitorConfigService.submit(monitor);
62 | return MapResult.ok();
63 | }
64 |
65 | /**
66 | * 删除
67 | *
68 | * @param id
69 | * @return
70 | */
71 | @GetMapping("/delete")
72 | public MapResult delete(@RequestParam("id") Long id) {
73 | liteMonitorConfigService.removeById(id);
74 | return MapResult.ok();
75 | }
76 |
77 | /**
78 | * 启用
79 | *
80 | * @param id
81 | * @return
82 | */
83 | @GetMapping("/enabled")
84 | public MapResult enabled(@RequestParam("id") Long id) {
85 | liteMonitorConfigService.enabled(id);
86 | return MapResult.ok();
87 | }
88 |
89 | /**
90 | * 启用
91 | *
92 | * @param id
93 | * @return
94 | */
95 | @GetMapping("/info")
96 | public MapResult info(@RequestParam("id") Long id) {
97 | return MapResult.ok().put("entity", liteMonitorConfigService.info(id));
98 | }
99 |
100 | /**
101 | * info
102 | *
103 | * @param id
104 | * @return
105 | */
106 | @GetMapping("/disabled")
107 | public MapResult disabled(@RequestParam("id") Long id) {
108 | liteMonitorConfigService.disabled(id);
109 | return MapResult.ok();
110 | }
111 |
112 | /**
113 | * frequency
114 | *
115 | * @return
116 | */
117 | @GetMapping("/frequency")
118 | public MapResult frequency() {
119 | List