[manager,common]企业微信告警通知优化

This commit is contained in:
tomsun28
2022-02-22 10:01:15 +08:00
parent c0fa283993
commit c1010da2fe
8 changed files with 83 additions and 584 deletions

View File

@@ -51,11 +51,6 @@
<artifactId>httpclient</artifactId> <artifactId>httpclient</artifactId>
<version>4.5.13</version> <version>4.5.13</version>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>
<!--network--> <!--network-->
<dependency> <dependency>
<groupId>commons-net</groupId> <groupId>commons-net</groupId>

View File

@@ -1,473 +0,0 @@
package com.usthe.collector.collect.common.http;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Http各种请求方法的实现
*
* @author 花城
* @version 1.0
* @date 2022/2/21 7:16 下午
* @Description
*/
@Slf4j
public class HttpUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpUtils.class);
private CloseableHttpClient httpClient;
//连接超时时间为30s
private int connectionTimeout = 30000;
//读取超时时间为300s
private int socketTimeout = 300000;
private int MAX_TOTAL = 3000;
//单路由的最大并发连接数
private int MAX_PER_ROUTE = 1000;
//设置从链接池中获取连接时间为无限大
private int CONNECTION_REQUEST_TIMEOUT = 0;
private static ObjectMapper objectMapper;
static {
objectMapper = new ObjectMapper();
// asr返回格式为命名用下划线格式java为驼峰式json需要转换
// objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, false);
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
}
public void init() {
RequestConfig config = RequestConfig.custom().setConnectTimeout(connectionTimeout).
setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT).setSocketTimeout(socketTimeout).build();
PoolingHttpClientConnectionManager pccm = new PoolingHttpClientConnectionManager();
pccm.setMaxTotal(MAX_TOTAL);
pccm.setDefaultMaxPerRoute(MAX_PER_ROUTE);
httpClient = HttpClients.custom().setConnectionManager(pccm).setDefaultRequestConfig(config).build();
}
public HttpResult doGet(String url, Map<String, String> params) {
try {
URIBuilder uriBuilder = new URIBuilder(url);
for (Map.Entry<String, String> entry : params.entrySet()) {
uriBuilder.addParameter(entry.getKey(), entry.getValue());
}
HttpGet httpget = new HttpGet(uriBuilder.build());
httpget.addHeader("contentEncoding", "UTF-8");
return doExecuteHttpRqeuestBase(httpget);
} catch (Exception e) {
LOGGER.error("Error when doGet : " + url, e);
return failHttp(e.getMessage());
}
}
public HttpResult doGetWithHeaders(String url, Map<String, String> params, Map<String, String> headers) {
try {
URIBuilder uriBuilder = new URIBuilder(url);
params.forEach(uriBuilder::addParameter);
HttpGet httpget = new HttpGet(uriBuilder.build());
httpget.addHeader("contentEncoding", "UTF-8");
headers.forEach(httpget::addHeader);
return doExecuteHttpRqeuestBase(httpget);
} catch (Exception e) {
LOGGER.error("Error when doGet : " + url, e);
return failHttp(e.getMessage());
}
}
public HttpResult doPutWithHeaders(String url, Map<String, String> params, Map<String, String> headers) {
try {
URIBuilder uriBuilder = new URIBuilder(url);
params.forEach(uriBuilder::addParameter);
HttpPut httpget = new HttpPut(uriBuilder.build());
httpget.addHeader("contentEncoding", "UTF-8");
headers.forEach(httpget::addHeader);
return doExecuteHttpRqeuestBase(httpget);
} catch (Exception e) {
LOGGER.error("Error when doPut : " + url, e);
return failHttp(e.getMessage());
}
}
public HttpResult doPost(String url, Map<String, String> formParams) {
// 创建httppost
HttpPost httpPost = new HttpPost(url);
// 设置参数
RequestConfig.Builder customReqConf = RequestConfig.custom();
customReqConf.setConnectTimeout(connectionTimeout);
customReqConf.setSocketTimeout(socketTimeout);
httpPost.setConfig(customReqConf.build());
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : formParams.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity entity = null;
try {
entity = new UrlEncodedFormEntity(formparams, "UTF-8");
entity.setChunked(true);
} catch (UnsupportedEncodingException e) {
LOGGER.error("UrlEncode error : " + url, e);
}
httpPost.setEntity(entity);
return doExecuteHttpRqeuestBase(httpPost);
}
public HttpResult doPostWithHeader(String url, String bodyJsonString, Map<String, String> headers) {
// 创建httppost
HttpPost httpPost = new HttpPost(url);
// 设置参数
RequestConfig.Builder customReqConf = RequestConfig.custom();
customReqConf.setConnectTimeout(connectionTimeout);
customReqConf.setSocketTimeout(socketTimeout);
httpPost.setConfig(customReqConf.build());
headers.forEach(httpPost::addHeader);
StringEntity entity = new StringEntity(bodyJsonString, "UTF-8");//解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType(ContentType.APPLICATION_JSON.toString());
httpPost.setEntity(entity);
return doExecuteHttpRqeuestBase(httpPost);
}
public HttpResponse doPostWithOriginalResponse(String url, List<NameValuePair> nvpList, String charset) {
HttpResponse response = null;
try{
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader(HttpHeaders.CONNECTION, "close");
httpPost.setEntity(new UrlEncodedFormEntity(nvpList,charset));
response = httpClient.execute(httpPost);
}catch (Exception e){
LOGGER.error("doPostWithOriginalResponse error : ", e);
}
return response;
}
public HttpResult doPost(String url, String bodyJsonString) {
// 创建httppost
HttpPost httpPost = new HttpPost(url);
// 设置参数
RequestConfig.Builder customReqConf = RequestConfig.custom();
customReqConf.setConnectTimeout(connectionTimeout);
customReqConf.setSocketTimeout(socketTimeout);
httpPost.setConfig(customReqConf.build());
StringEntity entity = new StringEntity(bodyJsonString, "UTF-8");//解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType(ContentType.APPLICATION_JSON.toString());
httpPost.setEntity(entity);
return doExecuteHttpRqeuestBase(httpPost);
}
public HttpResult doPostWithEntity(String url, HttpEntity entity) {
// 创建httppost
HttpPost httpPost = new HttpPost(url);
// 设置参数
RequestConfig.Builder customReqConf = RequestConfig.custom();
customReqConf.setConnectTimeout(connectionTimeout);
customReqConf.setSocketTimeout(socketTimeout);
httpPost.setConfig(customReqConf.build());
httpPost.setEntity(entity);
return doExecuteHttpRqeuestBase(httpPost);
}
public HttpResult doDeleteWithHeaders(String url, Map<String, String> params, Map<String,String> headers) {
try {
URIBuilder uriBuilder = new URIBuilder(url);
params.forEach(uriBuilder::addParameter);
HttpDelete httpDelete = new HttpDelete(uriBuilder.build());
httpDelete.addHeader("contentEncoding", "UTF-8");
headers.forEach(httpDelete::addHeader);
return doExecuteHttpRqeuestBase(httpDelete);
} catch (Exception e) {
LOGGER.error("Error when doDelete : " + url, e);
return failHttp(e.getMessage());
}
}
private HttpResult doExecuteHttpRqeuestBase(HttpRequestBase httpRequestBase) {
try {
HttpResultResponseHandler sph = new HttpResultResponseHandler();
return httpClient.execute(httpRequestBase, sph);
} catch (Exception e) {
LOGGER.error("Error when " + httpRequestBase.getMethod() + " : " + httpRequestBase.getURI(), e);
return failHttp(e.getMessage());
} finally {
if (httpRequestBase != null) {
httpRequestBase.abort();
}
}
}
/**
* 请求体为json格式对象
*
* @param path
* @param form
* @return
*/
public static HttpPost newJsonBodyPostRequest(String path, Map<String, Object> form) {
HttpPost post = new HttpPost(path);
String json = toJsonString(form);
HttpEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
post.setEntity(entity);
post.addHeader(HttpHeaders.ACCEPT, CONTENT_TYPE);
post.addHeader(HttpHeaders.CONTENT_TYPE, CONTENT_TYPE);
return post;
}
static final String CONTENT_TYPE = "application/json";
public static String toJsonString(Object val) {
try {
byte[] bytes = objectMapper.writeValueAsBytes(val);
return new String(bytes, Charsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException("toJsonString error", e);
}
}
public static <T> T parseHttpResponseObject(String json, TypeReference<T> type) throws IOException {
if (Strings.isNullOrEmpty(json)) {
return null;
}
return objectMapper.readValue(json, type);
}
public static String toRequestString(HttpRequestBase r) {
if (r == null) {
return "null";
}
return r.getMethod() + " " + r.getURI();
}
private class HttpResultResponseHandler implements ResponseHandler<HttpResult> {
@Override
public HttpResult handleResponse(HttpResponse response) throws IOException {
HttpResult result = new HttpResult();
HttpEntity entity = response.getEntity();
Header[] headers = response.getAllHeaders();
if (isNotEmpty(headers)) {
HashMap<String, String> headerMap = new HashMap<String, String>();
for (Header h : headers) {
headerMap.put(h.getName(), h.getValue());
}
result.setHeader(headerMap);
}
InputStream i = entity.getContent();
String res = IOUtils.toString(new InputStreamReader(i, "UTF-8"));
result.setCode(response.getStatusLine().getStatusCode());
result.setBody(res);
i.close();
return result;
}
}
private boolean isNotEmpty(Object[] array) {
return array != null && array.length > 0;
}
private HttpResult failHttp(String message) {
HttpResult result = new HttpResult();
result.setCode(500);
result.setBody(message);
return result;
}
public static class HttpResult {
private String body;
private Map<String, String> header;
private int code;
public String getBody() {
return body;
}
void setBody(String body) {
this.body = body;
}
public Map<String, String> getHeader() {
return header;
}
void setHeader(Map<String, String> header) {
this.header = header;
}
public int getCode() {
return code;
}
void setCode(int code) {
this.code = code;
}
public boolean is200() {
return this.code == 200;
}
public boolean isNot200() {
return !this.is200();
}
public HttpResult checkHttpCode() {
if (isNot200()) {
throw new RuntimeException("执行http方法出错返回值非200body=" + body);
}
return this;
}
}
public void setConnectionTimeout(int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}
public void setSocketTimeout(int socketTimeout) {
this.socketTimeout = socketTimeout;
}
public void setMAX_PER_ROUTE(int MAX_PER_ROUTE) {
this.MAX_PER_ROUTE = MAX_PER_ROUTE;
}
public void setMAX_TOTAL(int MAX_TOTAL) {
this.MAX_TOTAL = MAX_TOTAL;
}
public void setCONNECTION_REQUEST_TIMEOUT(int CONNECTION_REQUEST_TIMEOUT) {
this.CONNECTION_REQUEST_TIMEOUT = CONNECTION_REQUEST_TIMEOUT;
}
/**
* 发送post请求
*
* @param url 请求的url
* @param body json串
* @return
*/
public static String sendPostJsonBody(String url, String body) {
log.debug("[HttpClientUtil][sendPostJsonBody] 入参 url={} body={}", url, body);
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json;charset=utf-8");
StringEntity entity = new StringEntity(body, "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
try {
HttpClient client = httpClientBuilder.build();
HttpResponse response = client.execute(httpPost);
if (response.getStatusLine() != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity(), "utf-8");
log.debug("[HttpClientUtil][sendPostJsonBody] 结果 url={} result={}", url, result);
return result;
}
log.warn("[HttpClientUtil][sendPostJsonBody] 请求失败 response={}", url, response.toString());
return "";
} catch (IOException ex) {
log.error("[HttpClientUtil][sendPostJsonBody] 请求异常 ex={}", url, ex);
return "";
}
}
/**
* 发送post请求
*
* @param url 请求的url
* @param body json串
* @return
*/
public static String sendPostJsonBodyNoEncoding(String url, String body) {
log.debug("[HttpClientUtil][sendPostJsonBody] 入参 url={} body={}", url, body);
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json;charset=utf-8");
StringEntity entity = new StringEntity(body, "utf-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
try {
HttpClient client = httpClientBuilder.build();
HttpResponse response = client.execute(httpPost);
if (response.getStatusLine() != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity(), "utf-8");
log.debug("[HttpClientUtil][sendPostJsonBody] 结果 url={} result={}", url, result);
return result;
}
log.warn("[HttpClientUtil][sendPostJsonBody] 请求失败 response={}", url, response.toString());
return "";
} catch (IOException ex) {
log.error("[HttpClientUtil][sendPostJsonBody] 请求异常 ex={}", url, ex);
return "";
}
}
public static String sendGet(String url) {
log.debug("[HttpClientUtil][sendPostJsonBody] 入参 url={} ", url);
HttpGet httpPost = new HttpGet(url);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
try {
HttpClient client = httpClientBuilder.build();
HttpResponse response = client.execute(httpPost);
if (response.getStatusLine() != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity(), "utf-8");
log.debug("[HttpClientUtil][sendPostJsonBody] 结果 url={} result={}", url, result);
return result;
}
log.warn("[HttpClientUtil][sendPostJsonBody] 请求失败 response={}", url, response.toString());
return "";
} catch (IOException ex) {
log.error("[HttpClientUtil][sendPostJsonBody] 请求异常 ex={}", url, ex);
return "";
}
}
}

View File

@@ -1,54 +0,0 @@
package com.usthe.common.entity.dto;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* @author 花城
* @version 1.0
* @date 2022/2/21 6:55 下午
* @Description
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "企业微信WebHook模版")
public class WeChatWebHookDTO {
/**
* 消息类型
*/
private String msgtype;
private TextDTO text;
private MarkdownDTO markdown;
@Data
public static class TextDTO{
/**
* 消息内容
*/
private String content;
/**
* @人的名称英文拼写列表
*/
private List<String> mentioned_list;
}
@Data
public static class MarkdownDTO{
/**
* 消息内容
*/
private String content;
}
}

View File

@@ -46,7 +46,7 @@ public class NoticeReceiver {
@NotNull @NotNull
private String name; private String name;
@ApiModelProperty(value = "通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号", accessMode = READ_WRITE, position = 2) @ApiModelProperty(value = "通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号 4-企业微信机器人 5-钉钉机器人", accessMode = READ_WRITE, position = 2)
@Min(0) @Min(0)
@Max(3) @Max(3)
@NotNull @NotNull
@@ -61,13 +61,17 @@ public class NoticeReceiver {
private String email; private String email;
@ApiModelProperty(value = "URL地址, 通知方式为webhook有效", example = "https://www.tancloud.cn", accessMode = READ_WRITE, position = 5) @ApiModelProperty(value = "URL地址, 通知方式为webhook有效", example = "https://www.tancloud.cn", accessMode = READ_WRITE, position = 5)
@Length(max = 100) @Length(max = 300)
private String hookUrl; private String hookUrl;
@ApiModelProperty(value = "wechat用户openId, 通知方式为微信公众号有效", example = "343432", accessMode = READ_WRITE, position = 6) @ApiModelProperty(value = "openId, 通知方式为微信公众号或企业微信机器人有效", example = "343432", accessMode = READ_WRITE, position = 6)
@Length(max = 100) @Length(max = 300)
private String wechatId; private String wechatId;
@ApiModelProperty(value = "访问token, 通知方式为钉钉机器人有效", example = "34823984635647", accessMode = READ_WRITE, position = 7)
@Length(max = 300)
private String accessToken;
@ApiModelProperty(value = "此条记录创建者", example = "tom", accessMode = READ_ONLY, position = 7) @ApiModelProperty(value = "此条记录创建者", example = "tom", accessMode = READ_ONLY, position = 7)
private String creator; private String creator;

View File

@@ -1,24 +0,0 @@
package com.usthe.common.util;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
/**
* @author 花城
* @version 1.0
* @date 2022/2/21 7:07 下午
* @Description
*/
@AllArgsConstructor
@Getter
public enum PriorityLevelEnum {
EMERGENCY(0,"紧急告警"),
CRITICAL(1,"严重告警"),
WARNING(2,"警告告警"),
;
private Integer level;
private String message;
}

View File

@@ -1,14 +1,11 @@
package com.usthe.manager.component.alerter; package com.usthe.manager.component.alerter;
import com.alibaba.fastjson.JSON;
import com.usthe.alert.AlerterDataQueue; import com.usthe.alert.AlerterDataQueue;
import com.usthe.alert.AlerterWorkerPool; import com.usthe.alert.AlerterWorkerPool;
import com.usthe.collector.collect.common.http.HttpUtils;
import com.usthe.common.util.CommonUtil; import com.usthe.common.util.CommonUtil;
import com.usthe.common.util.PriorityLevelEnum;
import com.usthe.common.entity.alerter.Alert; import com.usthe.common.entity.alerter.Alert;
import com.usthe.alert.service.AlertService; import com.usthe.alert.service.AlertService;
import com.usthe.common.entity.dto.WeChatWebHookDTO; import com.usthe.manager.pojo.dto.WeWorkWebHookDTO;
import com.usthe.common.util.CommonConstants; import com.usthe.common.util.CommonConstants;
import com.usthe.common.entity.manager.Monitor; import com.usthe.common.entity.manager.Monitor;
import com.usthe.common.entity.manager.NoticeReceiver; import com.usthe.common.entity.manager.NoticeReceiver;
@@ -120,13 +117,14 @@ public class DispatchAlarm {
List<NoticeReceiver> receivers = matchReceiverByNoticeRules(alert); List<NoticeReceiver> receivers = matchReceiverByNoticeRules(alert);
// todo 发送通知这里暂时单线程 // todo 发送通知这里暂时单线程
for (NoticeReceiver receiver : receivers) { for (NoticeReceiver receiver : receivers) {
sendWeWorkRobotAlert(receiver, alert);
switch (receiver.getType()) { switch (receiver.getType()) {
// todo 短信通知 // todo 短信通知
case 0: break; case 0: break;
case 1: sendEmailAlert(receiver, alert); break; case 1: sendEmailAlert(receiver, alert); break;
case 2: sendWebHookAlert(receiver, alert); break; case 2: sendWebHookAlert(receiver, alert); break;
case 3: sendWeChatAlert(receiver, alert); break; case 3: sendWeChatAlert(receiver, alert); break;
case 4: sendWeChatWebHookAlert(receiver, alert);break; case 4: sendWeWorkRobotAlert(receiver, alert);break;
default: break; default: break;
} }
} }
@@ -137,26 +135,37 @@ public class DispatchAlarm {
* @param receiver 通知配置信息 * @param receiver 通知配置信息
* @param alert 告警信息 * @param alert 告警信息
*/ */
private void sendWeChatWebHookAlert(NoticeReceiver receiver, Alert alert) { private void sendWeWorkRobotAlert(NoticeReceiver receiver, Alert alert) {
WeChatWebHookDTO weChatWebHookDTO = new WeChatWebHookDTO(); WeWorkWebHookDTO weWorkWebHookDTO = new WeWorkWebHookDTO();
weChatWebHookDTO.setMsgtype("markdown"); WeWorkWebHookDTO.MarkdownDTO markdownDTO = new WeWorkWebHookDTO.MarkdownDTO();
WeChatWebHookDTO.MarkdownDTO markdownDTO = new WeChatWebHookDTO.MarkdownDTO();
StringBuilder content = new StringBuilder(); StringBuilder content = new StringBuilder();
content.append("\t\t\t\t<font color=\"info\">[TanCloud探云告警]</font>\n" + content.append("<font color=\"info\">[TanCloud探云告警通知]</font>\n告警目标对象 : <font color=\"info\">")
"告警目标对象 : <font color=\"info\">" + alert.getTarget() + "</font>\n" + .append(alert.getTarget()).append("</font>\n")
"所属监控ID : " + alert.getMonitorId() + "\n" + .append("所属监控ID : ").append(alert.getMonitorId()).append("\n")
"所属监控名称 : " + alert.getMonitorName() + "\n"); .append("所属监控名称 : ").append(alert.getMonitorName()).append("\n");
if (alert.getPriority() < PriorityLevelEnum.WARNING.getLevel()){ if (alert.getPriority() < CommonConstants.ALERT_PRIORITY_CODE_WARNING) {
content.append("告警级别 <font color=\"warning\">: " + CommonUtil.transferAlertPriority(alert.getPriority()) + "</font>\n"); content.append("告警级别 : <font color=\"warning\">")
.append(CommonUtil.transferAlertPriority(alert.getPriority())).append("</font>\n");
}else { }else {
content.append("告警级别 <font color=\"comment\">: " + CommonUtil.transferAlertPriority(alert.getPriority()) + "</font>\n"); content.append("告警级别 : <font color=\"comment\">")
.append(CommonUtil.transferAlertPriority(alert.getPriority())).append("</font>\n");
} }
content.append("内容详情 : " + alert.getContent()); content.append("内容详情 : ").append(alert.getContent());
markdownDTO.setContent(content.toString()); markdownDTO.setContent(content.toString());
weChatWebHookDTO.setMarkdown(markdownDTO); weWorkWebHookDTO.setMarkdown(markdownDTO);
//TODO 以后转移到实体类中 String webHookUrl = WeWorkWebHookDTO.WEBHOOK_URL + receiver.getWechatId();
String webHookUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=fcf9ddxxx-ebaf-48a2-810c-404xxxxxxd3bf"; try {
HttpUtils.sendPostJsonBody(webHookUrl, JSON.toJSONString(weChatWebHookDTO)); ResponseEntity<String> entity = restTemplate.postForEntity(webHookUrl, weWorkWebHookDTO, String.class);
if (entity.getStatusCode() == HttpStatus.OK) {
log.debug("Send weWork webHook: {} Success", webHookUrl);
} else {
log.warn("Send weWork webHook: {} Failed: {}", webHookUrl, entity.getBody());
}
} catch (ResourceAccessException e) {
log.warn("Send WebHook: {} Failed: {}.", receiver.getHookUrl(), e.getMessage());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
} }
private void sendWeChatAlert(NoticeReceiver receiver, Alert alert) { private void sendWeChatAlert(NoticeReceiver receiver, Alert alert) {

View File

@@ -0,0 +1,41 @@
package com.usthe.manager.pojo.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 企业微信机器人请求消息体
* @author 花城
* @version 1.0
* @date 2022/2/21 6:55 下午
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class WeWorkWebHookDTO {
public static final String WEBHOOK_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=";
private static final String MARKDOWN = "markdown";
/**
* 消息类型
*/
private String msgtype = MARKDOWN;
/**
* markdown消息
*/
private MarkdownDTO markdown;
@Data
public static class MarkdownDTO {
/**
* 消息内容
*/
private String content;
}
}

View File

@@ -148,11 +148,12 @@ CREATE TABLE notice_receiver
( (
id bigint not null auto_increment comment '消息接收人ID', id bigint not null auto_increment comment '消息接收人ID',
name varchar(100) not null comment '消息接收人姓名', name varchar(100) not null comment '消息接收人姓名',
type tinyint not null comment '通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号', type tinyint not null comment '通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号 4-企业微信机器人 5-钉钉机器人',
phone varchar(100) comment '手机号, 通知方式为手机短信时有效', phone varchar(100) comment '手机号, 通知方式为手机短信时有效',
email varchar(100) comment '邮箱账号, 通知方式为邮箱时有效', email varchar(100) comment '邮箱账号, 通知方式为邮箱时有效',
hook_url varchar(100) comment 'URL地址, 通知方式为webhook有效', hook_url varchar(255) comment 'URL地址, 通知方式为webhook有效',
wechat_id varchar(100) comment 'wechat用户openId, 通知方式为微信公众号有效', wechat_id varchar(255) comment 'openId, 通知方式为微信公众号或企业微信机器人有效',
access_token varchar(255) comment '访问token, 通知方式为钉钉机器人有效',
creator varchar(100) comment '创建者', creator varchar(100) comment '创建者',
modifier varchar(100) comment '最新修改者', modifier varchar(100) comment '最新修改者',
gmt_create timestamp default current_timestamp comment 'create time', gmt_create timestamp default current_timestamp comment 'create time',