From 7afa3cd888f0ce808c7bd0a614fb974ab43dcf6d Mon Sep 17 00:00:00 2001
From: Richard Hensman <richard@onevisionconsulting.co.uk>
Date: Wed, 18 Jul 2012 15:09:10 +0100
Subject: [PATCH] Add support for monitoring table spaces to the Oracle plugin

---
 .../plugins/oracle/OracleTablespaceComponent.java  |   83 ++++++++++++++++++++
 .../src/main/resources/META-INF/rhq-plugin.xml     |   25 ++++++-
 2 files changed, 106 insertions(+), 2 deletions(-)
 create mode 100644 modules/plugins/oracle/src/main/java/org/rhq/plugins/oracle/OracleTablespaceComponent.java

diff --git a/modules/plugins/oracle/src/main/java/org/rhq/plugins/oracle/OracleTablespaceComponent.java b/modules/plugins/oracle/src/main/java/org/rhq/plugins/oracle/OracleTablespaceComponent.java
new file mode 100644
index 0000000..e99787b
--- /dev/null
+++ b/modules/plugins/oracle/src/main/java/org/rhq/plugins/oracle/OracleTablespaceComponent.java
@@ -0,0 +1,83 @@
+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2008 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.rhq.plugins.oracle;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.rhq.core.domain.measurement.AvailabilityType;
+import org.rhq.core.domain.measurement.MeasurementDataNumeric;
+import org.rhq.core.domain.measurement.MeasurementReport;
+import org.rhq.core.domain.measurement.MeasurementScheduleRequest;
+import org.rhq.core.pluginapi.measurement.MeasurementFacet;
+import org.rhq.core.util.jdbc.JDBCUtil;
+import org.rhq.plugins.database.AbstractDatabaseComponent;
+import org.rhq.plugins.database.DatabaseQueryUtility;
+
+/**
+ * Oracle Tablespace Component.
+ * 
+ * @author Richard Hensman
+ */
+public class OracleTablespaceComponent extends AbstractDatabaseComponent implements MeasurementFacet {
+
+    private static final String SQL_AVAILABLE = "SELECT COUNT(*) FROM dba_tablespaces WHERE tablespace_name = ?";
+    private static final String SQL_VALUES =
+        "SELECT USED_SPACE usedSpace, TABLESPACE_SIZE totalSize, (USED_PERCENT/100) usedPercent " +
+        "FROM dba_tablespace_usage_metrics where tablespace_name = ?";
+
+
+    private static Log log = LogFactory.getLog(OracleTablespaceComponent.class);
+
+    public AvailabilityType getAvailability() {
+        PreparedStatement statement = null;
+        ResultSet resultSet = null;
+        try {
+            statement = getConnection().prepareStatement(SQL_AVAILABLE);
+            statement.setString(1, this.resourceContext.getResourceKey());
+            resultSet = statement.executeQuery();
+            if (resultSet.next() && (resultSet.getInt(1) == 1)) {
+                return AvailabilityType.UP;
+            }
+        } catch (SQLException e) {
+            log.debug("unable to query", e);
+        } finally {
+            JDBCUtil.safeClose(statement, resultSet);
+        }
+
+        return AvailabilityType.DOWN;
+    }
+
+    public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) throws Exception {
+        Map<String, Double> values = DatabaseQueryUtility.getNumericQueryValues(this, SQL_VALUES,
+                this.resourceContext.getResourceKey());
+        for (MeasurementScheduleRequest request : metrics) {
+            Double d = values.get(request.getName().toUpperCase(Locale.US));
+            if (d != null) {
+                report.addData(new MeasurementDataNumeric(request, d));
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/modules/plugins/oracle/src/main/resources/META-INF/rhq-plugin.xml b/modules/plugins/oracle/src/main/resources/META-INF/rhq-plugin.xml
index 00f0284..8ef56cb 100644
--- a/modules/plugins/oracle/src/main/resources/META-INF/rhq-plugin.xml
+++ b/modules/plugins/oracle/src/main/resources/META-INF/rhq-plugin.xml
@@ -593,8 +593,29 @@
             <c:simple-property name="description" default="Oracle User"/>
          </plugin-configuration>
 
-          <metric property="connections" displayName="Total Connections" displayType="summary"/>
-          <metric property="active" displayName="Active Connections" displayType="summary"/>
+         <metric property="connections" displayName="Total Connections" displayType="summary"/>
+         <metric property="active" displayName="Active Connections" displayType="summary"/>
+
+      </service>
+
+      <service name="Oracle Tablespaces"
+         discovery="org.rhq.plugins.database.CustomTableRowDiscoveryComponent"
+         class="org.rhq.plugins.oracle.OracleTablespaceComponent">
+
+         <plugin-configuration>
+            <c:simple-property name="table" default="DBA_TABLESPACES"/>
+            <c:simple-property name="metricQuery" default="SELECT {key} FROM DBA_TABLESPACES"/>
+            <c:simple-property name="keyColumn" default="tablespace_name"/>
+            <c:simple-property name="name" default="{key}"/>
+            <c:simple-property name="description" default="Oracle Tablespace"/>
+         </plugin-configuration>
+
+         <!-- Space used in the tablespace (database blocks) -->
+         <metric property="usedSpace" displayName="Used Space" description="Space used in the tablespace (database blocks)" displayType="summary"/>
+         <!-- Total size of the tablespace (database blocks) -->
+         <metric property="totalSize" displayName="Total Size" description="Total size of the tablespace (database blocks)" displayType="summary"/>
+         <!-- Percentage of the tablespace used -->
+         <metric property="usedPercent" displayName="Used Percent" description="Percentage of the tablespace used" displayType="summary" units="percentage"/>
 
       </service>
 
-- 
1.7.8.3

From 117cbfbe4887f609bc9dc4bf53ec501d90344561 Mon Sep 17 00:00:00 2001
From: Richard Hensman <richard@onevisionconsulting.co.uk>
Date: Thu, 19 Jul 2012 21:44:44 +0100
Subject: [PATCH] Add support for monitoring Flash Recovery Areas to the
 Oracle plugin

---
 .../oracle/OracleFlashRecoveryAreaComponent.java   |   83 ++++++++++++++++++++
 .../src/main/resources/META-INF/rhq-plugin.xml     |   23 +++++-
 2 files changed, 103 insertions(+), 3 deletions(-)
 create mode 100644 modules/plugins/oracle/src/main/java/org/rhq/plugins/oracle/OracleFlashRecoveryAreaComponent.java

diff --git a/modules/plugins/oracle/src/main/java/org/rhq/plugins/oracle/OracleFlashRecoveryAreaComponent.java b/modules/plugins/oracle/src/main/java/org/rhq/plugins/oracle/OracleFlashRecoveryAreaComponent.java
new file mode 100644
index 0000000..0446d7f
--- /dev/null
+++ b/modules/plugins/oracle/src/main/java/org/rhq/plugins/oracle/OracleFlashRecoveryAreaComponent.java
@@ -0,0 +1,83 @@
+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2008 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.rhq.plugins.oracle;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.rhq.core.domain.measurement.AvailabilityType;
+import org.rhq.core.domain.measurement.MeasurementDataNumeric;
+import org.rhq.core.domain.measurement.MeasurementReport;
+import org.rhq.core.domain.measurement.MeasurementScheduleRequest;
+import org.rhq.core.pluginapi.measurement.MeasurementFacet;
+import org.rhq.core.util.jdbc.JDBCUtil;
+import org.rhq.plugins.database.AbstractDatabaseComponent;
+import org.rhq.plugins.database.DatabaseQueryUtility;
+
+/**
+ * Oracle Flash Recovery Area Component.
+ * 
+ * @author Richard Hensman
+ */
+public class OracleFlashRecoveryAreaComponent extends AbstractDatabaseComponent implements MeasurementFacet {
+
+    private static final String SQL_AVAILABLE = "SELECT COUNT(*) FROM v$recovery_file_dest WHERE name = ?";
+    private static final String SQL_VALUES =
+        "SELECT space_limit spaceLimit, space_used spaceUsed, space_reclaimable spaceReclaimable, number_of_files numberOfFiles, (space_used/space_limit) usedPercent " +
+        "FROM v$recovery_file_dest WHERE name = ?";
+
+
+    private static Log log = LogFactory.getLog(OracleFlashRecoveryAreaComponent.class);
+
+    public AvailabilityType getAvailability() {
+        PreparedStatement statement = null;
+        ResultSet resultSet = null;
+        try {
+            statement = getConnection().prepareStatement(SQL_AVAILABLE);
+            statement.setString(1, this.resourceContext.getResourceKey());
+            resultSet = statement.executeQuery();
+            if (resultSet.next() && (resultSet.getInt(1) == 1)) {
+                return AvailabilityType.UP;
+            }
+        } catch (SQLException e) {
+            log.debug("unable to query", e);
+        } finally {
+            JDBCUtil.safeClose(statement, resultSet);
+        }
+
+        return AvailabilityType.DOWN;
+    }
+
+    public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) throws Exception {
+        Map<String, Double> values = DatabaseQueryUtility.getNumericQueryValues(this, SQL_VALUES,
+                this.resourceContext.getResourceKey());
+        for (MeasurementScheduleRequest request : metrics) {
+            Double d = values.get(request.getName().toUpperCase(Locale.US));
+            if (d != null) {
+                report.addData(new MeasurementDataNumeric(request, d));
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/modules/plugins/oracle/src/main/resources/META-INF/rhq-plugin.xml b/modules/plugins/oracle/src/main/resources/META-INF/rhq-plugin.xml
index 8ef56cb..d8606e1 100644
--- a/modules/plugins/oracle/src/main/resources/META-INF/rhq-plugin.xml
+++ b/modules/plugins/oracle/src/main/resources/META-INF/rhq-plugin.xml
@@ -610,14 +610,31 @@
             <c:simple-property name="description" default="Oracle Tablespace"/>
          </plugin-configuration>
 
-         <!-- Space used in the tablespace (database blocks) -->
          <metric property="usedSpace" displayName="Used Space" description="Space used in the tablespace (database blocks)" displayType="summary"/>
-         <!-- Total size of the tablespace (database blocks) -->
          <metric property="totalSize" displayName="Total Size" description="Total size of the tablespace (database blocks)" displayType="summary"/>
-         <!-- Percentage of the tablespace used -->
          <metric property="usedPercent" displayName="Used Percent" description="Percentage of the tablespace used" displayType="summary" units="percentage"/>
 
       </service>
+      
+      <service name="Oracle Flash Recovery Areas"
+         discovery="org.rhq.plugins.database.CustomTableRowDiscoveryComponent"
+         class="org.rhq.plugins.oracle.OracleFlashRecoveryAreaComponent">
+
+         <plugin-configuration>
+            <c:simple-property name="table" default="V$RECOVERY_FILE_DEST"/>
+            <c:simple-property name="metricQuery" default="SELECT {key} FROM V$RECOVERY_FILE_DEST"/>
+            <c:simple-property name="keyColumn" default="name"/>
+            <c:simple-property name="name" default="{key}"/>
+            <c:simple-property name="description" default="Oracle Flash Recovery Areas"/>
+         </plugin-configuration>
+
+         <metric property="spaceLimit" displayName="Space Limit" description="Flash Recovery Area space limit (database blocks)" displayType="summary"/>
+         <metric property="spaceUsed" displayName="Space Used" description="Space used in the Flash Recovery Area (database blocks)" displayType="summary"/>
+         <metric property="spaceReclaimable" displayName="Space Reclaimable" description="Space reclaimable in the Flash Recovery Area (database blocks)" displayType="summary"/>
+         <metric property="numberOfFiles" displayName="Number Of Files" description="Number of files in the Flash Recovery Area" displayType="summary"/>
+         <metric property="usedPercent" displayName="Used Percent" description="Percentage of the Flash Recovery Area used" displayType="summary" units="percentage"/>
+
+      </service>
 
    </server>
 
-- 
1.7.8.3

