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: */package org.apache.geronimo.tomcat.stats;
017:
018: import java.util.Iterator;
019: import java.util.Set;
020:
021: import javax.management.MBeanServer;
022: import javax.management.ObjectInstance;
023: import javax.management.ObjectName;
024: import javax.management.j2ee.statistics.Stats;
025: import org.apache.geronimo.management.geronimo.stats.TomcatWebConnectorStatsImpl;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.tomcat.util.modeler.Registry;
030:
031: /**
032: * This will query MBeanServer and provide jsr77 Stats for connectors.
033: *
034: * @version $Revision: 603663 $ $Date: 2007-12-12 08:23:47 -0800 (Wed, 12 Dec 2007) $
035: */
036: public class ConnectorStats {
037: private static final Log log = LogFactory
038: .getLog(ConnectorStats.class);
039: protected MBeanServer mBeanServer = null;
040:
041: protected Registry registry;
042:
043: private ObjectName grpName;
044:
045: private ObjectName tpName;
046:
047: private TomcatWebConnectorStatsImpl stats = new TomcatWebConnectorStatsImpl();
048:
049: public ConnectorStats() {
050: // Retrieve the MBean server
051: registry = Registry.getRegistry(null, null);
052: mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
053: try {
054: grpName = new ObjectName("*:type=GlobalRequestProcessor,*");
055: tpName = new ObjectName("*:type=ThreadPool,*");
056: } catch (Exception ex) {
057: log.error("Error - " + ex.toString());
058: }
059: }
060:
061: public Stats getStats(String port) {
062: stats.setStartTime();
063: updateStats(stats, port);
064: return stats;
065:
066: }
067:
068: public Stats updateStats(String port) {
069: updateStats(stats, port);
070: return stats;
071:
072: }
073:
074: private void updateStats(TomcatWebConnectorStatsImpl stats,
075: 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,
105: "maxTime"))).longValue();
106: long processingTime = ((Long) (mBeanServer.getAttribute(
107: grpName, "processingTime"))).longValue();
108: int requestCount = ((Integer) (mBeanServer.getAttribute(
109: grpName, "requestCount"))).intValue();
110: int errorCount = ((Integer) (mBeanServer.getAttribute(
111: grpName, "errorCount"))).intValue();
112: long bytesReceived = ((Long) (mBeanServer.getAttribute(
113: grpName, "bytesReceived"))).longValue();
114: long bytesSent = ((Long) (mBeanServer.getAttribute(grpName,
115: "bytesSent"))).longValue();
116: // Tomcat does not keep min Time, using 0 as Undefined value
117: stats.setRequestTime(requestCount, 0, maxTime,
118: processingTime);
119: stats.setErrorCount(errorCount);
120: stats.setBytesSentCount(bytesSent);
121: stats.setBytesReceivedCount(bytesReceived);
122: long openConnections = 0;
123: // TODO find these
124: //long openConnections = ((Long) (mBeanServer.getAttribute(grpName, "countOpenConnections"))).longValue();
125: long maxOpenConnections = 0;
126: //long maxOpenConnections = ((Long) (mBeanServer.getAttribute(grpName, "maxOpenConnections"))).longValue();
127: stats.setOpenConnection(openConnections,
128: maxOpenConnections, 0);
129: // ThreadPool
130: int currentThreadsBusy = ((Integer) (mBeanServer
131: .getAttribute(tpName, "currentThreadsBusy")))
132: .intValue();
133: //stats.setActiveRequestCount(currentThreadsBusy); ??
134: int currentThreadCount = ((Integer) (mBeanServer
135: .getAttribute(tpName, "currentThreadCount")))
136: .intValue();
137: // these are available from "*:type=Connector,*"
138: //int minSpareThreads = ((Integer) (mBeanServer.getAttribute(tpName, "minSpareThreads"))).intValue();
139: //int maxSpareThreads = ((Integer) (mBeanServer.getAttribute(tpName, "maxSpareThreads"))).intValue();
140: int maxThreads = ((Integer) (mBeanServer.getAttribute(
141: tpName, "maxThreads"))).intValue();
142: // keepAliveCount is also available
143: stats.setBusyThreads(currentThreadsBusy,
144: currentThreadCount, 0, maxThreads, 0);
145: // TODO - spareThreads (current-busy, maxSpareThread, minSpareThreads)
146: } catch (Exception ex) {
147: log.error("Error getting attribute " + grpName + " "
148: + ex.toString());
149: }
150:
151: }
152: }
|