From c2ee1471a238933aa704b8740f01d2ed0e2257f0 Mon Sep 17 00:00:00 2001
From: pancm <1060589146@qq.com>
Date: Fri, 8 Sep 2023 17:22:29 +0800
Subject: [PATCH] update
---
springboot-summarizing/pom.xml | 7 +-
.../java/com/pancm/web/AlertController.java | 90 +++++++++++++++++++
2 files changed, 95 insertions(+), 2 deletions(-)
create mode 100644 springboot-summarizing/src/main/java/com/pancm/web/AlertController.java
diff --git a/springboot-summarizing/pom.xml b/springboot-summarizing/pom.xml
index 7e48895..c3a5e8a 100644
--- a/springboot-summarizing/pom.xml
+++ b/springboot-summarizing/pom.xml
@@ -197,13 +197,16 @@
-
+
+ cn.hutool
+ hutool-all
+ 5.3.7
+
compile
- src
springboot-summarizing
diff --git a/springboot-summarizing/src/main/java/com/pancm/web/AlertController.java b/springboot-summarizing/src/main/java/com/pancm/web/AlertController.java
new file mode 100644
index 0000000..dea7fb0
--- /dev/null
+++ b/springboot-summarizing/src/main/java/com/pancm/web/AlertController.java
@@ -0,0 +1,90 @@
+package com.pancm.web;
+
+import cn.hutool.core.date.DateUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.HashMap;
+import java.util.Map;
+
+
+/**
+ * @Author pancm
+ * @Description Grafana和飞书webhook中转服务的代码
+ * @Date 2023/9/8
+ * @Param
+ * @return
+ **/
+@RestController
+@RequestMapping("alert")
+@Slf4j
+public class AlertController {
+
+ @Value("${feishu.webhook-url:https://open.feishu.cn/open-apis/bot/v2/hook/07f75330-f57b-40dd-8236-30092b0f4cc2}")
+ private String webhookUrl;
+
+ @Autowired
+ private RestTemplate restTemplate;
+
+
+ private String msg = "告警标题:%s\n告警描述:%s\n面板URL:%s\n发生时间:%s";
+
+
+ @RequestMapping("/webhook/receive")
+ public void receive(@RequestBody String message) {
+ log.info("获取的Grafana的告警数据:{}",message);
+ Map m = createMessage(getText(message));
+ HttpHeaders headers = new HttpHeaders();
+ headers.setContentType(MediaType.APPLICATION_JSON);
+ restTemplate.postForEntity(webhookUrl, new HttpEntity<>(m, headers), Void.class);
+ }
+
+ /**
+ * 构建通知报文
+ * @param content
+ * @return
+ */
+ protected Map createMessage(String content) {
+ Map messageJson = new HashMap<>();
+ messageJson.put("msg_type", "text");
+
+ Map text = new HashMap<>();
+ text.put("text", content);
+ messageJson.put("content", text);
+ Long timestamp = System.currentTimeMillis() / 1000;
+ messageJson.put("timestamp", timestamp);
+
+ return messageJson;
+ }
+ /**
+ * 构建文本内容
+ * @param body
+ * @return
+ */
+ private String getText(String body) {
+ JSONObject json = JSON.parseObject(body);
+ Map root = new HashMap<>();
+ root.put("title", json.getString("title"));
+ root.put("description", json.getJSONObject("commonAnnotations").getString("description"));
+ JSONObject alertObject = json.getJSONArray("alerts").getJSONObject(0);
+ root.put("panelUrl", alertObject.getString("panelURL"));
+ root.put("startTime",DateUtil.now());
+ String senMsg = String.format(msg,
+ json.getString("title"),
+ json.getJSONObject("commonAnnotations").getString("description"),
+ alertObject.getString("panelURL"),
+ DateUtil.now()
+ );
+ return senMsg;
+ }
+}