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    
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    import org.apache.tomcat.util.modeler.Registry;
031    import org.apache.geronimo.management.stats.WebConnectorStatsImpl;
032    
033    /**
034     * This will query MBeanServer and provide jsr77 Stats for connectors.
035     * 
036     * @version $Revision: 511136 $ $Date: 2007-02-23 17:12:09 -0500 (Fri, 23 Feb 2007) $
037     */
038    public class ConnectorStats {
039        private static final Log log = LogFactory.getLog(ModuleStats.class);
040        protected MBeanServer mBeanServer = null;
041    
042        protected Registry registry;
043    
044        private ObjectName grpName;
045    
046        private ObjectName tpName;
047    
048        private WebConnectorStatsImpl stats = new WebConnectorStatsImpl();
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(WebConnectorStatsImpl 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                stats.setRequestTime(requestCount, -1, maxTime, processingTime);
111                stats.setErrorCount(errorCount);            
112                stats.setBytesSentCount(bytesSent);
113                stats.setBytesReceivedCount(bytesReceived);
114                long openConnections = 0;
115                // TODO find these
116                //long openConnections = ((Long) (mBeanServer.getAttribute(grpName, "countOpenConnections"))).longValue();
117                long maxOpenConnections = 0;
118                //long maxOpenConnections = ((Long) (mBeanServer.getAttribute(grpName, "maxOpenConnections"))).longValue();
119                stats.setOpenConnection(openConnections, maxOpenConnections, 0);
120                // ThreadPool 
121                int currentThreadsBusy = ((Integer) (mBeanServer.getAttribute(tpName, "currentThreadsBusy"))).intValue();
122                //stats.setActiveRequestCount(currentThreadsBusy); ??
123                int currentThreadCount = ((Integer) (mBeanServer.getAttribute(tpName, "currentThreadCount"))).intValue();
124                // these are available from "*:type=Connector,*"
125                //int minSpareThreads = ((Integer) (mBeanServer.getAttribute(tpName, "minSpareThreads"))).intValue();
126                //int maxSpareThreads = ((Integer) (mBeanServer.getAttribute(tpName, "maxSpareThreads"))).intValue();
127                int maxThreads = ((Integer) (mBeanServer.getAttribute(tpName, "maxThreads"))).intValue();
128                // keepAliveCount is also available
129                stats.setBusyThreads(currentThreadsBusy, currentThreadCount, 0, maxThreads, 0);
130                // TODO - spareThreads (current-busy, maxSpareThread, minSpareThreads)
131            } catch (Exception ex) {
132                log.error("Error getting attribute " + grpName + " " + ex.toString());
133            }
134    
135        }
136    }