mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
monitor client support
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.support.http;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
|
||||
import com.alibaba.druid.support.monitor.MonitorClient;
|
||||
|
||||
public class MonitorClientContextListener implements ServletContextListener {
|
||||
|
||||
private MonitorClient client;
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent event) {
|
||||
client = new MonitorClient();
|
||||
client.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent event) {
|
||||
if (client != null) {
|
||||
client.stop();
|
||||
this.client = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,10 +15,14 @@
|
||||
*/
|
||||
package com.alibaba.druid.support.monitor;
|
||||
|
||||
import static com.alibaba.druid.util.Utils.getBoolean;
|
||||
import static com.alibaba.druid.util.Utils.getInteger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
@@ -51,8 +55,85 @@ public class MonitorClient {
|
||||
private long timeBeetweenWebUriCollect = DEFAULT_TIME_BETWEEN_COLLECT;
|
||||
private TimeUnit timeUnit = TimeUnit.SECONDS;
|
||||
|
||||
private boolean collectSqlEnable = true;
|
||||
private boolean collectSqlWallEnable = true;
|
||||
private boolean collectSpringMethodEanble = true;
|
||||
private boolean collectWebAppEanble = true;
|
||||
private boolean collectWebURIEnable = true;
|
||||
|
||||
private MonitorDao dao;
|
||||
|
||||
public MonitorClient(){
|
||||
configFromPropety(System.getProperties());
|
||||
}
|
||||
|
||||
public void configFromPropety(Properties properties) {
|
||||
{
|
||||
Integer value = getInteger(properties, "druid.monitor.client.schedulerThreadSize");
|
||||
if (value != null) {
|
||||
this.setSchedulerThreadSize(value);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
Integer value = getInteger(properties, "druid.monitor.client.timeBeetweenSqlCollect");
|
||||
if (value != null) {
|
||||
this.setTimeBeetweenSqlCollect(value);
|
||||
}
|
||||
}
|
||||
{
|
||||
Integer value = getInteger(properties, "druid.monitor.client.timeBeetweenSpringCollect");
|
||||
if (value != null) {
|
||||
this.setTimeBeetweenSpringCollect(value);
|
||||
}
|
||||
}
|
||||
{
|
||||
Integer value = getInteger(properties, "druid.monitor.client.timeBeetweenWebUriCollect");
|
||||
if (value != null) {
|
||||
this.setTimeBeetweenWebUriCollect(value);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
Boolean value = getBoolean(properties, "druid.monitor.client.collectSqlEnable");
|
||||
if (value != null) {
|
||||
this.setCollectSqlEnable(value);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
Boolean value = getBoolean(properties, "druid.monitor.client.collectSqlWallEnable");
|
||||
if (value != null) {
|
||||
this.setCollectSqlWallEnable(value);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
Boolean value = getBoolean(properties, "druid.monitor.client.collectSpringMethodEanble");
|
||||
if (value != null) {
|
||||
this.setCollectSpringMethodEanble(value);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
Boolean value = getBoolean(properties, "druid.monitor.client.collectWebAppEanble");
|
||||
if (value != null) {
|
||||
this.setCollectWebAppEanble(value);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
Boolean value = getBoolean(properties, "druid.monitor.client.collectWebURIEnable");
|
||||
if (value != null) {
|
||||
this.setCollectWebURIEnable(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (scheduler == null) {
|
||||
scheduler = new ScheduledThreadPoolExecutor(schedulerThreadSize);
|
||||
@@ -93,6 +174,10 @@ public class MonitorClient {
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
public void collectSql() {
|
||||
if ((!collectSqlEnable) && !collectSqlWallEnable) {
|
||||
return;
|
||||
}
|
||||
|
||||
Set<Object> dataSources = DruidDataSourceStatManager.getInstances().keySet();
|
||||
|
||||
List<DruidDataSourceStatValue> statValueList = new ArrayList<DruidDataSourceStatValue>(dataSources.size());
|
||||
@@ -104,19 +189,29 @@ public class MonitorClient {
|
||||
}
|
||||
|
||||
DruidDataSource dataSource = (DruidDataSource) item;
|
||||
DruidDataSourceStatValue statValue = dataSource.getStatValueAndReset();
|
||||
statValueList.add(statValue);
|
||||
|
||||
dataSource.getWallStatMap();
|
||||
|
||||
WallProviderStatValue wallStatValue = dataSource.getWallStatValue(true);
|
||||
if (wallStatValue != null && wallStatValue.getCheckCount() > 0) {
|
||||
wallStatValueList.add(wallStatValue);
|
||||
if (collectSqlEnable) {
|
||||
DruidDataSourceStatValue statValue = dataSource.getStatValueAndReset();
|
||||
statValueList.add(statValue);
|
||||
}
|
||||
|
||||
if (collectSqlWallEnable) {
|
||||
WallProviderStatValue wallStatValue = dataSource.getWallStatValue(true);
|
||||
if (wallStatValue != null && wallStatValue.getCheckCount() > 0) {
|
||||
wallStatValueList.add(wallStatValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MonitorContext ctx = createContext();
|
||||
dao.saveSql(ctx, statValueList);
|
||||
|
||||
if (statValueList.size() > 0) {
|
||||
dao.saveSql(ctx, statValueList);
|
||||
}
|
||||
|
||||
if (wallStatValueList.size() > 0) {
|
||||
dao.saveSqlWall(ctx, wallStatValueList);
|
||||
}
|
||||
}
|
||||
|
||||
private MonitorContext createContext() {
|
||||
@@ -128,6 +223,10 @@ public class MonitorClient {
|
||||
}
|
||||
|
||||
private void collectSpringMethod() {
|
||||
if (!collectSpringMethodEanble) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<SpringMethodStatValue> statValueList = new ArrayList<SpringMethodStatValue>();
|
||||
|
||||
Set<Object> stats = SpringStatManager.getInstance().getSpringStatSet();
|
||||
@@ -141,11 +240,17 @@ public class MonitorClient {
|
||||
statValueList.addAll(sprintStat.getStatList(true));
|
||||
}
|
||||
|
||||
MonitorContext ctx = createContext();
|
||||
dao.saveSpringMethod(ctx, statValueList);
|
||||
if (statValueList.size() > 0) {
|
||||
MonitorContext ctx = createContext();
|
||||
dao.saveSpringMethod(ctx, statValueList);
|
||||
}
|
||||
}
|
||||
|
||||
private void collectWebURI() {
|
||||
if ((!collectWebAppEanble) && !collectWebURIEnable) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<WebURIStatValue> webURIValueList = new ArrayList<WebURIStatValue>();
|
||||
List<WebAppStatValue> webAppStatValueList = new ArrayList<WebAppStatValue>();
|
||||
|
||||
@@ -157,12 +262,26 @@ public class MonitorClient {
|
||||
}
|
||||
|
||||
WebAppStat webAppStat = (WebAppStat) item;
|
||||
webURIValueList.addAll(webAppStat.getURIStatValueList(true));
|
||||
|
||||
if (collectWebAppEanble) {
|
||||
WebAppStatValue webAppStatValue = webAppStat.getStatValue(true);
|
||||
webAppStatValueList.add(webAppStatValue);
|
||||
}
|
||||
|
||||
if (collectWebURIEnable) {
|
||||
webURIValueList.addAll(webAppStat.getURIStatValueList(true));
|
||||
}
|
||||
}
|
||||
|
||||
MonitorContext ctx = createContext();
|
||||
dao.saveWebURI(ctx, webURIValueList);
|
||||
dao.saveWebApp(ctx, webAppStatValueList);
|
||||
|
||||
if (webURIValueList.size() > 0) {
|
||||
dao.saveWebURI(ctx, webURIValueList);
|
||||
}
|
||||
|
||||
if (webAppStatValueList.size() > 0) {
|
||||
dao.saveWebApp(ctx, webAppStatValueList);
|
||||
}
|
||||
}
|
||||
|
||||
public List<JdbcSqlStatValue> loadSqlList(Map<String, Object> filters) {
|
||||
@@ -209,4 +328,52 @@ public class MonitorClient {
|
||||
this.timeUnit = timeUnit;
|
||||
}
|
||||
|
||||
public boolean isCollectSqlEnable() {
|
||||
return collectSqlEnable;
|
||||
}
|
||||
|
||||
public void setCollectSqlEnable(boolean collectSqlEnable) {
|
||||
this.collectSqlEnable = collectSqlEnable;
|
||||
}
|
||||
|
||||
public boolean isCollectSqlWallEnable() {
|
||||
return collectSqlWallEnable;
|
||||
}
|
||||
|
||||
public void setCollectSqlWallEnable(boolean collectSqlWallEnable) {
|
||||
this.collectSqlWallEnable = collectSqlWallEnable;
|
||||
}
|
||||
|
||||
public boolean isCollectSpringMethodEanble() {
|
||||
return collectSpringMethodEanble;
|
||||
}
|
||||
|
||||
public void setCollectSpringMethodEanble(boolean collectSpringMethodEanble) {
|
||||
this.collectSpringMethodEanble = collectSpringMethodEanble;
|
||||
}
|
||||
|
||||
public boolean isCollectWebAppEanble() {
|
||||
return collectWebAppEanble;
|
||||
}
|
||||
|
||||
public void setCollectWebAppEanble(boolean collectWebAppEanble) {
|
||||
this.collectWebAppEanble = collectWebAppEanble;
|
||||
}
|
||||
|
||||
public boolean isCollectWebURIEnable() {
|
||||
return collectWebURIEnable;
|
||||
}
|
||||
|
||||
public void setCollectWebURIEnable(boolean collectWebURIEnable) {
|
||||
this.collectWebURIEnable = collectWebURIEnable;
|
||||
}
|
||||
|
||||
public int getSchedulerThreadSize() {
|
||||
return schedulerThreadSize;
|
||||
}
|
||||
|
||||
public void setSchedulerThreadSize(int schedulerThreadSize) {
|
||||
this.schedulerThreadSize = schedulerThreadSize;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.servlet.GenericServlet;
|
||||
|
||||
public class Utils {
|
||||
|
||||
public final static int DEFAULT_BUFFER_SIZE = 1024 * 4;
|
||||
@@ -168,6 +170,16 @@ public class Utils {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Boolean getBoolean(GenericServlet servlet, String key) {
|
||||
String property = servlet.getInitParameter(key);
|
||||
if ("true".equals(property)) {
|
||||
return Boolean.TRUE;
|
||||
} else if ("false".equals(property)) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Integer getInteger(Properties properties, String key) {
|
||||
String property = properties.getProperty(key);
|
||||
|
||||
Reference in New Issue
Block a user