Authored by qinchao

grafana提示告警发送到dingding

@@ -71,6 +71,13 @@ @@ -71,6 +71,13 @@
71 <artifactId>commons-lang</artifactId> 71 <artifactId>commons-lang</artifactId>
72 <version>2.6</version> 72 <version>2.6</version>
73 </dependency> 73 </dependency>
  74 +
  75 + <dependency>
  76 + <groupId>commons-io</groupId>
  77 + <artifactId>commons-io</artifactId>
  78 + <version>2.2</version>
  79 + </dependency>
  80 +
74 <dependency> 81 <dependency>
75 <groupId>org.projectlombok</groupId> 82 <groupId>org.projectlombok</groupId>
76 <artifactId>lombok</artifactId> 83 <artifactId>lombok</artifactId>
1 package com.ui.ctrl; 1 package com.ui.ctrl;
2 2
  3 +import com.alibaba.fastjson.JSON;
3 import com.alibaba.fastjson.JSONArray; 4 import com.alibaba.fastjson.JSONArray;
4 import com.alibaba.fastjson.JSONObject; 5 import com.alibaba.fastjson.JSONObject;
5 import com.google.common.collect.Sets; 6 import com.google.common.collect.Sets;
@@ -7,6 +8,7 @@ import com.ui.http.HttpRestClient; @@ -7,6 +8,7 @@ import com.ui.http.HttpRestClient;
7 import com.ui.model.BaseResponse; 8 import com.ui.model.BaseResponse;
8 import com.ui.model.MonitAlarmInfo; 9 import com.ui.model.MonitAlarmInfo;
9 import com.ui.model.WebHooksReleaseBO; 10 import com.ui.model.WebHooksReleaseBO;
  11 +import org.apache.commons.io.IOUtils;
10 import org.apache.commons.lang.StringUtils; 12 import org.apache.commons.lang.StringUtils;
11 import org.apache.commons.lang.math.NumberUtils; 13 import org.apache.commons.lang.math.NumberUtils;
12 import org.slf4j.Logger; 14 import org.slf4j.Logger;
@@ -17,6 +19,8 @@ import org.springframework.web.bind.annotation.RequestBody; @@ -17,6 +19,8 @@ import org.springframework.web.bind.annotation.RequestBody;
17 import org.springframework.web.bind.annotation.RequestMapping; 19 import org.springframework.web.bind.annotation.RequestMapping;
18 import org.springframework.web.bind.annotation.ResponseBody; 20 import org.springframework.web.bind.annotation.ResponseBody;
19 21
  22 +import javax.servlet.http.HttpServletRequest;
  23 +import java.io.IOException;
20 import java.util.*; 24 import java.util.*;
21 25
22 /** 26 /**
@@ -141,5 +145,35 @@ public class OuterIntfCtrl { @@ -141,5 +145,35 @@ public class OuterIntfCtrl {
141 } 145 }
142 146
143 147
  148 + /**
  149 + * 接口:grafana报警提示
  150 + * @param request
  151 + * @return
  152 + */
  153 + @RequestMapping(value = "/grafanaWebHook")
  154 + @ResponseBody
  155 + public BaseResponse grafanaWebHook(HttpServletRequest request) {
  156 + try {
  157 + String jsonBody = IOUtils.toString(request.getInputStream());
  158 + log.info("receive grafana webhook message:{}", jsonBody);
  159 + JSONObject alert = (JSONObject) JSON.parse(jsonBody);
  160 + String title = alert.getString("title");
  161 + String state = alert.getString("state");
  162 + String imgUrl = alert.getString("imageUrl");
  163 + String message = alert.getString("message");
  164 +
  165 + Map<String, String> map=new HashMap<>();
  166 + map.put("title",title);
  167 + map.put("state",state);
  168 + map.put("imgUrl",imgUrl);
  169 + map.put("message",message);
  170 + return httpClient.defaultGet("/grafanaAlert/sendDing",BaseResponse.class,map);
  171 + } catch (IOException e) {
  172 + e.printStackTrace();
  173 + }
  174 +
  175 + return new BaseResponse(201);
  176 + }
  177 +
144 178
145 } 179 }