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

add mail title when storing alert infos

上级 816b7ece
......@@ -20,6 +20,7 @@ import com.dianping.cat.consumer.top.TopAnalyzer;
import com.dianping.cat.core.config.ConfigDao;
import com.dianping.cat.core.dal.HostinfoDao;
import com.dianping.cat.core.dal.ProjectDao;
import com.dianping.cat.home.dal.report.AlertDao;
import com.dianping.cat.home.dal.report.EventDao;
import com.dianping.cat.home.dal.report.TopologyGraphDao;
import com.dianping.cat.report.baseline.BaselineService;
......@@ -184,15 +185,15 @@ public class ComponentsConfigurator extends AbstractResourceConfigurator {
all.add(C(DataChecker.class, DefaultDataChecker.class));
all.add(C(BusinessAlert.class).req(MetricConfigManager.class, ProductLineConfigManager.class,
BaselineService.class, MailSMS.class, BusinessAlertConfig.class, AlertInfo.class)//
BaselineService.class, MailSMS.class, BusinessAlertConfig.class, AlertInfo.class, AlertDao.class)//
.req(RemoteMetricReportService.class, BusinessRuleConfigManager.class, DataChecker.class));
all.add(C(NetworkAlert.class).req(MetricConfigManager.class, ProductLineConfigManager.class,
BaselineService.class, MailSMS.class, NetworkAlertConfig.class, AlertInfo.class)//
BaselineService.class, MailSMS.class, NetworkAlertConfig.class, AlertInfo.class, AlertDao.class)//
.req(RemoteMetricReportService.class, NetworkRuleConfigManager.class, DataChecker.class));
all.add(C(SystemAlert.class).req(MetricConfigManager.class, ProductLineConfigManager.class,
BaselineService.class, MailSMS.class, SystemAlertConfig.class, AlertInfo.class)//
BaselineService.class, MailSMS.class, SystemAlertConfig.class, AlertInfo.class, AlertDao.class)//
.req(RemoteMetricReportService.class, SystemRuleConfigManager.class, DataChecker.class));
all.add(C(AlertExceptionBuilder.class).req(ExceptionConfigManager.class));
......
package com.dianping.cat.report.task.alert;
import java.util.Date;
public class AlertResultEntity {
private boolean m_isTriggered;
......@@ -7,16 +9,24 @@ public class AlertResultEntity {
private String m_alertType;
private Date m_alertTime;
public AlertResultEntity(){
this.m_isTriggered = false;
this.m_content = "";
this.m_alertType = "";
this.m_alertTime = new Date();
}
public AlertResultEntity(boolean result, String content, String alertType){
this.m_isTriggered = result;
this.m_content = content;
this.m_alertType = alertType;
this.m_alertTime = new Date();
}
public Date getAlertTime() {
return m_alertTime;
}
public String getAlertType() {
......
......@@ -9,6 +9,7 @@ import java.util.Map;
import java.util.Map.Entry;
import org.codehaus.plexus.logging.Logger;
import org.unidal.dal.jdbc.DalException;
import org.unidal.lookup.annotation.Inject;
import org.unidal.tuple.Pair;
......@@ -22,6 +23,8 @@ import com.dianping.cat.consumer.metric.model.entity.MetricItem;
import com.dianping.cat.consumer.metric.model.entity.MetricReport;
import com.dianping.cat.consumer.metric.model.entity.Segment;
import com.dianping.cat.helper.TimeUtil;
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.report.baseline.BaselineService;
......@@ -38,6 +41,9 @@ public abstract class BaseAlert {
@Inject
protected MailSMS m_mailSms;
@Inject
protected AlertDao m_alertDao;
@Inject
protected AlertInfo m_alertInfo;
......@@ -68,6 +74,19 @@ public abstract class BaseAlert {
protected Map<String, MetricReport> m_lastReports = new HashMap<String, MetricReport>();
private Alert buildAlert(String domainName, String metricTitle, String mailTitle, AlertResultEntity alertResult) {
Alert alert = new Alert();
alert.setDomain(domainName);
alert.setAlertTime(alertResult.getAlertTime());
alert.setCategory(getName());
alert.setType(alertResult.getAlertType());
alert.setContent(mailTitle + "<br/>" + alertResult.getContent());
alert.setMetric(metricTitle);
return alert;
}
private String buildMetricTitle(String metricKey) {
try {
return metricKey.split(":")[2];
......@@ -86,8 +105,7 @@ public abstract class BaseAlert {
return result;
}
protected AlertResultEntity computeAlertInfo(int minute, String product, String metricKey,
MetricType type) {
protected AlertResultEntity computeAlertInfo(int minute, String product, String metricKey, MetricType type) {
double[] value = null;
double[] baseline = null;
List<Config> configs = m_ruleConfigManager.queryConfigs(metricKey, type);
......@@ -220,13 +238,16 @@ public abstract class BaseAlert {
private void processMetricItem(int minute, ProductLine productLine, String metricKey) {
for (MetricType type : MetricType.values()) {
AlertResultEntity alert = computeAlertInfo(minute, productLine.getId(), metricKey, type);
String productlineName = productLine.getId();
AlertResultEntity alertResult = computeAlertInfo(minute, productlineName, metricKey, type);
if (alert != null && alert.isTriggered()) {
if (alertResult != null && alertResult.isTriggered()) {
String metricTitle = buildMetricTitle(metricKey);
String mailTitle = getAlertConfig().buildMailTitle(productLine.getTitle(), metricTitle);
m_alertInfo.addAlertInfo(metricKey, new Date().getTime());
sendAlertInfo(productLine, metricTitle, alert.getContent(), alert.getAlertType());
storeAlert(productlineName, metricTitle, mailTitle, alertResult);
sendAlertInfo(productLine, mailTitle, alertResult.getContent(), alertResult.getAlertType());
}
}
}
......@@ -303,5 +324,23 @@ public abstract class BaseAlert {
return result;
}
protected abstract void sendAlertInfo(ProductLine productLine, String metricTitle, String content, String alertType);
protected void storeAlert(String domainName, String metricTitle, String mailTitle, AlertResultEntity alertResult) {
Alert alert = buildAlert(domainName, metricTitle, mailTitle, alertResult);
try {
int count = m_alertDao.insert(alert);
if (count != 1) {
Cat.logError("insert alert error: " + alert.toString(), new RuntimeException());
}
} catch (DalException e) {
Cat.logError(e);
}
}
protected abstract String getName();
protected abstract BaseAlertConfig getAlertConfig();
protected abstract void sendAlertInfo(ProductLine productLine, String mailTitle, String content, String alertType);
}
......@@ -68,6 +68,8 @@ public abstract class BaseAlertConfig {
return smsReceivers;
}
}
protected abstract String buildMailTitle(String artifactName, String configTitle);
public abstract String getId();
......
......@@ -17,12 +17,13 @@ import com.dianping.cat.message.Event;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.report.task.alert.AlertResultEntity;
import com.dianping.cat.report.task.alert.BaseAlert;
import com.dianping.cat.report.task.alert.BaseAlertConfig;
import com.dianping.cat.report.task.alert.MetricType;
public class BusinessAlert extends BaseAlert implements Task, LogEnabled {
@Inject
private BusinessAlertConfig m_alertConfig;
protected BusinessAlertConfig m_alertConfig;
private Logger m_logger;
......@@ -35,6 +36,11 @@ public class BusinessAlert extends BaseAlert implements Task, LogEnabled {
public String getName() {
return "metric-alert";
}
@Override
public BaseAlertConfig getAlertConfig() {
return m_alertConfig;
}
public boolean needAlert(MetricItemConfig config) {
if ((config.getAlarm() || config.isShowAvgDashboard() || config.isShowSumDashboard() || config
......@@ -48,24 +54,27 @@ public class BusinessAlert extends BaseAlert implements Task, LogEnabled {
private void processMetricItemConfig(MetricItemConfig config, int minute, ProductLine productLine) {
if (needAlert(config)) {
String product = productLine.getId();
String metricKey = m_metricConfigManager.buildMetricKey(config.getDomain(), config.getType(),
config.getMetricKey());
String domain = config.getDomain();
String metric = config.getMetricKey();
String metricKey = m_metricConfigManager.buildMetricKey(domain, config.getType(), metric);
AlertResultEntity alert = null;
AlertResultEntity alertResult = null;
if (config.isShowAvg()) {
alert = computeAlertInfo(minute, product, metricKey, MetricType.AVG);
alertResult = computeAlertInfo(minute, product, metricKey, MetricType.AVG);
}
if (config.isShowCount()) {
alert = computeAlertInfo(minute, product, metricKey, MetricType.COUNT);
alertResult = computeAlertInfo(minute, product, metricKey, MetricType.COUNT);
}
if (config.isShowSum()) {
alert = computeAlertInfo(minute, product, metricKey, MetricType.SUM);
alertResult = computeAlertInfo(minute, product, metricKey, MetricType.SUM);
}
if (alert != null && alert.isTriggered()) {
if (alertResult != null && alertResult.isTriggered()) {
String mailTitle = m_alertConfig.buildMailTitle(productLine.getTitle(), config.getTitle());
m_alertInfo.addAlertInfo(metricKey, new Date().getTime());
sendAlertInfo(productLine, config.getTitle(), alert.getContent(), alert.getAlertType());
storeAlert(domain, metric, mailTitle, alertResult);
sendAlertInfo(productLine, mailTitle, alertResult.getContent(), alertResult.getAlertType());
}
}
}
......@@ -138,9 +147,8 @@ public class BusinessAlert extends BaseAlert implements Task, LogEnabled {
}
@Override
public void sendAlertInfo(ProductLine productLine, String metricTitle, String content, String alertType) {
public void sendAlertInfo(ProductLine productLine, String title, String content, String alertType) {
List<String> emails = m_alertConfig.buildMailReceivers(productLine);
String title = m_alertConfig.buildMailTitle(productLine, metricTitle);
m_logger.info(title + " " + content + " " + emails);
m_mailSms.sendEmail(title, content, emails);
......
package com.dianping.cat.report.task.alert.business;
import com.dianping.cat.consumer.company.model.entity.ProductLine;
import com.dianping.cat.report.task.alert.BaseAlertConfig;
public class BusinessAlertConfig extends BaseAlertConfig {
private String m_id = "business";
public String buildMailTitle(ProductLine productLine, String configTitle) {
@Override
public String buildMailTitle(String productlineName, String configTitle) {
StringBuilder sb = new StringBuilder();
sb.append("[业务告警] [产品线 ").append(productLine.getTitle()).append("]");
sb.append("[业务告警] [产品线 ").append(productlineName).append("]");
sb.append("[业务指标 ").append(configTitle).append("]");
return sb.toString();
}
......
......@@ -125,13 +125,6 @@ public class AlertExceptionBuilder {
return limits;
}
public String buildMailTitle(String domain) {
StringBuilder sb = new StringBuilder();
sb.append("[CAT异常告警] [项目: ").append(domain).append("]");
return sb.toString();
}
public String buildMailContent(String exceptions, String domain) {
String content = buildContent(exceptions, domain);
String url = "http://cat.dianpingoa.com/cat/r/p?domain=" + domain;
......
......@@ -132,7 +132,7 @@ public class ExceptionAlert implements Task, LogEnabled {
for (Entry<String, List<AlertException>> entry : alertExceptions.entrySet()) {
try {
sendAlertForDomain(entry.getKey(), entry.getValue());
sendAndStoreAlert(entry.getKey(), entry.getValue());
} catch (Exception e) {
m_logger.error(e.getMessage());
}
......@@ -154,17 +154,19 @@ public class ExceptionAlert implements Task, LogEnabled {
}
}
}
private void sendAlertForDomain(String domain, List<AlertException> exceptions) {
private void sendAndStoreAlert(String domain, List<AlertException> exceptions) {
Project project = queryProjectByDomain(domain);
List<String> emails = m_alertConfig.buildMailReceivers(project);
List<String> phones = m_alertConfig.buildSMSReceivers(project);
String mailTitle = m_alertBuilder.buildMailTitle(domain);
String mailTitle = m_alertConfig.buildMailTitle(domain, null);
String mailContent = m_alertBuilder.buildMailContent(exceptions.toString(), domain);
m_mailSms.sendEmail(mailTitle, mailContent, emails);
m_logger.info(mailTitle + " " + mailContent + " " + emails);
Cat.logEvent("ExceptionAlert", domain, Event.SUCCESS, "[邮件告警] " + mailTitle + " " + mailContent);
storeAlert(domain, exceptions, mailTitle+"<br/>"+mailContent);
List<AlertException> errorExceptions = m_alertBuilder.buildErrorException(exceptions);
......@@ -176,6 +178,10 @@ public class ExceptionAlert implements Task, LogEnabled {
Cat.logEvent("ExceptionAlert", domain, Event.SUCCESS, "[短信告警] " + smsContent);
}
}
private void storeAlert(String domain, List<AlertException> exceptions, String mailContent) {
}
@Override
public void shutdown() {
......
......@@ -25,10 +25,18 @@ public class ExceptionAlertConfig extends BaseAlertConfig {
}
}
@Override
public String buildMailTitle(String domain, String configTitle) {
StringBuilder sb = new StringBuilder();
sb.append("[CAT异常告警] [项目: ").append(domain).append("]");
return sb.toString();
}
private List<String> buildProjectMailReceivers(Project project) {
return split(project.getEmail());
}
public List<String> buildSMSReceivers(Project project) {
List<String> smsReceivers = new ArrayList<String>();
Receiver receiver = m_manager.queryReceiverById(getId());
......@@ -42,7 +50,7 @@ public class ExceptionAlertConfig extends BaseAlertConfig {
return smsReceivers;
}
}
private List<String> buildProjectSMSReceivers(Project project) {
return split(project.getPhone());
}
......
......@@ -14,6 +14,7 @@ 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;
public class NetworkAlert extends BaseAlert implements Task, LogEnabled {
......@@ -29,6 +30,11 @@ public class NetworkAlert extends BaseAlert implements Task, LogEnabled {
public String getName() {
return "network-alert";
}
@Override
public BaseAlertConfig getAlertConfig() {
return m_alertConfig;
}
@Override
public void run() {
......@@ -82,9 +88,8 @@ public class NetworkAlert extends BaseAlert implements Task, LogEnabled {
}
@Override
protected void sendAlertInfo(ProductLine productLine, String metricTitle, String content, String alertType) {
protected void sendAlertInfo(ProductLine productLine, String title, String content, String alertType) {
List<String> emails = m_alertConfig.buildMailReceivers(productLine);
String title = m_alertConfig.buildMailTitle(productLine, metricTitle);
m_logger.info(title + " " + content + " " + emails);
m_mailSms.sendEmail(title, content, emails);
......
package com.dianping.cat.report.task.alert.network;
import com.dianping.cat.consumer.company.model.entity.ProductLine;
import com.dianping.cat.report.task.alert.BaseAlertConfig;
public class NetworkAlertConfig extends BaseAlertConfig {
private String m_id = "network";
public String buildMailTitle(ProductLine productLine, String configTitle) {
@Override
public String buildMailTitle(String productlineName, String configTitle) {
StringBuilder sb = new StringBuilder();
sb.append("[网络告警] [产品线 ").append(productLine.getTitle()).append("]");
sb.append("[网络告警] [产品线 ").append(productlineName).append("]");
sb.append("[网络指标 ").append(configTitle).append("]");
return sb.toString();
}
......
......@@ -14,6 +14,7 @@ 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;
public class SystemAlert extends BaseAlert implements Task, LogEnabled {
......@@ -29,6 +30,11 @@ public class SystemAlert extends BaseAlert implements Task, LogEnabled {
public String getName() {
return "system-alert";
}
@Override
public BaseAlertConfig getAlertConfig() {
return m_alertConfig;
}
@Override
public void run() {
......@@ -82,9 +88,8 @@ public class SystemAlert extends BaseAlert implements Task, LogEnabled {
}
@Override
protected void sendAlertInfo(ProductLine productLine, String metricTitle, String content, String alertType) {
protected void sendAlertInfo(ProductLine productLine, String title, String content, String alertType) {
List<String> emails = m_alertConfig.buildMailReceivers(productLine);
String title = m_alertConfig.buildMailTitle(productLine, metricTitle);
m_logger.info(title + " " + content + " " + emails);
m_mailSms.sendEmail(title, content, emails);
......
package com.dianping.cat.report.task.alert.system;
import com.dianping.cat.consumer.company.model.entity.ProductLine;
import com.dianping.cat.report.task.alert.BaseAlertConfig;
public class SystemAlertConfig extends BaseAlertConfig {
private String m_id = "system";
public String buildMailTitle(ProductLine productLine, String configTitle) {
@Override
public String buildMailTitle(String productlineName, String configTitle) {
StringBuilder sb = new StringBuilder();
sb.append("[系统告警] [产品线 ").append(productLine.getTitle()).append("]");
sb.append("[系统告警] [产品线 ").append(productlineName).append("]");
sb.append("[系统指标 ").append(configTitle).append("]");
return sb.toString();
}
......
<?xml version="1.0" encoding="UTF-8"?>
<entities do-package="com.dianping.cat.home.dal.report" gen="true">
<entity name="alert" table="alert" alias="al">
<member name="creation-date" insert-expr="NOW()" />
</entity>
<entity name="alteration" table="alteration" alias="a">
<member name="creation-date" insert-expr="NOW()" />
<var name="start-time" value-type="Date" />
......
......@@ -1814,6 +1814,9 @@
<requirement>
<role>com.dianping.cat.report.task.alert.AlertInfo</role>
</requirement>
<requirement>
<role>com.dianping.cat.home.dal.report.AlertDao</role>
</requirement>
<requirement>
<role>com.dianping.cat.report.task.alert.RemoteMetricReportService</role>
</requirement>
......@@ -1847,6 +1850,9 @@
<requirement>
<role>com.dianping.cat.report.task.alert.AlertInfo</role>
</requirement>
<requirement>
<role>com.dianping.cat.home.dal.report.AlertDao</role>
</requirement>
<requirement>
<role>com.dianping.cat.report.task.alert.RemoteMetricReportService</role>
</requirement>
......@@ -1880,6 +1886,9 @@
<requirement>
<role>com.dianping.cat.report.task.alert.AlertInfo</role>
</requirement>
<requirement>
<role>com.dianping.cat.home.dal.report.AlertDao</role>
</requirement>
<requirement>
<role>com.dianping.cat.report.task.alert.RemoteMetricReportService</role>
</requirement>
......@@ -1941,6 +1950,16 @@
<datasourceFile>/data/appdatas/cat/datasources.xml</datasourceFile>
</configuration>
</component>
<component>
<role>org.unidal.dal.jdbc.mapping.TableProvider</role>
<role-hint>alert</role-hint>
<implementation>org.unidal.dal.jdbc.mapping.SimpleTableProvider</implementation>
<configuration>
<logical-table-name>alert</logical-table-name>
<physical-table-name>alert</physical-table-name>
<data-source-name>cat</data-source-name>
</configuration>
</component>
<component>
<role>org.unidal.dal.jdbc.mapping.TableProvider</role>
<role-hint>alteration</role-hint>
......@@ -2021,6 +2040,15 @@
<data-source-name>cat</data-source-name>
</configuration>
</component>
<component>
<role>com.dianping.cat.home.dal.report.AlertDao</role>
<implementation>com.dianping.cat.home.dal.report.AlertDao</implementation>
<requirements>
<requirement>
<role>org.unidal.dal.jdbc.QueryEngine</role>
</requirement>
</requirements>
</component>
<component>
<role>com.dianping.cat.home.dal.report.AlterationDao</role>
<implementation>com.dianping.cat.home.dal.report.AlterationDao</implementation>
......
......@@ -311,3 +311,14 @@ CREATE TABLE `alteration` (
KEY `ind_date_domain_host` (`date`,`domain`,`hostname`)
) ENGINE=InnoDB AUTO_INCREMENT=1241 DEFAULT CHARSET=utf8 COMMENT='变更表';
CREATE TABLE `alert` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增长ID',
`domain` varchar(128) NOT NULL COMMENT '告警项目',
`alert_time` datetime NOT NULL COMMENT '告警时间',
`category` varchar(64) NOT NULL COMMENT '告警分类:network/business/system/exception -alert',
`type` varchar(64) NOT NULL COMMENT '告警类型:error/warning',
`content` text NOT NULL COMMENT '告警内容',
`metric` varchar(128) NOT NULL COMMENT '告警指标',
`creation_date` datetime NOT NULL COMMENT '数据插入时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='存储告警信息';
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册