Alertmanager报警使用webhook飞秋通知
本文介绍Alertmanager报警使用飞秋通知
1、Alertmanager换成webhook进行测试
global:
# 在没有报警的情况下声明为已解决的时间
resolve_timeout: 5m
templates:
# 告警模板文件
- "/etc/alertmanager/templates/wechat.tmpl"
route:
# 接收到告警后到自定义分组
group_by: ["alertname"]
# 分组创建后初始化等待时长
group_wait: 10s
# 告警信息发送之前的等待时长
group_interval: 30s
# 重复报警的间隔时长
repeat_interval: 5m
# 默认消息接收
receiver: "webhook"
receivers:
- name: 'webhook'
webhook_configs:
- url: 'http://192.168.0.14:9090/webhook'
2、测试通过代码发送消息到飞秋
飞秋下载
public static DatagramSocket datagramSocket = null;
public static boolean sendFeiqiu(String content) throws IOException {
if(datagramSocket == null){
datagramSocket = new DatagramSocket();
}
String data = getData(content);
DatagramPacket datagramPacket = new DatagramPacket(data.getBytes("gbk"), data.getBytes("gbk").length, InetAddress.getByName("192.168.0.14"), 2425);
datagramSocket.send(datagramPacket);
return true;
}
public static String getData(String data){
StringBuilder sb = new StringBuilder();
sb.append("1.0:");
sb.append(System.currentTimeMillis()+":");
sb.append("测试:");
sb.append("0.0.0.0:");
sb.append("32:");
sb.append(data);
return sb.toString();
}
@RequestMapping(value = "/feiqiu")
@ResponseBody
public String test() {
Map<String, Object> result = new HashMap<>();
try {
boolean success = sendFeiqiu("测试发送消息");
if (success) {
result.put("msg", "报警成功");
result.put("code", 1);
}
} catch (IOException e) {
e.printStackTrace();
}
return JSON.toJSONString(result);
}
3、将Alertmanager的消息发送给飞秋
内容需要自己组装一下
@RequestMapping(value = "/webhook", produces = "application/json;charset=UTF-8")
@ResponseBody
public String pstn(@RequestBody String json) {
log.info("alert notify params: {}", json);
Map<String, Object> result = new HashMap<>();
result.put("msg", "报警失败");
result.put("code", 0);
if(StringUtils.isBlank(json)){
return JSON.toJSONString(result);
}
JSONObject jo = JSON.parseObject(json);
JSONObject commonAnnotations = jo.getJSONObject("commonAnnotations");
String status = jo.getString("status");
if (commonAnnotations == null) {
return JSON.toJSONString(result);
}
String subject = commonAnnotations.getString("summary");
String content = commonAnnotations.getString("description");
try {
boolean success = sendFeiqiu(json);
if (success) {
result.put("msg", "报警成功");
result.put("code", 1);
}
} catch (Exception e) {
log.error("=alert email notify error. json={}", json, e);
}
return JSON.toJSONString(result);
}
代码下载
|