2 * collectd/java - org/collectd/java/GenericJMXConfConnection.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;
30 import javax.management.MalformedObjectNameException;
32 import javax.management.remote.JMXServiceURL;
33 import javax.management.remote.JMXConnector;
34 import javax.management.remote.JMXConnectorFactory;
36 import org.collectd.api.Collectd;
37 import org.collectd.api.PluginData;
38 import org.collectd.api.OConfigValue;
39 import org.collectd.api.OConfigItem;
41 class GenericJMXConfConnection
43 private String _host = null;
44 private String _service_url = null;
45 private MBeanServerConnection _jmx_connection = null;
46 private List<GenericJMXConfMBean> _mbeans = null;
51 private String getConfigString (OConfigItem ci) /* {{{ */
53 List<OConfigValue> values;
56 values = ci.getValues ();
57 if (values.size () != 1)
59 Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
60 + " configuration option needs exactly one string argument.");
65 if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
67 Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
68 + " configuration option needs exactly one string argument.");
72 return (v.getString ());
73 } /* }}} String getConfigString */
75 private void connect () /* {{{ */
77 JMXServiceURL service_url;
78 JMXConnector connector;
80 if (_jmx_connection != null)
85 service_url = new JMXServiceURL (this._service_url);
86 connector = JMXConnectorFactory.connect (service_url);
87 _jmx_connection = connector.getMBeanServerConnection ();
91 Collectd.logError ("GenericJMXConfConnection: "
92 + "Creating MBean server connection failed: " + e);
95 } /* }}} void connect */
101 * Host "tomcat0.mycompany"
102 * ServiceURL "service:jmx:rmi:///jndi/rmi://localhost:17264/jmxrmi"
103 * Collect "java.lang:type=GarbageCollector,name=Copy"
104 * Collect "java.lang:type=Memory"
108 public GenericJMXConfConnection (OConfigItem ci) /* {{{ */
109 throws IllegalArgumentException
111 List<OConfigItem> children;
112 Iterator<OConfigItem> iter;
114 this._mbeans = new ArrayList<GenericJMXConfMBean> ();
116 children = ci.getChildren ();
117 iter = children.iterator ();
118 while (iter.hasNext ())
120 OConfigItem child = iter.next ();
122 if (child.getKey ().equalsIgnoreCase ("Host"))
124 String tmp = getConfigString (child);
128 else if (child.getKey ().equalsIgnoreCase ("ServiceURL"))
130 String tmp = getConfigString (child);
132 this._service_url = tmp;
134 else if (child.getKey ().equalsIgnoreCase ("Collect"))
136 String tmp = getConfigString (child);
139 GenericJMXConfMBean mbean;
141 mbean = GenericJMX.getMBean (tmp);
143 throw (new IllegalArgumentException ("No such MBean defined: "
144 + tmp + ". Please make sure all `MBean' blocks appear "
145 + "before (above) all `Connection' blocks."));
146 Collectd.logDebug ("GenericJMXConfConnection: " + this._host + ": Add " + tmp);
147 this._mbeans.add (mbean);
151 throw (new IllegalArgumentException ("Unknown option: "
155 if (this._service_url == null)
156 throw (new IllegalArgumentException ("No service URL was defined."));
157 if (this._mbeans.size () == 0)
158 throw (new IllegalArgumentException ("No valid collect statement "
160 } /* }}} GenericJMXConfConnection (OConfigItem ci) */
162 public void query () /* {{{ */
168 if (this._jmx_connection == null)
171 Collectd.logDebug ("GenericJMXConfConnection.query: "
172 + "Reading " + this._mbeans.size () + " mbeans from "
173 + ((this._host != null) ? this._host : "(null)"));
175 pd = new PluginData ();
176 pd.setHost ((this._host != null) ? this._host : "localhost");
177 pd.setPlugin ("GenericJMX");
179 for (int i = 0; i < this._mbeans.size (); i++)
180 this._mbeans.get (i).query (this._jmx_connection, pd);
181 } /* }}} void query */
183 public String toString ()
185 return (new String ("host = " + this._host + "; "
186 + "url = " + this._service_url));
190 /* vim: set sw=2 sts=2 et fdm=marker : */