001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.tomcat.stats;
019
020 import java.util.Iterator;
021 import java.util.Set;
022
023 import javax.management.MBeanServer;
024 import javax.management.ObjectInstance;
025 import javax.management.ObjectName;
026 import javax.management.j2ee.statistics.Stats;
027 import org.apache.geronimo.management.geronimo.stats.TomcatWebConnectorStatsImpl;
028
029 import org.apache.commons.logging.Log;
030 import org.apache.commons.logging.LogFactory;
031 import org.apache.tomcat.util.modeler.Registry;
032
033 /**
034 * This will query MBeanServer and provide jsr77 Stats for connectors.
035 *
036 * @version $Revision: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
037 */
038 public class ConnectorStats {
039 private static final Log log = LogFactory.getLog(ConnectorStats.class);
040 protected MBeanServer mBeanServer = null;
041
042 protected Registry registry;
043
044 private ObjectName grpName;
045
046 private ObjectName tpName;
047
048 private TomcatWebConnectorStatsImpl stats = new TomcatWebConnectorStatsImpl();
049
050 public ConnectorStats() {
051 // Retrieve the MBean server
052 registry = Registry.getRegistry(null, null);
053 mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
054 try {
055 grpName = new ObjectName("*:type=GlobalRequestProcessor,*");
056 tpName = new ObjectName("*:type=ThreadPool,*");
057 } catch (Exception ex) {
058 log.error("Error - " + ex.toString());
059 }
060 }
061
062 public Stats getStats(String port) {
063 stats.setStartTime();
064 updateStats(stats, port);
065 return stats;
066
067 }
068
069 public Stats updateStats(String port) {
070 updateStats(stats, port);
071 return stats;
072
073 }
074
075 private void updateStats(TomcatWebConnectorStatsImpl stats, String port) {
076 Iterator iterator;
077 Set set;
078 ObjectName objectName;
079 try {
080 // Query Thread Pools
081 set = mBeanServer.queryMBeans(tpName, null);
082 iterator = set.iterator();
083 while (iterator.hasNext()) {
084 ObjectInstance oi = (ObjectInstance) iterator.next();
085 objectName = oi.getObjectName();
086 if (objectName.getKeyProperty("name").indexOf(port) > -1) {
087 tpName = objectName;
088 break;
089 }
090 }
091 // Query Global Request Processors
092 set = mBeanServer.queryMBeans(grpName, null);
093 iterator = set.iterator();
094 while (iterator.hasNext()) {
095 ObjectInstance oi = (ObjectInstance) iterator.next();
096 objectName = oi.getObjectName();
097 if (objectName.getKeyProperty("name").indexOf(port) > -1) {
098 grpName = objectName;
099 break;
100 }
101 }
102 stats.setLastSampleTime();
103 // Any http connector !
104 long maxTime = ((Long) (mBeanServer.getAttribute(grpName, "maxTime"))).longValue();
105 long processingTime = ((Long) (mBeanServer.getAttribute(grpName, "processingTime"))).longValue();
106 int requestCount = ((Integer) (mBeanServer.getAttribute(grpName, "requestCount"))).intValue();
107 int errorCount = ((Integer) (mBeanServer.getAttribute(grpName, "errorCount"))).intValue();
108 long bytesReceived = ((Long) (mBeanServer.getAttribute(grpName, "bytesReceived"))).longValue();
109 long bytesSent = ((Long) (mBeanServer.getAttribute(grpName, "bytesSent"))).longValue();
110 // Tomcat does not keep min Time, using 0 as Undefined value
111 stats.setRequestTime(requestCount, 0, maxTime, processingTime);
112 stats.setErrorCount(errorCount);
113 stats.setBytesSentCount(bytesSent);
114 stats.setBytesReceivedCount(bytesReceived);
115 long openConnections = 0;
116 // TODO find these
117 //long openConnections = ((Long) (mBeanServer.getAttribute(grpName, "countOpenConnections"))).longValue();
118 long maxOpenConnections = 0;
119 //long maxOpenConnections = ((Long) (mBeanServer.getAttribute(grpName, "maxOpenConnections"))).longValue();
120 stats.setOpenConnection(openConnections, maxOpenConnections, 0);
121 // ThreadPool
122 int currentThreadsBusy = ((Integer) (mBeanServer.getAttribute(tpName, "currentThreadsBusy"))).intValue();
123 //stats.setActiveRequestCount(currentThreadsBusy); ??
124 int currentThreadCount = ((Integer) (mBeanServer.getAttribute(tpName, "currentThreadCount"))).intValue();
125 // these are available from "*:type=Connector,*"
126 //int minSpareThreads = ((Integer) (mBeanServer.getAttribute(tpName, "minSpareThreads"))).intValue();
127 //int maxSpareThreads = ((Integer) (mBeanServer.getAttribute(tpName, "maxSpareThreads"))).intValue();
128 int maxThreads = ((Integer) (mBeanServer.getAttribute(tpName, "maxThreads"))).intValue();
129 // keepAliveCount is also available
130 stats.setBusyThreads(currentThreadsBusy, currentThreadCount, 0, maxThreads, 0);
131 // TODO - spareThreads (current-busy, maxSpareThread, minSpareThreads)
132 } catch (Exception ex) {
133 log.error("Error getting attribute " + grpName + " " + ex.toString());
134 }
135
136 }
137 }