2 * collectd - bindings/java/org/collectd/java/GenericJMXConfValue.java
3 * Copyright (C) 2009 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
27 package org.collectd.java;
29 import java.util.Arrays;
30 import java.util.List;
32 import java.util.Iterator;
33 import java.util.ArrayList;
35 import java.math.BigDecimal;
36 import java.math.BigInteger;
38 import javax.management.MBeanServerConnection;
39 import javax.management.ObjectName;
40 import javax.management.openmbean.OpenType;
41 import javax.management.openmbean.CompositeData;
42 import javax.management.openmbean.InvalidKeyException;
44 import org.collectd.api.Collectd;
45 import org.collectd.api.DataSet;
46 import org.collectd.api.DataSource;
47 import org.collectd.api.ValueList;
48 import org.collectd.api.PluginData;
49 import org.collectd.api.OConfigValue;
50 import org.collectd.api.OConfigItem;
53 * Representation of a <value /> block and query functionality.
55 * This class represents a <value /> block in the configuration. As
56 * such, the constructor takes an {@link org.collectd.api.OConfigValue} to
57 * construct an object of this class.
59 * The object can then be asked to query data from JMX and dispatch it to
62 * @see GenericJMXConfMBean
64 class GenericJMXConfValue
66 private String _ds_name;
68 private List<String> _attributes;
69 private String _instance_prefix;
70 private List<String> _instance_from;
71 private boolean _is_table;
74 * Converts a generic (OpenType) object to a number.
76 * Returns null if a conversion is not possible or not implemented.
78 private Number genericObjectToNumber (Object obj, int ds_type) /* {{{ */
80 if (obj instanceof String)
82 String str = (String) obj;
86 if (ds_type == DataSource.TYPE_GAUGE)
87 return (new Double (str));
89 return (new Long (str));
91 catch (NumberFormatException e)
96 else if (obj instanceof Byte)
98 return (new Byte ((Byte) obj));
100 else if (obj instanceof Short)
102 return (new Short ((Short) obj));
104 else if (obj instanceof Integer)
106 return (new Integer ((Integer) obj));
108 else if (obj instanceof Long)
110 return (new Long ((Long) obj));
112 else if (obj instanceof Float)
114 return (new Float ((Float) obj));
116 else if (obj instanceof Double)
118 return (new Double ((Double) obj));
120 else if (obj instanceof BigDecimal)
122 return (BigDecimal.ZERO.add ((BigDecimal) obj));
124 else if (obj instanceof BigInteger)
126 return (BigInteger.ZERO.add ((BigInteger) obj));
130 } /* }}} Number genericObjectToNumber */
133 * Converts a generic list to a list of numbers.
135 * Returns null if one or more objects could not be converted.
137 private List<Number> genericListToNumber (List<Object> objects) /* {{{ */
139 List<Number> ret = new ArrayList<Number> ();
140 List<DataSource> dsrc = this._ds.getDataSources ();
142 assert (objects.size () == dsrc.size ());
144 for (int i = 0; i < objects.size (); i++)
148 n = genericObjectToNumber (objects.get (i), dsrc.get (i).getType ());
155 } /* }}} List<Number> genericListToNumber */
158 * Converts a list of CompositeData to a list of numbers.
160 * From each <em>CompositeData </em> the key <em>key</em> is received and all
161 * those values are converted to a number. If one of the
162 * <em>CompositeData</em> doesn't have the specified key or one returned
163 * object cannot converted to a number then the function will return null.
165 private List<Number> genericCompositeToNumber (List<CompositeData> cdlist, /* {{{ */
168 List<Object> objects = new ArrayList<Object> ();
170 for (int i = 0; i < cdlist.size (); i++)
178 value = cd.get (key);
180 catch (InvalidKeyException e)
187 return (genericListToNumber (objects));
188 } /* }}} List<Number> genericCompositeToNumber */
190 private void submitTable (List<Object> objects, ValueList vl, /* {{{ */
191 String instancePrefix)
193 List<CompositeData> cdlist;
194 Set<String> keySet = null;
195 Iterator<String> keyIter;
197 cdlist = new ArrayList<CompositeData> ();
198 for (int i = 0; i < objects.size (); i++)
202 obj = objects.get (i);
203 if (obj instanceof CompositeData)
207 cd = (CompositeData) obj;
210 keySet = cd.getCompositeType ().keySet ();
216 Collectd.logError ("GenericJMXConfValue: At least one of the "
217 + "attributes was not of type `CompositeData', as required "
218 + "when table is set to `true'.");
223 assert (keySet != null);
225 keyIter = keySet.iterator ();
226 while (keyIter.hasNext ())
231 key = keyIter.next ();
232 values = genericCompositeToNumber (cdlist, key);
235 Collectd.logError ("GenericJMXConfValue: Cannot build a list of "
236 + "numbers for key " + key + ". Most likely not all attributes "
241 if (instancePrefix == null)
242 vl.setTypeInstance (key);
244 vl.setTypeInstance (instancePrefix + key);
245 vl.setValues (values);
247 Collectd.dispatchValues (vl);
249 } /* }}} void submitTable */
251 private void submitScalar (List<Object> objects, ValueList vl, /* {{{ */
252 String instancePrefix)
256 values = genericListToNumber (objects);
259 Collectd.logError ("GenericJMXConfValue: Cannot convert list of "
260 + "objects to numbers.");
264 if (instancePrefix == null)
265 vl.setTypeInstance ("");
267 vl.setTypeInstance (instancePrefix);
268 vl.setValues (values);
270 Collectd.dispatchValues (vl);
271 } /* }}} void submitScalar */
273 private Object queryAttributeRecursive (CompositeData parent, /* {{{ */
274 List<String> attrName)
279 key = attrName.remove (0);
283 value = parent.get (key);
285 catch (InvalidKeyException e)
290 if (attrName.size () == 0)
296 if (value instanceof CompositeData)
297 return (queryAttributeRecursive ((CompositeData) value, attrName));
301 } /* }}} queryAttributeRecursive */
303 private Object queryAttribute (MBeanServerConnection conn, /* {{{ */
304 ObjectName objName, String attrName)
306 List<String> attrNameList;
309 String[] attrNameArray;
311 attrNameList = new ArrayList<String> ();
313 attrNameArray = attrName.split ("\\.");
314 key = attrNameArray[0];
315 for (int i = 1; i < attrNameArray.length; i++)
316 attrNameList.add (attrNameArray[i]);
322 value = conn.getAttribute (objName, key);
324 catch (javax.management.AttributeNotFoundException e)
326 value = conn.invoke (objName, key, /* args = */ null, /* types = */ null);
331 Collectd.logError ("GenericJMXConfValue.query: getAttribute failed: "
336 if (attrNameList.size () == 0)
342 if (value instanceof CompositeData)
343 return (queryAttributeRecursive((CompositeData) value, attrNameList));
344 else if (value instanceof OpenType)
346 OpenType ot = (OpenType) value;
347 Collectd.logNotice ("GenericJMXConfValue: Handling of OpenType \""
348 + ot.getTypeName () + "\" is not yet implemented.");
353 Collectd.logError ("GenericJMXConfValue: Received object of "
358 } /* }}} Object queryAttribute */
360 private String join (String separator, List<String> list) /* {{{ */
364 sb = new StringBuffer ();
366 for (int i = 0; i < list.size (); i++)
370 sb.append (list.get (i));
373 return (sb.toString ());
374 } /* }}} String join */
376 private String getConfigString (OConfigItem ci) /* {{{ */
378 List<OConfigValue> values;
381 values = ci.getValues ();
382 if (values.size () != 1)
384 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
385 + " configuration option needs exactly one string argument.");
390 if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
392 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
393 + " configuration option needs exactly one string argument.");
397 return (v.getString ());
398 } /* }}} String getConfigString */
400 private Boolean getConfigBoolean (OConfigItem ci) /* {{{ */
402 List<OConfigValue> values;
406 values = ci.getValues ();
407 if (values.size () != 1)
409 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
410 + " configuration option needs exactly one boolean argument.");
415 if (v.getType () != OConfigValue.OCONFIG_TYPE_BOOLEAN)
417 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
418 + " configuration option needs exactly one boolean argument.");
422 return (new Boolean (v.getBoolean ()));
423 } /* }}} String getConfigBoolean */
426 * Constructs a new value with the configured properties.
428 public GenericJMXConfValue (OConfigItem ci) /* {{{ */
429 throws IllegalArgumentException
431 List<OConfigItem> children;
432 Iterator<OConfigItem> iter;
434 this._ds_name = null;
436 this._attributes = new ArrayList<String> ();
437 this._instance_prefix = null;
438 this._instance_from = new ArrayList<String> ();
439 this._is_table = false;
445 * Attribute "HeapMemoryUsage"
449 * InstancePrefix "heap-"
452 children = ci.getChildren ();
453 iter = children.iterator ();
454 while (iter.hasNext ())
456 OConfigItem child = iter.next ();
458 if (child.getKey ().equalsIgnoreCase ("Type"))
460 String tmp = getConfigString (child);
464 else if (child.getKey ().equalsIgnoreCase ("Table"))
466 Boolean tmp = getConfigBoolean (child);
468 this._is_table = tmp.booleanValue ();
470 else if (child.getKey ().equalsIgnoreCase ("Attribute"))
472 String tmp = getConfigString (child);
474 this._attributes.add (tmp);
476 else if (child.getKey ().equalsIgnoreCase ("InstancePrefix"))
478 String tmp = getConfigString (child);
480 this._instance_prefix = tmp;
482 else if (child.getKey ().equalsIgnoreCase ("InstanceFrom"))
484 String tmp = getConfigString (child);
486 this._instance_from.add (tmp);
489 throw (new IllegalArgumentException ("Unknown option: "
493 if (this._ds_name == null)
494 throw (new IllegalArgumentException ("No data set was defined."));
495 else if (this._attributes.size () == 0)
496 throw (new IllegalArgumentException ("No attribute was defined."));
497 } /* }}} GenericJMXConfValue (OConfigItem ci) */
500 * Query values via JMX according to the object's configuration and dispatch
503 * @param conn Connection to the MBeanServer.
504 * @param objName Object name of the MBean to query.
505 * @param pd Preset naming components. The members host, plugin and
506 * plugin instance will be used.
508 public void query (MBeanServerConnection conn, ObjectName objName, /* {{{ */
512 List<DataSource> dsrc;
514 List<String> instanceList;
515 String instancePrefix;
517 if (this._ds == null)
519 this._ds = Collectd.getDS (this._ds_name);
520 if (this._ds == null)
522 Collectd.logError ("GenericJMXConfValue: Unknown type: "
528 dsrc = this._ds.getDataSources ();
529 if (dsrc.size () != this._attributes.size ())
531 Collectd.logError ("GenericJMXConfValue.query: The data set "
532 + this._ds_name + " has " + this._ds.getDataSources ().size ()
533 + " data sources, but there were " + this._attributes.size ()
534 + " attributes configured. This doesn't match!");
539 vl = new ValueList (pd);
540 vl.setType (this._ds_name);
543 * Build the instnace prefix from the fixed string prefix and the
544 * properties of the objName.
546 instanceList = new ArrayList<String> ();
547 for (int i = 0; i < this._instance_from.size (); i++)
550 String propertyValue;
552 propertyName = this._instance_from.get (i);
553 propertyValue = objName.getKeyProperty (propertyName);
554 if (propertyValue == null)
556 Collectd.logError ("GenericJMXConfMBean: "
557 + "No such property in object name: " + propertyName);
561 instanceList.add (propertyValue);
565 if (this._instance_prefix != null)
566 instancePrefix = new String (this._instance_prefix
567 + join ("-", instanceList));
569 instancePrefix = join ("-", instanceList);
572 * Build a list of `Object's which is then passed to `submitTable' and
575 values = new ArrayList<Object> ();
576 assert (dsrc.size () == this._attributes.size ());
577 for (int i = 0; i < this._attributes.size (); i++)
581 v = queryAttribute (conn, objName, this._attributes.get (i));
584 Collectd.logError ("GenericJMXConfValue.query: "
585 + "Querying attribute " + this._attributes.get (i) + " failed.");
593 submitTable (values, vl, instancePrefix);
595 submitScalar (values, vl, instancePrefix);
596 } /* }}} void query */
597 } /* class GenericJMXConfValue */
599 /* vim: set sw=2 sts=2 et fdm=marker : */