Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwujing committed Sep 11, 2023
1 parent c2ee147 commit 4d7694e
Showing 1 changed file with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

/**
* @Author pancm
* @Description Grafana和飞书webhook中转服务的代码
* @Date 2023/9/8
* @Description 飞书告警中转服务
* https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot#756b882f
* https://cloud.tencent.com/developer/article/2209466
* @Date 2023/9/11
* @Param
* @return
**/
Expand All @@ -30,27 +32,30 @@
@Slf4j
public class AlertController {

// @Value("${feishu.webhook-url:https://open.feishu.cn/open-apis/bot/v2/hook/d4329724-9e06-453c-9078-a2d52cac7585}")
@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 RestTemplate restTemplate;


private String msg = "告警标题:%s\n告警描述:%s\n面板URL:%s\n发生时间:%s";
private String msg = "告警状态: %s\n告警标题:%s\n告警描述:%s\n面板URL:%s\n发生时间:%s\n负责人: %s";


@RequestMapping("/webhook/receive")
public void receive(@RequestBody String message) {
log.info("获取的Grafana的告警数据:{}",message);
log.info("AlertController.receive 获取的Grafana的告警数据:{}", message);
Map<String, Object> m = createMessage(getText(message));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
log.info("AlertController.receive 发送飞书的告警内容:{}", m);
restTemplate.postForEntity(webhookUrl, new HttpEntity<>(m, headers), Void.class);
}

/**
* 构建通知报文
*
* @param content
* @return
*/
Expand All @@ -63,11 +68,12 @@ protected Map<String, Object> createMessage(String content) {
messageJson.put("content", text);
Long timestamp = System.currentTimeMillis() / 1000;
messageJson.put("timestamp", timestamp);

return messageJson;
}

/**
* 构建文本内容
*
* @param body
* @return
*/
Expand All @@ -78,13 +84,22 @@ private String getText(String body) {
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());
root.put("startTime", DateUtil.now());
String senMsg = String.format(msg,
getStatusMsg(json.getString("status")),
json.getString("title"),
json.getJSONObject("commonAnnotations").getString("description"),
alertObject.getString("panelURL"),
DateUtil.now()
);
DateUtil.now(),
json.getJSONObject("commonAnnotations").getString("handler")
);
return senMsg;
}

private String getStatusMsg(String msg) {
if ("firing".equals(msg)) {
return "告警产生";
}
return "告警恢复";
}
}

0 comments on commit 4d7694e

Please sign in to comment.