Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
SkyWalking
提交
0496938f
S
SkyWalking
项目概览
apache
/
SkyWalking
上一次同步 1 年多
通知
302
Star
21345
Fork
6091
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SkyWalking
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0496938f
编写于
12月 10, 2015
作者:
wu-sheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1.规范化部分告警识别和发送逻辑。使代码更清晰,逻辑更简单。
上级
436eacf9
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
145 addition
and
140 deletion
+145
-140
skywalking-alarm/src/main/java/com/ai/cloud/skywalking/alarm/procesor/AlarmMessageProcessor.java
...loud/skywalking/alarm/procesor/AlarmMessageProcessor.java
+143
-138
skywalking-alarm/src/main/java/com/ai/cloud/skywalking/alarm/util/MailUtil.java
...ain/java/com/ai/cloud/skywalking/alarm/util/MailUtil.java
+2
-2
未找到文件。
skywalking-alarm/src/main/java/com/ai/cloud/skywalking/alarm/procesor/AlarmMessageProcessor.java
浏览文件 @
0496938f
package
com.ai.cloud.skywalking.alarm.procesor
;
package
com.ai.cloud.skywalking.alarm.procesor
;
import
java.io.IOException
;
import
java.io.StringReader
;
import
java.io.StringWriter
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.Map
;
import
java.util.Set
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
import
redis.clients.jedis.Jedis
;
import
com.ai.cloud.skywalking.alarm.model.AlarmRule
;
import
com.ai.cloud.skywalking.alarm.model.AlarmRule
;
import
com.ai.cloud.skywalking.alarm.model.ApplicationInfo
;
import
com.ai.cloud.skywalking.alarm.model.ApplicationInfo
;
import
com.ai.cloud.skywalking.alarm.model.MailInfo
;
import
com.ai.cloud.skywalking.alarm.model.UserInfo
;
import
com.ai.cloud.skywalking.alarm.model.UserInfo
;
import
com.ai.cloud.skywalking.alarm.model.parameter.Application
;
import
com.ai.cloud.skywalking.alarm.util.MailUtil
;
import
com.ai.cloud.skywalking.alarm.util.MailUtil
;
import
com.ai.cloud.skywalking.alarm.util.RedisUtil
;
import
com.ai.cloud.skywalking.alarm.util.RedisUtil
;
import
freemarker.template.Configuration
;
import
freemarker.template.Configuration
;
import
freemarker.template.Template
;
import
freemarker.template.Template
;
import
freemarker.template.TemplateException
;
import
freemarker.template.TemplateException
;
import
freemarker.template.Version
;
import
freemarker.template.Version
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
import
redis.clients.jedis.Jedis
;
import
java.io.IOException
;
import
java.io.StringReader
;
import
java.io.StringWriter
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
public
class
AlarmMessageProcessor
{
public
class
AlarmMessageProcessor
{
private
static
Logger
logger
=
LogManager
.
getLogger
(
AlarmMessageProcessor
.
class
);
private
static
Logger
logger
=
LogManager
.
getLogger
(
AlarmMessageProcessor
.
class
);
public
static
void
process
(
UserInfo
userInfo
,
AlarmRule
rule
)
{
Set
<
String
>
sentData
=
new
HashSet
<
String
>();
public
void
process
(
UserInfo
userInfo
,
AlarmRule
rule
)
{
List
<
Application
>
applications
=
new
ArrayList
<
Application
>();
Set
<
String
>
warningTracingIds
=
new
HashSet
<
String
>();
Set
<
String
>
toBeSendData
;
Set
<
String
>
warningMessageKeys
=
new
HashSet
<
String
>();
Application
temApplication
;
long
currentFireMinuteTime
=
System
.
currentTimeMillis
()
/
(
10000
*
6
);
long
currentFireTimeM
=
System
.
currentTimeMillis
()
/
(
10000
*
6
);
long
warningTimeWindowSize
=
currentFireMinuteTime
// 获取待发送数据
-
rule
.
getPreviousFireTimeM
();
if
(
checkerProcessInterval
(
rule
,
currentFireTimeM
))
{
// 获取待发送数据
for
(
ApplicationInfo
applicationInfo
:
rule
.
getApplicationInfos
())
{
if
(
warningTimeWindowSize
>=
rule
.
getConfigArgsDescriber
().
getPeriod
())
{
toBeSendData
=
new
HashSet
<
String
>();
for
(
ApplicationInfo
applicationInfo
:
rule
.
getApplicationInfos
())
{
for
(
int
period
=
0
;
period
<
warningTimeWindowSize
;
period
++)
{
for
(
int
i
=
0
;
i
<
currentFireTimeM
-
rule
.
getPreviousFireTimeM
();
i
++)
{
String
alarmKey
=
userInfo
.
getUserId
()
toBeSendData
.
addAll
(
getAlarmMessage
(
generateAlarmKey
(
userInfo
.
getUserId
(),
+
"-"
applicationInfo
.
getAppCode
(),
i
)));
+
applicationInfo
.
getAppCode
()
+
"-"
toBeSendData
.
removeAll
(
sentData
);
+
((
System
.
currentTimeMillis
()
/
(
10000
*
6
))
-
period
);
sentData
.
addAll
(
toBeSendData
);
}
warningMessageKeys
.
add
(
alarmKey
);
warningTracingIds
.
addAll
(
getAlarmMessages
(
alarmKey
));
temApplication
=
new
Application
(
applicationInfo
.
getAppId
());
}
temApplication
.
setTraceIds
(
toBeSendData
);
}
applications
.
add
(
temApplication
);
}
// 发送告警数据
if
(
warningTracingIds
.
size
()
>
0
)
{
// 没有数据需要发送
if
(
"0"
.
equals
(
rule
.
getTodoType
()))
{
if
(
sentData
.
size
()
<=
0
)
{
// 发送邮件
// 修改-保存上次处理时间
String
subjects
=
generateSubject
(
warningTracingIds
.
size
(),
dealPreviousFireTime
(
userInfo
,
rule
,
currentFireTimeM
);
rule
.
getPreviousFireTimeM
(),
currentFireMinuteTime
);
}
else
{
Map
parameter
=
new
HashMap
();
if
(
"0"
.
equals
(
rule
.
getTodoType
()))
{
// TODO:已使用新的参数,warningTracingIds包含所有的告警tracingId,需要在模板中生成链接
// 发送邮件
parameter
.
put
(
"warningTracingIds"
,
warningTracingIds
);
String
subjects
=
generateSubjects
(
sentData
.
size
(),
// TODO:请转换为USERNAME,ID无法识别
rule
.
getPreviousFireTimeM
(),
currentFireTimeM
);
parameter
.
put
(
"name"
,
userInfo
.
getUserId
());
Map
parameter
=
new
HashMap
();
String
mailContext
=
generateContent
(
rule
parameter
.
put
(
"applications"
,
applications
);
.
getConfigArgsDescriber
().
getMailInfo
()
parameter
.
put
(
"name"
,
userInfo
.
getUserId
());
.
getMailTemp
(),
parameter
);
MailUtil
.
sendMail
(
rule
.
getConfigArgsDescriber
().
getMailInfo
().
getMailTo
(),
if
(
mailContext
.
length
()
>
0
)
{
rule
.
getConfigArgsDescriber
().
getMailInfo
().
getMailCc
(),
MailInfo
mailInfo
=
rule
.
getConfigArgsDescriber
()
generateContent
(
rule
.
getConfigArgsDescriber
().
getMailInfo
()
.
getMailInfo
();
.
getMailTemp
(),
parameter
),
MailUtil
.
sendMail
(
mailInfo
.
getMailTo
(),
subjects
);
mailInfo
.
getMailCc
(),
mailContext
,
subjects
);
}
}
// 清理数据
}
for
(
ApplicationInfo
applicationInfo
:
rule
.
getApplicationInfos
())
{
}
for
(
int
i
=
0
;
i
<
currentFireTimeM
-
rule
.
getPreviousFireTimeM
();
i
++)
{
expiredAlarmMessage
(
generateAlarmKey
(
userInfo
.
getUserId
(),
// 清理数据
applicationInfo
.
getAppCode
(),
i
));
for
(
String
toBeRemovedKey
:
warningMessageKeys
)
{
}
expiredAlarmMessage
(
toBeRemovedKey
);
}
}
// 修改-保存上次处理时间
dealPreviousFireTime
(
userInfo
,
rule
,
currentFireTimeM
);
// 修改-保存上次处理时间
}
dealPreviousFireTime
(
userInfo
,
rule
,
currentFireMinuteTime
);
}
}
}
}
private
void
dealPreviousFireTime
(
UserInfo
userInfo
,
AlarmRule
rule
,
private
static
void
dealPreviousFireTime
(
UserInfo
userInfo
,
AlarmRule
rule
,
long
currentFireTimeM
)
{
long
currentFireMinuteTime
)
{
rule
.
setPreviousFireTimeM
(
currentFireTimeM
);
rule
.
setPreviousFireTimeM
(
currentFireMinuteTime
);
savePreviousFireTime
(
userInfo
.
getUserId
(),
rule
.
getRuleId
(),
currentFireTimeM
);
savePreviousFireTime
(
userInfo
.
getUserId
(),
rule
.
getRuleId
(),
}
currentFireMinuteTime
);
}
private
static
String
generateSubjects
(
int
count
,
long
startTime
,
long
endTime
)
{
String
title
=
"[Warning] There were "
+
count
+
" alarm information between "
+
private
String
generateSubject
(
int
count
,
long
startTime
,
long
endTime
)
{
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
).
format
(
new
Date
(
startTime
))
+
String
title
=
"[Warning] There were "
" to "
+
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
).
format
(
new
Date
(
endTime
));
+
count
+
" alarm information between "
return
title
;
+
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
).
format
(
new
Date
(
}
startTime
))
+
" to "
private
static
boolean
checkerProcessInterval
(
AlarmRule
rule
,
long
currentFireTimeM
)
{
+
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
).
format
(
new
Date
(
return
currentFireTimeM
-
rule
.
getPreviousFireTimeM
()
>=
rule
.
getConfigArgsDescriber
().
getPeriod
();
endTime
));
}
return
title
;
private
static
void
expiredAlarmMessage
(
String
key
)
{
}
Jedis
client
=
RedisUtil
.
getRedisClient
();
client
.
expire
(
key
,
0
);
private
void
expiredAlarmMessage
(
String
key
)
{
if
(
client
!=
null
)
{
Jedis
client
=
RedisUtil
.
getRedisClient
();
client
.
close
();
client
.
expire
(
key
,
0
);
}
if
(
client
!=
null
)
{
}
client
.
close
();
}
}
private
static
void
savePreviousFireTime
(
String
userId
,
String
ruleId
,
long
currentFireTimeM
)
{
Jedis
client
=
RedisUtil
.
getRedisClient
();
private
void
savePreviousFireTime
(
String
userId
,
String
ruleId
,
client
.
hset
(
userId
,
ruleId
,
String
.
valueOf
(
currentFireTimeM
));
long
currentFireMinuteTime
)
{
if
(
client
!=
null
)
{
Jedis
client
=
RedisUtil
.
getRedisClient
();
client
.
close
();
client
.
hset
(
userId
,
ruleId
,
String
.
valueOf
(
currentFireMinuteTime
));
}
if
(
client
!=
null
)
{
}
client
.
close
();
}
}
private
static
String
generateAlarmKey
(
String
userId
,
String
appCode
,
int
period
)
{
return
userId
+
"-"
+
appCode
+
"-"
+
((
System
.
currentTimeMillis
()
/
(
10000
*
6
))
private
Collection
<
String
>
getAlarmMessages
(
String
key
)
{
-
period
);
Jedis
client
=
RedisUtil
.
getRedisClient
();
}
Map
<
String
,
String
>
result
=
client
.
hgetAll
(
key
);
if
(
result
==
null
)
{
private
static
Collection
<
String
>
getAlarmMessage
(
String
key
)
{
return
new
ArrayList
<
String
>();
Jedis
client
=
RedisUtil
.
getRedisClient
();
}
Map
<
String
,
String
>
result
=
client
.
hgetAll
(
key
);
if
(
result
==
null
)
{
client
.
close
();
return
new
ArrayList
<
String
>();
}
return
result
.
values
();
}
client
.
close
();
private
String
generateContent
(
String
templateStr
,
Map
parameter
)
{
return
result
.
values
();
Configuration
cfg
=
new
Configuration
(
new
Version
(
"2.3.23"
));
}
cfg
.
setDefaultEncoding
(
"UTF-8"
);
Template
t
=
null
;
private
static
String
generateContent
(
String
templateStr
,
Map
parameter
)
{
try
{
Configuration
cfg
=
new
Configuration
(
new
Version
(
"2.3.23"
));
t
=
new
Template
(
null
,
new
StringReader
(
templateStr
),
cfg
);
cfg
.
setDefaultEncoding
(
"UTF-8"
);
StringWriter
out
=
new
StringWriter
();
Template
t
=
null
;
t
.
process
(
parameter
,
out
);
try
{
return
out
.
getBuffer
().
toString
();
t
=
new
Template
(
null
,
new
StringReader
(
templateStr
),
cfg
);
}
catch
(
IOException
e
)
{
StringWriter
out
=
new
StringWriter
();
logger
.
error
(
"Template illegal."
,
e
);
t
.
process
(
parameter
,
out
);
}
catch
(
TemplateException
e
)
{
return
out
.
getBuffer
().
toString
();
logger
.
error
(
"Failed to generate content."
,
e
);
}
catch
(
IOException
e
)
{
}
logger
.
error
(
"Template illegal."
,
e
);
}
catch
(
TemplateException
e
)
{
return
""
;
logger
.
error
(
"Failed to generate content."
,
e
);
}
}
return
""
;
}
}
}
skywalking-alarm/src/main/java/com/ai/cloud/skywalking/alarm/util/MailUtil.java
浏览文件 @
0496938f
...
@@ -32,7 +32,7 @@ public class MailUtil {
...
@@ -32,7 +32,7 @@ public class MailUtil {
}
}
public
static
void
sendMail
(
String
[]
recipientAccounts
,
String
[]
ccList
,
String
conte
x
t
,
String
title
)
{
public
static
void
sendMail
(
String
[]
recipientAccounts
,
String
[]
ccList
,
String
conte
n
t
,
String
title
)
{
try
{
try
{
MimeMessage
message
=
new
MimeMessage
(
session
);
MimeMessage
message
=
new
MimeMessage
(
session
);
message
.
setFrom
(
new
InternetAddress
(
sendAccount
));
message
.
setFrom
(
new
InternetAddress
(
sendAccount
));
...
@@ -49,7 +49,7 @@ public class MailUtil {
...
@@ -49,7 +49,7 @@ public class MailUtil {
message
.
addRecipients
(
Message
.
RecipientType
.
CC
,
ccAccountArray
);
message
.
addRecipients
(
Message
.
RecipientType
.
CC
,
ccAccountArray
);
message
.
setSubject
(
title
);
message
.
setSubject
(
title
);
message
.
setContent
(
conte
x
t
,
"text/html;charset=UTF-8"
);
message
.
setContent
(
conte
n
t
,
"text/html;charset=UTF-8"
);
ts
.
sendMessage
(
message
,
message
.
getAllRecipients
());
ts
.
sendMessage
(
message
,
message
.
getAllRecipients
());
}
catch
(
AddressException
e
)
{
}
catch
(
AddressException
e
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录