提交 cd6035f3 编写于 作者: L leon.li

add send weixins for ExceptionAlert

上级 d01caaf7
......@@ -177,6 +177,7 @@ public class ExceptionAlert implements Task, LogEnabled {
Project project = queryProjectByDomain(domain);
List<String> emails = m_alertConfig.buildMailReceivers(project);
List<String> phones = m_alertConfig.buildSMSReceivers(project);
String weixins = m_alertConfig.buildWeiXinReceivers(project);
String mailTitle = m_alertConfig.buildMailTitle(domain, null);
String mailContent = m_alertBuilder.buildMailContent(exceptions.toString(), domain);
......@@ -184,6 +185,11 @@ public class ExceptionAlert implements Task, LogEnabled {
m_logger.info(mailTitle + " " + mailContent + " " + emails);
Cat.logEvent("ExceptionAlert", domain, Event.SUCCESS, "[邮件告警] " + mailTitle + " " + mailContent);
m_mailSms.sendWeiXin(mailTitle, mailContent, domain, weixins);
m_logger.info(mailTitle + " " + mailContent + " " + domain + " " + weixins);
Cat.logEvent("ExceptionAlert", domain, Event.SUCCESS, "[微信告警] " + mailTitle + " " + mailContent + " " + domain
+ " " + weixins);
storeAlerts(domain, exceptions, mailTitle + "<br/>" + mailContent);
List<AlertException> errorExceptions = m_alertBuilder.buildErrorException(exceptions);
......
......@@ -25,6 +25,38 @@ public class ExceptionAlertConfig extends BaseAlertConfig {
}
}
public String buildWeiXinReceivers(Project project) {
StringBuilder builder = new StringBuilder();
Receiver receiver = m_manager.queryReceiverById(getId());
if (receiver != null && !receiver.isEnable()) {
return null;
} else {
builder.append(buildDefaultWeixinReceivers(receiver));
builder.append(project.getEmail());
String result = builder.toString();
if (result.endsWith(",")) {
return result.substring(0, result.length() - 1);
} else {
return result;
}
}
}
private String buildDefaultWeixinReceivers(Receiver receiver) {
StringBuilder builder = new StringBuilder();
if (receiver != null) {
for (String weixin : receiver.getWeixins()) {
builder.append(weixin + ",");
}
}
return builder.toString();
}
@Override
public String buildMailTitle(String domain, String configTitle) {
StringBuilder sb = new StringBuilder();
......
package com.dianping.cat.system.tool;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
......@@ -43,6 +47,10 @@ public class DefaultMailImpl implements MailSMS, Initializable, LogEnabled {
private Logger m_logger;
private static final String WEIXIN_URL = "http://dpoa-monitorapp-web01.beta/app/monitor/cat/push";
private static final String SUCCESS_TEXT = "{\"success\":\"1\"}";
private HtmlEmail createHtmlEmail() throws EmailException {
HtmlEmail email = new HtmlEmail();
......@@ -151,7 +159,7 @@ public class DefaultMailImpl implements MailSMS, Initializable, LogEnabled {
@Override
public boolean sendSms(String title, String content, List<String> phones) {
StringBuilder sb = new StringBuilder();
for (String phone : phones) {
InputStream in = null;
try {
......@@ -180,6 +188,63 @@ public class DefaultMailImpl implements MailSMS, Initializable, LogEnabled {
}
}
@Override
public boolean sendWeiXin(String title, String content, String domain, String weixins) {
String urlParameters = "domain=" + domain + "&email=" + weixins + "&title=" + title + "&content=" + content;
try {
HttpURLConnection connection = (HttpURLConnection) new URL(WEIXIN_URL).openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder builder = new StringBuilder();
while ((inputLine = reader.readLine()) != null) {
builder.append(inputLine);
}
reader.close();
String responseText = builder.toString();
if (responseText.equals(SUCCESS_TEXT)) {
Cat.logEvent("WeiXinSend", "send_success", Event.SUCCESS, "send success:" + domain + " " + title + " "
+ content + " " + weixins + " " + responseText);
return true;
} else {
Cat.logEvent("WeiXinSend", "send_fail", Event.SUCCESS, "send fail:" + domain + " " + title + " "
+ content + " " + weixins + " " + responseText);
return false;
}
} else {
Cat.logEvent("WeiXinSend", "network_fail", Event.SUCCESS, "network fail:" + domain + " " + title + " "
+ content + " " + weixins);
return false;
}
} catch (Exception ex) {
Cat.logEvent("WeiXinSend", "error", Event.SUCCESS, "error:" + domain + " " + title + " " + content + " "
+ weixins);
Cat.logError("send weixin error:" + domain + " " + title + " " + content + " " + weixins, ex);
return false;
}
}
public static void main(String[] args) {
boolean result = new DefaultMailImpl().sendWeiXin("test", "test", "test", "tianwen.zhou@dianping.com");
System.out.println(result);
}
public static class Item {
private String m_title;
......@@ -252,4 +317,5 @@ public class DefaultMailImpl implements MailSMS, Initializable, LogEnabled {
}
}
}
......@@ -9,5 +9,7 @@ public interface MailSMS {
public boolean sendEmailByGmail(String title,String content,List<String> emails);
public boolean sendSms(String title,String content,List<String> phones);
public boolean sendWeiXin(String title, String content, String domain, String emails);
}
......@@ -8,6 +8,7 @@
<attribute name="enable" value-type="boolean" />
<element name="email" value-type="String" type="list" names="emails" />
<element name="phone" value-type="String" type="list" names="phones" />
<element name="weixin" value-type="String" type="list" names="weixins" />
</entity>
</model>
......@@ -10,6 +10,7 @@
<attribute name="enable" value-type="boolean" />
<element name="email" value-type="String" type="list" names="emails" />
<element name="phone" value-type="String" type="list" names="phones" />
<element name="weixin" value-type="String" type="list" names="weixins" />
</entity>
</model>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册