karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject [1/2] karaf-decanter git commit: [KARAF-4850] Be able to specify several object names for the JMX collector
Date Tue, 06 Dec 2016 16:02:04 GMT
Repository: karaf-decanter
Updated Branches:
  refs/heads/master 22f593bd0 -> 8333068f0


[KARAF-4850] Be able to specify several object names for the JMX collector


Project: http://git-wip-us.apache.org/repos/asf/karaf-decanter/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf-decanter/commit/605904a6
Tree: http://git-wip-us.apache.org/repos/asf/karaf-decanter/tree/605904a6
Diff: http://git-wip-us.apache.org/repos/asf/karaf-decanter/diff/605904a6

Branch: refs/heads/master
Commit: 605904a6f09b98c92c7462d06baf968cddbd3ef7
Parents: 22f593b
Author: Vincent Zurczak <vincent.zurczak@gmail.com>
Authored: Tue Nov 29 16:15:51 2016 +0100
Committer: Jean-Baptiste Onofré <jbonofre@apache.org>
Committed: Tue Dec 6 16:43:39 2016 +0100

----------------------------------------------------------------------
 collector/jmx/pom.xml                           |  9 +++
 ...he.karaf.decanter.collector.jmx-activemq.cfg |  6 ++
 ...pache.karaf.decanter.collector.jmx-camel.cfg |  6 ++
 ...pache.karaf.decanter.collector.jmx-local.cfg |  6 ++
 .../decanter/collector/jmx/JmxCollector.java    | 85 +++++++++++++-------
 .../collector/jmx/TestMapAttribute.java         | 48 +++++++++++
 .../main/asciidoc/user-guide/collectors.adoc    |  8 +-
 7 files changed, 137 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/605904a6/collector/jmx/pom.xml
----------------------------------------------------------------------
diff --git a/collector/jmx/pom.xml b/collector/jmx/pom.xml
index 202f643..652aca6 100644
--- a/collector/jmx/pom.xml
+++ b/collector/jmx/pom.xml
@@ -33,6 +33,15 @@
     <packaging>bundle</packaging>
     <name>Apache Karaf :: Decanter :: Collector :: JMX</name>
 
+    <dependencies>
+    	<dependency>
+			<groupId>org.mockito</groupId>
+			<artifactId>mockito-core</artifactId>
+			<version>1.10.19</version>
+			<scope>test</scope>
+		</dependency>
+	</dependencies>
+
     <build>
         <plugins>
             <plugin>

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/605904a6/collector/jmx/src/main/cfg/org.apache.karaf.decanter.collector.jmx-activemq.cfg
----------------------------------------------------------------------
diff --git a/collector/jmx/src/main/cfg/org.apache.karaf.decanter.collector.jmx-activemq.cfg
b/collector/jmx/src/main/cfg/org.apache.karaf.decanter.collector.jmx-activemq.cfg
index fe4d75e..7de5623 100644
--- a/collector/jmx/src/main/cfg/org.apache.karaf.decanter.collector.jmx-activemq.cfg
+++ b/collector/jmx/src/main/cfg/org.apache.karaf.decanter.collector.jmx-activemq.cfg
@@ -20,6 +20,12 @@ url=local
 # some MBeans matching the object name filter
 object.name=org.apache.activemq:*
 
+# Several object names can also be specified.
+# What matters is that the property names begin with "object.name".
+#object.name.system=java.lang:*
+#object.name.karaf=org.apache.karaf:type=http,name=*
+#object.name.3=org.apache.activemq:*
+
 # You can add any custom field that the collector will "forward" to the dispatcher
 # For instance:
 #

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/605904a6/collector/jmx/src/main/cfg/org.apache.karaf.decanter.collector.jmx-camel.cfg
----------------------------------------------------------------------
diff --git a/collector/jmx/src/main/cfg/org.apache.karaf.decanter.collector.jmx-camel.cfg
b/collector/jmx/src/main/cfg/org.apache.karaf.decanter.collector.jmx-camel.cfg
index 0674e52..1522b9d 100644
--- a/collector/jmx/src/main/cfg/org.apache.karaf.decanter.collector.jmx-camel.cfg
+++ b/collector/jmx/src/main/cfg/org.apache.karaf.decanter.collector.jmx-camel.cfg
@@ -20,6 +20,12 @@ url=local
 # some MBeans matching the object name filter
 object.name=org.apache.camel:context=*,type=routes,name=*
 
+# Several object names can also be specified.
+# What matters is that the property names begin with "object.name".
+#object.name.system=java.lang:*
+#object.name.karaf=org.apache.karaf:type=http,name=*
+#object.name.3=org.apache.activemq:*
+
 # You can add any custom field that the collector will "forward" to the dispatcher
 # For instance:
 #

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/605904a6/collector/jmx/src/main/cfg/org.apache.karaf.decanter.collector.jmx-local.cfg
----------------------------------------------------------------------
diff --git a/collector/jmx/src/main/cfg/org.apache.karaf.decanter.collector.jmx-local.cfg
b/collector/jmx/src/main/cfg/org.apache.karaf.decanter.collector.jmx-local.cfg
index bceacbd..d995393 100644
--- a/collector/jmx/src/main/cfg/org.apache.karaf.decanter.collector.jmx-local.cfg
+++ b/collector/jmx/src/main/cfg/org.apache.karaf.decanter.collector.jmx-local.cfg
@@ -20,6 +20,12 @@ url=local
 # some MBeans matching the object name filter
 #object.name=org.apache.camel:context=*,type=routes,name=*
 
+# Several object names can also be specified.
+# What matters is that the property names begin with "object.name".
+#object.name.system=java.lang:*
+#object.name.karaf=org.apache.karaf:type=http,name=*
+#object.name.3=org.apache.activemq:*
+
 # You can add any custom field that the collector will "forward" to the dispatcher
 # For instance:
 #

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/605904a6/collector/jmx/src/main/java/org/apache/karaf/decanter/collector/jmx/JmxCollector.java
----------------------------------------------------------------------
diff --git a/collector/jmx/src/main/java/org/apache/karaf/decanter/collector/jmx/JmxCollector.java
b/collector/jmx/src/main/java/org/apache/karaf/decanter/collector/jmx/JmxCollector.java
index 5af10b4..5541fdb 100644
--- a/collector/jmx/src/main/java/org/apache/karaf/decanter/collector/jmx/JmxCollector.java
+++ b/collector/jmx/src/main/java/org/apache/karaf/decanter/collector/jmx/JmxCollector.java
@@ -17,7 +17,13 @@
 package org.apache.karaf.decanter.collector.jmx;
 
 import java.lang.management.ManagementFactory;
-import java.util.*;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.Set;
 
 import javax.management.MBeanServerConnection;
 import javax.management.MalformedObjectNameException;
@@ -49,50 +55,59 @@ public class JmxCollector implements Runnable {
     private String url;
     private String username;
     private String password;
-    private String objectName;
+    private Set<String> objectNames;
     private EventAdmin eventAdmin;
     private Dictionary<String, Object> properties;
 
     @SuppressWarnings("unchecked")
     public void activate(ComponentContext context) {
-        properties = context.getProperties();
-        String type = getProperty(properties, "type", "jmx-local");
-        String url = getProperty(properties, "url", "local");
-        String username = getProperty(properties, "username", null);
-        String password = getProperty(properties, "password", null);
-        String objectName = getProperty(properties, "object.name", null);
-        Dictionary<String, String> serviceProperties = new Hashtable<String, String>();
+        this.properties = context.getProperties();
+        String type = getProperty(this.properties, "type", "jmx-local");
+        String url = getProperty(this.properties, "url", "local");
+        String username = getProperty(this.properties, "username", null);
+        String password = getProperty(this.properties, "password", null);
+        Dictionary<String, String> serviceProperties = new Hashtable<> ();
         serviceProperties.put("decanter.collector.name", type);
+
         this.type = type;
         this.url = url;
         this.username = username;
         this.password = password;
-        this.objectName = objectName;
+
+        this.objectNames = new HashSet<> ();
+        for (Enumeration<String> e = this.properties.keys(); e.hasMoreElements(); )
{
+        	String key = e.nextElement();
+        	if( "object.name".equals( key ) || key.startsWith( "object.name." )) {
+        		Object value = this.properties.get( key );
+        		if (value != null)
+        			this.objectNames.add( value.toString());
+        	}
+        }
     }
-    
+
     private String getProperty(Dictionary<String, Object> properties, String key, String
defaultValue) {
         return (properties.get(key) != null) ? (String) properties.get(key) : defaultValue;
     }
 
     @Override
     public void run() {
-        LOGGER.debug("Karaf Decanter JMX Collector starts harvesting {}...", type);
+        LOGGER.debug("Karaf Decanter JMX Collector starts harvesting {}...", this.type);
 
         JMXConnector connector = null;
         MBeanServerConnection connection = null;
 
         String host = null;
-        if (url == null || url.equalsIgnoreCase("local")) {
-            LOGGER.debug("Harvesting local MBeanServer ({})...", type);
+        if (this.url == null || this.url.equalsIgnoreCase("local")) {
+            LOGGER.debug("Harvesting local MBeanServer ({})...", this.type);
             connection = ManagementFactory.getPlatformMBeanServer();
         } else {
             try {
-                JMXServiceURL jmxServiceURL = new JMXServiceURL(url);
+                JMXServiceURL jmxServiceURL = new JMXServiceURL(this.url);
                 connector = JMXConnectorFactory.connect(jmxServiceURL, getEnv());
                 connection = connector.getMBeanServerConnection();
                 host = jmxServiceURL.toString();
             } catch (Exception e) {
-                LOGGER.error("Can't connect to MBeanServer {} ({})", url, type, e);
+                LOGGER.error("Can't connect to MBeanServer {} ({})", this.url, this.type,
e);
             }
         }
 
@@ -101,22 +116,28 @@ public class JmxCollector implements Runnable {
             return;
         }
 
+        String currentObjectName = null;
         try {
             String karafName = System.getProperty("karaf.name");
-            BeanHarvester harvester = new BeanHarvester(connection, type, host, karafName);
-            Set<ObjectName> names = connection.queryNames(getObjectName(objectName),
null);
+            BeanHarvester harvester = new BeanHarvester(connection, this.type, host, karafName);
+            Set<ObjectName> names = new HashSet<> ();
+            for (String objectName : this.objectNames) {
+            	currentObjectName = objectName;
+            	names.addAll( connection.queryNames(getObjectName(objectName), null));
+            }
+
             for (ObjectName name : names) {
                 try {
                     Map<String, Object> data = harvester.harvestBean(name);
                     addUserProperties(data);
-                    Event event = new Event("decanter/collect/jmx/" + type + "/" + getTopic(name),
data);
-                    eventAdmin.postEvent(event);
+                    Event event = new Event("decanter/collect/jmx/" + this.type + "/" + getTopic(name),
data);
+                    this.eventAdmin.postEvent(event);
                 } catch (Exception e) {
-                    LOGGER.warn("Can't read MBean {} ({})", name, type, e);
+                    LOGGER.warn("Can't read MBean {} ({})", name, this.type, e);
                 }
             }
         } catch (Exception e) {
-            LOGGER.warn("Can't query object name from {} ({}) {}", url, type, objectName);
+            LOGGER.warn("Can't query object name from {} ({}) {}", this.url, this.type, currentObjectName);
         }
 
         try {
@@ -125,23 +146,23 @@ public class JmxCollector implements Runnable {
             LOGGER.trace("Can't close JMX connector", e);
         }
 
-        LOGGER.debug("Karaf Decanter JMX Collector harvesting {} done", type);
+        LOGGER.debug("Karaf Decanter JMX Collector harvesting {} done", this.type);
     }
 
     private Map<String, Object> getEnv() {
-        Map<String, Object> env = new HashMap<>();
-        if (username != null && password != null) {
-            env.put(JMXConnector.CREDENTIALS, new String[] { username, password });
-        }            
+        Map<String,Object> env = new HashMap<> ();
+        if (this.username != null && this.password != null) {
+            env.put(JMXConnector.CREDENTIALS, new String[] { this.username, this.password
});
+        }
         return env;
     }
 
     private void addUserProperties(Map<String, Object> data) {
-        if (properties != null) {
-            Enumeration<String> keys = properties.keys();
+        if (this.properties != null) {
+            Enumeration<String> keys = this.properties.keys();
             while (keys.hasMoreElements()) {
                 String property = keys.nextElement();
-                data.put(property, properties.get(property));
+                data.put(property, this.properties.get(property));
             }
         }
     }
@@ -158,4 +179,8 @@ public class JmxCollector implements Runnable {
     public void setEventAdmin(EventAdmin eventAdmin) {
         this.eventAdmin = eventAdmin;
     }
+
+	Set<String> getObjectNames() {
+		return this.objectNames;
+	}
 }

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/605904a6/collector/jmx/src/test/java/org/apache/karaf/decanter/collector/jmx/TestMapAttribute.java
----------------------------------------------------------------------
diff --git a/collector/jmx/src/test/java/org/apache/karaf/decanter/collector/jmx/TestMapAttribute.java
b/collector/jmx/src/test/java/org/apache/karaf/decanter/collector/jmx/TestMapAttribute.java
index 8bb9562..ccb6fe7 100644
--- a/collector/jmx/src/test/java/org/apache/karaf/decanter/collector/jmx/TestMapAttribute.java
+++ b/collector/jmx/src/test/java/org/apache/karaf/decanter/collector/jmx/TestMapAttribute.java
@@ -1,7 +1,12 @@
 package org.apache.karaf.decanter.collector.jmx;
 
 import java.lang.management.ManagementFactory;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
 
 import javax.management.MBeanServerConnection;
 import javax.management.MalformedObjectNameException;
@@ -9,6 +14,8 @@ import javax.management.ObjectName;
 
 import org.junit.Assert;
 import org.junit.Test;
+import org.mockito.Mockito;
+import org.osgi.service.component.ComponentContext;
 
 public class TestMapAttribute {
 
@@ -24,4 +31,45 @@ public class TestMapAttribute {
         Assert.assertTrue((Long)freeMem > 10000);
         System.out.println(data);
     }
+
+    @Test
+    public void testSingleObjectName() throws Exception {
+    	ComponentContext ctx = Mockito.mock( ComponentContext.class );
+    	Properties props = new Properties();
+    	Mockito.when( ctx.getProperties()).thenReturn( props );
+
+    	props.put( "object.name", "java.lang:*" );
+    	JmxCollector collector = new JmxCollector();
+    	Assert.assertNull( collector.getObjectNames());
+    	collector.activate( ctx );
+
+    	Set<String> objectNames = collector.getObjectNames();
+    	Assert.assertEquals( 1, objectNames.size());
+    	Assert.assertEquals( "java.lang:*", objectNames.iterator().next());
+    }
+
+    @Test
+    public void testSeveralObjectNames() throws Exception {
+    	ComponentContext ctx = Mockito.mock( ComponentContext.class );
+    	Properties props = new Properties();
+    	Mockito.when( ctx.getProperties()).thenReturn( props );
+
+    	props.put( "object.name.system", "java.lang:*" );
+    	props.put( "object.name", "org.something.else:*" );
+    	props.put( "object.name.karaf", "org.apache.karaf:type=http" );
+    	props.put( "object.name.2", "whatever" );
+    	props.put( "object.name-invalid", "not expected" );
+
+    	JmxCollector collector = new JmxCollector();
+    	Assert.assertNull( collector.getObjectNames());
+    	collector.activate( ctx );
+
+    	List<String> objectNames = new ArrayList<>( collector.getObjectNames());
+    	Collections.sort( objectNames );
+    	Assert.assertEquals( 4, objectNames.size());
+    	Assert.assertEquals( "java.lang:*", objectNames.get( 0 ));
+    	Assert.assertEquals( "org.apache.karaf:type=http", objectNames.get( 1 ));
+    	Assert.assertEquals( "org.something.else:*", objectNames.get( 2 ));
+    	Assert.assertEquals( "whatever", objectNames.get( 3 ));
+    }
 }

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/605904a6/manual/src/main/asciidoc/user-guide/collectors.adoc
----------------------------------------------------------------------
diff --git a/manual/src/main/asciidoc/user-guide/collectors.adoc b/manual/src/main/asciidoc/user-guide/collectors.adoc
index 257a6e1..0b49c85 100644
--- a/manual/src/main/asciidoc/user-guide/collectors.adoc
+++ b/manual/src/main/asciidoc/user-guide/collectors.adoc
@@ -206,6 +206,12 @@ url=local
 # Object name filter to use. Instead of harvesting all MBeans, you can select only
 # some MBeans matching the object name filter
 #object.name=org.apache.camel:context=*,type=routes,name=*
+
+# Several object names can also be specified.
+# What matters is that the property names begin with "object.name".
+#object.name.system=java.lang:*
+#object.name.karaf=org.apache.karaf:type=http,name=*
+#object.name.3=org.apache.activemq:*
 ----
 
 This file harvests the data of the local MBeanServer:
@@ -221,7 +227,7 @@ is secured.
 is secured.
 * the `object.name` property is optional. If this property is not specified, the collector
will retrieve the attributes
 of all MBeans. You can filter to consider only some MBeans. This property contains the ObjectName
filter to retrieve
-the attributes only to some MBeans.
+the attributes only to some MBeans. Several object names can be listed, provided the property
prefix is `object.name.`.
 * any other values will be part of the collected data. It means that you can add your own
property if you want to add
 additional data, and create queries based on this data.
 


Mime
View raw message