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 boolean _is_table;
68 * Converts a generic (OpenType) object to a number.
70 * Returns null if a conversion is not possible or not implemented.
72 private Number genericObjectToNumber (Object obj, int ds_type) /* {{{ */
74 if (obj instanceof String)
76 String str = (String) obj;
80 if (ds_type == DataSource.TYPE_GAUGE)
81 return (new Double (str));
83 return (new Long (str));
85 catch (NumberFormatException e)
90 else if (obj instanceof Byte)
92 return (new Byte ((Byte) obj));
94 else if (obj instanceof Short)
96 return (new Short ((Short) obj));
98 else if (obj instanceof Integer)
100 return (new Integer ((Integer) obj));
102 else if (obj instanceof Long)
104 return (new Long ((Long) obj));
106 else if (obj instanceof Float)
108 return (new Float ((Float) obj));
110 else if (obj instanceof Double)
112 return (new Double ((Double) obj));
114 else if (obj instanceof BigDecimal)
116 return (BigDecimal.ZERO.add ((BigDecimal) obj));
118 else if (obj instanceof BigInteger)
120 return (BigInteger.ZERO.add ((BigInteger) obj));
124 } /* }}} Number genericObjectToNumber */
126 private List<Number> genericListToNumber (List<Object> objects) /* {{{ */
128 List<Number> ret = new ArrayList<Number> ();
129 List<DataSource> dsrc = this._ds.getDataSources ();
131 assert (objects.size () == dsrc.size ());
133 for (int i = 0; i < objects.size (); i++)
137 n = genericObjectToNumber (objects.get (i), dsrc.get (i).getType ());
144 } /* }}} List<Number> genericListToNumber */
146 private List<Number> genericCompositeToNumber (List<CompositeData> cdlist, /* {{{ */
149 List<Object> objects = new ArrayList<Object> ();
151 for (int i = 0; i < cdlist.size (); i++)
159 value = cd.get (key);
161 catch (InvalidKeyException e)
168 return (genericListToNumber (objects));
169 } /* }}} List<Number> genericCompositeToNumber */
171 private void submitTable (List<Object> objects, ValueList vl) /* {{{ */
173 List<CompositeData> cdlist;
174 Set<String> keySet = null;
175 Iterator<String> keyIter;
177 cdlist = new ArrayList<CompositeData> ();
178 for (int i = 0; i < objects.size (); i++)
182 obj = objects.get (i);
183 if (obj instanceof CompositeData)
187 cd = (CompositeData) obj;
190 keySet = cd.getCompositeType ().keySet ();
196 Collectd.logError ("GenericJMXConfValue: At least one of the "
197 + "attributes was not of type `CompositeData', as required "
198 + "when table is set to `true'.");
203 assert (keySet != null);
205 keyIter = keySet.iterator ();
206 while (keyIter.hasNext ())
211 key = keyIter.next ();
212 values = genericCompositeToNumber (cdlist, key);
215 Collectd.logError ("GenericJMXConfValue: Cannot build a list of "
216 + "numbers for key " + key + ". Most likely not all attributes "
221 if (this._instance_prefix == null)
222 vl.setTypeInstance (key);
224 vl.setTypeInstance (this._instance_prefix + key);
225 vl.setValues (values);
227 Collectd.dispatchValues (vl);
229 } /* }}} void submitTable */
231 private void submitScalar (List<Object> objects, ValueList vl) /* {{{ */
235 values = genericListToNumber (objects);
238 Collectd.logError ("GenericJMXConfValue: Cannot convert list of "
239 + "objects to numbers.");
243 if (this._instance_prefix == null)
244 vl.setTypeInstance ("");
246 vl.setTypeInstance (this._instance_prefix);
247 vl.setValues (values);
249 Collectd.dispatchValues (vl);
250 } /* }}} void submitScalar */
252 private Object queryAttributeRecursive (CompositeData parent, /* {{{ */
253 List<String> attrName)
258 key = attrName.remove (0);
262 value = parent.get (key);
264 catch (InvalidKeyException e)
269 if (attrName.size () == 0)
275 if (value instanceof CompositeData)
276 return (queryAttributeRecursive ((CompositeData) value, attrName));
280 } /* }}} queryAttributeRecursive */
282 private Object queryAttribute (MBeanServerConnection conn, /* {{{ */
283 ObjectName objName, String attrName)
285 List<String> attrNameList;
288 String[] attrNameArray;
290 attrNameList = new ArrayList<String> ();
292 attrNameArray = attrName.split ("\\.");
293 key = attrNameArray[0];
294 for (int i = 1; i < attrNameArray.length; i++)
295 attrNameList.add (attrNameArray[i]);
299 value = conn.getAttribute (objName, key);
303 Collectd.logError ("GenericJMXConfValue.query: getAttribute failed: "
308 if (attrNameList.size () == 0)
314 if (value instanceof CompositeData)
315 return (queryAttributeRecursive((CompositeData) value, attrNameList));
316 else if (value instanceof OpenType)
318 OpenType ot = (OpenType) value;
319 Collectd.logNotice ("GenericJMXConfValue: Handling of OpenType \""
320 + ot.getTypeName () + "\" is not yet implemented.");
325 Collectd.logError ("GenericJMXConfValue: Received object of "
330 } /* }}} Object queryAttribute */
332 private String getConfigString (OConfigItem ci) /* {{{ */
334 List<OConfigValue> values;
337 values = ci.getValues ();
338 if (values.size () != 1)
340 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
341 + " configuration option needs exactly one string argument.");
346 if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
348 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
349 + " configuration option needs exactly one string argument.");
353 return (v.getString ());
354 } /* }}} String getConfigString */
356 private Boolean getConfigBoolean (OConfigItem ci) /* {{{ */
358 List<OConfigValue> values;
362 values = ci.getValues ();
363 if (values.size () != 1)
365 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
366 + " configuration option needs exactly one boolean argument.");
371 if (v.getType () != OConfigValue.OCONFIG_TYPE_BOOLEAN)
373 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
374 + " configuration option needs exactly one boolean argument.");
378 return (new Boolean (v.getBoolean ()));
379 } /* }}} String getConfigBoolean */
382 * Constructs a new value with the configured properties.
384 public GenericJMXConfValue (OConfigItem ci) /* {{{ */
385 throws IllegalArgumentException
387 List<OConfigItem> children;
388 Iterator<OConfigItem> iter;
390 this._ds_name = null;
392 this._attributes = new ArrayList<String> ();
393 this._instance_prefix = null;
394 this._is_table = false;
400 * Attribute "HeapMemoryUsage"
404 * InstancePrefix "heap-"
407 children = ci.getChildren ();
408 iter = children.iterator ();
409 while (iter.hasNext ())
411 OConfigItem child = iter.next ();
413 if (child.getKey ().equalsIgnoreCase ("Type"))
415 String tmp = getConfigString (child);
419 else if (child.getKey ().equalsIgnoreCase ("Table"))
421 Boolean tmp = getConfigBoolean (child);
423 this._is_table = tmp.booleanValue ();
425 else if (child.getKey ().equalsIgnoreCase ("Attribute"))
427 String tmp = getConfigString (child);
429 this._attributes.add (tmp);
431 else if (child.getKey ().equalsIgnoreCase ("InstancePrefix"))
433 String tmp = getConfigString (child);
435 this._instance_prefix = tmp;
438 throw (new IllegalArgumentException ("Unknown option: "
442 if (this._ds_name == null)
443 throw (new IllegalArgumentException ("No data set was defined."));
444 else if (this._attributes.size () == 0)
445 throw (new IllegalArgumentException ("No attribute was defined."));
446 } /* }}} GenericJMXConfValue (OConfigItem ci) */
449 * Query values via JMX according to the object's configuration and dispatch
452 * @param conn Connection to the MBeanServer.
453 * @param objName Object name of the MBean to query.
454 * @param pd Preset naming components. The members host, plugin and
455 * plugin instance will be used.
457 public void query (MBeanServerConnection conn, ObjectName objName, /* {{{ */
461 List<DataSource> dsrc;
464 if (this._ds == null)
466 this._ds = Collectd.getDS (this._ds_name);
467 if (this._ds == null)
469 Collectd.logError ("GenericJMXConfValue: Unknown type: "
475 dsrc = this._ds.getDataSources ();
476 if (dsrc.size () != this._attributes.size ())
478 Collectd.logError ("GenericJMXConfValue.query: The data set "
479 + this._ds_name + " has " + this._ds.getDataSources ().size ()
480 + " data sources, but there were " + this._attributes.size ()
481 + " attributes configured. This doesn't match!");
486 vl = new ValueList (pd);
487 vl.setType (this._ds_name);
488 vl.setTypeInstance (this._instance_prefix);
490 values = new ArrayList<Object> ();
492 assert (dsrc.size () == this._attributes.size ());
493 for (int i = 0; i < this._attributes.size (); i++)
497 v = queryAttribute (conn, objName, this._attributes.get (i));
500 Collectd.logError ("GenericJMXConfValue.query: "
501 + "Querying attribute " + this._attributes.get (i) + " failed.");
509 submitTable (values, vl);
511 submitScalar (values, vl);
512 } /* }}} void query */
515 /* vim: set sw=2 sts=2 et fdm=marker : */