Spring Boot 推送告警信息到 Alertmanager
告警信息直接通过http请求推送到Alertmanager告警
代码如下:
package com.legends.process.engine;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.Base64;
public class AlertmanagerTest {
public static void main(String[] args) {
String url="http://ip:port/api/v1/alerts";
RestTemplate restTemplate=new RestTemplate();
HttpHeaders headers = new HttpHeaders();
//test:test 为用户密码
String authorization=new String(Base64.getEncoder().encode("test:test".getBytes()));
System.out.println(authorization);;
// 若开启了用户认证需要在头添加
headers.add("Authorization","Basic "+authorization);
String body="[\n" +
" {\n" +
" \"status\": \"firing\",\n" +
" \"receiver\": \"webhook\",\n" +
" \"labels\": {\n" +
" \"alertname\": \"测试告警\",\n" +
" \"application\": \"test\",\n" +
" \"host\": \"192.181.4.110\",\n" +
" \"instance\": \"test\",\n" +
" \"job\": \"legends\",\n" +
" \"namespace\": \"test\",\n" +
" \"product\": \"test\"\n" +
" },\n" +
" \"annotations\": {\n" +
" \"description\": \"test告警\",\n" +
" \"summary\": \"test down\"\n" +
" },\n" +
" \"startsAt\": \"2020-10-23T17:20:02.679354841+08:00\",\n" +
" \"endsAt\": \"2020-12-02T10:32:47.686229825+08:00\",\n" +
" \"generatorURL\": \"http://localhost:9090/prometheus/graph?g0.expr=up+%3D%3D+0&g0.tab=1\"\n" +
" }\n" +
" ]";
HttpEntity entity = new HttpEntity<>(body, headers);
ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
System.out.println(result.getBody());
}
}
响应success表示成功
{"status":"success"}