Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦中观雨
cat
提交
0067e5f6
C
cat
项目概览
梦中观雨
/
cat
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
cat
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0067e5f6
编写于
6月 12, 2014
作者:
J
jialinsun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cat agent代码重构
上级
0eadfe1e
变更
19
展开全部
隐藏空白更改
内联
并排
Showing
19 changed file
with
1102 addition
and
76 deletion
+1102
-76
cat-agent/src/main/java/com/dianping/cat/agent/CatAgentModule.java
.../src/main/java/com/dianping/cat/agent/CatAgentModule.java
+29
-0
cat-agent/src/main/java/com/dianping/cat/agent/build/ComponentsConfigurator.java
.../com/dianping/cat/agent/build/ComponentsConfigurator.java
+21
-1
cat-agent/src/main/java/com/dianping/cat/agent/core/page/index/Handler.java
.../java/com/dianping/cat/agent/core/page/index/Handler.java
+15
-2
cat-agent/src/main/java/com/dianping/cat/agent/monitor/AbstractExecutor.java
...java/com/dianping/cat/agent/monitor/AbstractExecutor.java
+34
-0
cat-agent/src/main/java/com/dianping/cat/agent/monitor/DataEntity.java
.../main/java/com/dianping/cat/agent/monitor/DataEntity.java
+16
-11
cat-agent/src/main/java/com/dianping/cat/agent/monitor/DataSender.java
.../main/java/com/dianping/cat/agent/monitor/DataSender.java
+95
-20
cat-agent/src/main/java/com/dianping/cat/agent/monitor/EnvironmentConfig.java
...ava/com/dianping/cat/agent/monitor/EnvironmentConfig.java
+51
-7
cat-agent/src/main/java/com/dianping/cat/agent/monitor/Executor.java
...rc/main/java/com/dianping/cat/agent/monitor/Executor.java
+1
-0
cat-agent/src/main/java/com/dianping/cat/agent/monitor/TaskExecutors.java
...in/java/com/dianping/cat/agent/monitor/TaskExecutors.java
+7
-9
cat-agent/src/main/java/com/dianping/cat/agent/monitor/jvm/JVMMemoryExecutor.java
...com/dianping/cat/agent/monitor/jvm/JVMMemoryExecutor.java
+45
-11
cat-agent/src/main/java/com/dianping/cat/agent/monitor/jvm/JVMStateExecutor.java
.../com/dianping/cat/agent/monitor/jvm/JVMStateExecutor.java
+61
-0
cat-agent/src/main/java/com/dianping/cat/agent/monitor/system/SystemPerformanceExecutor.java
...g/cat/agent/monitor/system/SystemPerformanceExecutor.java
+162
-0
cat-agent/src/main/java/com/dianping/cat/agent/monitor/system/SystemStateExecutor.java
...ianping/cat/agent/monitor/system/SystemStateExecutor.java
+185
-0
cat-agent/src/main/resources/META-INF/plexus/components.xml
cat-agent/src/main/resources/META-INF/plexus/components.xml
+95
-0
cat-home/src/main/java/com/dianping/cat/report/page/monitor/Action.java
...ain/java/com/dianping/cat/report/page/monitor/Action.java
+3
-1
cat-home/src/main/java/com/dianping/cat/report/page/monitor/Handler.java
...in/java/com/dianping/cat/report/page/monitor/Handler.java
+60
-13
cat-home/src/main/java/com/dianping/cat/report/page/monitor/JspViewer.java
.../java/com/dianping/cat/report/page/monitor/JspViewer.java
+1
-0
cat-home/src/main/java/com/dianping/cat/report/page/monitor/Payload.java
...in/java/com/dianping/cat/report/page/monitor/Payload.java
+12
-1
log
log
+209
-0
未找到文件。
cat-agent/src/main/java/com/dianping/cat/agent/CatAgentModule.java
0 → 100644
浏览文件 @
0067e5f6
package
com.dianping.cat.agent
;
import
org.unidal.helper.Threads
;
import
org.unidal.initialization.AbstractModule
;
import
org.unidal.initialization.Module
;
import
org.unidal.initialization.ModuleContext
;
import
com.dianping.cat.agent.monitor.DataSender
;
import
com.dianping.cat.agent.monitor.TaskExecutors
;
public
class
CatAgentModule
extends
AbstractModule
{
public
static
final
String
ID
=
"cat-agent"
;
@Override
protected
void
execute
(
ModuleContext
ctx
)
throws
Exception
{
System
.
out
.
println
(
"coming catagentmodule"
);
DataSender
dataSender
=
ctx
.
lookup
(
DataSender
.
class
);
Threads
.
forGroup
(
"Cat"
).
start
(
dataSender
);
TaskExecutors
taskExecutors
=
ctx
.
lookup
(
TaskExecutors
.
class
);
Threads
.
forGroup
(
"Cat"
).
start
(
taskExecutors
);
}
@Override
public
Module
[]
getDependencies
(
ModuleContext
ctx
)
{
return
null
;
}
}
cat-agent/src/main/java/com/dianping/cat/agent/build/ComponentsConfigurator.java
浏览文件 @
0067e5f6
...
...
@@ -3,21 +3,41 @@ package com.dianping.cat.agent.build;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.unidal.initialization.Module
;
import
org.unidal.lookup.configuration.AbstractResourceConfigurator
;
import
org.unidal.lookup.configuration.Component
;
import
com.dianping.cat.agent.CatAgentModule
;
import
com.dianping.cat.agent.monitor.DataSender
;
import
com.dianping.cat.agent.monitor.EnvironmentConfig
;
import
com.dianping.cat.agent.monitor.Executor
;
import
com.dianping.cat.agent.monitor.TaskExecutors
;
import
com.dianping.cat.agent.monitor.jvm.JVMMemoryExecutor
;
import
com.dianping.cat.agent.monitor.jvm.JVMStateExecutor
;
import
com.dianping.cat.agent.monitor.system.SystemPerformanceExecutor
;
import
com.dianping.cat.agent.monitor.system.SystemStateExecutor
;
public
class
ComponentsConfigurator
extends
AbstractResourceConfigurator
{
@Override
public
List
<
Component
>
defineComponents
()
{
List
<
Component
>
all
=
new
ArrayList
<
Component
>();
all
.
add
(
C
(
EnvironmentConfig
.
class
));
all
.
add
(
C
(
Executor
.
class
,
JVMMemoryExecutor
.
ID
,
JVMMemoryExecutor
.
class
).
req
(
EnvironmentConfig
.
class
));
all
.
add
(
C
(
Executor
.
class
,
JVMStateExecutor
.
ID
,
JVMStateExecutor
.
class
).
req
(
EnvironmentConfig
.
class
));
all
.
add
(
C
(
Executor
.
class
,
SystemPerformanceExecutor
.
ID
,
SystemPerformanceExecutor
.
class
).
req
(
EnvironmentConfig
.
class
));
all
.
add
(
C
(
Executor
.
class
,
SystemStateExecutor
.
ID
,
SystemStateExecutor
.
class
).
req
(
EnvironmentConfig
.
class
));
all
.
add
(
C
(
DataSender
.
class
).
req
(
EnvironmentConfig
.
class
));
all
.
add
(
C
(
TaskExecutors
.
class
).
req
(
DataSender
.
class
));
// Please keep it as last
all
.
addAll
(
new
WebComponentConfigurator
().
defineComponents
());
all
.
add
(
C
(
Executor
.
class
,
JVMMemoryExecutor
.
ID
,
JVMMemoryExecutor
.
class
));
all
.
add
(
C
(
Module
.
class
,
CatAgentModule
.
ID
,
CatAgentModule
.
class
));
return
all
;
}
...
...
cat-agent/src/main/java/com/dianping/cat/agent/core/page/index/Handler.java
浏览文件 @
0067e5f6
...
...
@@ -4,17 +4,27 @@ import java.io.IOException;
import
javax.servlet.ServletException
;
import
com.dianping.cat.agent.core.CorePage
;
import
org.unidal.helper.Threads
;
import
org.unidal.lookup.annotation.Inject
;
import
org.unidal.web.mvc.PageHandler
;
import
org.unidal.web.mvc.annotation.InboundActionMeta
;
import
org.unidal.web.mvc.annotation.OutboundActionMeta
;
import
org.unidal.web.mvc.annotation.PayloadMeta
;
import
com.dianping.cat.agent.core.CorePage
;
import
com.dianping.cat.agent.monitor.DataSender
;
import
com.dianping.cat.agent.monitor.TaskExecutors
;
public
class
Handler
implements
PageHandler
<
Context
>
{
@Inject
private
JspViewer
m_jspViewer
;
@Inject
private
DataSender
m_dataSender
;
@Inject
private
TaskExecutors
m_taskExecutors
;
@Override
@PayloadMeta
(
Payload
.
class
)
@InboundActionMeta
(
name
=
"index"
)
...
...
@@ -28,9 +38,12 @@ public class Handler implements PageHandler<Context> {
model
.
setAction
(
Action
.
VIEW
);
model
.
setPage
(
CorePage
.
INDEX
);
Threads
.
forGroup
(
"Cat"
).
start
(
m_dataSender
);
Threads
.
forGroup
(
"Cat"
).
start
(
m_taskExecutors
);
if
(!
ctx
.
isProcessStopped
())
{
m_jspViewer
.
view
(
ctx
,
model
);
m_jspViewer
.
view
(
ctx
,
model
);
}
}
}
cat-agent/src/main/java/com/dianping/cat/agent/monitor/AbstractExecutor.java
0 → 100644
浏览文件 @
0067e5f6
package
com.dianping.cat.agent.monitor
;
import
org.unidal.lookup.annotation.Inject
;
public
abstract
class
AbstractExecutor
implements
Executor
{
@Inject
protected
EnvironmentConfig
m_environmentConfig
;
public
static
final
String
SUM_TYPE
=
"sum"
;
public
static
final
String
AVG_TYPE
=
"avg"
;
public
static
final
String
COUNT_TYPE
=
"count"
;
public
static
final
String
JVM_TYPE
=
"jvm"
;
public
static
final
String
SYSTEM_TYPE
=
"system"
;
public
static
final
String
NGINX_TYPE
=
"nginx"
;
protected
String
buildSystemDataEntityId
(
String
id
)
{
return
SYSTEM_TYPE
+
"_"
+
id
+
"_"
+
m_environmentConfig
.
getIp
();
}
protected
String
buildJVMDataEntityId
(
String
id
)
{
return
JVM_TYPE
+
"_"
+
id
+
"_"
+
m_environmentConfig
.
getIp
();
}
protected
String
buildNginxDataEntityId
(
String
id
)
{
return
NGINX_TYPE
+
"_"
+
id
+
"_"
+
m_environmentConfig
.
getIp
();
}
}
cat-agent/src/main/java/com/dianping/cat/agent/monitor/DataEntity.java
浏览文件 @
0067e5f6
package
com.dianping.cat.agent.monitor
;
public
class
DataEntity
{
private
String
m_key
;
private
String
m_op
;
private
String
m_id
;
private
String
m_type
;
private
double
m_value
;
public
String
get
Key
()
{
return
m_
key
;
public
String
get
Id
()
{
return
m_
id
;
}
public
DataEntity
setKey
(
String
key
)
{
m_key
=
key
;
@Override
public
String
toString
()
{
return
"DataEntity [m_id="
+
m_id
+
", m_type="
+
m_type
+
", m_value="
+
m_value
+
"]"
;
}
public
DataEntity
setId
(
String
id
)
{
m_id
=
id
;
return
this
;
}
public
String
get
Op
()
{
return
m_
op
;
public
String
get
Type
()
{
return
m_
type
;
}
public
DataEntity
set
Op
(
String
op
)
{
m_
op
=
op
;
public
DataEntity
set
Type
(
String
type
)
{
m_
type
=
type
;
return
this
;
}
...
...
cat-agent/src/main/java/com/dianping/cat/agent/monitor/DataSender.java
浏览文件 @
0067e5f6
package
com.dianping.cat.agent.monitor
;
import
java.io.InputStream
;
import
java.io.OutputStreamWriter
;
import
java.net.URL
;
import
java.net.URLConnection
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.concurrent.ArrayBlockingQueue
;
import
java.util.concurrent.BlockingQueue
;
...
...
@@ -8,23 +12,27 @@ import java.util.concurrent.TimeUnit;
import
org.unidal.helper.Files
;
import
org.unidal.helper.Threads.Task
;
import
org.unidal.helper.Urls
;
import
org.unidal.lookup.annotation.Inject
;
import
com.dianping.cat.Cat
;
import
com.dianping.cat.message.Transaction
;
public
class
DataSender
implements
Task
{
@Inject
private
EnvironmentConfig
m_
c
onfig
;
private
EnvironmentConfig
m_
environmentC
onfig
;
private
BlockingQueue
<
DataEntity
>
m_entities
=
new
ArrayBlockingQueue
<
DataEntity
>(
5000
);
private
static
final
long
DURATION
=
60
*
1000
;
private
BlockingQueue
<
DataEntity
>
m_entityQue
=
new
ArrayBlockingQueue
<
DataEntity
>(
5000
);
private
List
<
DataEntity
>
m_dataEntities
=
new
ArrayList
<
DataEntity
>();
public
boolean
put
(
List
<
DataEntity
>
entities
)
{
boolean
result
=
true
;
for
(
DataEntity
entity
:
entities
)
{
boolean
temp
=
m_entit
ies
.
offer
(
entity
);
boolean
temp
=
m_entit
yQue
.
offer
(
entity
);
if
(!
temp
)
{
result
=
temp
;
...
...
@@ -33,45 +41,112 @@ public class DataSender implements Task {
return
result
;
}
private
void
send
(
DataEntity
entity
)
{
List
<
String
>
servers
=
m_config
.
getServers
();
private
String
buildBatchEntities
(
List
<
DataEntity
>
entities
)
{
StringBuilder
sb
=
new
StringBuilder
();
for
(
DataEntity
entity
:
entities
)
{
sb
.
append
(
entity
.
getId
()).
append
(
"\t"
).
append
(
entity
.
getType
()).
append
(
"\t"
).
append
(
entity
.
getValue
())
.
append
(
"\n"
);
}
return
sb
.
toString
();
}
private
boolean
sendBatchEntities
(
List
<
DataEntity
>
entities
)
{
List
<
String
>
servers
=
m_environmentConfig
.
getServers
();
for
(
String
server
:
servers
)
{
try
{
String
url
=
buildUrl
(
server
,
entity
);
InputStream
in
=
Urls
.
forIO
().
readTimeout
(
3000
).
connectTimeout
(
3000
).
openStream
(
url
);
String
content
=
Files
.
forIO
().
readFrom
(
in
,
"utf-8"
);
String
url
=
buildUrl
(
server
);
URLConnection
conn
=
new
URL
(
url
).
openConnection
();
conn
.
setDoOutput
(
true
);
conn
.
setDoInput
(
true
);
OutputStreamWriter
writer
=
new
OutputStreamWriter
(
conn
.
getOutputStream
());
String
entityContent
=
buildBatchEntities
(
m_dataEntities
);
String
content
=
"&batch="
+
entityContent
;
writer
.
write
(
content
);
writer
.
flush
();
InputStream
in
=
conn
.
getInputStream
();
String
result
=
Files
.
forIO
().
readFrom
(
in
,
"utf-8"
);
if
(
sendOK
(
conten
t
))
{
break
;
if
(
sendOK
(
resul
t
))
{
return
true
;
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
Cat
.
logError
(
e
);
}
return
false
;
}
return
false
;
}
private
boolean
sendOK
(
String
content
)
{
return
true
;
if
(
content
.
contains
(
"{\"statusCode\":\"0\"}"
))
{
return
true
;
}
else
{
return
false
;
}
}
private
String
buildUrl
(
String
server
,
DataEntity
entity
)
{
return
null
;
private
String
buildUrl
(
String
server
)
{
String
group
=
m_environmentConfig
.
getGroup
();
String
domain
=
m_environmentConfig
.
getDomain
();
long
current
=
System
.
currentTimeMillis
();
String
urlFormat
=
"http://%1$s:2281/cat/r/monitor?op=batch×tamp=%2$s&group=%3$s&domain=%4$s"
;
String
url
=
String
.
format
(
urlFormat
,
server
,
current
,
group
,
domain
);
return
url
;
}
@Override
public
void
run
()
{
while
(
true
)
{
try
{
DataEntity
entity
=
m_entities
.
poll
(
5
,
TimeUnit
.
MILLISECONDS
);
Transaction
t
=
Cat
.
newTransaction
(
"Data"
,
"Send"
);
if
(
entity
!=
null
)
{
send
(
entity
);
try
{
long
current
=
System
.
currentTimeMillis
();
try
{
while
(
m_entityQue
.
size
()
>
0
)
{
DataEntity
entity
=
m_entityQue
.
poll
(
5
,
TimeUnit
.
MILLISECONDS
);
m_dataEntities
.
add
(
entity
);
}
if
(!
m_dataEntities
.
isEmpty
())
{
boolean
success
=
sendBatchEntities
(
m_dataEntities
);
if
(
success
)
{
Cat
.
logEvent
(
"DataSender"
,
"OK"
);
}
else
{
Cat
.
logEvent
(
"DataSender"
,
"ERROR"
);
}
}
}
catch
(
Exception
e
)
{
Cat
.
logError
(
e
);
}
}
catch
(
InterruptedException
e
)
{
break
;
long
duration
=
System
.
currentTimeMillis
()
-
current
;
long
sleeptime
=
DURATION
-
duration
;
if
(
sleeptime
>
0
)
{
try
{
Thread
.
sleep
(
sleeptime
);
}
catch
(
InterruptedException
e
)
{
break
;
}
}
t
.
setStatus
(
Transaction
.
SUCCESS
);
}
catch
(
Exception
e
)
{
Cat
.
logError
(
e
);
}
finally
{
t
.
complete
();
}
}
}
@Override
...
...
cat-agent/src/main/java/com/dianping/cat/agent/monitor/EnvironmentConfig.java
浏览文件 @
0067e5f6
package
com.dianping.cat.agent.monitor
;
import
java.io.BufferedInputStream
;
import
java.io.FileInputStream
;
import
java.io.InputStream
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.Properties
;
import
com.dianping.cat.Cat
;
public
class
EnvironmentConfig
{
public
static
String
m_path
=
"/data/webapps/server.properties"
;
private
static
final
String
CONFIG_FILE
=
"/data/webapps/server.properties"
;
private
String
m_ip
=
"10.128.120.53"
;
private
String
m_domain
=
"Cat"
;
public
EnvironmentConfig
()
{
initialize
();
}
private
void
initialize
()
{
try
{
Properties
properties
=
new
Properties
();
InputStream
in
=
new
BufferedInputStream
(
new
FileInputStream
(
CONFIG_FILE
));
properties
.
load
(
in
);
// host.name=shop-web01.nh 黄永确认下
// host.ip=10.1.4.61
String
hostName
=
properties
.
getProperty
(
"host.name"
,
"Cat01.nh"
);
m_domain
=
buildDomain
(
hostName
);
m_ip
=
properties
.
getProperty
(
"host.ip"
,
"10.10.1.1"
);
}
catch
(
Exception
e
)
{
Cat
.
logError
(
e
);
}
}
// [**01.nh0] [**01.beta] [**-ppe01.hm]
private
String
buildDomain
(
String
hostName
)
{
String
domain
=
""
;
try
{
if
(
hostName
.
endsWith
(
".nh"
)
||
hostName
.
endsWith
(
".beta"
))
{
domain
=
hostName
.
substring
(
0
,
hostName
.
lastIndexOf
(
"."
)
-
2
);
}
else
if
(
hostName
.
endsWith
(
"hm"
))
{
domain
=
hostName
.
substring
(
0
,
hostName
.
lastIndexOf
(
"."
)
-
6
);
}
else
{
Cat
.
logError
(
new
RuntimeException
(
"Unrecognized hostName ["
+
hostName
+
"] occurs"
));
}
}
catch
(
Exception
e
)
{
Cat
.
logError
(
e
);
}
return
domain
;
}
public
String
getIp
()
{
return
"10.1.1.1"
;
return
m_ip
;
}
public
String
getDomain
()
{
return
"Cat"
;
return
m_domain
;
}
public
String
getGroup
()
{
return
"
Cat"
;
return
"
system-"
+
m_domain
;
}
public
List
<
String
>
getServers
()
{
return
Arrays
.
asList
(
"
cat.qa.dianpingoa.com
"
);
return
Arrays
.
asList
(
"
127.0.0.1
"
);
}
}
cat-agent/src/main/java/com/dianping/cat/agent/monitor/Executor.java
浏览文件 @
0067e5f6
...
...
@@ -7,4 +7,5 @@ public interface Executor {
public
String
getId
();
public
List
<
DataEntity
>
execute
();
}
cat-agent/src/main/java/com/dianping/cat/agent/monitor/TaskExecutors.java
浏览文件 @
0067e5f6
...
...
@@ -29,10 +29,9 @@ public class TaskExecutors extends ContainerHolder implements Task, Initializabl
try
{
long
current
=
System
.
currentTimeMillis
();
for
(
Executor
executor
:
m_executors
)
{
Transaction
t2
=
Cat
.
newTransaction
(
"Executor"
,
executor
.
getId
());
try
{
List
<
DataEntity
>
entities
=
executor
.
execute
();
...
...
@@ -65,20 +64,19 @@ public class TaskExecutors extends ContainerHolder implements Task, Initializabl
}
@Override
public
String
getName
()
{
return
"data-fetcher"
;
public
void
initialize
()
throws
InitializationException
{
Map
<
String
,
Executor
>
map
=
lookupMap
(
Executor
.
class
);
m_executors
=
map
.
values
();
}
@Override
public
void
shutdown
()
{
public
String
getName
()
{
return
"data-fetcher"
;
}
@Override
public
void
initialize
()
throws
InitializationException
{
Map
<
String
,
Executor
>
map
=
lookupMap
(
Executor
.
class
);
public
void
shutdown
()
{
m_executors
=
map
.
values
();
}
}
cat-agent/src/main/java/com/dianping/cat/agent/monitor/jvm/JVMMemoryExecutor.java
浏览文件 @
0067e5f6
...
...
@@ -6,17 +6,26 @@ import java.util.ArrayList;
import
java.util.List
;
import
com.dianping.cat.Cat
;
import
com.dianping.cat.agent.monitor.AbstractExecutor
;
import
com.dianping.cat.agent.monitor.DataEntity
;
import
com.dianping.cat.agent.monitor.Executor
;
public
class
JVMMemoryExecutor
implements
Executor
{
public
class
JVMMemoryExecutor
extends
Abstract
Executor
{
public
static
String
ID
=
"JVMMemoryExecutor"
;
public
static
final
String
ID
=
"JVMMemoryExecutor"
;
@Override
public
List
<
DataEntity
>
execute
()
{
List
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
entities
.
addAll
(
buildJVMMemoryInfo
());
return
new
ArrayList
<
DataEntity
>();
}
private
List
<
DataEntity
>
buildJVMMemoryInfo
()
{
List
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
try
{
String
pid
=
""
;
String
pid
=
findPidOfTomcat
()
;
Process
process
=
Runtime
.
getRuntime
().
exec
(
"jstat -gcutil "
+
pid
);
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
process
.
getInputStream
()));
...
...
@@ -24,28 +33,53 @@ public class JVMMemoryExecutor implements Executor {
String
output
=
reader
.
readLine
();
String
[]
metrics
=
output
.
split
(
" +"
);
List
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
DataEntity
eden
=
new
DataEntity
();
e
den
.
setKey
(
"jvm_edenUsage_"
).
setOp
(
"avg"
).
setValue
(
Double
.
valueOf
(
metrics
[
2
])
/
100
);
eden
.
setId
(
buildJVMDataEntityId
(
"edenUsage"
)).
setType
(
AVG_TYPE
).
setValue
(
Double
.
valueOf
(
metrics
[
2
])
/
100
);
e
ntities
.
add
(
eden
);
DataEntity
old
=
new
DataEntity
();
old
.
setKey
(
"jvm_oldUsage_"
).
setOp
(
"avg"
).
setValue
(
Double
.
valueOf
(
metrics
[
3
])
/
100
);
old
.
setId
(
buildJVMDataEntityId
(
"oldUsage"
)).
setType
(
AVG_TYPE
).
setValue
(
Double
.
valueOf
(
metrics
[
3
])
/
100
);
entities
.
add
(
old
);
DataEntity
perm
=
new
DataEntity
();
perm
.
setKey
(
"jvm_permUsage_"
).
setOp
(
"avg"
).
setValue
(
Double
.
valueOf
(
metrics
[
4
])
/
100
);
perm
.
setId
(
buildJVMDataEntityId
(
"permUsage"
)).
setType
(
AVG_TYPE
).
setValue
(
Double
.
valueOf
(
metrics
[
4
])
/
100
);
entities
.
add
(
perm
);
return
entities
;
}
catch
(
Exception
e
)
{
Cat
.
logError
(
e
);
}
return
entities
;
}
return
new
ArrayList
<
DataEntity
>();
public
static
String
findPidOfTomcat
()
{
String
pid
=
null
;
try
{
Process
process
=
Runtime
.
getRuntime
().
exec
(
new
String
[]
{
"/bin/sh"
,
"-c"
,
"ps aux | grep tomcat | grep -v grep"
});
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
process
.
getInputStream
()));
String
output
=
reader
.
readLine
();
if
(
output
!=
null
)
{
if
(
reader
.
readLine
()
!=
null
)
{
Cat
.
logError
(
new
RuntimeException
(
"More than one tomcat is running"
));
}
reader
.
close
();
String
[]
outputs
=
output
.
split
(
" +"
);
pid
=
outputs
[
1
];
}
else
{
Cat
.
logError
(
new
RuntimeException
(
"No tomcat is running"
));
}
}
catch
(
Exception
e
)
{
Cat
.
logError
(
e
);
}
return
pid
;
}
@Override
public
String
getId
()
{
public
String
getId
()
{
return
ID
;
}
...
...
cat-agent/src/main/java/com/dianping/cat/agent/monitor/jvm/JVMStateExecutor.java
0 → 100644
浏览文件 @
0067e5f6
package
com.dianping.cat.agent.monitor.jvm
;
import
java.io.File
;
import
java.util.ArrayList
;
import
java.util.List
;
import
com.dianping.cat.agent.monitor.AbstractExecutor
;
import
com.dianping.cat.agent.monitor.DataEntity
;
public
class
JVMStateExecutor
extends
AbstractExecutor
{
public
static
final
String
ID
=
"JVMStateExecutor"
;
private
static
final
String
CATALINA_PATH
=
"/data/applogs/tomcat/catalina.out"
;
@Override
public
List
<
DataEntity
>
execute
()
{
List
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
entities
.
addAll
(
buildCatalinaLogInfo
());
entities
.
addAll
(
buildTomcatLiveInfo
());
return
entities
;
}
public
List
<
DataEntity
>
buildCatalinaLogInfo
()
{
List
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
File
logFile
=
new
File
(
CATALINA_PATH
);
if
(
logFile
.
exists
())
{
double
bytes
=
logFile
.
length
();
double
kilobytes
=
(
bytes
/
1024
);
DataEntity
entity
=
new
DataEntity
();
entity
.
setId
(
buildJVMDataEntityId
(
"catalinaLogSize"
)).
setType
(
SUM_TYPE
).
setValue
(
kilobytes
);
entities
.
add
(
entity
);
}
return
entities
;
}
public
List
<
DataEntity
>
buildTomcatLiveInfo
()
{
List
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
DataEntity
entity
=
new
DataEntity
();
String
pid
=
JVMMemoryExecutor
.
findPidOfTomcat
();
entity
.
setId
(
buildJVMDataEntityId
(
"tomcatLive"
)).
setType
(
AVG_TYPE
);
if
(
pid
==
null
)
{
entity
.
setValue
(
0
);
}
else
{
entity
.
setValue
(
1
);
}
entities
.
add
(
entity
);
return
entities
;
}
@Override
public
String
getId
()
{
return
ID
;
}
}
cat-agent/src/main/java/com/dianping/cat/agent/monitor/system/SystemPerformanceExecutor.java
0 → 100644
浏览文件 @
0067e5f6
package
com.dianping.cat.agent.monitor.system
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
import
org.hyperic.sigar.Cpu
;
import
org.hyperic.sigar.FileSystem
;
import
org.hyperic.sigar.FileSystemUsage
;
import
org.hyperic.sigar.NetInterfaceStat
;
import
org.hyperic.sigar.Sigar
;
import
org.hyperic.sigar.SigarException
;
import
org.hyperic.sigar.Swap
;
import
com.dianping.cat.Cat
;
import
com.dianping.cat.agent.monitor.AbstractExecutor
;
import
com.dianping.cat.agent.monitor.DataEntity
;
public
class
SystemPerformanceExecutor
extends
AbstractExecutor
{
public
static
final
String
ID
=
"PerformanceExecutor"
;
private
static
final
String
ETH_NAME
=
"en0"
;
private
static
final
List
<
String
>
DISK_LIST
=
new
ArrayList
<
String
>(
Arrays
.
asList
(
"/data"
,
"/usr"
,
"/var"
));
private
Sigar
m_sigar
=
new
Sigar
();
private
Cpu
m_preCpu
;
private
Cpu
m_curCpu
;
private
NetInterfaceStat
m_preIfStat
;
private
NetInterfaceStat
m_curIfStat
;
@Override
public
List
<
DataEntity
>
execute
()
{
List
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
entities
.
addAll
(
buildCpuInfo
());
entities
.
addAll
(
buildDiskUsage
());
entities
.
addAll
(
buildFlowInfo
());
entities
.
addAll
(
buildSwapInfo
());
entities
.
addAll
(
buildLoadInfo
());
return
entities
;
}
public
List
<
DataEntity
>
buildCpuInfo
()
{
ArrayList
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
try
{
double
cpuUsage
=
0.0
;
if
(
m_preCpu
!=
null
)
{
m_curCpu
=
m_sigar
.
getCpu
();
long
totalIdle
=
m_curCpu
.
getIdle
()
-
m_preCpu
.
getIdle
();
long
totalTime
=
m_curCpu
.
getTotal
()
-
m_preCpu
.
getTotal
();
if
(
totalIdle
>
0
&&
totalTime
>
0
)
{
cpuUsage
=
1
-
1.0
*
totalIdle
/
totalTime
;
m_preCpu
=
m_curCpu
;
}
DataEntity
entity
=
new
DataEntity
();
entity
.
setId
(
buildSystemDataEntityId
(
"cpu"
)).
setType
(
AVG_TYPE
).
setValue
(
cpuUsage
);
entities
.
add
(
entity
);
}
else
{
m_preCpu
=
m_sigar
.
getCpu
();
}
}
catch
(
SigarException
e
)
{
Cat
.
logError
(
e
);
}
return
entities
;
}
public
List
<
DataEntity
>
buildDiskUsage
()
{
ArrayList
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
try
{
FileSystem
[]
fileSystems
=
m_sigar
.
getFileSystemList
();
for
(
FileSystem
fs
:
fileSystems
)
{
if
(
fs
.
getType
()
==
FileSystem
.
TYPE_LOCAL_DISK
&&
DISK_LIST
.
contains
(
fs
.
getDirName
()))
{
FileSystemUsage
usage
=
m_sigar
.
getFileSystemUsage
(
fs
.
getDirName
());
DataEntity
entity
=
new
DataEntity
();
entity
.
setId
(
buildSystemDataEntityId
(
fs
.
getDirName
()
+
"-usage"
)).
setType
(
AVG_TYPE
)
.
setValue
(
usage
.
getUsePercent
());
entities
.
add
(
entity
);
}
}
}
catch
(
SigarException
e
)
{
Cat
.
logError
(
e
);
}
return
entities
;
}
public
List
<
DataEntity
>
buildFlowInfo
()
{
List
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
try
{
if
(
m_preIfStat
!=
null
)
{
m_curIfStat
=
m_sigar
.
getNetInterfaceStat
(
ETH_NAME
);
long
totalRxBytes
=
m_curIfStat
.
getRxBytes
()
-
m_preIfStat
.
getRxBytes
();
long
totalTxBytes
=
m_curIfStat
.
getTxBytes
()
-
m_preIfStat
.
getTxBytes
();
m_preIfStat
=
m_curIfStat
;
DataEntity
inFlow
=
new
DataEntity
();
inFlow
.
setId
(
buildSystemDataEntityId
(
ETH_NAME
+
"-in-flow"
)).
setType
(
SUM_TYPE
).
setValue
(
totalRxBytes
);
entities
.
add
(
inFlow
);
DataEntity
outFlow
=
new
DataEntity
();
outFlow
.
setId
(
buildSystemDataEntityId
(
ETH_NAME
+
"-out-flow"
)).
setType
(
SUM_TYPE
).
setValue
(
totalTxBytes
);
entities
.
add
(
outFlow
);
}
else
{
m_preIfStat
=
m_sigar
.
getNetInterfaceStat
(
ETH_NAME
);
}
}
catch
(
SigarException
e
)
{
Cat
.
logError
(
e
);
}
return
entities
;
}
public
List
<
DataEntity
>
buildSwapInfo
()
{
ArrayList
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
try
{
Swap
curSwap
=
m_sigar
.
getSwap
();
double
swapUsage
=
1.0
*
curSwap
.
getUsed
()
/
curSwap
.
getTotal
();
DataEntity
entity
=
new
DataEntity
();
entity
.
setId
(
buildSystemDataEntityId
(
"swap"
)).
setType
(
AVG_TYPE
).
setValue
(
swapUsage
);
entities
.
add
(
entity
);
}
catch
(
SigarException
e
)
{
Cat
.
logError
(
e
);
}
return
entities
;
}
public
ArrayList
<
DataEntity
>
buildLoadInfo
()
{
ArrayList
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
try
{
double
[]
loadAverages
=
m_sigar
.
getLoadAverage
();
DataEntity
entity
=
new
DataEntity
();
entity
.
setId
(
buildSystemDataEntityId
(
"load"
)).
setType
(
AVG_TYPE
).
setValue
(
loadAverages
[
0
]);
entities
.
add
(
entity
);
}
catch
(
SigarException
e
)
{
Cat
.
logError
(
e
);
}
return
entities
;
}
@Override
public
String
getId
()
{
return
ID
;
}
}
cat-agent/src/main/java/com/dianping/cat/agent/monitor/system/SystemStateExecutor.java
0 → 100644
浏览文件 @
0067e5f6
package
com.dianping.cat.agent.monitor.system
;
import
java.io.BufferedReader
;
import
java.io.FileReader
;
import
java.io.IOException
;
import
java.net.InetAddress
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable
;
import
org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException
;
import
org.hyperic.sigar.NetInterfaceConfig
;
import
org.hyperic.sigar.Sigar
;
import
org.hyperic.sigar.SigarException
;
import
org.hyperic.sigar.Uptime
;
import
com.dianping.cat.Cat
;
import
com.dianping.cat.agent.monitor.AbstractExecutor
;
import
com.dianping.cat.agent.monitor.DataEntity
;
public
class
SystemStateExecutor
extends
AbstractExecutor
implements
Initializable
{
public
static
final
String
ID
=
"SystemStateExecutor"
;
private
static
final
String
MD5_PATH
=
"/usr/sbin/sshd"
;
private
String
m_md5String
;
private
String
m_hostName
;
private
String
m_ipAddr
;
@Override
public
void
initialize
()
throws
InitializationException
{
m_hostName
=
tellHostName
();
m_ipAddr
=
m_environmentConfig
.
getIp
();
try
{
m_md5String
=
readFileContent
(
MD5_PATH
);
}
catch
(
IOException
e
)
{
Cat
.
logError
(
e
);
}
}
public
String
tellHostName
()
{
String
hostname
=
""
;
try
{
hostname
=
InetAddress
.
getLocalHost
().
getHostName
();
}
catch
(
Exception
exc
)
{
try
{
Sigar
sigar
=
new
Sigar
();
hostname
=
sigar
.
getNetInfo
().
getHostName
();
}
catch
(
SigarException
e
)
{
Cat
.
logError
(
e
);
}
}
return
hostname
;
}
@Override
public
List
<
DataEntity
>
execute
()
{
List
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
entities
.
addAll
(
buildUptimeInfo
());
entities
.
addAll
(
buildHostIpAddInfo
());
entities
.
addAll
(
buildHostNameInfo
());
entities
.
addAll
(
buildSshdInfo
());
return
entities
;
}
public
String
readFileContent
(
String
path
)
throws
IOException
{
BufferedReader
br
=
new
BufferedReader
(
new
FileReader
(
path
));
try
{
StringBuilder
sb
=
new
StringBuilder
();
String
line
=
br
.
readLine
();
while
(
line
!=
null
)
{
sb
.
append
(
line
);
sb
.
append
(
System
.
getProperty
(
"line.separator"
));
line
=
br
.
readLine
();
}
return
sb
.
toString
();
}
catch
(
Exception
e
)
{
Cat
.
logError
(
e
);
}
finally
{
br
.
close
();
}
return
null
;
}
public
List
<
DataEntity
>
buildUptimeInfo
()
{
List
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
try
{
Sigar
sigar
=
new
Sigar
();
Uptime
uptime
=
sigar
.
getUptime
();
double
time
=
uptime
.
getUptime
()
/
60
;
DataEntity
entity
=
new
DataEntity
();
entity
.
setId
(
buildSystemDataEntityId
(
"uptime"
)).
setType
(
AVG_TYPE
).
setValue
(
time
);
entities
.
add
(
entity
);
}
catch
(
SigarException
e
)
{
Cat
.
logError
(
e
);
}
return
entities
;
}
public
boolean
hostIpAddrChanged
()
{
try
{
Sigar
sigar
=
new
Sigar
();
String
ifNames
[]
=
sigar
.
getNetInterfaceList
();
for
(
int
i
=
0
;
i
<
ifNames
.
length
;
i
++)
{
String
name
=
ifNames
[
i
];
NetInterfaceConfig
ifconfig
=
sigar
.
getNetInterfaceConfig
(
name
);
String
currentIp
=
ifconfig
.
getAddress
();
if
(
currentIp
.
equals
(
m_ipAddr
))
{
return
false
;
}
}
}
catch
(
SigarException
e
)
{
Cat
.
logError
(
e
);
}
return
true
;
}
public
List
<
DataEntity
>
buildHostIpAddInfo
()
{
List
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
DataEntity
entity
=
new
DataEntity
();
entity
.
setId
(
buildSystemDataEntityId
(
"hostIpChange"
)).
setType
(
AVG_TYPE
);
if
(!
hostIpAddrChanged
())
{
entity
.
setValue
(
1
);
}
else
{
entity
.
setValue
(
0
);
}
entities
.
add
(
entity
);
return
entities
;
}
public
List
<
DataEntity
>
buildHostNameInfo
()
{
List
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
DataEntity
entity
=
new
DataEntity
();
String
hostName
=
tellHostName
();
entity
.
setId
(
buildSystemDataEntityId
(
"hostNameChange"
)).
setType
(
AVG_TYPE
);
if
(
m_hostName
.
equals
(
hostName
))
{
entity
.
setValue
(
1
);
}
else
{
entity
.
setValue
(
0
);
}
entities
.
add
(
entity
);
return
entities
;
}
public
List
<
DataEntity
>
buildSshdInfo
()
{
ArrayList
<
DataEntity
>
entities
=
new
ArrayList
<
DataEntity
>();
try
{
String
currMd5String
=
readFileContent
(
MD5_PATH
);
DataEntity
entity
=
new
DataEntity
();
entity
.
setId
(
buildSystemDataEntityId
(
"md5Change"
)).
setType
(
AVG_TYPE
);
if
(
m_md5String
!=
null
&&
m_md5String
.
equals
(
currMd5String
))
{
entity
.
setValue
(
1
);
}
else
{
entity
.
setValue
(
0
);
}
entities
.
add
(
entity
);
}
catch
(
IOException
e
)
{
Cat
.
logError
(
e
);
}
return
entities
;
}
@Override
public
String
getId
()
{
return
ID
;
}
}
cat-agent/src/main/resources/META-INF/plexus/components.xml
浏览文件 @
0067e5f6
<plexus>
<components>
<component>
<role>
com.dianping.cat.agent.monitor.EnvironmentConfig
</role>
<implementation>
com.dianping.cat.agent.monitor.EnvironmentConfig
</implementation>
</component>
<component>
<role>
com.dianping.cat.agent.monitor.Executor
</role>
<role-hint>
JVMMemoryExecutor
</role-hint>
<implementation>
com.dianping.cat.agent.monitor.jvm.JVMMemoryExecutor
</implementation>
<requirements>
<requirement>
<role>
com.dianping.cat.agent.monitor.EnvironmentConfig
</role>
</requirement>
</requirements>
</component>
<component>
<role>
com.dianping.cat.agent.monitor.Executor
</role>
<role-hint>
JVMStateExecutor
</role-hint>
<implementation>
com.dianping.cat.agent.monitor.jvm.JVMStateExecutor
</implementation>
<requirements>
<requirement>
<role>
com.dianping.cat.agent.monitor.EnvironmentConfig
</role>
</requirement>
</requirements>
</component>
<component>
<role>
com.dianping.cat.agent.monitor.Executor
</role>
<role-hint>
PerformanceExecutor
</role-hint>
<implementation>
com.dianping.cat.agent.monitor.system.SystemPerformanceExecutor
</implementation>
<requirements>
<requirement>
<role>
com.dianping.cat.agent.monitor.EnvironmentConfig
</role>
</requirement>
</requirements>
</component>
<component>
<role>
com.dianping.cat.agent.monitor.Executor
</role>
<role-hint>
SystemStateExecutor
</role-hint>
<implementation>
com.dianping.cat.agent.monitor.system.SystemStateExecutor
</implementation>
<requirements>
<requirement>
<role>
com.dianping.cat.agent.monitor.EnvironmentConfig
</role>
</requirement>
</requirements>
</component>
<component>
<role>
com.dianping.cat.agent.monitor.DataSender
</role>
<implementation>
com.dianping.cat.agent.monitor.DataSender
</implementation>
<requirements>
<requirement>
<role>
com.dianping.cat.agent.monitor.EnvironmentConfig
</role>
</requirement>
</requirements>
</component>
<component>
<role>
com.dianping.cat.agent.monitor.TaskExecutors
</role>
<implementation>
com.dianping.cat.agent.monitor.TaskExecutors
</implementation>
<requirements>
<requirement>
<role>
com.dianping.cat.agent.monitor.DataSender
</role>
</requirement>
</requirements>
</component>
<component>
<role>
org.unidal.web.mvc.model.ModuleRegistry
</role>
<implementation>
org.unidal.web.mvc.model.ModuleRegistry
</implementation>
...
...
@@ -20,6 +82,12 @@
<requirement>
<role>
com.dianping.cat.agent.core.page.index.JspViewer
</role>
</requirement>
<requirement>
<role>
com.dianping.cat.agent.monitor.DataSender
</role>
</requirement>
<requirement>
<role>
com.dianping.cat.agent.monitor.TaskExecutors
</role>
</requirement>
</requirements>
</component>
<component>
...
...
@@ -31,5 +99,32 @@
</requirement>
</requirements>
</component>
<component>
<role>
com.dianping.cat.agent.monitor.DataSender
</role>
<implementation>
com.dianping.cat.agent.monitor.DataSender
</implementation>
<requirements>
<requirement>
<role>
com.dianping.cat.agent.monitor.EnvironmentConfig
</role>
</requirement>
</requirements>
</component>
<component>
<role>
com.dianping.cat.agent.monitor.EnvironmentConfig
</role>
<implementation>
com.dianping.cat.agent.monitor.EnvironmentConfig
</implementation>
</component>
<component>
<role>
com.dianping.cat.agent.monitor.TaskExecutors
</role>
<implementation>
com.dianping.cat.agent.monitor.TaskExecutors
</implementation>
<requirements>
<requirement>
<role>
com.dianping.cat.agent.monitor.DataSender
</role>
</requirement>
</requirements>
</component>
<component>
<role>
org.unidal.initialization.Module
</role>
<role-hint>
cat-agent
</role-hint>
<implementation>
com.dianping.cat.agent.CatAgentModule
</implementation>
</component>
</components>
</plexus>
cat-home/src/main/java/com/dianping/cat/report/page/monitor/Action.java
浏览文件 @
0067e5f6
...
...
@@ -5,7 +5,9 @@ public enum Action implements org.unidal.web.mvc.Action {
AVG_API
(
"avg"
),
SUM_API
(
"sum"
);
SUM_API
(
"sum"
),
BATCH_API
(
"batch"
);
private
String
m_name
;
...
...
cat-home/src/main/java/com/dianping/cat/report/page/monitor/Handler.java
浏览文件 @
0067e5f6
...
...
@@ -21,6 +21,7 @@ import com.dianping.cat.message.internal.DefaultMetric;
import
com.dianping.cat.message.internal.DefaultTransaction
;
import
com.dianping.cat.message.spi.internal.DefaultMessageTree
;
import
com.dianping.cat.report.ReportPage
;
import
com.dianping.cat.report.task.alert.MetricType
;
import
com.google.gson.FieldNamingPolicy
;
import
com.google.gson.Gson
;
import
com.google.gson.GsonBuilder
;
...
...
@@ -37,6 +38,7 @@ public class Handler implements PageHandler<Context> {
String
domain
=
payload
.
getDomain
();
String
group
=
payload
.
getGroup
();
String
key
=
payload
.
getKey
();
String
action
=
payload
.
getAction
().
getName
();
HttpStatus
httpStatus
=
new
HttpStatus
();
boolean
error
=
false
;
...
...
@@ -48,7 +50,7 @@ public class Handler implements PageHandler<Context> {
sb
.
append
(
"group "
);
error
=
true
;
}
if
(
StringUtils
.
isEmpty
(
key
))
{
if
(
StringUtils
.
isEmpty
(
key
)
&&
!
Action
.
BATCH_API
.
getName
().
equalsIgnoreCase
(
action
)
)
{
sb
.
append
(
"key "
);
error
=
true
;
}
...
...
@@ -94,24 +96,18 @@ public class Handler implements PageHandler<Context> {
time
=
System
.
currentTimeMillis
();
}
Metric
metric
=
Cat
.
getProducer
().
newMetric
(
group
,
key
);
DefaultMetric
defaultMetric
=
(
DefaultMetric
)
metric
;
if
(
defaultMetric
!=
null
)
{
defaultMetric
.
setTimestamp
(
time
);
}
switch
(
action
)
{
case
COUNT_API:
defaultMetric
.
setStatus
(
"C"
);
defaultMetric
.
addData
(
String
.
valueOf
(
count
));
buildMetric
(
group
,
key
,
MetricType
.
COUNT
.
name
(),
count
,
time
);
break
;
case
AVG_API:
defaultMetric
.
setStatus
(
"T"
);
defaultMetric
.
addData
(
String
.
format
(
"%.2f"
,
payload
.
getAvg
()));
buildMetric
(
group
,
key
,
MetricType
.
AVG
.
name
(),
payload
.
getAvg
(),
time
);
break
;
case
SUM_API:
defaultMetric
.
setStatus
(
"S,C"
);
defaultMetric
.
addData
(
String
.
format
(
"%s,%.2f"
,
count
,
payload
.
getSum
()));
buildMetric
(
group
,
key
,
MetricType
.
SUM
.
name
(),
payload
.
getSum
(),
time
);
break
;
case
BATCH_API:
buildBatchMetric
(
group
,
payload
.
getBatch
(),
time
);
break
;
default
:
throw
new
RuntimeException
(
"Unknown action: "
+
action
);
...
...
@@ -129,4 +125,55 @@ public class Handler implements PageHandler<Context> {
m_jspViewer
.
view
(
ctx
,
model
);
}
private
Metric
buildMetric
(
String
group
,
String
key
,
String
type
,
double
value
,
long
time
)
{
Metric
metric
=
Cat
.
getProducer
().
newMetric
(
group
,
key
);
DefaultMetric
defaultMetric
=
(
DefaultMetric
)
metric
;
if
(
defaultMetric
!=
null
)
{
defaultMetric
.
setTimestamp
(
time
);
if
(
MetricType
.
SUM
.
name
().
equalsIgnoreCase
(
type
))
{
defaultMetric
.
setStatus
(
"S,C"
);
defaultMetric
.
addData
(
String
.
format
(
"%s,%.2f"
,
1
,
value
));
}
else
if
(
MetricType
.
AVG
.
name
().
equalsIgnoreCase
(
type
))
{
defaultMetric
.
setStatus
(
"T"
);
defaultMetric
.
addData
(
String
.
format
(
"%.2f"
,
value
));
}
else
if
(
MetricType
.
AVG
.
name
().
equalsIgnoreCase
(
type
))
{
defaultMetric
.
setStatus
(
"C"
);
defaultMetric
.
addData
(
String
.
valueOf
(
value
));
}
}
return
defaultMetric
;
}
private
boolean
validateDouble
(
String
value
)
{
try
{
if
(
StringUtils
.
isNotEmpty
(
value
))
{
Double
.
parseDouble
(
value
);
return
true
;
}
else
{
return
false
;
}
}
catch
(
Exception
e
)
{
return
false
;
}
}
private
void
buildBatchMetric
(
String
group
,
String
content
,
long
time
)
{
String
[]
lines
=
content
.
split
(
"\n"
);
for
(
String
line
:
lines
)
{
String
[]
tabs
=
line
.
split
(
"\t"
);
if
(
tabs
.
length
==
3
&
validateDouble
(
tabs
[
2
]))
{
String
key
=
tabs
[
0
];
String
type
=
tabs
[
1
];
double
value
=
Double
.
parseDouble
(
tabs
[
2
]);
buildMetric
(
group
,
key
,
type
,
value
,
time
);
}
else
{
Cat
.
logError
(
new
RuntimeException
(
"Unrecognized batch data: "
+
line
));
}
}
}
}
cat-home/src/main/java/com/dianping/cat/report/page/monitor/JspViewer.java
浏览文件 @
0067e5f6
...
...
@@ -12,6 +12,7 @@ public class JspViewer extends BaseJspViewer<ReportPage, Action, Context, Model>
case
COUNT_API:
case
AVG_API:
case
SUM_API:
case
BATCH_API:
return
JspFile
.
VIEW
.
getPath
();
default
:
break
;
...
...
cat-home/src/main/java/com/dianping/cat/report/page/monitor/Payload.java
浏览文件 @
0067e5f6
...
...
@@ -38,13 +38,24 @@ public class Payload extends AbstractReportPayload<Action> {
@FieldMeta
(
"value"
)
private
double
m_value
;
@FieldMeta
(
"batch"
)
private
String
m_batch
;
public
String
getBatch
()
{
return
m_batch
;
}
public
void
setBatch
(
String
batch
)
{
m_batch
=
batch
;
}
public
double
getValue
()
{
return
m_value
;
}
public
void
setValue
(
double
value
)
{
m_value
=
value
;
m_count
=
(
long
)
value
;
m_count
=
(
long
)
value
;
m_avg
=
value
;
m_sum
=
value
;
}
...
...
log
0 → 100644
浏览文件 @
0067e5f6
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录