You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ak...@apache.org on 2007/10/25 05:56:44 UTC

svn commit: r588129 - /geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControl.java

Author: akulshreshtha
Date: Wed Oct 24 20:56:43 2007
New Revision: 588129

URL: http://svn.apache.org/viewvc?rev=588129&view=rev
Log:
GERONIMO-3535 Monitoring Plugin : Use DataSource, Patch by Viet H. Nguyen

Modified:
    geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControl.java

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=588129&r1=588128&r2=588129&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 Wed Oct 24 20:56:43 2007
@@ -73,14 +73,15 @@
  * This is a Stateful Session Bean that will be the bottleneck for the communication
  * between the management node and the data in the server node.
  */
-@RolesAllowed("mejbadmin")
+@RolesAllowed("mejbuser")
 @Stateful(name="ejb/mgmt/MRC")
 @Remote(MasterRemoteControlRemote.class)
-@RunAs("mejbadmin")
+@RunAs("mejbuser")
 public class MasterRemoteControl {
     // constants
     private static final String GERONIMO_DEFAULT_DOMAIN = "geronimo";
     private static final Long DEFAULT_DURATION = new Long(300000);
+    private static final int DEFAULT_RETENTION = 30; // 30 days
 
     //private static final Log log = LogFactory.getLog(MasterRemoteControl.class);
 
@@ -99,14 +100,16 @@
     // use this to call utility functions pertaining to mbeans
     private static MBeanHelper mbeanHelper;
 
-    @Resource(name="ActiveDS") private DataSource ds;
+    // inject Data Sources
+    @Resource(name="ActiveDS") private DataSource activeDS;
+    @Resource(name="ArchiveDS") private DataSource archiveDS;
 
     public MasterRemoteControl() {
+        
     }
 
     @PostConstruct
     private void init() {
-
         // retrieve the mbean server
         ArrayList mbServerList = MBeanServerFactory.findMBeanServer(null);
         if(mbServerList.size() >= 1) {
@@ -128,21 +131,15 @@
 
         // set up databases
         setUpDatabase();
-
-        if(ds == null) {
-            System.out.println("DataSource is NOT GOOD");
-        } else {
-            System.out.println("DataSource is GOOD");
-        }
-
+        
+        // set up SnaphotDBHelper with the necessary data sources
+        SnapshotDBHelper.setDataSources(activeDS, archiveDS);
     }
 
     private void setUpDatabase() {
         Connection conn = null;
         try {
-            // TODO: 
-            //      Use a DataSource instead.
-            conn = DriverManager.getConnection("jdbc:derby:ActiveMRCDB;create=true", "monitor", "monitor");
+            conn = activeDS.getConnection();
             Statement stmt = conn.createStatement();
             try {
                 // fetch all tables in the db
@@ -181,16 +178,15 @@
         try {
             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 +=     "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");
@@ -206,7 +202,7 @@
     /**
      * Retrieves and instance of the MEJB and starts the snapshot process
      */
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     public void setUpMEJB(String username, String password) {
         // instantiate the MEJB, which will be our gateway to communicate to MBeans
         try {
@@ -221,13 +217,6 @@
             this.username = username;
             this.password = password;
 
-            // start the snapshot process
-            // startSnapshot(fetchSnapshotDuration());
-
-            // Uncomment to see if mejb is working well
-            //System.out.println("SUCCESS!");
-            //System.out.println("MEJB Default Domain: " + mejb.getDefaultDomain());
-            //System.out.println("MEJB Count: " + mejb.getMBeanCount());
         } catch(Exception e) {
             e.printStackTrace();
         }
@@ -240,7 +229,7 @@
      * @return HashMap
      * @throws Exception
      */
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     public static HashMap getStats(String objectName) throws Exception {
         HashMap statsMap = new HashMap();
         Stats stats = (Stats)mejb.getAttribute(new ObjectName(objectName), "stats");
@@ -296,17 +285,53 @@
      * 
      * @param interval
      */
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     public boolean startSnapshot(Long interval) {
+        // get the saved/default retention period
+        String retentionStr = null;
+        try {
+            retentionStr = SnapshotConfigXMLBuilder.getAttributeValue("retention");
+        } catch(Exception e){
+            // happens when there is not an instance of "retention" in the config
+            // which is okay.
+        }
+        int retention;
+        if(retentionStr == null) {
+            retention = DEFAULT_RETENTION;
+        } else {
+            retention = Integer.parseInt(retentionStr);
+        }
+        saveDuration(interval.longValue());
+        saveRetention(retention);
+        // check to make sure that another snaphshot isn't running
+        if((snapshotThread == null || (snapshotThread != null && !snapshotThread.isAlive())) &&
+                interval.longValue() > 0) {
+            snapshotThread = new SnapshotThread(interval.longValue(),
+                                                retention, 
+                                                mbServer, 
+                                                getStatisticsProviderMBeanNames(),
+                                                username,
+                                                password);
+            snapshotThread.start();
+            return true;
+        } else {
+            System.out.println("There is already a snapshot thread running; Duration: " + interval.longValue());
+            return false;
+        }
+    }
+    
+    public boolean startSnapshot(Long interval, int retention) {
+        saveDuration(interval.longValue());
+        saveRetention(retention);
         if((snapshotThread == null || (snapshotThread != null && !snapshotThread.isAlive())) &&
                 interval.longValue() > 0) {
             snapshotThread = new SnapshotThread(interval.longValue(),
+                                                retention,
                                                 mbServer, 
                                                 getStatisticsProviderMBeanNames(),
                                                 username,
                                                 password);
             snapshotThread.start();
-            saveDuration(interval.longValue());
             return true;
         } else {
             System.out.println("There is already a snapshot thread running; Duration: " + interval.longValue());
@@ -317,12 +342,14 @@
     /**
      * Stops the snapshot thread
      */
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     public boolean stopSnapshot() {
         if(snapshotThread != null) {
             if(snapshotThread.getSnapshotDuration() == Long.MAX_VALUE) {
                 saveDuration(DEFAULT_DURATION);
+                saveRetention(DEFAULT_RETENTION);
             } else {
+                saveRetention(snapshotThread.getSnapshotRetention());
                 saveDuration(snapshotThread.getSnapshotDuration());
             }
             snapshotThread.setEnd();
@@ -348,7 +375,7 @@
      * @param everyNthSnapshot
      * @return ArrayList
      */ 
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     public ArrayList fetchSnapshotData(Integer numberOfSnapshot, Integer everyNthSnapshot) {
         ArrayList retval = (ArrayList)org.apache.geronimo.monitor.snapshot.SnapshotDBHelper.fetchData(numberOfSnapshot, everyNthSnapshot);
         return retval;
@@ -361,7 +388,7 @@
      * @param numberOfSnapshot
      * @return HashMap
      */
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     public HashMap fetchMaxSnapshotData(Integer numberOfSnapshot) {
         HashMap retval = (HashMap)org.apache.geronimo.monitor.snapshot.SnapshotDBHelper.fetchMaxSnapshotData(numberOfSnapshot);
         return retval;
@@ -374,7 +401,7 @@
      * @param numberOfSnapshot
      * @return HashMap
      */
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     public HashMap fetchMinSnapshotData(Integer numberOfSnapshot) {
         HashMap retval = (HashMap)org.apache.geronimo.monitor.snapshot.SnapshotDBHelper.fetchMinSnapshotData(numberOfSnapshot);
         return retval;
@@ -385,7 +412,7 @@
      * 
      * @return Long
      */
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     public Long getSnapshotDuration() {
         if(snapshotThread != null) {
             return new Long(snapshotThread.getSnapshotDuration());
@@ -400,7 +427,7 @@
      * 
      * @param snapshotDuration
      */
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     public void setSnapshotDuration(Long snapshotDuration) {
         if(snapshotThread != null) {
             snapshotThread.setSnapshotDuration(snapshotDuration.longValue());
@@ -410,7 +437,16 @@
         }
     }
     
-    @RolesAllowed("mejbadmin")
+    public void setSnapshotRetention(int retention) {
+        if(snapshotThread != null) {
+            snapshotThread.setSnapshotRetention(retention);
+            saveRetention(snapshotThread.getSnapshotRetention());
+        } else {
+            System.out.println("There is not a snapshot thread instantiated.");
+        }
+    }
+    
+    @RolesAllowed("mejbuser")
     public Long getSnapshotCount() {
         return org.apache.geronimo.monitor.snapshot.SnapshotDBHelper.getSnapshotCount();
     }
@@ -421,7 +457,7 @@
      * @return A set containing all mbean names of mbeans that provide
      * statistics
      */
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     public Set<ObjectName> getStatisticsProviderMBeanNames() {
         return mbeanHelper.getStatsProvidersMBeans( getAllMBeanNames() );
     }
@@ -431,7 +467,7 @@
      * 
      * @return A set containing all mbean names
      */
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     public Set<ObjectName> getAllMBeanNames() {
         try {
             return (Set<ObjectName>)mejb.queryNames(null, null);
@@ -441,12 +477,17 @@
         }
     }
     
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     private void saveDuration(long duration) {
-        SnapshotConfigXMLBuilder.save(duration);
+        SnapshotConfigXMLBuilder.saveDuration(duration);
     }
     
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
+    private void saveRetention(int retention) {
+        SnapshotConfigXMLBuilder.saveRetention(retention);
+    }
+    
+    @RolesAllowed("mejbuser")
     private Long fetchSnapshotDuration() {
         final String DURATION = "duration";
         try {
@@ -463,7 +504,7 @@
      * 
      * @param mbeanName
      */
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     public void addMBeanForSnapshot(String mbeanName) {
         SnapshotConfigXMLBuilder.addMBeanName(mbeanName);
         snapshotThread.addMBeanForSnapshot(mbeanName);
@@ -475,7 +516,7 @@
      * 
      * @param mbeanName
      */
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     public void removeMBeanForSnapshot(String mbeanName) {
         SnapshotConfigXMLBuilder.removeMBeanName(mbeanName);
         snapshotThread.removeMBeanForSnapshot(mbeanName);
@@ -484,7 +525,7 @@
     /**
      * @return A map: mbeanName --> ArrayList of statistic attributes for that mbean
      */
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     public HashMap<String, ArrayList<String>> getAllSnapshotStatAttributes() {
         return org.apache.geronimo.monitor.snapshot.SnapshotDBHelper.getAllSnapshotStatAttributes();
     }
@@ -492,7 +533,7 @@
     /**
      * @return Returns true if snapshot is running.
      */
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     public boolean isSnapshotRunning() {
         if(snapshotThread == null) {
             return false;
@@ -521,8 +562,21 @@
      * @param everyNthSnapshot
      * @return HashMap which maps from a snapshot_time --> value of the mbean.statsName at that time
      */
-    @RolesAllowed("mejbadmin")
+    @RolesAllowed("mejbuser")
     public TreeMap<Long, Long> getSpecificStatistics(String mbeanName, String statsName, int numberOfSnapshots, int everyNthSnapshot) {
         return SnapshotDBHelper.getSpecificStatistics(mbeanName, statsName, numberOfSnapshots, everyNthSnapshot);
+    }
+    
+    /**
+     * @return A set of all mbeans being tracked from the db
+     */
+    @RolesAllowed("mejbuser")
+    public Set getTrackedMBeans() {
+        ArrayList<String> mbeans = (ArrayList<String>)SnapshotConfigXMLBuilder.getMBeanNames();
+        Set<String> set = new HashSet<String>();
+        for(int i = 0; i < mbeans.size(); i++) {
+            set.add(mbeans.get(i));
+        }
+        return set;
     }
 }