[manager] 告警信息Webhook转发

This commit is contained in:
tomsun28
2021-12-18 10:20:54 +08:00
parent 91d7f216eb
commit 65ddcadafc
5 changed files with 99 additions and 3 deletions

View File

@@ -10,9 +10,16 @@ import com.usthe.manager.pojo.entity.NoticeReceiver;
import com.usthe.manager.service.MonitorService;
import com.usthe.manager.service.NoticeConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;
import java.util.Date;
import java.util.List;
@@ -32,16 +39,18 @@ public class DispatchAlarm {
private MonitorService monitorService;
private NoticeConfigService noticeConfigService;
private JavaMailSender javaMailSender;
private RestTemplate restTemplate;
public DispatchAlarm(AlerterWorkerPool workerPool, AlerterDataQueue dataQueue,
JavaMailSender javaMailSender,NoticeConfigService noticeConfigService,
AlertService alertService, MonitorService monitorService) {
AlertService alertService, MonitorService monitorService, RestTemplate restTemplate) {
this.workerPool = workerPool;
this.dataQueue = dataQueue;
this.alertService = alertService;
this.monitorService = monitorService;
this.noticeConfigService = noticeConfigService;
this.javaMailSender = javaMailSender;
this.restTemplate = restTemplate;
startDispatch();
}
@@ -117,7 +126,18 @@ public class DispatchAlarm {
}
private void sendWebHookAlert(NoticeReceiver receiver, Alert alert) {
try {
ResponseEntity<String> entity = restTemplate.postForEntity(receiver.getHookUrl(), alert, String.class);
if (entity.getStatusCode().value() < HttpStatus.BAD_REQUEST.value()) {
log.debug("Send WebHook: {} Success", receiver.getHookUrl());
} else {
log.warn("Send WebHook: {} Failed", receiver.getHookUrl());
}
} catch (ResourceAccessException e) {
log.warn("Send WebHook: {} Failed: {}.", receiver.getHookUrl(), e.getMessage());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
private void sendEmailAlert(NoticeReceiver receiver, Alert alert) {

View File

@@ -0,0 +1,31 @@
package com.usthe.manager.config;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import java.io.IOException;
import static java.net.Proxy.Type.HTTP;
/**
* restTemplate拦截器添加请求头信息
* @author tom
*/
public class HeaderRequestInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
// 默认发送json
if (request.getHeaders().getContentType() == null) {
request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
}
// 使用短链接
request.getHeaders().add(HttpHeaders.CONNECTION, "close");
return execution.execute(request, body);
}
}

View File

@@ -0,0 +1,35 @@
package com.usthe.manager.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import java.util.Collections;
/**
* restTemplate config
* todo 连接池
* @author tom
* @date 2021/12/18 08:46
*/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
RestTemplate restTemplate = new RestTemplate(factory);
restTemplate.setInterceptors(Collections.singletonList(new HeaderRequestInterceptor()));
return restTemplate;
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(5000);
factory.setReadTimeout(5000);
return factory;
}
}

View File

@@ -57,6 +57,7 @@
<logger name="io.netty" level="info"/>
<logger name="org.slf4j" level="info"/>
<logger name="ch.qos.logback" level="info"/>
<logger name="org.apache.kafka.clients" level="info"/>
<!-- 生产环境配置 -->
<springProfile name="prod">
@@ -69,7 +70,7 @@
<!-- 开发环境配置 -->
<springProfile name="dev">
<root level="INFO">
<root level="DEBUG">
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="SystemOutFileAppender"/>
<appender-ref ref="ErrOutFileAppender"/>

View File

@@ -94,6 +94,7 @@
<th nzAlign="center">策略名称</th>
<th nzAlign="center">接收人</th>
<th nzAlign="center">转发所有</th>
<th nzAlign="center">是否启用</th>
<th nzAlign="center">最新修改时间</th>
<th nzAlign="center" nzRight>操作</th>
</tr>
@@ -114,6 +115,14 @@
<span></span>
</nz-tag>
</td>
<td nzAlign="center">
<nz-tag *ngIf="data.enable" nzColor="green">
<span>开启</span>
</nz-tag>
<nz-tag *ngIf="!data.enable" nzColor="orange">
<span>关闭</span>
</nz-tag>
</td>
<td nzAlign="center">{{ data.gmtUpdate? data.gmtUpdate : data.gmtCreate }}</td>
<td nzAlign="center" nzRight>
<button nz-button nzType="primary" (click)="onEditOneNoticeRule(data)">