提交 acdfd823 编写于 作者: wu-sheng's avatar wu-sheng

1.修复邮件底色的问题

2.改写新的邮件标题规则,使用户可以通过邮件判断邮件内容的告警类型,优先显示系统异常。
上级 9629db51
...@@ -36,164 +36,189 @@ import freemarker.template.TemplateException; ...@@ -36,164 +36,189 @@ import freemarker.template.TemplateException;
public class AlarmMessageProcessor { public class AlarmMessageProcessor {
private static Logger logger = LogManager .getLogger(AlarmMessageProcessor.class); private static Logger logger = LogManager
.getLogger(AlarmMessageProcessor.class);
static List<AlarmType> alarmTypeList;
static Template t; private static final String TYPE_OF_EXCEPTION_WARNING = "default";
static { static List<AlarmType> alarmTypeList;
alarmTypeList = AlarmTypeUtil.getAlarmTypeList(); static Template t;
}
static {
alarmTypeList = AlarmTypeUtil.getAlarmTypeList();
public void process(UserInfo userInfo, AlarmRule rule) throws TemplateException, IOException, SQLException { }
Map<String, List<AlarmMessage>> warningMap = new HashMap<String, List<AlarmMessage>>(); public void process(UserInfo userInfo, AlarmRule rule)
Set<String> warningMessageKeys = new HashSet<String>(); throws TemplateException, IOException, SQLException {
long currentFireMinuteTime = System.currentTimeMillis() / (1000 * 60);
long warningTimeWindowSize = currentFireMinuteTime Map<String, List<AlarmMessage>> warningMap = new HashMap<String, List<AlarmMessage>>();
- rule.getPreviousFireTimeM(); Set<String> warningMessageKeys = new HashSet<String>();
long currentFireMinuteTime = System.currentTimeMillis() / (1000 * 60);
// 获取待发送数据 long warningTimeWindowSize = currentFireMinuteTime
if (warningTimeWindowSize >= rule.getConfigArgsDescriber().getPeriod()) { - rule.getPreviousFireTimeM();
for(AlarmType alarmType : alarmTypeList) {
String type = alarmType.getType(); // 获取待发送数据
List<AlarmMessage> warningObjects = new ArrayList<AlarmMessage>(); if (warningTimeWindowSize >= rule.getConfigArgsDescriber().getPeriod()) {
for (ApplicationInfo applicationInfo : rule.getApplicationInfos()) { for (AlarmType alarmType : alarmTypeList) {
for (int period = 0; period < warningTimeWindowSize; period++) { String type = alarmType.getType();
Long currentMinuteTime = currentFireMinuteTime - period - 1; List<AlarmMessage> warningObjects = new ArrayList<AlarmMessage>();
String alarmKey = userInfo.getUserId() for (ApplicationInfo applicationInfo : rule
+ "-" .getApplicationInfos()) {
+ applicationInfo.getAppCode() for (int period = 0; period < warningTimeWindowSize; period++) {
+ "-" Long currentMinuteTime = currentFireMinuteTime - period
+ currentMinuteTime; - 1;
if(!type.equals("default")) { String alarmKey = userInfo.getUserId() + "-"
alarmKey += "-" + type; + applicationInfo.getAppCode() + "-"
} + currentMinuteTime;
warningMessageKeys.add(alarmKey); if (!TYPE_OF_EXCEPTION_WARNING.equals(type)) {
setAlarmMessages(alarmKey, warningObjects); alarmKey += "-" + type;
} }
} warningMessageKeys.add(alarmKey);
if(warningObjects.size() > 0) { setAlarmMessages(alarmKey, warningObjects);
warningMap.put(type, warningObjects); }
} }
} if (warningObjects.size() > 0) {
warningMap.put(type, warningObjects);
// 发送告警数据 }
int warningSize = this.getWarningSize(warningMap); }
if ( warningSize > 0) {
if ("0".equals(rule.getTodoType())) { // 发送告警数据
logger.info("A total of {} alarm information needs to be sent {}", warningSize, int warningSize = this.getWarningSize(warningMap);
rule.getConfigArgsDescriber().getMailInfo().getMailTo()); int exceptionAlarmSize = this.getExceptionSize(warningMap);
// 发送邮件 if (warningSize > 0) {
String subjects = generateSubject(userInfo.getUserName(), warningSize, if ("0".equals(rule.getTodoType())) {
rule.getPreviousFireTimeM(), currentFireMinuteTime); logger.info(
Map<String, Object> parameter = new HashMap<String, Object>(); "A total of {} alarm information needs to be sent {}",
warningSize, rule.getConfigArgsDescriber()
parameter.put("alarmTypeList", alarmTypeList); .getMailInfo().getMailTo());
parameter.put("warningMap", warningMap); // 发送邮件
parameter.put("name", userInfo.getUserName()); String subjects = generateSubject(userInfo.getUserName(),
parameter.put("startDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date( exceptionAlarmSize, warningSize,
rule.getPreviousFireTimeM() * 10000 * 6))); rule.getPreviousFireTimeM(), currentFireMinuteTime);
parameter.put("endDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date( Map<String, Object> parameter = new HashMap<String, Object>();
currentFireMinuteTime * 10000 * 6)));
parameter.put("alarmTypeList", alarmTypeList);
parameter.put("warningMap", warningMap);
String mailContext = generateContent(parameter); parameter.put("name", userInfo.getUserName());
if (mailContext.length() > 0) { parameter.put("startDate", new SimpleDateFormat(
MailInfo mailInfo = rule.getConfigArgsDescriber() "yyyy-MM-dd HH:mm:ss").format(new Date(rule
.getMailInfo(); .getPreviousFireTimeM() * 10000 * 6)));
MailUtil.sendMail(mailInfo.getMailTo(), parameter.put("endDate", new SimpleDateFormat(
mailInfo.getMailCc(), mailContext, subjects); "yyyy-MM-dd HH:mm:ss").format(new Date(
} currentFireMinuteTime * 10000 * 6)));
}
} String mailContext = generateContent(parameter);
if (mailContext.length() > 0) {
// 清理数据 MailInfo mailInfo = rule.getConfigArgsDescriber()
for (String toBeRemovedKey : warningMessageKeys) { .getMailInfo();
expiredAlarmMessage(toBeRemovedKey); MailUtil.sendMail(mailInfo.getMailTo(),
} mailInfo.getMailCc(), mailContext, subjects);
}
// 修改-保存上次处理时间 }
dealPreviousFireTime(userInfo, rule, currentFireMinuteTime); }
}
// 清理数据
} for (String toBeRemovedKey : warningMessageKeys) {
expiredAlarmMessage(toBeRemovedKey);
private void dealPreviousFireTime(UserInfo userInfo, AlarmRule rule, }
long currentFireMinuteTime) {
rule.setPreviousFireTimeM(currentFireMinuteTime); // 修改-保存上次处理时间
savePreviousFireTime(userInfo.getUserId(), rule.getRuleId(), dealPreviousFireTime(userInfo, rule, currentFireMinuteTime);
currentFireMinuteTime); }
}
}
private String generateSubject(String userName, int count, long startTime, long endTime) {
//TODO:邮件标题修改,添加了名称 private void dealPreviousFireTime(UserInfo userInfo, AlarmRule rule,
String title = "[Warning] Dear " long currentFireMinuteTime) {
+ userName rule.setPreviousFireTimeM(currentFireMinuteTime);
+ ", there were " savePreviousFireTime(userInfo.getUserId(), rule.getRuleId(),
+ count currentFireMinuteTime);
+ " alarm information between " }
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(
startTime * 10000 * 6)) private String generateSubject(String userName, int exceptionAlarmSize,
+ " to " int count, long startTime, long endTime) {
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date( String title = (exceptionAlarmSize > 0 ? "[Error]" : "[Warning]")
endTime * 10000 * 6)); + " Dear "
+ userName
return title; + ", SkyWalking collects "
} + (exceptionAlarmSize > 0 ? exceptionAlarmSize
+ " tid of system exceptions, " : "");
private void expiredAlarmMessage(final String key) { if (count > exceptionAlarmSize) {
RedisUtil.execute(new Executable<Long>() { title += (count - exceptionAlarmSize) + " tid of warings, ";
}
title += "between "
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(
startTime * 10000 * 6))
+ " to "
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(
endTime * 10000 * 6));
return title;
}
private void expiredAlarmMessage(final String key) {
RedisUtil.execute(new Executable<Long>() {
@Override @Override
public Long exe(Jedis client) { public Long exe(Jedis client) {
return client.expire(key, 0); return client.expire(key, 0);
} }
}); });
} }
private void savePreviousFireTime(final String userId, final String ruleId, private void savePreviousFireTime(final String userId, final String ruleId,
final long currentFireMinuteTime) { final long currentFireMinuteTime) {
RedisUtil.execute(new Executable<Long>() { RedisUtil.execute(new Executable<Long>() {
@Override @Override
public Long exe(Jedis client) { public Long exe(Jedis client) {
return client.hset(userId, ruleId, String.valueOf(currentFireMinuteTime)); return client.hset(userId, ruleId,
String.valueOf(currentFireMinuteTime));
} }
}); });
} }
private void setAlarmMessages(final String key, final Collection<AlarmMessage> warningTracingIds) { private void setAlarmMessages(final String key,
RedisUtil.execute(new Executable<Object>() { final Collection<AlarmMessage> warningTracingIds) {
RedisUtil.execute(new Executable<Object>() {
@Override @Override
public Collection<String> exe(Jedis client) { public Collection<String> exe(Jedis client) {
Map<String, String> result = client.hgetAll(key); Map<String, String> result = client.hgetAll(key);
if (result != null) { if (result != null) {
for(String traceid : result.keySet()){ for (String traceid : result.keySet()) {
warningTracingIds.add(new AlarmMessage(traceid, result.get(traceid))); warningTracingIds.add(new AlarmMessage(traceid, result
} .get(traceid)));
} }
return null; }
return null;
} }
}); });
} }
private String generateContent(Map parameter) throws IOException, TemplateException, SQLException { private String generateContent(Map parameter) throws IOException,
TemplateException, SQLException {
if(t == null) {
t = TemplateConfigurationUtil.getConfiguration().getTemplate("mail-template.ftl"); if (t == null) {
} t = TemplateConfigurationUtil.getConfiguration().getTemplate(
StringWriter out = new StringWriter(); "mail-template.ftl");
t.process(parameter, out); }
return out.getBuffer().toString(); StringWriter out = new StringWriter();
} t.process(parameter, out);
return out.getBuffer().toString();
private int getWarningSize(Map<String, List<AlarmMessage>> warningMap) { }
int result = 0;
for(Entry<String, List<AlarmMessage>> entry :warningMap.entrySet()) { private int getExceptionSize(Map<String, List<AlarmMessage>> warningMap) {
if(entry.getValue() != null) { if (warningMap.containsKey(TYPE_OF_EXCEPTION_WARNING)) {
result += entry.getValue().size(); return warningMap.get(TYPE_OF_EXCEPTION_WARNING).size();
} }
} return 0;
return result; }
}
private int getWarningSize(Map<String, List<AlarmMessage>> warningMap) {
int result = 0;
for (Entry<String, List<AlarmMessage>> entry : warningMap.entrySet()) {
if (entry.getValue() != null) {
result += entry.getValue().size();
}
}
return result;
}
} }
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
color: #c75f3e; color: #c75f3e;
} }
.greetings { .greetings {
background:#f9f9f9;
font-family:Microsoft YaHei,SimHei,Arial; font-family:Microsoft YaHei,SimHei,Arial;
} }
...@@ -33,7 +32,6 @@ ...@@ -33,7 +32,6 @@
padding:0; padding:0;
} }
#mainDiv { #mainDiv {
background:#f9f9f9;
padding:25px; padding:25px;
font-size:14px; font-size:14px;
line-height:25px; line-height:25px;
...@@ -92,7 +90,7 @@ ...@@ -92,7 +90,7 @@
<div id="mainDiv"> <div id="mainDiv">
<p class="greetings">Dear ${name!}:</p> <p class="greetings">Dear ${name!}:</p>
<p class="greetings">&nbsp&nbsp&nbsp&nbspOur platform received alarm infomation between <b>${startDate!} </b>to<b> ${endDate!}</b> as follows: <p class="greetings">&nbsp&nbsp&nbsp&nbspOur platform collects alarm infomation between <b>${startDate!} </b>to<b> ${endDate!}</b> as follows:
</p> </p>
<table id="dataTable" width="80%"> <table id="dataTable" width="80%">
<tr id="dataTableHead"> <tr id="dataTableHead">
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册