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;
31 import java.util.Collection;
33 import java.util.Iterator;
34 import java.util.ArrayList;
36 import java.math.BigDecimal;
37 import java.math.BigInteger;
39 import javax.management.MBeanServerConnection;
40 import javax.management.ObjectName;
41 import javax.management.openmbean.OpenType;
42 import javax.management.openmbean.CompositeData;
43 import javax.management.openmbean.TabularData;
44 import javax.management.openmbean.InvalidKeyException;
46 import org.collectd.api.Collectd;
47 import org.collectd.api.DataSet;
48 import org.collectd.api.DataSource;
49 import org.collectd.api.ValueList;
50 import org.collectd.api.PluginData;
51 import org.collectd.api.OConfigValue;
52 import org.collectd.api.OConfigItem;
55 * Representation of a <value /> block and query functionality.
57 * This class represents a <value /> block in the configuration. As
58 * such, the constructor takes an {@link org.collectd.api.OConfigValue} to
59 * construct an object of this class.
61 * The object can then be asked to query data from JMX and dispatch it to
64 * @see GenericJMXConfMBean
66 class GenericJMXConfValue
68 private String _ds_name;
70 private List<String> _attributes;
71 private String _instance_prefix;
72 private List<String> _instance_from;
73 private String _plugin_name;
74 private boolean _is_table;
77 * Converts a generic (OpenType) object to a number.
79 * Returns null if a conversion is not possible or not implemented.
81 private Number genericObjectToNumber (Object obj, int ds_type) /* {{{ */
83 if (obj instanceof String)
85 String str = (String) obj;
89 if (ds_type == DataSource.TYPE_GAUGE)
90 return (new Double (str));
92 return (new Long (str));
94 catch (NumberFormatException e)
99 else if (obj instanceof Byte)
101 return (new Byte ((Byte) obj));
103 else if (obj instanceof Short)
105 return (new Short ((Short) obj));
107 else if (obj instanceof Integer)
109 return (new Integer ((Integer) obj));
111 else if (obj instanceof Long)
113 return (new Long ((Long) obj));
115 else if (obj instanceof Float)
117 return (new Float ((Float) obj));
119 else if (obj instanceof Double)
121 return (new Double ((Double) obj));
123 else if (obj instanceof BigDecimal)
125 return (BigDecimal.ZERO.add ((BigDecimal) obj));
127 else if (obj instanceof BigInteger)
129 return (BigInteger.ZERO.add ((BigInteger) obj));
133 } /* }}} Number genericObjectToNumber */
136 * Converts a generic list to a list of numbers.
138 * Returns null if one or more objects could not be converted.
140 private List<Number> genericListToNumber (List<Object> objects) /* {{{ */
142 List<Number> ret = new ArrayList<Number> ();
143 List<DataSource> dsrc = this._ds.getDataSources ();
145 assert (objects.size () == dsrc.size ());
147 for (int i = 0; i < objects.size (); i++)
151 n = genericObjectToNumber (objects.get (i), dsrc.get (i).getType ());
158 } /* }}} List<Number> genericListToNumber */
161 * Converts a list of CompositeData to a list of numbers.
163 * From each <em>CompositeData </em> the key <em>key</em> is received and all
164 * those values are converted to a number. If one of the
165 * <em>CompositeData</em> doesn't have the specified key or one returned
166 * object cannot converted to a number then the function will return null.
168 private List<Number> genericCompositeToNumber (List<CompositeData> cdlist, /* {{{ */
171 List<Object> objects = new ArrayList<Object> ();
173 for (int i = 0; i < cdlist.size (); i++)
181 value = cd.get (key);
183 catch (InvalidKeyException e)
190 return (genericListToNumber (objects));
191 } /* }}} List<Number> genericCompositeToNumber */
193 private void submitTable (List<Object> objects, ValueList vl, /* {{{ */
194 String instancePrefix)
196 List<CompositeData> cdlist;
197 Set<String> keySet = null;
198 Iterator<String> keyIter;
200 cdlist = new ArrayList<CompositeData> ();
201 for (int i = 0; i < objects.size (); i++)
205 obj = objects.get (i);
206 if (obj instanceof CompositeData)
210 cd = (CompositeData) obj;
213 keySet = cd.getCompositeType ().keySet ();
219 Collectd.logError ("GenericJMXConfValue: At least one of the "
220 + "attributes was not of type `CompositeData', as required "
221 + "when table is set to `true'.");
226 assert (keySet != null);
228 keyIter = keySet.iterator ();
229 while (keyIter.hasNext ())
234 key = keyIter.next ();
235 values = genericCompositeToNumber (cdlist, key);
238 Collectd.logError ("GenericJMXConfValue: Cannot build a list of "
239 + "numbers for key " + key + ". Most likely not all attributes "
244 if (instancePrefix == null)
245 vl.setTypeInstance (key);
247 vl.setTypeInstance (instancePrefix + key);
248 vl.setValues (values);
250 Collectd.dispatchValues (vl);
252 } /* }}} void submitTable */
254 private void submitScalar (List<Object> objects, ValueList vl, /* {{{ */
255 String instancePrefix)
259 values = genericListToNumber (objects);
262 Collectd.logError ("GenericJMXConfValue: Cannot convert list of "
263 + "objects to numbers.");
267 if (instancePrefix == null)
268 vl.setTypeInstance ("");
270 vl.setTypeInstance (instancePrefix);
271 vl.setValues (values);
273 Collectd.dispatchValues (vl);
274 } /* }}} void submitScalar */
276 private Object queryAttributeRecursive (CompositeData parent, /* {{{ */
277 List<String> attrName)
282 key = attrName.remove (0);
286 value = parent.get (key);
288 catch (InvalidKeyException e)
293 if (attrName.size () == 0)
299 if (value instanceof CompositeData)
300 return (queryAttributeRecursive ((CompositeData) value, attrName));
301 else if (value instanceof TabularData)
302 return (queryAttributeRecursive ((TabularData) value, attrName));
306 } /* }}} queryAttributeRecursive */
308 private Object queryAttributeRecursive (TabularData parent, /* {{{ */
309 List<String> attrName)
314 key = attrName.remove (0);
316 @SuppressWarnings("unchecked")
317 Collection<CompositeData> table = (Collection<CompositeData>) parent.values();
318 for (CompositeData compositeData : table)
320 if (key.equals(compositeData.get("key")))
322 value = compositeData.get("value");
330 if (attrName.size () == 0)
336 if (value instanceof CompositeData)
337 return (queryAttributeRecursive ((CompositeData) value, attrName));
338 else if (value instanceof TabularData)
339 return (queryAttributeRecursive ((TabularData) value, attrName));
343 } /* }}} queryAttributeRecursive */
345 private Object queryAttribute (MBeanServerConnection conn, /* {{{ */
346 ObjectName objName, String attrName)
348 List<String> attrNameList;
351 String[] attrNameArray;
353 attrNameList = new ArrayList<String> ();
355 attrNameArray = attrName.split ("\\.");
356 key = attrNameArray[0];
357 for (int i = 1; i < attrNameArray.length; i++)
358 attrNameList.add (attrNameArray[i]);
364 value = conn.getAttribute (objName, key);
366 catch (javax.management.AttributeNotFoundException e)
368 value = conn.invoke (objName, key, /* args = */ null, /* types = */ null);
373 Collectd.logError ("GenericJMXConfValue.query: getAttribute failed: "
378 if (attrNameList.size () == 0)
384 if (value instanceof CompositeData)
385 return (queryAttributeRecursive((CompositeData) value, attrNameList));
386 else if (value instanceof TabularData)
387 return (queryAttributeRecursive((TabularData) value, attrNameList));
388 else if (value instanceof OpenType)
390 OpenType ot = (OpenType) value;
391 Collectd.logNotice ("GenericJMXConfValue: Handling of OpenType \""
392 + ot.getTypeName () + "\" is not yet implemented.");
397 Collectd.logError ("GenericJMXConfValue: Received object of "
398 + "unknown class. " + attrName + " " + ((value == null)?"null":value.getClass().getName()));
402 } /* }}} Object queryAttribute */
404 private String join (String separator, List<String> list) /* {{{ */
408 sb = new StringBuffer ();
410 for (int i = 0; i < list.size (); i++)
414 sb.append (list.get (i));
417 return (sb.toString ());
418 } /* }}} String join */
420 private String getConfigString (OConfigItem ci) /* {{{ */
422 List<OConfigValue> values;
425 values = ci.getValues ();
426 if (values.size () != 1)
428 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
429 + " configuration option needs exactly one string argument.");
434 if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
436 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
437 + " configuration option needs exactly one string argument.");
441 return (v.getString ());
442 } /* }}} String getConfigString */
444 private Boolean getConfigBoolean (OConfigItem ci) /* {{{ */
446 List<OConfigValue> values;
450 values = ci.getValues ();
451 if (values.size () != 1)
453 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
454 + " configuration option needs exactly one boolean argument.");
459 if (v.getType () != OConfigValue.OCONFIG_TYPE_BOOLEAN)
461 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
462 + " configuration option needs exactly one boolean argument.");
466 return (new Boolean (v.getBoolean ()));
467 } /* }}} String getConfigBoolean */
470 * Constructs a new value with the configured properties.
472 public GenericJMXConfValue (OConfigItem ci) /* {{{ */
473 throws IllegalArgumentException
475 List<OConfigItem> children;
476 Iterator<OConfigItem> iter;
478 this._ds_name = null;
480 this._attributes = new ArrayList<String> ();
481 this._instance_prefix = null;
482 this._instance_from = new ArrayList<String> ();
483 this._plugin_name = null;
484 this._is_table = false;
490 * Attribute "HeapMemoryUsage"
494 * InstancePrefix "heap-"
497 children = ci.getChildren ();
498 iter = children.iterator ();
499 while (iter.hasNext ())
501 OConfigItem child = iter.next ();
503 if (child.getKey ().equalsIgnoreCase ("Type"))
505 String tmp = getConfigString (child);
509 else if (child.getKey ().equalsIgnoreCase ("Table"))
511 Boolean tmp = getConfigBoolean (child);
513 this._is_table = tmp.booleanValue ();
515 else if (child.getKey ().equalsIgnoreCase ("Attribute"))
517 String tmp = getConfigString (child);
519 this._attributes.add (tmp);
521 else if (child.getKey ().equalsIgnoreCase ("InstancePrefix"))
523 String tmp = getConfigString (child);
525 this._instance_prefix = tmp;
527 else if (child.getKey ().equalsIgnoreCase ("InstanceFrom"))
529 String tmp = getConfigString (child);
531 this._instance_from.add (tmp);
533 else if (child.getKey ().equalsIgnoreCase ("PluginName"))
535 String tmp = getConfigString (child);
537 this._plugin_name = tmp;
540 throw (new IllegalArgumentException ("Unknown option: "
544 if (this._ds_name == null)
545 throw (new IllegalArgumentException ("No data set was defined."));
546 else if (this._attributes.size () == 0)
547 throw (new IllegalArgumentException ("No attribute was defined."));
548 } /* }}} GenericJMXConfValue (OConfigItem ci) */
551 * Query values via JMX according to the object's configuration and dispatch
554 * @param conn Connection to the MBeanServer.
555 * @param objName Object name of the MBean to query.
556 * @param pd Preset naming components. The members host, plugin and
557 * plugin instance will be used.
559 public void query (MBeanServerConnection conn, ObjectName objName, /* {{{ */
563 List<DataSource> dsrc;
565 List<String> instanceList;
566 String instancePrefix;
568 if (this._ds == null)
570 this._ds = Collectd.getDS (this._ds_name);
571 if (this._ds == null)
573 Collectd.logError ("GenericJMXConfValue: Unknown type: "
579 dsrc = this._ds.getDataSources ();
580 if (dsrc.size () != this._attributes.size ())
582 Collectd.logError ("GenericJMXConfValue.query: The data set "
583 + this._ds_name + " has " + this._ds.getDataSources ().size ()
584 + " data sources, but there were " + this._attributes.size ()
585 + " attributes configured. This doesn't match!");
590 vl = new ValueList (pd);
591 vl.setType (this._ds_name);
592 if (this._plugin_name != null)
594 vl.setPlugin (this._plugin_name);
598 * Build the instnace prefix from the fixed string prefix and the
599 * properties of the objName.
601 instanceList = new ArrayList<String> ();
602 for (int i = 0; i < this._instance_from.size (); i++)
605 String propertyValue;
607 propertyName = this._instance_from.get (i);
608 propertyValue = objName.getKeyProperty (propertyName);
609 if (propertyValue == null)
611 Collectd.logError ("GenericJMXConfMBean: "
612 + "No such property in object name: " + propertyName);
616 instanceList.add (propertyValue);
620 if (this._instance_prefix != null)
621 instancePrefix = new String (this._instance_prefix
622 + join ("-", instanceList));
624 instancePrefix = join ("-", instanceList);
627 * Build a list of `Object's which is then passed to `submitTable' and
630 values = new ArrayList<Object> ();
631 assert (dsrc.size () == this._attributes.size ());
632 for (int i = 0; i < this._attributes.size (); i++)
636 v = queryAttribute (conn, objName, this._attributes.get (i));
639 Collectd.logError ("GenericJMXConfValue.query: "
640 + "Querying attribute " + this._attributes.get (i) + " failed.");
648 submitTable (values, vl, instancePrefix);
650 submitScalar (values, vl, instancePrefix);
651 } /* }}} void query */
652 } /* class GenericJMXConfValue */
654 /* vim: set sw=2 sts=2 et fdm=marker : */