|
|
@@ -0,0 +1,59 @@
|
|
|
+package com.zd.common.core.alert;
|
|
|
+
|
|
|
+import com.zd.common.core.utils.HttpUtils;
|
|
|
+import com.zd.common.core.utils.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>钉钉告警</p>
|
|
|
+ *
|
|
|
+ * @author linft
|
|
|
+ * @version 1.0
|
|
|
+ * @date 3/2/2023
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class DingTalkAlert {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 告警类应用
|
|
|
+ */
|
|
|
+ private static String SUBJECT = "设备监控";
|
|
|
+
|
|
|
+ private static Boolean dingTalkNotice;
|
|
|
+ private static String dingTalkSubject;
|
|
|
+ private static String dingTalkUrl;
|
|
|
+
|
|
|
+ @Value("${monitor.alert.dingTalkNotice}")
|
|
|
+ public void setDingTalkNotice(Boolean dingTalkNotice) {
|
|
|
+ this.dingTalkNotice = dingTalkNotice;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Value("${monitor.alert.dingTalkSubject}")
|
|
|
+ public void setDingTalkSubject(String dingTalkSubject) {
|
|
|
+ this.dingTalkSubject = dingTalkSubject;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Value("${monitor.alert.dingTalkUrl}")
|
|
|
+ public void setDingTalkUrl(String dingTalkUrl) {
|
|
|
+ this.dingTalkUrl = dingTalkUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送告警
|
|
|
+ * @param content 告警内容描述
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String sendAlert(String content) {
|
|
|
+ if (dingTalkNotice != null && dingTalkNotice && StringUtils.isNotEmpty(dingTalkUrl)) {
|
|
|
+ StringBuffer buffer = new StringBuffer();
|
|
|
+ buffer.append("【" + dingTalkSubject +"--"+ SUBJECT + "】\n");
|
|
|
+ buffer.append(content);
|
|
|
+ //封装请求内容
|
|
|
+ String msg = "{\"msgtype\": \"text\",\"text\": {\"content\":\"" + buffer.toString() + "\"}}";
|
|
|
+ return HttpUtils.sendPost(dingTalkUrl, msg, "application/json", null);
|
|
|
+ }
|
|
|
+ return "告警未启动或参数未配置!";
|
|
|
+ }
|
|
|
+
|
|
|
+}
|