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.List;
25 import java.util.Iterator;
26 import java.util.ArrayList;
28 import javax.management.MBeanServerConnection;
29 import javax.management.ObjectName;
31 import org.collectd.api.Collectd;
32 import org.collectd.api.DataSet;
33 import org.collectd.api.DataSource;
34 import org.collectd.api.ValueList;
35 import org.collectd.api.PluginData;
36 import org.collectd.api.OConfigValue;
37 import org.collectd.api.OConfigItem;
39 class GenericJMXConfValue
41 private String ds_name;
43 private List<String> _attributes;
44 private String instance_prefix;
46 private Number genericObjectToNumber (Object obj, int ds_type) /* {{{ */
48 if (obj instanceof String)
50 String str = (String) obj;
54 if (ds_type == DataSource.TYPE_GAUGE)
55 return (new Double (str));
57 return (new Long (str));
59 catch (NumberFormatException e)
64 else if (obj instanceof Integer)
66 return (new Integer ((Integer) obj));
68 else if (obj instanceof Long)
70 return (new Long ((Long) obj));
72 else if (obj instanceof Double)
74 return (new Double ((Double) obj));
78 } /* }}} Number genericObjectToNumber */
80 private Number queryAttribute (MBeanServerConnection conn, /* {{{ */
81 ObjectName objName, String attrName,
88 attrObj = conn.getAttribute (objName, attrName);
92 Collectd.logError ("GenericJMXConfValue.query: getAttribute failed: "
97 return (genericObjectToNumber (attrObj, dsrc.getType ()));
98 } /* }}} int queryAttribute */
100 private String getConfigString (OConfigItem ci) /* {{{ */
102 List<OConfigValue> values;
105 values = ci.getValues ();
106 if (values.size () != 1)
108 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
109 + " configuration option needs exactly one string argument.");
114 if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
116 Collectd.logError ("GenericJMXConfValue: The " + ci.getKey ()
117 + " configuration option needs exactly one string argument.");
121 return (v.getString ());
122 } /* }}} String getConfigString */
127 * Attribute "HeapMemoryUsage"
129 * InstancePrefix "heap-"
132 public GenericJMXConfValue (OConfigItem ci) /* {{{ */
133 throws IllegalArgumentException
135 List<OConfigItem> children;
136 Iterator<OConfigItem> iter;
140 this._attributes = new ArrayList<String> ();
141 this.instance_prefix = null;
144 children = ci.getChildren ();
145 iter = children.iterator ();
146 while (iter.hasNext ())
148 OConfigItem child = iter.next ();
150 if (child.getKey ().equalsIgnoreCase ("Type"))
152 String tmp = getConfigString (child);
156 else if (child.getKey ().equalsIgnoreCase ("Attribute"))
158 String tmp = getConfigString (child);
160 this._attributes.add (tmp);
162 else if (child.getKey ().equalsIgnoreCase ("InstancePrefix"))
164 String tmp = getConfigString (child);
166 this.instance_prefix = tmp;
169 throw (new IllegalArgumentException ("Unknown option: "
173 if (this.ds_name == null)
174 throw (new IllegalArgumentException ("No data set was defined."));
175 else if (this._attributes.size () == 0)
176 throw (new IllegalArgumentException ("No attribute was defined."));
177 } /* }}} GenericJMXConfValue (OConfigItem ci) */
179 public void query (MBeanServerConnection conn, ObjectName objName, /* {{{ */
183 List<DataSource> dsrc;
187 this.ds = Collectd.getDS (this.ds_name);
190 Collectd.logError ("GenericJMXConfValue: Unknown type: "
196 dsrc = this.ds.getDataSources ();
197 if (dsrc.size () != this._attributes.size ())
199 Collectd.logError ("GenericJMXConfValue.query: The data set "
200 + ds_name + " has " + this.ds.getDataSources ().size ()
201 + " data sources, but there were " + this._attributes.size ()
202 + " attributes configured. This doesn't match!");
207 vl = new ValueList (pd);
208 vl.setType (this.ds_name);
209 vl.setTypeInstance (this.instance_prefix);
211 assert (dsrc.size () == this._attributes.size ());
212 for (int i = 0; i < this._attributes.size (); i++)
216 v = queryAttribute (conn, objName, this._attributes.get (i),
220 Collectd.logError ("GenericJMXConfValue.query: "
221 + "Querying attribute " + this._attributes.get (i) + " failed.");
224 Collectd.logDebug ("GenericJMXConfValue.query: dsrc[" + i + "]: v = " + v);
228 Collectd.dispatchValues (vl);
229 } /* }}} void query */
232 /* vim: set sw=2 sts=2 et fdm=marker : */