怎么实现钉钉告警的功能?


                           怎么实现钉钉告警的功能?



一,【前言】


     作为架构组成员之一,为了更好的维护集团几十个系统,做了一个日志搜集告警系统---"统一告警平台"; 可以设置方式在规定时间错误次数达到多少就提醒相关的人.

       需求: 事项告警通知.  

          


二,【详情】


实现方式: 有短信, 钉钉,邮件等等.

再三思考: 短信是要收费的, 邮件不是很及时用的通信工具, 所以考虑先用钉钉进行告警通知.



先目睹为空一下,最终显示展示:

                            


实体类  AlarmMsgEntity---用于获取相关的参数:
 
 
package com.hwl.ulap.domain.model.alarm;

import com.hwl.ulap.domain.model.alarm.type.WarnType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;

import java.io.Serializable;

@Log4j2
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AlarmMsgEntity implements Serializable {
    //startTime
private Long id;
    private Integer sum;
    private String projectName;
    private Integer alarmType;
    private WarnType type;
    private Integer category;
    private Long alarmTime;
    private Integer status;
    private Integer retryTimes;
    private String receiveGroup;
    private String receiveGroupName;
    private String created;

}



具体的Service实现代码展示:


 
 
package com.hwl.ulap.domain.model.alarm;

import com.hwl.common.DateUtil;
import com.hwl.ulap.domain.model.alarm.type.AlarmLevelType;
import lombok.extern.log4j.Log4j2;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;

@Service
@Log4j2
@RestController
@RequestMapping("/alarm/service")
public class AlarmMsgService {
    //发送具体内容
    public static final String msg_template = "{ \"msgtype\": \"text\", \"text\": {\"content\": \"告警信息:%s;\n %s-系统项目;\n 错误数量:%s;\n 出错时间:%s,\n 请尽快排查处理!\" }}";

    private HttpClient httpclient = HttpClients.createDefault();
    //定义是否要发送开关
    private boolean openAlarm = true;

    //发送业务逻辑, 发送三次失败后重发 并记录日志
    public void sendSMS(AlarmMsgEntity entity) {
        if (openAlarm) {
            boolean isok = sendMsg(entity);
            if (!isok) {
                for (int i = entity.getRetryTimes(); i < 3; entity.setRetryTimes(entity.getRetryTimes() + 1)) {
                    if (sendMsg(entity)) {
                        break;
                    }
                }
            } else {
                log.info("<=sendSMS=>ok");
            }
        } else {
            log.info("<=sendSMS=>openAlarm is false.");
        }
    }
    //发送内容获取其中参数
    private String createMsgContext(AlarmMsgEntity alarmMsg) {
        return String.format(msg_template, AlarmLevelType.valueOf(alarmMsg.getAlarmType()).getName(), alarmMsg.getProjectName(), alarmMsg.getSum(), DateUtil.format(new Date(alarmMsg.getAlarmTime())));
    }
    //发送告警通知
    private boolean sendMsg(AlarmMsgEntity entity) {
        String msgValue = createMsgContext(entity);
        HttpPost httppost = new HttpPost(entity.getReceiveGroup());
        httppost.addHeader("Content-Type", "application/json; charset=utf-8");
        httppost.setEntity(new StringEntity(msgValue, "utf-8"));
        log.info("=sendSMS=>msg=" + msgValue);

        HttpResponse response = null;
        try {
            response = httpclient.execute(httppost);
            String result = EntityUtils.toString(response.getEntity(), "utf-8");
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                log.info("<=sendSMS=>send ok ,msg=" + result);
            } else {
                log.error("<=sendSMS=>send error ,status={} ,msg=" + result, response.getStatusLine().getStatusCode());
            }
        } catch (IOException e) {
            log.error("<=sendSMS=>error:", e);
            return false;
        }
        return true;
    }

    @RequestMapping(value = "/getOpenAlarm", method = {RequestMethod.GET, RequestMethod.POST})
    public String getOpenAlarm() {
        return "Current OpenAlarm is :"+openAlarm;
    }

    @RequestMapping(value = "/setOpenAlarm/{openAlarm}", method = {RequestMethod.GET, RequestMethod.POST})
    public String setOpenAlarm(@PathVariable boolean openAlarm) {
        this.openAlarm = openAlarm;
        return "Current OpenAlarm is :"+openAlarm;
    }
}




 

三,【小结】                                            

    博客是用文字来整理生命的工具!
 
    这个告警功能提示是最后的一个现实, 前面还要用SpringBoot框架, ES, flume和ZK进行后台系统日志的一个搜集工作, 最后将这个告警提示给相关的人和 交流群.
    这个通知群的话是通过钉钉机器人来实现的, 那么什么是钉钉机器人呢?请期待下一博文.

   下阶段进阶目标: 实现可以 @ 某人, 这样会更加的高效.


       

                         



评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值