2 * collectd - bindings/java/org/collectd/java/GenericJMXConfConnection.java
3 * Copyright (C) 2009-2012 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.List;
31 import java.util.Iterator;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.net.InetAddress;
35 import java.net.UnknownHostException;
37 import javax.management.MBeanServerConnection;
39 import javax.management.remote.JMXServiceURL;
40 import javax.management.remote.JMXConnector;
41 import javax.management.remote.JMXConnectorFactory;
43 import org.collectd.api.Collectd;
44 import org.collectd.api.PluginData;
45 import org.collectd.api.OConfigValue;
46 import org.collectd.api.OConfigItem;
48 class GenericJMXConfConnection
50 private String _username = null;
51 private String _password = null;
52 private String _host = null;
53 private String _instance_prefix = null;
54 private String _service_url = null;
55 private JMXConnector _jmx_connector = null;
56 private MBeanServerConnection _mbean_connection = null;
57 private List<GenericJMXConfMBean> _mbeans = null;
62 private String getConfigString (OConfigItem ci) /* {{{ */
64 List<OConfigValue> values;
67 values = ci.getValues ();
68 if (values.size () != 1)
70 Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
71 + " configuration option needs exactly one string argument.");
76 if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
78 Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
79 + " configuration option needs exactly one string argument.");
83 return (v.getString ());
84 } /* }}} String getConfigString */
86 private String getHost () /* {{{ */
88 if (this._host != null)
93 return Collectd.getHostname();
94 } /* }}} String getHost */
96 private void connect () /* {{{ */
98 JMXServiceURL service_url;
99 Map<String,Object> environment;
102 if (this._jmx_connector != null) {
107 if (this._password != null)
109 String[] credentials;
111 if (this._username == null)
112 this._username = new String ("monitorRole");
114 credentials = new String[] { this._username, this._password };
116 environment = new HashMap<String,Object> ();
117 environment.put (JMXConnector.CREDENTIALS, credentials);
118 environment.put (JMXConnectorFactory.PROTOCOL_PROVIDER_CLASS_LOADER, this.getClass().getClassLoader());
123 service_url = new JMXServiceURL (this._service_url);
124 this._jmx_connector = JMXConnectorFactory.connect (service_url, environment);
125 this._mbean_connection = _jmx_connector.getMBeanServerConnection ();
129 Collectd.logError ("GenericJMXConfConnection: "
130 + "Creating MBean server connection failed: " + e);
134 } /* }}} void connect */
136 private void disconnect () /* {{{ */
140 if (this._jmx_connector != null) {
141 this._jmx_connector.close();
146 // It's fine if close throws an exception
149 this._jmx_connector = null;
150 this._mbean_connection = null;
151 } /* }}} void disconnect */
157 * Host "tomcat0.mycompany"
158 * ServiceURL "service:jmx:rmi:///jndi/rmi://localhost:17264/jmxrmi"
159 * Collect "java.lang:type=GarbageCollector,name=Copy"
160 * Collect "java.lang:type=Memory"
164 public GenericJMXConfConnection (OConfigItem ci) /* {{{ */
165 throws IllegalArgumentException
167 List<OConfigItem> children;
168 Iterator<OConfigItem> iter;
170 this._mbeans = new ArrayList<GenericJMXConfMBean> ();
172 children = ci.getChildren ();
173 iter = children.iterator ();
174 while (iter.hasNext ())
176 OConfigItem child = iter.next ();
178 if (child.getKey ().equalsIgnoreCase ("Host"))
180 String tmp = getConfigString (child);
184 else if (child.getKey ().equalsIgnoreCase ("User"))
186 String tmp = getConfigString (child);
188 this._username = tmp;
190 else if (child.getKey ().equalsIgnoreCase ("Password"))
192 String tmp = getConfigString (child);
194 this._password = tmp;
196 else if (child.getKey ().equalsIgnoreCase ("ServiceURL"))
198 String tmp = getConfigString (child);
200 this._service_url = tmp;
202 else if (child.getKey ().equalsIgnoreCase ("InstancePrefix"))
204 String tmp = getConfigString (child);
206 this._instance_prefix = tmp;
208 else if (child.getKey ().equalsIgnoreCase ("Collect"))
210 String tmp = getConfigString (child);
213 GenericJMXConfMBean mbean;
215 mbean = GenericJMX.getMBean (tmp);
217 throw (new IllegalArgumentException ("No such MBean defined: "
218 + tmp + ". Please make sure all `MBean' blocks appear "
219 + "before (above) all `Connection' blocks."));
220 Collectd.logDebug ("GenericJMXConfConnection: " + this._host + ": Add " + tmp);
221 this._mbeans.add (mbean);
225 throw (new IllegalArgumentException ("Unknown option: "
229 if (this._service_url == null)
230 throw (new IllegalArgumentException ("No service URL was defined."));
231 if (this._mbeans.size () == 0)
232 throw (new IllegalArgumentException ("No valid collect statement "
234 } /* }}} GenericJMXConfConnection (OConfigItem ci) */
236 public void query () /* {{{ */
243 if (this._mbean_connection == null)
246 Collectd.logDebug ("GenericJMXConfConnection.query: "
247 + "Reading " + this._mbeans.size () + " mbeans from "
248 + ((this._host != null) ? this._host : "(null)"));
250 pd = new PluginData ();
251 pd.setHost (this.getHost ());
252 pd.setPlugin ("GenericJMX");
254 for (int i = 0; i < this._mbeans.size (); i++)
258 status = this._mbeans.get (i).query (this._mbean_connection, pd,
259 this._instance_prefix);
266 } /* }}} void query */
268 public String toString ()
270 return (new String ("host = " + this._host + "; "
271 + "url = " + this._service_url));
275 /* vim: set sw=2 sts=2 et fdm=marker : */