|
|
@@ -0,0 +1,101 @@
|
|
|
+package cn.reghao.devops.mgr.mgr.thirdparty.graylog;
|
|
|
+
|
|
|
+import cn.reghao.devops.mgr.admin.sys.service.notifier.ding.DingMsg;
|
|
|
+import cn.reghao.devops.mgr.mgr.thirdparty.graylog.pc.MessageProducer;
|
|
|
+import cn.reghao.jutil.jdk.converter.DateTimeConverter;
|
|
|
+import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
+import com.google.gson.JsonArray;
|
|
|
+import com.google.gson.JsonElement;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.text.ParseException;
|
|
|
+import java.time.Duration;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author reghao
|
|
|
+ * @date 2025-01-20 11:46:16
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class GraylogService {
|
|
|
+ private Map<String, ExceptionMessage> map = new HashMap<>();
|
|
|
+ private final MessageProducer messageProducer;
|
|
|
+
|
|
|
+ public GraylogService(MessageProducer messageProducer) {
|
|
|
+ this.messageProducer = messageProducer;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void processLog(String json) {
|
|
|
+ JsonObject jsonObject = JsonConverter.jsonToJsonElement(json).getAsJsonObject();
|
|
|
+ JsonArray jsonArray = jsonObject.get("backlog").getAsJsonArray();
|
|
|
+ for (JsonElement jsonElement : jsonArray) {
|
|
|
+ JsonObject jsonObject1 = jsonElement.getAsJsonObject();
|
|
|
+ JsonObject fieldObject = jsonObject1.get("fields").getAsJsonObject();
|
|
|
+
|
|
|
+ try {
|
|
|
+ String title = "运行告警通知";
|
|
|
+ DingMsg dingMsg = processDingMsg(title, fieldObject);
|
|
|
+ if (dingMsg != null) {
|
|
|
+ messageProducer.put(dingMsg);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private DingMsg processDingMsg(String title, JsonObject jsonObject) {
|
|
|
+ try {
|
|
|
+ String date = jsonObject.get("Date").getAsString();
|
|
|
+ LocalDateTime localDateTime = DateTimeConverter.localDateTime2(date);
|
|
|
+
|
|
|
+ String appName = jsonObject.get("ApplicationName").getAsString();
|
|
|
+ String localIp = jsonObject.get("LocalIp").getAsString();
|
|
|
+
|
|
|
+ String url = "";
|
|
|
+ JsonElement jsonElement = jsonObject.get("Url");
|
|
|
+ if (jsonElement != null) {
|
|
|
+ url = jsonElement.getAsString();
|
|
|
+ }
|
|
|
+
|
|
|
+ String exceptionType = jsonObject.get("ExceptionType").getAsString();
|
|
|
+ String exceptionMessage = jsonObject.get("ExceptionMessage").getAsString();
|
|
|
+ ExceptionMessage currentMessage = new ExceptionMessage(localDateTime, appName, localIp, url, exceptionType, exceptionMessage);
|
|
|
+
|
|
|
+ String mapKey = String.format("%s-%s-%s", appName, localIp, url);
|
|
|
+ ExceptionMessage lastMessage = map.get(mapKey);
|
|
|
+ if (lastMessage != null) {
|
|
|
+ LocalDateTime localDateTime1 = lastMessage.getDate();
|
|
|
+ long millis = Duration.between(localDateTime, localDateTime1).toMillis();
|
|
|
+ if (millis > 600_000) {
|
|
|
+ map.put(mapKey, currentMessage);
|
|
|
+ return getDingMsg(title, currentMessage);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ map.put(mapKey, currentMessage);
|
|
|
+ return getDingMsg(title, currentMessage);
|
|
|
+ }
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private DingMsg getDingMsg(String title, ExceptionMessage message) {
|
|
|
+ String appName = message.getAppName();
|
|
|
+ String localIp = message.getLocalIp();
|
|
|
+ String head = String.format("# %s(%s) 运行时异常", appName, localIp);
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append(head).append(System.lineSeparator());
|
|
|
+ sb.append("### 时间: ").append(DateTimeConverter.format(message.getDate())).append(System.lineSeparator());
|
|
|
+ sb.append("### 请求 URL: ").append(message.getUrl()).append(System.lineSeparator());
|
|
|
+ sb.append("### 异常类型: ").append(message.getExceptionType()).append(System.lineSeparator());
|
|
|
+ sb.append("### 异常消息: ").append(message.getExceptionMessage()).append(System.lineSeparator());
|
|
|
+ return new DingMsg(title, sb.toString());
|
|
|
+ }
|
|
|
+}
|