Spring Boot 推送告警信息到 Alertmanager

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"}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值