2 * collectd/java - org/collectd/java/GenericJMXConfValue.java
3 * Copyright (C) 2009 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
22 package org.collectd.java;
24 import java.util.Arrays;
25 import java.util.List;
27 import java.util.Iterator;
28 import java.util.ArrayList;
30 import java.math.BigDecimal;
31 import java.math.BigInteger;
33 import javax.management.MBeanServerConnection;
34 import javax.management.ObjectName;
35 import javax.management.openmbean.OpenType;
36 import javax.management.openmbean.CompositeData;
37 import javax.management.openmbean.InvalidKeyException;
39 import org.collectd.api.Collectd;
40 import org.collectd.api.DataSet;
41 import org.collectd.api.DataSource;
42 import org.collectd.api.ValueList;
43 import org.collectd.api.PluginData;
44 import org.collectd.api.OConfigValue;
45 import org.collectd.api.OConfigItem;
48 * Representation of a <value /> block and query functionality.
50 * This class represents a <value /> block in the configuration. As
51 * such, the constructor takes an {@link org.collectd.api.OConfigValue} to
52 * construct an object of this class.
54 * The object can then be asked to query data from JMX and dispatch it to
57 * @see GenericJMXConfMBean
59 class GenericJMXConfValue
61 private String _ds_name;
63 private List<String> _attributes;
64 private String _instance_prefix;
65 private List<String> _instance_from;
66 private boolean _is_table;
69 * Converts a generic (OpenType) object to a number.
71 * Returns null if a conversion is not possible or not implemented.
73 private Number genericObjectToNumber (Object obj, int ds_type) /* {{{ */
75 if (obj instanceof String)
77 String str = (String) obj;
81 if (ds_type == DataSource.TYPE_GAUGE)
82 return (new Double (str));
84 return (new Long (str));
86 catch (NumberFormatException e)
91 else if (obj instanceof Byte)
93 return (new Byte ((Byte) obj));
95 else if (obj instanceof Short)
97 return (new Short ((Short) obj));
99 else if (obj instanceof Integer)
101 return (new Integer ((Integer) obj));
103 else if (obj instanceof Long)
105 return (new Long ((Long) obj));
107 else if (obj instanceof Float)
109 return (new Float ((Float) obj));
111 else if (obj instanceof Double)
113 return (new Double ((Double) obj));
115 else if (obj instanceof BigDecimal)
117 return (BigDecimal.ZERO.add ((BigDecimal) obj));
119 else if (obj instanceof BigInteger)
121 return (BigInteger.ZERO.add ((BigInteger) obj));
125 } /* }}} Number genericObjectToNumber */
128 * Converts a generic list to a list of numbers.
130 * Returns null if one or more objects could not be converted.
132 private List<Number> genericListToNumber (List<Object> objects) /* {{{ */
134 List<Number> ret = new ArrayList<Number> ();
135 List<DataSource> dsrc = this._ds.getDataSources ();
137 assert (objects.size () == dsrc.size ());
139 for (int i = 0; i < objects.size (); i++)
143 n = genericObjectToNumber (objects.get (i), dsrc.get (i).getType ());
150 } /* }}} List<Number> genericListToNumber */
153 * Converts a list of CompositeData to a list of numbers.
155 * From each <em>CompositeData </em> the key <em>key</em> is received and all
156 * those values are converted to a number. If one of the
157 * <em>CompositeData</em> doesn't have the specified key or one returned
158 * object cannot converted to a number then the function will return null.
160 private List<Number> genericCompositeToNumber (List<CompositeData> cdlist, /* {{{ */
163 List<Object> objects = new ArrayList<Object> ();
165 for (int i = 0; i < cdlist.size (); i++)
173 value = cd.get (key);
175 catch (InvalidKeyException e)
182 return (genericListToNumber (objects));
183 } /* }}} List<Number> genericCompositeToNumber */
185 private void submitTable (List<Object> objects, ValueList vl, /* {{{ */
186 String instancePrefix)
188 List<CompositeData> cdlist;
189 Set<String> keySet = null;
190 Iterator<String> keyIter;
192 cdlist = new ArrayList<CompositeData> ();
193 for (int i = 0; i < objects.size (); i++)
197 obj = objects.get (i);
198 if (obj instanceof CompositeData)
202 cd = (CompositeData) obj;
205 keySet = cd.getCompositeType ().keySet ();
211 Collectd.logError ("GenericJMXConfValue: At least one of the "
212 + "attributes was not of type `CompositeData', as required "
213 + "when table is set to `true'.");
218 assert (keySet != null);
220 keyIter = keySet.iterator ();
221 while (keyIter.hasNext ())
226 key = keyIter.next ();
227 values = genericCompositeToNumber (cdlist, key);
230 Collectd.logError ("GenericJMXConfValue: Cannot build a list of "
231 + "numbers for key " + key + ". Most likely not all attributes "
236 if (instancePrefix == null)
237 vl.setTypeInstance (key);
239 vl.setTypeInstance (instancePrefix + key);
240 vl.setValues (values);
242 Collectd.dispatchValues (vl);
244 } /* }}} void submitTable */
246 private void submitScalar (List<Object> objects, ValueList vl, /* {{{ */
247 String instancePrefix)
251 values = genericListToNumber (objects);
254 Collectd.logError ("GenericJMXConfValue: Cannot convert list of "
255 + "objects to numbers.");
259 if (instancePrefix == null)
260 vl.setTypeInstance ("");
262 vl.setTypeInstance (instancePrefix);
263 vl.setValues (values);
265 Collectd.dispatchValues (vl);
266 } /* }}} void submitScalar */
268 private Object queryAttributeRecursive (CompositeData parent, /* {{{ */
269 List<String> attrName)
274 key = attrName.remove (0);
278 value = parent.get (key);
280 catch (InvalidKeyException e)
285 if (attrName.size () == 0)
291 if (value instanceof CompositeData)
292 return (queryAttributeRecursive ((CompositeData) value, attrName));
296 } /* }}} queryAttributeRecursive */
298 private Object queryAttribute (MBeanServerConnection conn, /* {{{ */
299 ObjectName objName, String attrName)
301 List<String> attrNameList;
304 String[] attrNameArray;
306 attrNameList = new ArrayList<String> ();
308 attrNameArray = attrName.split ("\\.");
309 key = attrNameArray[0];
310 for (int i = 1; i < attrNameArray.length; i++)
311 attrNameList.add (attrNameArray[i]);
317 value = conn.getAttribute (objName, key);
319 catch (javax.management.AttributeNotFoundException e)
321 value = conn.invoke (objName, key, /* args = */ null, /* types = */ null);
326 Collectd.logError ("GenericJMXConfValue.query: getAttribute failed: "
331 if (attrNameList.size () == 0)
337 if (value instanceof CompositeData)
338 return (queryAttributeRecursive((CompositeData) value, attrNameList));
339 else if (value instanceof OpenType)
341 OpenType ot = (OpenType) value;
342 Collectd.logNotice ("GenericJMXConfValue: Handling of OpenType \""
343 + ot.getTypeName () + "\" is not yet implemented.");
348 Collectd.logError ("GenericJMXConfValue: Received object of "
353 } /* }}} Object queryAttribute */
355 private String join (String separator, List<String> list) /* {{{ */
359 sb = new StringBuffer ();
361 for (int i = 0; i < list.size (); i++)
365 sb.append (list.get (i));
368 return (sb.toString ());
369 } /* }}} String join */
371 private String getConfigString (OConfigItem ci) /* {{{ */
373 List<OConfigValue> values;
376 values = ci.getValues ();
377 if (values.size () != 1)
379 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
380 + " configuration option needs exactly one string argument.");
385 if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
387 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
388 + " configuration option needs exactly one string argument.");
392 return (v.getString ());
393 } /* }}} String getConfigString */
395 private Boolean getConfigBoolean (OConfigItem ci) /* {{{ */
397 List<OConfigValue> values;
401 values = ci.getValues ();
402 if (values.size () != 1)
404 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
405 + " configuration option needs exactly one boolean argument.");
410 if (v.getType () != OConfigValue.OCONFIG_TYPE_BOOLEAN)
412 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
413 + " configuration option needs exactly one boolean argument.");
417 return (new Boolean (v.getBoolean ()));
418 } /* }}} String getConfigBoolean */
421 * Constructs a new value with the configured properties.
423 public GenericJMXConfValue (OConfigItem ci) /* {{{ */
424 throws IllegalArgumentException
426 List<OConfigItem> children;
427 Iterator<OConfigItem> iter;
429 this._ds_name = null;
431 this._attributes = new ArrayList<String> ();
432 this._instance_prefix = null;
433 this._instance_from = new ArrayList<String> ();
434 this._is_table = false;
440 * Attribute "HeapMemoryUsage"
444 * InstancePrefix "heap-"
447 children = ci.getChildren ();
448 iter = children.iterator ();
449 while (iter.hasNext ())
451 OConfigItem child = iter.next ();
453 if (child.getKey ().equalsIgnoreCase ("Type"))
455 String tmp = getConfigString (child);
459 else if (child.getKey ().equalsIgnoreCase ("Table"))
461 Boolean tmp = getConfigBoolean (child);
463 this._is_table = tmp.booleanValue ();
465 else if (child.getKey ().equalsIgnoreCase ("Attribute"))
467 String tmp = getConfigString (child);
469 this._attributes.add (tmp);
471 else if (child.getKey ().equalsIgnoreCase ("InstancePrefix"))
473 String tmp = getConfigString (child);
475 this._instance_prefix = tmp;
477 else if (child.getKey ().equalsIgnoreCase ("InstanceFrom"))
479 String tmp = getConfigString (child);
481 this._instance_from.add (tmp);
484 throw (new IllegalArgumentException ("Unknown option: "
488 if (this._ds_name == null)
489 throw (new IllegalArgumentException ("No data set was defined."));
490 else if (this._attributes.size () == 0)
491 throw (new IllegalArgumentException ("No attribute was defined."));
492 } /* }}} GenericJMXConfValue (OConfigItem ci) */
495 * Query values via JMX according to the object's configuration and dispatch
498 * @param conn Connection to the MBeanServer.
499 * @param objName Object name of the MBean to query.
500 * @param pd Preset naming components. The members host, plugin and
501 * plugin instance will be used.
503 public void query (MBeanServerConnection conn, ObjectName objName, /* {{{ */
507 List<DataSource> dsrc;
509 List<String> instanceList;
510 String instancePrefix;
512 if (this._ds == null)
514 this._ds = Collectd.getDS (this._ds_name);
515 if (this._ds == null)
517 Collectd.logError ("GenericJMXConfValue: Unknown type: "
523 dsrc = this._ds.getDataSources ();
524 if (dsrc.size () != this._attributes.size ())
526 Collectd.logError ("GenericJMXConfValue.query: The data set "
527 + this._ds_name + " has " + this._ds.getDataSources ().size ()
528 + " data sources, but there were " + this._attributes.size ()
529 + " attributes configured. This doesn't match!");
534 vl = new ValueList (pd);
535 vl.setType (this._ds_name);
538 * Build the instnace prefix from the fixed string prefix and the
539 * properties of the objName.
541 instanceList = new ArrayList<String> ();
542 for (int i = 0; i < this._instance_from.size (); i++)
545 String propertyValue;
547 propertyName = this._instance_from.get (i);
548 propertyValue = objName.getKeyProperty (propertyName);
549 if (propertyValue == null)
551 Collectd.logError ("GenericJMXConfMBean: "
552 + "No such property in object name: " + propertyName);
556 instanceList.add (propertyValue);
560 if (this._instance_prefix != null)
561 instancePrefix = new String (this._instance_prefix
562 + join ("-", instanceList));
564 instancePrefix = join ("-", instanceList);
567 * Build a list of `Object's which is then passed to `submitTable' and
570 values = new ArrayList<Object> ();
571 assert (dsrc.size () == this._attributes.size ());
572 for (int i = 0; i < this._attributes.size (); i++)
576 v = queryAttribute (conn, objName, this._attributes.get (i));
579 Collectd.logError ("GenericJMXConfValue.query: "
580 + "Querying attribute " + this._attributes.get (i) + " failed.");
588 submitTable (values, vl, instancePrefix);
590 submitScalar (values, vl, instancePrefix);
591 } /* }}} void query */
592 } /* class GenericJMXConfValue */
594 /* vim: set sw=2 sts=2 et fdm=marker : */