From e1f4ed33a0dbf6f017ebae8395759893efa6b176 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sun, 2 Aug 2009 17:26:05 +0200 Subject: [PATCH] java bindings: GenericJMX: Add support for "InstanceFrom". This can be used to specify so called "properties" to include in the plugin instance. --- .../collectd/java/GenericJMXConfMBean.java | 82 +++++++++++++++---- 1 file changed, 68 insertions(+), 14 deletions(-) diff --git a/bindings/java/org/collectd/java/GenericJMXConfMBean.java b/bindings/java/org/collectd/java/GenericJMXConfMBean.java index 1b4d9ccf..27e9e329 100644 --- a/bindings/java/org/collectd/java/GenericJMXConfMBean.java +++ b/bindings/java/org/collectd/java/GenericJMXConfMBean.java @@ -39,7 +39,8 @@ class GenericJMXConfMBean { private String _name; /* name by which this mapping is referenced */ private ObjectName _obj_name; - private String _instance; + private String _instance_prefix; + private List _instance_from; private List _values; private String getConfigString (OConfigItem ci) /* {{{ */ @@ -66,10 +67,27 @@ class GenericJMXConfMBean return (v.getString ()); } /* }}} String getConfigString */ + private String join (String separator, List list) /* {{{ */ + { + StringBuffer sb; + + sb = new StringBuffer (); + + for (int i = 0; i < list.size (); i++) + { + if (i > 0) + sb.append ("-"); + sb.append (list.get (i)); + } + + return (sb.toString ()); + } /* }}} String join */ + /* * - * Instance "foobar" * ObjectName "object name" + * InstancePrefix "foobar" + * InstanceFrom "name" * * * : @@ -87,6 +105,8 @@ class GenericJMXConfMBean + "MBean blocks need exactly one string argument.")); this._obj_name = null; + this._instance_prefix = null; + this._instance_from = new ArrayList (); this._values = new ArrayList (); children = ci.getChildren (); @@ -97,13 +117,7 @@ class GenericJMXConfMBean Collectd.logDebug ("GenericJMXConfMBean: child.getKey () = " + child.getKey ()); - if (child.getKey ().equalsIgnoreCase ("Instance")) - { - String tmp = getConfigString (child); - if (tmp != null) - this._instance = tmp; - } - else if (child.getKey ().equalsIgnoreCase ("ObjectName")) + if (child.getKey ().equalsIgnoreCase ("ObjectName")) { String tmp = getConfigString (child); if (tmp == null) @@ -119,6 +133,18 @@ class GenericJMXConfMBean + tmp, e)); } } + else if (child.getKey ().equalsIgnoreCase ("InstancePrefix")) + { + String tmp = getConfigString (child); + if (tmp != null) + this._instance_prefix = tmp; + } + else if (child.getKey ().equalsIgnoreCase ("InstanceFrom")) + { + String tmp = getConfigString (child); + if (tmp != null) + this._instance_from.add (tmp); + } else if (child.getKey ().equalsIgnoreCase ("Value")) { GenericJMXConfValue cv; @@ -168,16 +194,44 @@ class GenericJMXConfMBean iter = names.iterator (); while (iter.hasNext ()) { - ObjectName objName; - PluginData pd_tmp; + ObjectName objName; + PluginData pd_tmp; + List instanceList; + String instance; - objName = iter.next (); - pd_tmp = new PluginData (pd); + objName = iter.next (); + pd_tmp = new PluginData (pd); + instanceList = new ArrayList (); Collectd.logDebug ("GenericJMXConfMBean: objName = " + objName.toString ()); - pd_tmp.setPluginInstance ((this._instance != null) ? this._instance : ""); + for (int i = 0; i < this._instance_from.size (); i++) + { + String propertyName; + String propertyValue; + + propertyName = this._instance_from.get (i); + propertyValue = objName.getKeyProperty (propertyName); + if (propertyValue == null) + { + Collectd.logError ("GenericJMXConfMBean: " + + "No such property in object name: " + propertyName); + } + else + { + instanceList.add (propertyValue); + } + } + + if (this._instance_prefix != null) + instance = new String (this._instance_prefix + + join ("-", instanceList)); + else + instance = join ("-", instanceList); + pd_tmp.setPluginInstance (instance); + + Collectd.logDebug ("GenericJMXConfMBean: instance = " + instance); for (int i = 0; i < this._values.size (); i++) this._values.get (i).query (conn, objName, pd_tmp); -- 2.30.2