pinpoint-docker開啟郵件報警和整合釘釘報警推送

蘇小林發表於2019-04-03

配置.env檔案

下載pinpoint-docker專案 github.com/naver/pinpo…

編輯.env檔案, 找到batch塊,開啟定時任務

pinpoint-docker開啟郵件報警和整合釘釘報警推送

找到mail塊,配置smtp郵件伺服器地址

pinpoint-docker開啟郵件報警和整合釘釘報警推送

其它配置項不用動

修改郵件推送檔案,支援同時推送釘釘

在釘釘群裡申請釘釘機器人,取得webhook url

pinpoint-docker開啟郵件報警和整合釘釘報警推送

解壓pinpoint-web/build/mail.zip檔案

編輯 com/navercorp/pinpoint/web/alarm/AlarmMessageSenderImple.java 檔案 檔案內容如下

package com.navercorp.pinpoint.web.alarm;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

import com.navercorp.pinpoint.web.alarm.checker.AlarmChecker;
import com.navercorp.pinpoint.web.service.UserGroupService;

import org.apache.hadoop.hbase.shaded.org.apache.http.HttpResponse;
import org.apache.hadoop.hbase.shaded.org.apache.http.HttpStatus;
import org.apache.hadoop.hbase.shaded.org.apache.http.client.HttpClient;
import org.apache.hadoop.hbase.shaded.org.apache.http.client.methods.HttpPost;
import org.apache.hadoop.hbase.shaded.org.apache.http.entity.StringEntity;
import org.apache.hadoop.hbase.shaded.org.apache.http.impl.client.DefaultHttpClient;
import org.apache.hadoop.hbase.shaded.org.apache.http.util.EntityUtils;

public class AlarmMessageSenderImple implements AlarmMessageSender {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private MailSender mailSender;

    public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}

	@Autowired
    private UserGroupService userGroupService;

    @Override
    public void sendSms(AlarmChecker checker, int sequenceCount) {
        List<String> receivers = userGroupService.selectPhoneNumberOfMember(checker.getuserGroupId());

        if (receivers.size() == 0) {
            return;
        }

        for (Object message : checker.getSmsMessage()) {
            logger.info("send SMS : {}", message);

            // TODO Implement logic for sending SMS
        }
    }

    @Override
    public void sendEmail(AlarmChecker checker, int sequenceCount) {
        List<String> receivers = userGroupService.selectEmailOfMember(checker.getuserGroupId());

        if (receivers.size() == 0) {
            return;
        }

        logger.info("send email : {}", checker.getEmailMessage());
        try{
            String content = String.format("%s\n\n%s", checker.getEmailMessage(), checker.getRule().getNotes());
	        SimpleMailMessage message = new SimpleMailMessage();
	        message.setTo(receivers.toArray(new String [receivers.size()]));
	        message.setSubject(String.format("[PINPOINT Alarm - %s]", checker.getRule().getApplicationId()));
	        message.setText(content);
	        mailSender.send(message);

            sendDingTalk(content);
        }catch(Exception e){
        	logger.error("send email error", e);
        }
    }

    private static String WEBHOOK_TOKEN = "https://oapi.dingtalk.com/robot/send?access_token=你的釘釘token";

    private static void sendDingTalk(String content) throws Exception{

        HttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost(WEBHOOK_TOKEN);
        httppost.addHeader("Content-Type", "application/json; charset=utf-8");

        String textMsg = "{ \"msgtype\": \"text\", \"text\": {\"content\": \"" + content + "\"}}";
        StringEntity se = new StringEntity(textMsg, "utf-8");
        httppost.setEntity(se);

        HttpResponse response = httpclient.execute(httppost);
        if (response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
            String result= EntityUtils.toString(response.getEntity(), "utf-8");
            System.out.println(result);
        }
    }

}
複製程式碼

注意將內容中的釘釘token替換成你自己的,然後編譯成class檔案

注意:

編譯必須在pinpoint專案內的web/src/main/java/com/navercorp/pinpoint/web/alarm內進行

重新打包mail.zip

重新構建pinpoint-web

docker build --no-cache -t pinpointdocker/pinpoint-web:1.8.2 .
複製程式碼

檢視效果

pinpoint-docker開啟郵件報警和整合釘釘報警推送

相關文章