From bc83ad4ebce3fef860f5a803b2925687ad74ab7d Mon Sep 17 00:00:00 2001 From: wenshao Date: Thu, 15 Aug 2013 01:30:48 +0800 Subject: [PATCH] monitor client support --- .../http/MonitorClientContextListener.java | 41 ++++ .../druid/support/monitor/MonitorClient.java | 193 ++++++++++++++++-- .../java/com/alibaba/druid/util/Utils.java | 12 ++ 3 files changed, 233 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/alibaba/druid/support/http/MonitorClientContextListener.java diff --git a/src/main/java/com/alibaba/druid/support/http/MonitorClientContextListener.java b/src/main/java/com/alibaba/druid/support/http/MonitorClientContextListener.java new file mode 100644 index 0000000000..1987b9b8fd --- /dev/null +++ b/src/main/java/com/alibaba/druid/support/http/MonitorClientContextListener.java @@ -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; + } + } + +} diff --git a/src/main/java/com/alibaba/druid/support/monitor/MonitorClient.java b/src/main/java/com/alibaba/druid/support/monitor/MonitorClient.java index 47d1b4d5d8..11badc1d39 100644 --- a/src/main/java/com/alibaba/druid/support/monitor/MonitorClient.java +++ b/src/main/java/com/alibaba/druid/support/monitor/MonitorClient.java @@ -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 dataSources = DruidDataSourceStatManager.getInstances().keySet(); List statValueList = new ArrayList(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 statValueList = new ArrayList(); Set 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 webURIValueList = new ArrayList(); List webAppStatValueList = new ArrayList(); @@ -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 loadSqlList(Map 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; + } + } diff --git a/src/main/java/com/alibaba/druid/util/Utils.java b/src/main/java/com/alibaba/druid/util/Utils.java index b939063e3a..984b8ff554 100644 --- a/src/main/java/com/alibaba/druid/util/Utils.java +++ b/src/main/java/com/alibaba/druid/util/Utils.java @@ -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);