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

refactor send alert module

上级 41921fd0
......@@ -30,7 +30,9 @@ import com.dianping.cat.home.dal.report.Alert;
import com.dianping.cat.home.dal.report.AlertDao;
import com.dianping.cat.home.rule.entity.Condition;
import com.dianping.cat.home.rule.entity.Config;
import com.dianping.cat.message.Event;
import com.dianping.cat.report.baseline.BaselineService;
import com.dianping.cat.report.task.alert.sender.BaseSender;
import com.dianping.cat.service.ModelPeriod;
import com.dianping.cat.service.ModelRequest;
import com.dianping.cat.system.config.BaseRuleConfigManager;
......@@ -69,6 +71,9 @@ public abstract class BaseAlert {
@Inject
private ProjectDao m_projectDao;
@Inject
protected BaseSender m_baseSender;
protected static final int DATA_AREADY_MINUTE = 1;
protected static final long DURATION = TimeUtil.ONE_MINUTE;
......@@ -282,12 +287,16 @@ public abstract class BaseAlert {
if (alertResult != null && alertResult.isTriggered()) {
String metricTitle = buildMetricTitle(metricKey);
String mailTitle = getAlertConfig().buildMailTitle(productLine.getTitle(), metricTitle);
String contactInfo = buildContactInfo(extractDomain(metricKey));
String domain = extractDomain(metricKey);
String contactInfo = buildContactInfo(domain);
alertResult.setContent(alertResult.getContent() + contactInfo);
String content = alertResult.getContent();
m_alertInfo.addAlertInfo(productlineName, metricKey, new Date().getTime());
storeAlert(productlineName, metricTitle, mailTitle, alertResult);
sendAlertInfo(productLine, mailTitle, alertResult.getContent(), alertResult.getAlertType());
String configId = getAlertConfig().getId();
m_baseSender.sendAllAlert(productLine, domain, mailTitle, content, alertResult.getAlertType(), configId);
Cat.logEvent(configId, productlineName, Event.SUCCESS, mailTitle + " " + content);
}
}
}
......@@ -381,6 +390,4 @@ public abstract class BaseAlert {
protected abstract String getName();
protected abstract BaseAlertConfig getAlertConfig();
protected abstract void sendAlertInfo(ProductLine productLine, String mailTitle, String content, String alertType);
}
......@@ -33,9 +33,9 @@ public abstract class BaseAlertConfig {
return smsReceivers;
}
public List<String> buildMailReceivers(ProductLine productLine) {
public List<String> buildMailReceivers(ProductLine productLine, String configId) {
List<String> mailReceivers = new ArrayList<String>();
Receiver receiver = m_manager.queryReceiverById(getId());
Receiver receiver = m_manager.queryReceiverById(configId);
if (receiver != null && !receiver.isEnable()) {
return mailReceivers;
......@@ -47,6 +47,10 @@ public abstract class BaseAlertConfig {
}
}
public List<String> buildMailReceivers(ProductLine productLine) {
return buildMailReceivers(productLine, getId());
}
private List<String> buildProductlineMailReceivers(ProductLine productLine) {
return split(productLine.getEmail());
}
......@@ -55,9 +59,9 @@ public abstract class BaseAlertConfig {
return split(productLine.getPhone());
}
public List<String> buildSMSReceivers(ProductLine productLine) {
public List<String> buildSMSReceivers(ProductLine productLine, String configId) {
List<String> smsReceivers = new ArrayList<String>();
Receiver receiver = m_manager.queryReceiverById(getId());
Receiver receiver = m_manager.queryReceiverById(configId);
if (receiver != null && !receiver.isEnable()) {
return smsReceivers;
......@@ -68,7 +72,11 @@ public abstract class BaseAlertConfig {
return smsReceivers;
}
}
public List<String> buildSMSReceivers(ProductLine productLine) {
return buildSMSReceivers(productLine, getId());
}
protected abstract String buildMailTitle(String artifactName, String configTitle);
public abstract String getId();
......
......@@ -25,8 +25,6 @@ public class BusinessAlert extends BaseAlert implements Task, LogEnabled {
@Inject
protected BusinessAlertConfig m_alertConfig;
private Logger m_logger;
@Override
public void enableLogging(Logger logger) {
m_logger = logger;
......@@ -73,10 +71,14 @@ public class BusinessAlert extends BaseAlert implements Task, LogEnabled {
String mailTitle = m_alertConfig.buildMailTitle(productLine.getTitle(), config.getTitle());
String contactInfo = buildContactInfo(domain);
alertResult.setContent(alertResult.getContent() + contactInfo);
m_alertInfo.addAlertInfo(productLine.getId(), metricKey, new Date().getTime());
String content = alertResult.getContent();
m_alertInfo.addAlertInfo(product, metricKey, new Date().getTime());
storeAlert(domain, metric, mailTitle, alertResult);
sendAlertInfo(productLine, mailTitle, alertResult.getContent(), alertResult.getAlertType());
String configId = getAlertConfig().getId();
m_baseSender.sendAllAlert(productLine, domain, mailTitle, content, alertResult.getAlertType(), configId);
Cat.logEvent(configId, product, Event.SUCCESS, mailTitle + " " + content);
}
}
}
......@@ -148,21 +150,6 @@ public class BusinessAlert extends BaseAlert implements Task, LogEnabled {
}
}
@Override
public void sendAlertInfo(ProductLine productLine, String title, String content, String alertType) {
List<String> emails = m_alertConfig.buildMailReceivers(productLine);
m_logger.info(title + " " + content + " " + emails);
m_mailSms.sendEmail(title, content, emails);
if (alertType != null && alertType.equals("error")) {
List<String> phones = m_alertConfig.buildSMSReceivers(productLine);
m_mailSms.sendSms(title + " " + content, content, phones);
}
Cat.logEvent("MetricAlert", productLine.getId(), Event.SUCCESS, title + " " + content);
}
@Override
public void shutdown() {
}
......
......@@ -205,6 +205,8 @@ public class ExceptionAlert implements Task, LogEnabled {
String contactInfo = buildContactInfo(domain);
String mailContent = m_alertBuilder.buildMailContent(exceptions.toString(), domain, contactInfo);
storeAlerts(domain, exceptions, mailTitle + "<br/>" + mailContent);
m_mailSms.sendEmail(mailTitle, mailContent, emails);
m_logger.info(mailTitle + " " + mailContent + " " + emails);
Cat.logEvent("ExceptionAlert", domain, Event.SUCCESS, "[邮件告警] " + mailTitle + " " + mailContent);
......@@ -214,8 +216,6 @@ public class ExceptionAlert implements Task, LogEnabled {
Cat.logEvent("ExceptionAlert", domain, Event.SUCCESS, "[微信告警] " + mailTitle + " " + mailContent + " " + domain
+ " " + weixins);
storeAlerts(domain, exceptions, mailTitle + "<br/>" + mailContent);
List<AlertException> errorExceptions = m_alertBuilder.buildErrorException(exceptions);
if (!errorExceptions.isEmpty()) {
......
package com.dianping.cat.report.task.alert.network;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import org.codehaus.plexus.logging.LogEnabled;
......@@ -11,7 +10,6 @@ import org.unidal.lookup.annotation.Inject;
import com.dianping.cat.Cat;
import com.dianping.cat.consumer.company.model.entity.ProductLine;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.report.task.alert.BaseAlert;
import com.dianping.cat.report.task.alert.BaseAlertConfig;
......@@ -30,7 +28,7 @@ public class NetworkAlert extends BaseAlert implements Task, LogEnabled {
public String getName() {
return "network-alert";
}
@Override
public BaseAlertConfig getAlertConfig() {
return m_alertConfig;
......@@ -87,21 +85,6 @@ public class NetworkAlert extends BaseAlert implements Task, LogEnabled {
}
}
@Override
protected void sendAlertInfo(ProductLine productLine, String title, String content, String alertType) {
List<String> emails = m_alertConfig.buildMailReceivers(productLine);
m_logger.info(title + " " + content + " " + emails);
m_mailSms.sendEmail(title, content, emails);
if (alertType != null && alertType.equals("error")) {
List<String> phones = m_alertConfig.buildSMSReceivers(productLine);
m_mailSms.sendSms(title + " " + content, content, phones);
}
Cat.logEvent("NetworkAlert", productLine.getId(), Event.SUCCESS, title + " " + content);
}
@Override
public void shutdown() {
}
......
package com.dianping.cat.report.task.alert.sender;
import java.util.List;
import org.unidal.lookup.annotation.Inject;
import com.dianping.cat.Cat;
import com.dianping.cat.consumer.company.model.entity.ProductLine;
import com.dianping.cat.report.task.alert.BaseAlertConfig;
import com.dianping.cat.system.tool.MailSMS;
public enum BaseSender {
MailSender {
@Override
protected void logSend(String title, String content, List<String> receivers) {
StringBuilder builder = new StringBuilder();
builder.append(title).append(",").append(content).append(",");
for (String receiver : receivers) {
builder.append(receiver).append(" ");
}
Cat.logEvent("SendMail", builder.toString());
}
@Override
protected List<String> queryReceivers(ProductLine productLine, String configId) {
return m_alertConfig.buildMailReceivers(productLine, configId);
}
@Override
protected boolean sendAlert(ProductLine productLine, String domain, String title, String content,
String alertType, String configId) {
try {
List<String> receivers = queryReceivers(productLine, configId);
m_mailSms.sendEmail(title, content, receivers);
logSend(title, content, receivers);
return true;
} catch (Exception ex) {
Cat.logError("send mail error" + productLine + " " + title + " " + content, ex);
return false;
}
}
},
WeixinSender {
@Override
protected void logSend(String title, String content, List<String> receivers) {
StringBuilder builder = new StringBuilder();
builder.append(title).append(" ").append(content).append(" ");
for (String receiver : receivers) {
builder.append(receiver).append(" ");
}
Cat.logEvent("SendWeixin", builder.toString());
}
@Override
protected List<String> queryReceivers(ProductLine productLine, String configId) {
return m_alertConfig.buildMailReceivers(productLine, configId);
}
@Override
protected boolean sendAlert(ProductLine productLine, String domain, String title, String content,
String alertType, String configId) {
if (alertType == null || !alertType.equals("error")) {
return true;
}
try {
List<String> receivers = queryReceivers(productLine, configId);
m_mailSms.sendWeiXin(title, content, domain, mergeList(receivers));
logSend(title, content, receivers);
return true;
} catch (Exception ex) {
Cat.logError("send weixin error" + productLine + " " + title + " " + content, ex);
return false;
}
}
private String mergeList(List<String> receivers) {
StringBuilder builder = new StringBuilder();
for (String receiver : receivers) {
builder.append(receiver).append(",");
}
String tmpResult = builder.toString();
if (tmpResult.endsWith(",")) {
return tmpResult.substring(0, tmpResult.length() - 1);
} else {
return tmpResult;
}
}
},
SmsSender {
@Override
protected void logSend(String title, String content, List<String> receivers) {
StringBuilder builder = new StringBuilder();
builder.append(title).append(" ").append(content).append(" ");
for (String receiver : receivers) {
builder.append(receiver).append(" ");
}
Cat.logEvent("SendSms", builder.toString());
}
@Override
protected List<String> queryReceivers(ProductLine productLine, String configId) {
return m_alertConfig.buildSMSReceivers(productLine, configId);
}
@Override
protected boolean sendAlert(ProductLine productLine, String domain, String title, String content,
String alertType, String configId) {
if (alertType == null || !alertType.equals("error")) {
return true;
}
try {
List<String> receivers = queryReceivers(productLine, configId);
m_mailSms.sendSms(title, content, receivers);
logSend(title, content, receivers);
return true;
} catch (Exception ex) {
Cat.logError("send sms error" + productLine + " " + title + " " + content, ex);
return false;
}
}
};
@Inject
protected BaseAlertConfig m_alertConfig;
@Inject
protected MailSMS m_mailSms;
public boolean sendAllAlert(ProductLine productLine, String domain, String title, String content, String alertType,
String configId) {
boolean sendResult = true;
for (BaseSender sender : BaseSender.values()) {
if (!sender.sendAlert(productLine, domain, title, content, alertType, configId)) {
sendResult = false;
}
}
return sendResult;
}
protected abstract List<String> queryReceivers(ProductLine productLine, String configId);
protected abstract void logSend(String title, String content, List<String> receivers);
protected abstract boolean sendAlert(ProductLine productLine, String domain, String title, String content,
String alertType, String configId);
}
package com.dianping.cat.report.task.alert.system;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import org.codehaus.plexus.logging.LogEnabled;
......@@ -11,7 +10,6 @@ import org.unidal.lookup.annotation.Inject;
import com.dianping.cat.Cat;
import com.dianping.cat.consumer.company.model.entity.ProductLine;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.report.task.alert.BaseAlert;
import com.dianping.cat.report.task.alert.BaseAlertConfig;
......@@ -30,7 +28,7 @@ public class SystemAlert extends BaseAlert implements Task, LogEnabled {
public String getName() {
return "system-alert";
}
@Override
public BaseAlertConfig getAlertConfig() {
return m_alertConfig;
......@@ -87,21 +85,6 @@ public class SystemAlert extends BaseAlert implements Task, LogEnabled {
}
}
@Override
protected void sendAlertInfo(ProductLine productLine, String title, String content, String alertType) {
List<String> emails = m_alertConfig.buildMailReceivers(productLine);
m_logger.info(title + " " + content + " " + emails);
m_mailSms.sendEmail(title, content, emails);
if (alertType != null && alertType.equals("error")) {
List<String> phones = m_alertConfig.buildSMSReceivers(productLine);
m_mailSms.sendSms(title + " " + content, content, phones);
}
Cat.logEvent("SystemAlert", productLine.getId(), Event.SUCCESS, title + " " + content);
}
@Override
public void shutdown() {
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册