You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jb...@apache.org on 2007/10/23 23:37:22 UTC
svn commit: r587666 - in /geronimo/sandbox/monitoring/mrc-server: ./
mrc-ejb/src/main/java/org/apache/geronimo/monitor/
mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/
Author: jbohn
Date: Tue Oct 23 14:37:21 2007
New Revision: 587666
URL: http://svn.apache.org/viewvc?rev=587666&view=rev
Log:
GERONIMO-3541 - use DB to store snapshots rather than xml files - patch by Viet Nguyen
Added:
geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotDBHelper.java (with props)
Modified:
geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControl.java
geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControlRemote.java
geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/SMP.java
geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotThread.java
geronimo/sandbox/monitoring/mrc-server/readme.txt
Modified: geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControl.java?rev=587666&r1=587665&r2=587666&view=diff
==============================================================================
--- geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControl.java (original)
+++ geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControl.java Tue Oct 23 14:37:21 2007
@@ -29,6 +29,7 @@
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
+import java.util.TreeMap;
import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
@@ -63,8 +64,8 @@
import org.apache.geronimo.management.geronimo.stats.WebModuleStats;
import org.apache.geronimo.management.stats.WebModuleStatsImpl;
+import org.apache.geronimo.monitor.snapshot.SnapshotDBHelper;
import org.apache.geronimo.monitor.snapshot.SnapshotThread;
-import org.apache.geronimo.monitor.snapshot.SnapshotXMLBuilder;
import org.apache.geronimo.monitor.snapshot.SnapshotConfigXMLBuilder;
/**
@@ -90,6 +91,9 @@
// threads
private static SnapshotThread snapshotThread = null;
+ // credentials for snapshot thread
+ private static String username = null;
+ private static String password = null;
// use this to call utility functions pertaining to mbeans
private static MBeanHelper mbeanHelper;
@@ -128,10 +132,11 @@
}
private void setUpDatabase() {
+ Connection conn = null;
try {
// TODO:
// Use a DataSource instead.
- Connection conn = DriverManager.getConnection("jdbc:derby:ActiveMRCDB;create=true", "monitor", "monitor");
+ conn = DriverManager.getConnection("jdbc:derby:ActiveMRCDB;create=true", "monitor", "monitor");
Statement stmt = conn.createStatement();
try {
// fetch all tables in the db
@@ -151,15 +156,40 @@
}
} catch(SQLException e) {
e.printStackTrace();
+ } finally {
+ try {
+ if(conn!= null) {
+ conn.close();
+ }
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
}
}
+ /**
+ * Creates a Statistics table in a DB that is associated with the Statement.
+ * @param stmt
+ */
private void createTables(Statement stmt) {
try {
- String tableStatisticsCreate = "CREATE TABLE Statistics( id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), mbeanId INT NOT NULL, statsValue BIGINT NOT NULL, statsName VARCHAR(100) NOT NULL, snapshot_time VARCHAR(20) NOT NULL, snapshot_date DATE NOT NULL, PRIMARY KEY(id))";
- String tableMBeanCreate = "CREATE TABLE MBeans( id INT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), name VARCHAR(256) NOT NULL, PRIMARY KEY(id) )";
- stmt.executeUpdate(tableMBeanCreate);
- System.out.println("MBean Table Created");
+ String tableMBeansCreate = "CREATE TABLE MBeans( id INT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), mbeanName VARCHAR(256) NOT NULL, PRIMARY KEY(id) )";
+ String tableSnapshotsCreate = "CREATE TABLE Snapshots(id INT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), snapshot_time BIGINT NOT NULL, snapshot_date VARCHAR(30) NOT NULL, PRIMARY KEY(id))";
+// String tableStatisticsCreate = "CREATE TABLE Statistics( id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), mbeanName VARCHAR(256) NOT NULL, statsValue BIGINT NOT NULL, statsName VARCHAR(100) NOT NULL, snapshot_time BIGINT NOT NULL, snapshot_date VARCHAR(30) NOT NULL, PRIMARY KEY(id))";
+ String tableStatisticsCreate = "CREATE TABLE Statistics (";
+ tableStatisticsCreate += "id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),";
+ tableStatisticsCreate += "mbeanId INT NOT NULL,";
+ tableStatisticsCreate += "snapshotId INT NOT NULL,";
+ tableStatisticsCreate += "statsValue BIGINT NOT NULL,";
+ tableStatisticsCreate += "statsName VARCHAR(100) NOT NULL,";
+ tableStatisticsCreate += "PRIMARY KEY(id),";
+ tableStatisticsCreate += "FOREIGN KEY (mbeanId) REFERENCES MBeans(id),";
+ tableStatisticsCreate += "FOREIGN KEY (snapshotId) REFERENCES Snapshots(id)";
+ tableStatisticsCreate += ")";
+ stmt.executeUpdate(tableMBeansCreate);
+ System.out.println("MBeans Table Created");
+ stmt.executeUpdate(tableSnapshotsCreate);
+ System.out.println("Snapshots Table Created");
stmt.executeUpdate(tableStatisticsCreate);
System.out.println("Statistics Table Created");
} catch(Exception e) {
@@ -167,8 +197,11 @@
}
}
+ /**
+ * Retrieves and instance of the MEJB and starts the snapshot process
+ */
@RolesAllowed("mejbadmin")
- public void setUpMEJB() {
+ public void setUpMEJB(String username, String password) {
// instantiate the MEJB, which will be our gateway to communicate to MBeans
try {
Properties p = new Properties();
@@ -177,9 +210,13 @@
ManagementHome mejbHome = (ManagementHome)ctx.lookup("ejb/mgmt/MEJBRemoteHome");
mejb = mejbHome.create();
+
+ // save credentials
+ this.username = username;
+ this.password = password;
// start the snapshot process
- startSnapshot(DEFAULT_DURATION);
+ // startSnapshot(fetchSnapshotDuration());
// Uncomment to see if mejb is working well
//System.out.println("SUCCESS!");
@@ -232,15 +269,6 @@
}
/**
- * Returns the handle of an MEJB.
- *
- * @return Handle of an MEJB
- */
- public static Management getMEJB() {
- return mejb;
- }
-
- /**
* Changes the objectName's attrName's value to attrValue
*
* @param objectName
@@ -266,7 +294,11 @@
public boolean startSnapshot(Long interval) {
if((snapshotThread == null || (snapshotThread != null && !snapshotThread.isAlive())) &&
interval.longValue() > 0) {
- snapshotThread = new SnapshotThread(interval.longValue(), mbServer, getStatisticsProviderMBeanNames());
+ snapshotThread = new SnapshotThread(interval.longValue(),
+ mbServer,
+ getStatisticsProviderMBeanNames(),
+ username,
+ password);
snapshotThread.start();
saveDuration(interval.longValue());
return true;
@@ -291,7 +323,7 @@
snapshotThread.interrupt();
return true;
} else {
- System.out.println("There is not a snapshot thread running. Stopping aborted.");
+ System.out.println("There is not a snapshot thread running.");
//log.error("There is not a snapshot thread running. Stopping aborted.");
return false;
}
@@ -312,10 +344,7 @@
*/
@RolesAllowed("mejbadmin")
public ArrayList fetchSnapshotData(Integer numberOfSnapshot, Integer everyNthSnapshot) {
- SnapshotXMLBuilder snapshotXMLBuilder = new SnapshotXMLBuilder();
- snapshotXMLBuilder.openDocument();
- ArrayList retval = snapshotXMLBuilder.fetchData(numberOfSnapshot, everyNthSnapshot);
- snapshotXMLBuilder.saveDocument( snapshotXMLBuilder.getDocument() );
+ ArrayList retval = (ArrayList)org.apache.geronimo.monitor.snapshot.SnapshotDBHelper.fetchData(numberOfSnapshot, everyNthSnapshot);
return retval;
}
@@ -328,10 +357,7 @@
*/
@RolesAllowed("mejbadmin")
public HashMap fetchMaxSnapshotData(Integer numberOfSnapshot) {
- SnapshotXMLBuilder snapshotXMLBuilder = new SnapshotXMLBuilder();
- snapshotXMLBuilder.openDocument();
- HashMap retval = snapshotXMLBuilder.fetchMaxSnapshotData(numberOfSnapshot);
- snapshotXMLBuilder.saveDocument( snapshotXMLBuilder.getDocument() );
+ HashMap retval = (HashMap)org.apache.geronimo.monitor.snapshot.SnapshotDBHelper.fetchMaxSnapshotData(numberOfSnapshot);
return retval;
}
@@ -344,10 +370,7 @@
*/
@RolesAllowed("mejbadmin")
public HashMap fetchMinSnapshotData(Integer numberOfSnapshot) {
- SnapshotXMLBuilder snapshotXMLBuilder = new SnapshotXMLBuilder();
- snapshotXMLBuilder.openDocument();
- HashMap retval = snapshotXMLBuilder.fetchMinSnapshotData(numberOfSnapshot);
- snapshotXMLBuilder.saveDocument( snapshotXMLBuilder.getDocument() );
+ HashMap retval = (HashMap)org.apache.geronimo.monitor.snapshot.SnapshotDBHelper.fetchMinSnapshotData(numberOfSnapshot);
return retval;
}
@@ -361,7 +384,8 @@
if(snapshotThread != null) {
return new Long(snapshotThread.getSnapshotDuration());
} else {
- return null;
+ // return what is stored in the snapshot-config.xml or default value
+ return fetchSnapshotDuration();
}
}
@@ -382,7 +406,7 @@
@RolesAllowed("mejbadmin")
public Long getSnapshotCount() {
- return SnapshotXMLBuilder.getSnapshotCount();
+ return org.apache.geronimo.monitor.snapshot.SnapshotDBHelper.getSnapshotCount();
}
/**
@@ -411,27 +435,6 @@
}
}
- public void doFail() {
- //log.warn("Failed");
- stopSnapshot();
- }
-
- /**
- * Executes when the GBean starts up. Also starts the snapshot thread.
- */
- public void doStart() {
- //log.debug("Started");
- startSnapshot( fetchSnapshotDuration() );
- }
-
- /**
- * Executes when the GBean stops. Also stops the snapshot thread.
- */
- public void doStop() {
- //log.debug("Stopped");
- stopSnapshot();
- }
-
@RolesAllowed("mejbadmin")
private void saveDuration(long duration) {
SnapshotConfigXMLBuilder.save(duration);
@@ -477,7 +480,7 @@
*/
@RolesAllowed("mejbadmin")
public HashMap<String, ArrayList<String>> getAllSnapshotStatAttributes() {
- return SnapshotXMLBuilder.getAllSnapshotStatAttributes();
+ return org.apache.geronimo.monitor.snapshot.SnapshotDBHelper.getAllSnapshotStatAttributes();
}
/**
@@ -485,7 +488,35 @@
*/
@RolesAllowed("mejbadmin")
public boolean isSnapshotRunning() {
- // snapshot is running when the duration is not infinity
- return !snapshotThread.getEnd();
+ if(snapshotThread == null) {
+ return false;
+ } else {
+ // snapshot is running when snapshotThread has not ended
+ return !snapshotThread.getEnd();
+ }
+ }
+
+ /**
+ * @param name - object name of the mbean
+ * @param operationName - method within the class
+ * @param params - parameters for the method
+ * @param signature - types for the parameters
+ * @return Invokes the method of a class defined.
+ */
+ @RolesAllowed("mejbadmin")
+ public Object invoke(ObjectName name, String operationName, Object[] params, String[] signature) throws Exception {
+ return mejb.invoke(name, operationName, params, signature);
+ }
+
+ /**
+ * @param mbeanName
+ * @param statsName
+ * @param numberOfSnapshots
+ * @param everyNthSnapshot
+ * @return HashMap which maps from a snapshot_time --> value of the mbean.statsName at that time
+ */
+ @RolesAllowed("mejbadmin")
+ public TreeMap<Long, Long> getSpecificStatistics(String mbeanName, String statsName, int numberOfSnapshots, int everyNthSnapshot) {
+ return SnapshotDBHelper.getSpecificStatistics(mbeanName, statsName, numberOfSnapshots, everyNthSnapshot);
}
-}
+}
\ No newline at end of file
Modified: geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControlRemote.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControlRemote.java?rev=587666&r1=587665&r2=587666&view=diff
==============================================================================
--- geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControlRemote.java (original)
+++ geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControlRemote.java Tue Oct 23 14:37:21 2007
@@ -20,9 +20,11 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
+import java.util.TreeMap;
import javax.annotation.security.RolesAllowed;
import javax.ejb.Remote;
+import javax.management.ObjectName;
/**
* Remote Interface for MasterRemoteControl. Defines the operations
@@ -64,6 +66,10 @@
@RolesAllowed("mejbadmin")
public boolean isSnapshotRunning();
@RolesAllowed("mejbadmin")
- public void setUpMEJB();
-
+ public void setUpMEJB(String username, String password);
+ @RolesAllowed("mejbadmin")
+ public Object invoke(ObjectName name, String operationName, Object[] params, String[] signature);
+ @RolesAllowed("mejbadmin")
+ public TreeMap<Long, Long> getSpecificStatistics(String mbeanName, String statsName, int numberOfSnapshots, int everyNthSnapshot);
+
}
Modified: geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/SMP.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/SMP.java?rev=587666&r1=587665&r2=587666&view=diff
==============================================================================
--- geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/SMP.java (original)
+++ geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/SMP.java Tue Oct 23 14:37:21 2007
@@ -23,6 +23,7 @@
import java.util.Hashtable;
import java.util.Properties;
import java.util.Set;
+import java.util.TreeMap;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
@@ -45,19 +46,21 @@
public class SMP {
private static String PATH = "";
+ private static final String username = "system";
+ private static final String password = "manager";
public static void main(String[] args) {
try {
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.openejb.client.RemoteInitialContextFactory");
- props.setProperty(Context.PROVIDER_URL, "127.0.0.1:4201");
- props.setProperty(Context.SECURITY_PRINCIPAL, "system");
- props.setProperty(Context.SECURITY_CREDENTIALS, "manager");
+ props.setProperty(Context.PROVIDER_URL, "ejbd://localhost:4201");
+ props.setProperty(Context.SECURITY_PRINCIPAL, username);
+ props.setProperty(Context.SECURITY_CREDENTIALS, password);
props.setProperty("openejb.authentication.realmName", "geronimo-admin");
Context ic = new InitialContext(props);
System.out.println("Success: Initialized InitialContext");
MasterRemoteControlRemote mrc = (MasterRemoteControlRemote)ic.lookup("ejb/mgmt/MRCRemote");
System.out.println("Success: look up MRCRemote");
- mrc.setUpMEJB();
+ mrc.setUpMEJB(username, password);
System.out.println("Success: setUpMEJB");
/*
JMXServiceURL serviceURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/JMXConnector");
@@ -107,10 +110,96 @@
// MasterRemoteControl mrc;
//System.out.println(m.getDefaultDomain());
testPrintStatisticsProviderMBeans(mrc);
- testPrintStats(mrc);
+// testPrintStats(mrc);
+ testFetchSnapshot(mrc, 2, 2);
+ testFetchMinSnapshot(mrc, 2);
+ testFetchMaxSnapshot(mrc, 2);
+ testFetchSnapshotCount(mrc);
+ testGetAllSnapshotAttributes(mrc);
+ testGetSpecificStats(mrc, "geronimo:J2EEServer=geronimo,j2eeType=JVM,name=JVM", "JVM Heap Size Current", 5, 2);
} catch (Exception e) {
e.printStackTrace();
}
+ }
+
+ private static void testGetSpecificStats(MasterRemoteControlRemote mrc, String mbeanName, String statsValue, int x, int y) {
+ TreeMap<Long, Long> retval = mrc.getSpecificStatistics(mbeanName, statsValue, x, y);
+ for(Iterator it = retval.keySet().iterator(); it.hasNext(); ) {
+ Long snapshot_time = (Long)it.next();
+ System.out.println(snapshot_time + " : " + retval.get(snapshot_time));
+ }
+ }
+
+ private static void testGetAllSnapshotAttributes(MasterRemoteControlRemote mrc) {
+ HashMap<String, ArrayList<String>> retval = mrc.getAllSnapshotStatAttributes();
+ for(Iterator it = retval.keySet().iterator(); it.hasNext(); ) {
+ String mbeanName = (String)it.next();
+ ArrayList<String> list = retval.get(mbeanName);
+ System.out.println(mbeanName);
+ for(int i = 0; i < list.size(); i++) {
+ System.out.println(list.get(i));
+ }
+ System.out.println("---------------------------------");
+ }
+ }
+
+ private static void testFetchSnapshotCount(MasterRemoteControlRemote mrc) {
+ System.out.println("Snapshot Count: " + mrc.getSnapshotCount());
+ }
+
+ private static void testFetchMaxSnapshot(MasterRemoteControlRemote mrc, int x) {
+ System.out.println("testFetchMaxSnapshot");
+ HashMap<String, HashMap<String, Long>> stats = mrc.fetchMaxSnapshotData(x);
+ for(Iterator itt = stats.keySet().iterator(); itt.hasNext(); ) {
+ String mbean = (String)itt.next();
+ HashMap<String, Long> s = stats.get(mbean);
+ System.out.println(mbean);
+ for(Iterator it = s.keySet().iterator(); it.hasNext(); ) {
+ String statName = (String)it.next();
+ Long statValue = (Long)s.get(statName);
+ System.out.println(statName + " : " + statValue);
+ }
+ System.out.println("------------------------------------");
+ }
+ }
+
+ private static void testFetchMinSnapshot(MasterRemoteControlRemote mrc, int x) {
+ System.out.println("testFetchMinSnapshot");
+ HashMap<String, HashMap<String, Long>> stats = mrc.fetchMinSnapshotData(x);
+ for(Iterator itt = stats.keySet().iterator(); itt.hasNext(); ) {
+ String mbean = (String)itt.next();
+ HashMap<String, Long> s = stats.get(mbean);
+ System.out.println(mbean);
+ for(Iterator it = s.keySet().iterator(); it.hasNext(); ) {
+ String statName = (String)it.next();
+ Long statValue = (Long)s.get(statName);
+ System.out.println(statName + " : " + statValue);
+ }
+ System.out.println("------------------------------------");
+ }
+ }
+
+ private static void testFetchSnapshot(MasterRemoteControlRemote mrc, int x, int y) {
+ System.out.println("testFetchSnapshot");
+ ArrayList<HashMap<String, HashMap<String, Object>>> allSnapshots = (ArrayList)mrc.fetchSnapshotData(new Integer(x), new Integer(y));
+ // for each snapshot
+ for(int i = 0; i < allSnapshots.size(); i++) {
+ System.out.println();
+ HashMap<String, HashMap<String, Object>> snapshot = allSnapshots.get(i);
+ // for each mbean
+ for(Iterator it = snapshot.keySet().iterator(); it.hasNext(); ) {
+ String mbeanName = (String)it.next();
+ System.out.println(mbeanName);
+ HashMap<String, Object> stats = snapshot.get(mbeanName);
+ // for each attribute
+ for(Iterator itt = stats.keySet().iterator(); itt.hasNext(); ) {
+ String key = (String)itt.next();
+ System.out.println(key + " : " + stats.get(key));
+ }
+ }
+ System.out.println("-----------------------------------------");
+ }
+
}
private static void testPrintStats(MasterRemoteControlRemote mrc) {
Added: geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotDBHelper.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotDBHelper.java?rev=587666&view=auto
==============================================================================
--- geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotDBHelper.java (added)
+++ geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotDBHelper.java Tue Oct 23 14:37:21 2007
@@ -0,0 +1,475 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.geronimo.monitor.snapshot;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.TreeMap;
+
+import javax.sql.DataSource;
+
+public class SnapshotDBHelper {
+ // field attributes for the Statistics table in the DB
+ private static final String SNAPSHOT_TIME = "snapshot_time";
+ private static final String SNAPSHOT_DATE = "snapshot_date";
+ private static final String MBEANNAME = "mbeanName";
+ private static final String STATSVALUE = "statsValue";
+ private static final String STATSNAME = "statsName";
+ // Connection object used for DB interaction
+ private static Connection conn = null;
+
+ /**
+ * @return A map: mbeanName --> ArrayList of statistic attributes for that mbean
+ */
+ public static HashMap<String, ArrayList<String>> getAllSnapshotStatAttributes() {
+ openConnection();
+ HashMap<String, ArrayList<String>> retval = new HashMap<String, ArrayList<String>>();
+ try {
+ Statement stmt = conn.createStatement();
+ String query = "SELECT DISTINCT M.mbeanName, S.statsName FROM Statistics S, MBeans M WHERE M.id=S.mbeanId";
+ ResultSet rs = stmt.executeQuery(query);
+ // add each mbean/statsValue combination to retval
+ while(rs.next()) {
+ String mbeanName = rs.getString(MBEANNAME);
+ String statsValue = rs.getString(STATSNAME);
+ ArrayList<String> mbeanAttributeList = retval.get(mbeanName);
+ if(mbeanAttributeList == null) {
+ mbeanAttributeList = new ArrayList<String>();
+ }
+ mbeanAttributeList.add(statsValue);
+ retval.put(mbeanName, mbeanAttributeList);
+ }
+ } catch(Exception e) {
+ e.printStackTrace();
+ } finally {
+ closeConnection();
+ }
+ return retval;
+ }
+
+ /**
+ *
+ * @return The number of snapshots present in the active database
+ */
+ public static Long getSnapshotCount() {
+ long retval = 0;
+ try {
+ openConnection();
+ Statement stmt = conn.createStatement();
+ String query = "SELECT COUNT(DISTINCT snapshot_time) FROM Snapshots";
+ ResultSet rs = stmt.executeQuery(query);
+ rs.next();
+ retval = rs.getLong(1);
+ } catch(Exception e) {
+ e.printStackTrace();
+ } finally {
+ closeConnection();
+ }
+ return new Long(retval);
+ }
+
+ /**
+ * @param numberOfSnapshots - the number of latest snapshots to look at
+ * @return A hashmap which maps an mbean --> a hashmap with an attribute name and its value . All values will be the max.
+ */
+ public static HashMap<String, HashMap<String, Long>> fetchMaxSnapshotData(Integer numberOfSnapshots) {
+ return fetchMaxOrMinSnapshotData(numberOfSnapshots, true);
+ }
+ /**
+ * @param numberOfSnapshots - the number of latest snapshots to look at
+ * @return A hashmap which maps an mbean --> a hashmap with an attribute name and its value . All values will be the min.
+ */
+ public static HashMap<String, HashMap<String, Long>> fetchMinSnapshotData(Integer numberOfSnapshots) {
+ return fetchMaxOrMinSnapshotData(numberOfSnapshots, false);
+ }
+
+ /**
+ * @param numberOfSnapshots - the number of latest snapshots to look at.
+ * @param isMax - true if the result should be all maximum values. otherwise, false.
+ * @return A hashmap which maps an mbean --> a hashmap with an attribute name and its value . All values will be the min
+ * or max, depending on the isMax parameter.
+ */
+ private static HashMap<String, HashMap<String, Long>> fetchMaxOrMinSnapshotData(Integer numberOfSnapshots, boolean isMax) {
+ ResultSet snapshotTimeTable = fetchSnapshotTimesFromDB();
+ HashMap<String, HashMap<String, Long>> stats = new HashMap<String, HashMap<String, Long>>();
+ try {
+ // for each snapshot in the table
+ while(snapshotTimeTable.next()) {
+ Long snapshotTime = snapshotTimeTable.getLong(SNAPSHOT_TIME);
+ // retrieve the snapshot information by time
+ ResultSet snapshotData = fetchSnapshotDataFromDB(snapshotTime);
+ // for each statistic, perform a relaxation
+ while(snapshotData.next()) {
+ String mbean = snapshotData.getString(MBEANNAME);
+ // get map associated with mbean
+ HashMap<String, Long> mbeanMap = stats.get(mbean);
+ if(mbeanMap == null) {
+ mbeanMap = new HashMap<String, Long>();
+ }
+ // compute the statsName and maximum statValue
+ String statsName = snapshotData.getString(STATSNAME);
+ Long statsValue = mbeanMap.get(statsName);
+ if(statsValue == null) {
+ if(isMax) {
+ statsValue = new Long(0);
+ } else {
+ statsValue = Long.MAX_VALUE;
+ }
+ }
+ if(isMax) {
+ statsValue = new Long(Math.max(Long.parseLong(snapshotData.getString(STATSVALUE)), statsValue.longValue()));
+ } else {
+ statsValue = new Long(Math.min(Long.parseLong(snapshotData.getString(STATSVALUE)), statsValue.longValue()));
+ }
+ // relax
+ mbeanMap.put(statsName, statsValue);
+ // save mbeanMap back into stats
+ stats.put(mbean, mbeanMap);
+ }
+
+ // compute the remaining snapshots left to look at
+ numberOfSnapshots--;
+ // discontinue once we have looked at numberOfSnapshots snapshots
+ if(numberOfSnapshots == 0) {
+ break;
+ }
+ }
+ } catch(Exception e) {
+ e.printStackTrace();
+ } finally {
+ closeConnection();
+ }
+ return stats;
+ }
+
+ /**
+ * @param ds
+ * @param aggregateStats
+ * @return Returns a boolean if the snapshot statistics were successfully added
+ * to the DB.
+ */
+ public static boolean addSnapshotToDB( DataSource ds,
+ HashMap<String, HashMap> aggregateStats) {
+ boolean success = true;
+ try {
+ openConnection();
+ Statement stmt = conn.createStatement();
+
+ // get the current date
+ Calendar cal = Calendar.getInstance();
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ String currDate = sdf.format(cal.getTime());
+
+ // get the current time from 1970
+ String currTime = "";
+ currTime += (new Date()).getTime();
+
+ // for each mbean
+ for(Iterator itt = aggregateStats.keySet().iterator(); itt.hasNext(); ) {
+ String mbean = (String)itt.next();
+ HashMap stats = aggregateStats.get(mbean);
+ System.out.println(mbean);
+ //--------Ensure MBeans are in place
+ int mbeanId = getMBeanId(mbean);
+ if(mbeanId != -1) {
+ // mbean already exists in the db
+ } else {
+ // doesn't exist in the db so add it
+ // add mbean record to the db
+ stmt.executeUpdate("INSERT INTO MBeans (mbeanName) VALUES ("+ surroundWithQuotes(mbean) + ")");
+ mbeanId = getMBeanId(mbean);
+ }
+ //--------Ensure Snapshots are in place
+ int snapshotId = getSnapshotId(currTime);
+ if(snapshotId != -1) {
+ // snapshot already exists in the db
+ } else {
+ // doesn't exist in the db
+ // add snapshot time record to the db
+ stmt.executeUpdate("INSERT INTO Snapshots (snapshot_time, snapshot_date) VALUES (" + currTime + ", " + surroundWithQuotes(currDate) +")");
+ snapshotId = getSnapshotId(currTime);
+ }
+ // for each stat within an mbean
+ for(Iterator it = stats.keySet().iterator(); it.hasNext(); ) {
+ String key = (String)it.next();
+ Long value = (Long)stats.get(key);
+ //--------Ensure Statistics are in place
+ stmt.executeUpdate( prepareInsertStatement(key, value, snapshotId, mbeanId) );
+ }
+ }
+ } catch(Exception e){
+ e.printStackTrace();
+ success = false;
+ } finally {
+ closeConnection();
+ }
+
+ return success;
+ }
+
+ /**
+ * @param snapshot_time
+ * @return The snapshot id of the snapshot time from table Snapshots. Returns -1 if record does not exist.
+ */
+ private static int getSnapshotId(String snapshot_time) throws Exception {
+ Statement stmt = conn.createStatement();
+ ResultSet rs = stmt.executeQuery("SELECT id FROM Snapshots WHERE snapshot_time=" + snapshot_time);
+ if(rs.next()) {
+ return rs.getInt("id");
+ } else {
+ return -1;
+ }
+ }
+
+ /**
+ * @param mbean
+ * @return The mbean id of the mbean from table MBean. Returns -1 if record does not exist.
+ */
+ private static int getMBeanId(String mbean) throws Exception {
+ Statement stmt = conn.createStatement();
+ ResultSet rs = stmt.executeQuery("SELECT id FROM MBeans WHERE mbeanName=" + surroundWithQuotes(mbean));
+ if(rs.next()) {
+ return rs.getInt("id");
+ } else {
+ return -1;
+ }
+ }
+
+ /**
+ * @param statsName
+ * @param statsValue
+ * @param currDate
+ * @param currTime
+ * @param mbean
+ * @return Returns an SQL insert statement for one statistic given the correct information.
+ */
+ public static String prepareInsertStatement(String statsName, Long statsValue, int snapshotId, int mbeanId) {
+ String retval = "INSERT INTO Statistics (statsName, statsValue, snapshotId, mbeanId) VALUES (";
+ retval += surroundWithQuotes(statsName);
+ retval += ",";
+ retval += statsValue;
+ retval += ",";
+ retval += snapshotId;
+ retval += ",";
+ retval += mbeanId;
+ retval += ")";
+ return retval;
+ }
+
+ /**
+ * @param s
+ * @return A String with ' at the beginning and end
+ */
+ private static String surroundWithQuotes(String s) {
+ return "'" + s.trim() + "'";
+ }
+
+ /**
+ * Fetches the data stored from the snapshot thread and returns
+ * it in a ArrayList with each element being a HashMap of the attribute
+ * mapping to the statistic. Grabs 'numberOfSnapshots' snapshots. Grabs
+ * one snapshot per 'everyNthsnapshot'
+ *
+ * @param numberOfSnapshot
+ * @param everyNthSnapshot
+ * @return ArrayList
+ */
+ public static ArrayList<HashMap<String, HashMap<String, Object>>> fetchData(Integer numberOfSnapshot,
+ Integer everyNthSnapshot) {
+ ArrayList<HashMap<String, HashMap<String, Object>>> stats = new ArrayList<HashMap<String, HashMap<String, Object>>>();
+ // get all records in the database grouped and ordered by time
+ ResultSet table = fetchSnapshotTimesFromDB();
+ // iterate through the table and finds the times (which uniquely IDs a snapshot)
+ // that are wanted and queries the rest of the DB using the time as the condition
+ // (i.e. the ones that are in the c*n-th snapshot where c <= numberOfSnapshot
+ // and n == everyNthSnapshot)
+ int nthSnapshot = 0;
+ try {
+ while(table.next()) {
+ Long snapshotTime = table.getLong(SNAPSHOT_TIME);
+ // grab 0*nth, 1*nth, 2*nth snapshot up to min(numberOfSnapshot*everyNthSnapshot, size of the table)
+ if(nthSnapshot % everyNthSnapshot == 0) {
+ HashMap<String, HashMap<String, Object>> snapshotData = packageSnapshotData(snapshotTime);
+ stats.add( 0, snapshotData );
+ numberOfSnapshot--;
+ }
+ nthSnapshot++;
+ // no more snapshots needs to be looked at, we have successfully acquired our goal
+ if(numberOfSnapshot == 0) {
+ break;
+ }
+ }
+ } catch(Exception e) {
+ e.printStackTrace();
+ } finally {
+ closeConnection();
+ }
+ return stats;
+ }
+
+ /**
+ * @param snapshotTime
+ * @return A hashmap in the form <String1, HashMap> where String1 is the mbean name
+ * and HashMap is a map containing a <String2, Object> where String2 is an attribute name
+ * and Object is the value. Additionally, if String is "times" then it maps to a HashMap
+ * containing snapshot_time and snapshot_date information.
+ */
+ private static HashMap<String, HashMap<String, Object>> packageSnapshotData(Long snapshotTime) {
+ HashMap<String, HashMap<String, Object>> snapshotPkg = new HashMap<String, HashMap<String, Object>>();
+ ResultSet snapshotData = fetchSnapshotDataFromDB(snapshotTime);
+ String snapshotDate = null;
+ try {
+ // for each record save it somewhere in the snapshotPkg
+ while(snapshotData.next()) {
+ String currMBean = snapshotData.getString(MBEANNAME);
+ // get the information for the mbean defined by currMBean
+ HashMap<String, Object> mbeanInfo = snapshotPkg.get(currMBean);
+ if(mbeanInfo == null) {
+ mbeanInfo = new HashMap<String, Object>();
+ }
+ // save the statistic name and value into a hashmap
+ mbeanInfo.put(snapshotData.getString(STATSNAME), snapshotData.getLong(STATSVALUE));
+ // save the hashmap into the snapshotpkg
+ snapshotPkg.put(currMBean, mbeanInfo);
+ // save the snapshotDate
+ if(snapshotDate == null) {
+ snapshotDate = snapshotData.getString(SNAPSHOT_DATE);
+ }
+ }
+ } catch(Exception e) {
+ e.printStackTrace();
+ } finally {
+ closeConnection();
+ }
+ // add the time and date
+ HashMap<String, Object> timeMap = new HashMap<String, Object>();
+ timeMap.put(SNAPSHOT_TIME, snapshotTime);
+ timeMap.put(SNAPSHOT_DATE, snapshotDate);
+ snapshotPkg.put("times", timeMap);
+
+ return snapshotPkg;
+ }
+
+ /**
+ * @param snapshotTime
+ * @return Returns a ResultSet with all statistic information that matches the snapshot_time.
+ */
+ private static ResultSet fetchSnapshotDataFromDB(Long snapshotTime) {
+ String query = "SELECT S.statsValue AS statsValue, S.statsName AS statsName, SN.snapshot_time AS snapshot_time, SN.snapshot_date AS snapshot_date, M.mbeanName AS mbeanName FROM Statistics S, Snapshots SN, MBeans M WHERE SN.snapshot_time=" + snapshotTime;
+ query += " AND S.snapshotId=SN.id AND S.mbeanId=M.id";
+ ResultSet retval = null;
+ try {
+ openConnection();
+ Statement stmt = conn.createStatement();
+ retval = stmt.executeQuery(query);
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
+ return retval;
+ }
+
+ /**
+ * @return Returns a ResultSet with one column (snapshot_time) sorted in descending order
+ */
+ private static ResultSet fetchSnapshotTimesFromDB() {
+ String query = "SELECT DISTINCT snapshot_time FROM Snapshots ORDER BY snapshot_time DESC";
+ ResultSet retval = null;
+ try {
+ openConnection();
+ Statement stmt = conn.createStatement();
+ retval = stmt.executeQuery(query);
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
+ return retval;
+ }
+
+ /**
+ * Opens the global connection to db
+ */
+ private static void openConnection() {
+ try {
+ conn = DriverManager.getConnection("jdbc:derby:ActiveMRCDB;create=true", "monitor", "monitor");
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Closes teh global connection to DB
+ */
+ private static void closeConnection() {
+ if(conn != null) {
+ try {
+ conn.close();
+ } catch(Exception e) {
+// e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * @param mbeanName
+ * @param statsName
+ * @param numberOfSnapshots
+ * @param everyNthSnapshot
+ * @return HashMap which maps from a snapshot_time --> value of the mbean.statsName at that time
+ */
+ public static TreeMap<Long, Long> getSpecificStatistics(String mbeanName, String statsName, int numberOfSnapshots, int everyNthSnapshot) {
+ openConnection();
+ TreeMap<Long, Long> stats = new TreeMap<Long, Long>();
+ try {
+ Statement stmt = conn.createStatement();
+ int mbeanId = getMBeanId(mbeanName);
+ if(mbeanId == -1) {
+ System.out.println("[ERROR] " + mbeanName + " does not exist in the database.");
+ } else {
+ String query = "SELECT DISTINCT SN.snapshot_time AS snapshot_time, S.statsValue AS statsValue FROM Snapshots SN, Statistics S WHERE S.mbeanId=" + mbeanId + " AND S.snapshotId=SN.id AND S.statsName=" + surroundWithQuotes(statsName) + " ORDER BY SN.snapshot_time DESC";
+ ResultSet rs = stmt.executeQuery(query);
+ int nthSnapshot = 0;
+ // iterator through the table paying attention to those at everyNthSnapshot-th position
+ while(rs.next()) {
+ System.out.println(nthSnapshot);
+ // every nth snapshot I save the information into my returning hashmap
+ if(nthSnapshot % everyNthSnapshot == 0) {
+ stats.put(rs.getLong(SNAPSHOT_TIME), rs.getLong(STATSVALUE));
+ numberOfSnapshots--;
+ }
+ // update counter
+ nthSnapshot++;
+ // enough data, end this thing
+ if(numberOfSnapshots == 0) {
+ break;
+ }
+ }
+ }
+ } catch(Exception e) {
+ e.printStackTrace();
+ } finally {
+ closeConnection();
+ }
+ return stats;
+ }
+}
Propchange: geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotDBHelper.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotDBHelper.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotDBHelper.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotThread.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotThread.java?rev=587666&r1=587665&r2=587666&view=diff
==============================================================================
--- geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotThread.java (original)
+++ geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotThread.java Tue Oct 23 14:37:21 2007
@@ -45,16 +45,21 @@
private Set<ObjectName> allMBeans = null;
// list of mbean names that we will be taking snapshots of
private ArrayList<String> mbeanNames;
+ // credentials for getting authenticated to access MRC
+ private static String username = null;
+ private static String password = null;
private boolean end = false;
- public SnapshotThread(long snapshot_length, MBeanServer mbServer, Set<ObjectName> allMBeans) {
+ public SnapshotThread(long snapshot_length, MBeanServer mbServer, Set<ObjectName> allMBeans, final String username, final String password) {
SNAPSHOT_DURATION = snapshot_length;
this.mbServer = mbServer;
snapshotXMLBuilder = new SnapshotXMLBuilder();
snapshotXMLBuilder.checkXMLExists();
mbeanNames = new ArrayList<String>();
this.allMBeans = allMBeans;
+ this.username = username;
+ this.password = password;
}
/**
@@ -127,11 +132,14 @@
HashMap stats = (HashMap)mrc.getStats(mbeanName);
aggregateStats.put(mbeanName, stats);
}
- // store the data
+ // store the data in XML
snapshotXMLBuilder.openDocument();
snapshotXMLBuilder.addNewSnapshot(aggregateStats);
snapshotXMLBuilder.saveDocument( snapshotXMLBuilder.getDocument() );
+ // store the data in a DB
+ SnapshotDBHelper.addSnapshotToDB(null, aggregateStats);
+
for(Iterator itt = aggregateStats.keySet().iterator(); itt.hasNext(); ) {
String mbean = (String)itt.next();
HashMap stats = aggregateStats.get(mbean);
@@ -166,17 +174,17 @@
// turn the statistics collection on
String methodName = "setStatsOn";
Object[] params = new Object[] { Boolean.TRUE };
- String[] signatures = new String[] { "java.lang.Boolean" };
+ String[] signatures = new String[] { "boolean" };
try {
ObjectName objName = new ObjectName(mbeanList.get(i));
- //mejb.invoke(objName, methodName, params, signatures);
- //System.out.println("Stats for " + mbeanList.get(i) + " was turned on.");
+ mrc.invoke(objName, methodName, params, signatures);
+ System.out.println("Stats for " + mbeanList.get(i) + " was turned on.");
} catch(MalformedObjectNameException e) {
e.printStackTrace();
} catch(Exception e) {
// an exception will be thrown if the methodName is not found for this class,
// which is very possible.
- System.out.println("Could not turn on stats for " + mbeanList.get(i));
+ System.out.println("[WARN] Could not turn on stats for " + mbeanList.get(i));
}
}
}
@@ -225,16 +233,25 @@
}
}
+ /**
+ * @return Returns true if the snapshot thread has ended. Otherwise, returns false.
+ */
public boolean getEnd() {
return end;
}
-
+
+ /**
+ * @return An instance of a MRC.
+ */
public MasterRemoteControlRemote getMRC() {
+ // TODO:
+ // Need to find a way to grab the MRC using LocalInitialContext
+ // so it can get through security and be more efficient.
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.openejb.client.RemoteInitialContextFactory");
props.setProperty(Context.PROVIDER_URL, "127.0.0.1:4201");
- props.setProperty(Context.SECURITY_PRINCIPAL, "system");
- props.setProperty(Context.SECURITY_CREDENTIALS, "manager");
+ props.setProperty(Context.SECURITY_PRINCIPAL, this.username);
+ props.setProperty(Context.SECURITY_CREDENTIALS, this.password);
props.setProperty("openejb.authentication.realmName", "geronimo-admin");
try {
Context ic = new InitialContext(props);
Modified: geronimo/sandbox/monitoring/mrc-server/readme.txt
URL: http://svn.apache.org/viewvc/geronimo/sandbox/monitoring/mrc-server/readme.txt?rev=587666&r1=587665&r2=587666&view=diff
==============================================================================
--- geronimo/sandbox/monitoring/mrc-server/readme.txt (original)
+++ geronimo/sandbox/monitoring/mrc-server/readme.txt Tue Oct 23 14:37:21 2007
@@ -1,12 +1,6 @@
HOW TO COMPILE AND DEPLOY
1. run `mvn clean install`
- 2. Deploy the JAR file present in the MRC-ejb/target folder along with
- MRC.xml present at the home directory into Geronimo.
- 2a. Navigate to <GERONIMO_HOME>/bin/ directory
- 2b. Give it this command
+ 2. Deploy the EAR file present in the mrc-ear/target folder
- java -jar ./deployer.jar -u system -p manager deploy <path-to-jar> <path-to-xml>
-
-
-**The snapshot information for the server will reside under the folder
- <GERONIMO_HOME>/var/
+**The snapshot configuration information for the server will reside under the folder
+ <GERONIMO_HOME>/var/monitoring